X-Git-Url: http://git.buserror.net/cgi-bin/gitweb.cgi?p=polintos%2Fscott%2Fpriv.git;a=blobdiff_plain;f=include%2Fc%2Flowlevel%2Fbitops.h;h=6aa90292b3bdcc3fb1d8e9542da8d6b830f70d4b;hp=9c3ce7e17f6d07b5af44063915281173e6995803;hb=1096537bdc1c23affc5eedfba311d37c7bf647bf;hpb=2f2638b54e0d3662d3be465a4035c3f28018907e diff --git a/include/c/lowlevel/bitops.h b/include/c/lowlevel/bitops.h index 9c3ce7e..6aa9029 100644 --- a/include/c/lowlevel/bitops.h +++ b/include/c/lowlevel/bitops.h @@ -7,9 +7,39 @@ #include #include _LL_INC(bitops.h) +// Find First (least-significant) Set, counting from 0, +// undefined if no bits set + +static inline int ll_ffs(unsigned long val) +{ + return __builtin_ctzl(val); +} + +// Find Last (most-significant) Set, counting from 0, +// undefined if no bits set + +static inline int ll_fls(unsigned long val) +{ + return (sizeof(unsigned long)*8 - 1) ^ __builtin_clzl(val); +} + +// As above, except on 64-bit values regardless of sizeof(long). +static inline int ll_ffs64(uint64_t val) +{ + return __builtin_ctzll(val); +} + +// Find Last (most-significant) Set, counting from 0, +// undefined if no bits set + +static inline int ll_fls64(unsigned long val) +{ + return (sizeof(unsigned long long)*8 - 1) ^ __builtin_clzll(val); +} + static inline int ll_get_order_round_up(unsigned long val) { - return val != 1 ? ll_fls(val - 1) + 1 : 0; + return val > 1 ? ll_fls(val - 1) + 1 : 0; } static inline int ll_get_order_round_down(unsigned long val)