From: Nathael Pajani Date: Thu, 21 Sep 2023 22:41:36 +0000 (+0200) Subject: Add msb() and lsb() utility functions X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=b6571003e22e68010978f33c8c35e0d687a88cf8;p=soft%2Flpc122x%2Fcore Add msb() and lsb() utility functions --- diff --git a/include/lib/utils.h b/include/lib/utils.h index bb2f761..f337216 100644 --- a/include/lib/utils.h +++ b/include/lib/utils.h @@ -44,11 +44,24 @@ */ uint8_t clz(uint32_t x); +/* Return the number of the highest '1' bit in a value + * Will return 255 when there's no bit set. + */ +static inline uint8_t msb(uint32_t x) +{ + return (31 - clz(x)); +} + /* Count traling zeroes * The following function is an effitient way to implement __builtin_ctz(). */ uint8_t ctz(uint32_t x); +/* Return the number of the lowest '1' bit in a value + * Will return 32 when there's no bit set. + */ +//static inline uint8_t lsb(uint32_t x) __attribute__ ((alias ("ctz"))); + /* Count bits set */ uint8_t bits_set(uint32_t x);