]> www.infradead.org Git - users/jedix/linux-maple.git/commit
linux/log2.h: Fix rounddown_pow_of_two(1)
authorLinus Torvalds <torvalds@linux-foundation.org>
Tue, 13 Dec 2011 06:06:55 +0000 (22:06 -0800)
committerMaxim Uvarov <maxim.uvarov@oracle.com>
Tue, 10 Jan 2012 21:00:13 +0000 (13:00 -0800)
commit3dd0a4c5d5636303bd81af3d43f45051d128d153
treed10977b976442182d0d268e5aa5b0d280f9d93c8
parent2b98750f8234d698ba73bd161e7aada73b49a8b5
linux/log2.h: Fix rounddown_pow_of_two(1)

commit 13c07b0286d340275f2d97adf085cecda37ede37 upstream.

Exactly like roundup_pow_of_two(1), the rounddown version was buggy for
the case of a compile-time constant '1' argument.  Probably because it
originated from the same code, sharing history with the roundup version
from before the bugfix (for that one, see commit 1a06a52ee1b0: "Fix
roundup_pow_of_two(1)").

However, unlike the roundup version, the fix for rounddown is to just
remove the broken special case entirely.  It's simply not needed - the
generic code

    1UL << ilog2(n)

does the right thing for the constant '1' argment too.  The only reason
roundup needed that special case was because rounding up does so by
subtracting one from the argument (and then adding one to the result)
causing the obvious problems with "ilog2(0)".

But rounddown doesn't do any of that, since ilog2() naturally truncates
(ie "rounds down") to the right rounded down value.  And without the
ilog2(0) case, there's no reason for the special case that had the wrong
value.

tl;dr: rounddown_pow_of_two(1) should be 1, not 0.

Acked-by: Dmitry Torokhov <dtor@vmware.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
include/linux/log2.h