[PATCH] roundup_pow_of_two() 64-bit fix
authorAndrew Morton <akpm@osdl.org>
Sat, 25 Mar 2006 11:08:01 +0000 (03:08 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Sat, 25 Mar 2006 16:22:58 +0000 (08:22 -0800)
fls() takes an integer, so roundup_pow_of_two() is busted for ulongs larger
than 2^32-1.

Fix this by implementing and using fls_long().

(Why does roundup_pow_of_two() return a long?)

(Why is roundup_pow_of_two() __attribute_const__ whereas long_log2() is
__attribute_pure__?)

(Why does long_log2() suck so much?  Because we were missing fls_long()?)

Cc: Roland Dreier <rdreier@cisco.com>
Cc: "Chen, Kenneth W" <kenneth.w.chen@intel.com>
Cc: John Hawkes <hawkes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
include/linux/bitops.h
include/linux/kernel.h

index 208650b1ad3ac08a340d93e87729890d807a3bdb..f17525a963d1954821386355da95077660fa43cf 100644 (file)
@@ -175,4 +175,11 @@ static inline __u32 ror32(__u32 word, unsigned int shift)
        return (word >> shift) | (word << (32 - shift));
 }
 
+static inline unsigned fls_long(unsigned long l)
+{
+       if (sizeof(l) == 4)
+               return fls(l);
+       return fls64(l);
+}
+
 #endif
index bb6e7ddee2fd456ff107e54aac6bc04dde588f0d..03d6cfaa5b8ab2d9e76f8717af5d1fa1c493f70e 100644 (file)
@@ -154,9 +154,10 @@ static inline int __attribute_pure__ long_log2(unsigned long x)
        return r;
 }
 
-static inline unsigned long __attribute_const__ roundup_pow_of_two(unsigned long x)
+static inline unsigned long
+__attribute_const__ roundup_pow_of_two(unsigned long x)
 {
-       return (1UL << fls(x - 1));
+       return 1UL << fls_long(x - 1);
 }
 
 extern int printk_ratelimit(void);