X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fbitops.h;h=ffc788c5f1b60f58d230f6593893d0a989a00071;hb=d9c55a4d021b4a317a25f14f89468d62592aae0b;hp=c1e6371027a5d39e6fef6dca8883af6e8f922280;hpb=1cf8ac51f5495ccd5187dc220ffc69e95d6e0cfc;p=libucw.git diff --git a/ucw/bitops.h b/ucw/bitops.h index c1e63710..ffc788c5 100644 --- a/ucw/bitops.h +++ b/ucw/bitops.h @@ -2,6 +2,7 @@ * UCW Library -- Bit Operations * * (c) 2005 Martin Mares + * (c) 2012 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -10,6 +11,11 @@ #ifndef _UCW_BITOPS_H #define _UCW_BITOPS_H +#ifdef CONFIG_UCW_CLEAN_ABI +#define bit_fls ucw_bit_fls +#define ffs_table ucw_ffs_table +#endif + /* Find highest bit set (i.e., the floor of the binary logarithm) (bit-fls.c) */ int bit_fls(u32 x); /* bit_fls(0)=-1 */ @@ -37,4 +43,28 @@ static inline uns bit_ffs(uns w) #endif +/* Count the number of bits set */ + +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) + +static inline uns bit_count(uns w) +{ + return __builtin_popcount(w); +} + +#else + +static inline uns bit_count(uns w) +{ + uns n = 0; + while (w) + { + w &= w - 1; + n++; + } + return n; +} + +#endif + #endif