From b6571003e22e68010978f33c8c35e0d687a88cf8 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Fri, 22 Sep 2023 00:41:36 +0200 Subject: [PATCH] Add msb() and lsb() utility functions --- include/lib/utils.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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); -- 2.43.0