Source-Changes archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src/sys/kern



On Sat, 17 Jan 2009, YAMAMOTO Takashi wrote:
> > > > cache_prune: use (a - b > 0) rather than (a > b) to compare ticks.
> > > 
> > > Why?  At first glance, both tests do the same thing.
> > 
> > Probably because thay are actually modulo 2^32 values, so if the
> > 'old' value is 0x7fffffff and the 'new' 0x80000001 you want the
> > difference of 'new - old' > 0 (2 > 0) being true not 'new > old'
> > which is false.
> 
> yes.

OK.  Then I think you want something like

        /* using signed types for the old and new values */
        int32_t old, new;
        if ((int32_t)((uint32_t)new - (uint32_t)old) > 0) ...

or

        /* using unsigned types for the old and new values */
        uint32_t old, new;
        if ((int32_t)(new - old) > 0) ...

There are probably some functions or macros in the TCP stack that will
do this sort of test for you.  Also see RFC 1982, which gives algorithms
in infinite-precision arithmetic.

--apb (Alan Barrett)


Home | Main Index | Thread Index | Old Index