2 * UCW Library -- Bit Operations
4 * (c) 2005 Martin Mares <mj@ucw.cz>
5 * (c) 2012 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 /* Find highest bit set (i.e., the floor of the binary logarithm) (bit-fls.c) */
16 int bit_fls(u32 x); /* bit_fls(0)=-1 */
18 /* Find lowest bit set, undefined for zero argument (bit-ffs.c) */
20 extern const byte ffs_table[256];
22 #ifdef __pentium4 /* On other ia32 machines, the C version is faster */
24 static inline uns bit_ffs(uns w)
26 asm("bsfl %1,%0" :"=r" (w) :"rm" (w));
32 static inline uns bit_ffs(uns w)
34 uns b = (w & 0xffff) ? 0 : 16;
35 b += ((w >> b) & 0xff) ? 0 : 8;
36 return b + ffs_table[(w >> b) & 0xff];
41 /* Count the number of bits set */
43 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
45 static inline uns bit_count(uns w)
47 return __builtin_popcount(w);
52 static inline uns bit_count(uns w)