[PATCH] Bug with powerof2 macro in sys/param.h

Colin Percival cperciva at freebsd.org
Fri Oct 15 12:27:56 UTC 2010


Garrett Cooper <gcooper at FreeBSD.org> wrote:
>     I was talking to someone today about this macro, and he noted that
> the algorithm is incorrect -- it fails the base case with ((x) == 0 --
> which makes sense because 2^(x) cannot equal 0 (mathematically
> impossible, unless you consider the limit as x goes to negative
> infinity as log (0) / log(2) is undefined).

Integer arithmetic on all platforms FreeBSD supports is in
the ring Z_{2^n} for appropriate values of n; and in this
ring, 2^n is equal to 0.

If you arbitrarily declare that !powerof2(0), then you get
into the bizarre situation of powerof2(x) && !powerof2(2*x)
for x = 1 << (n - 1); this seems far more astonishing than
the well-understood fact that machine arithmetic operates
with a modular ring, so it seems to me that your proposed
patch would do more harm than good.

-- 
Colin Percival
Security Officer, FreeBSD | freebsd.org | The power to serve
Founder / author, Tarsnap | tarsnap.com | Online backups for the truly paranoid


More information about the freebsd-hackers mailing list