1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

core/bitarithm: add bitarithm_clzb()

This commit is contained in:
Benjamin Valentin 2023-05-23 15:55:43 +02:00
parent f3150120f7
commit abe5949cd2

View File

@ -165,6 +165,30 @@ static inline unsigned bitarithm_msb(unsigned v)
#endif
}
/**
* @brief Returns the number of leading 0-bits in @p x, starting at the most
* significant bit position.
* If x is 0, the result is undefined.
*
* @param[in] x Input value
* @return Number of leading zero bits
*/
static inline uint8_t bitarithm_clzb(uint8_t x)
{
#if defined(BITARITHM_HAS_CLZ)
/* clz operates on `unsigned int`, so `x` will be promoted to the size
of an `unsigned int` */
return __builtin_clz(x) - 8 * (sizeof(unsigned) - 1);
#else
uint8_t l = 0;
while (!(x & 0x80)) {
++l;
x <<= 1;
}
return l;
#endif
}
/**
* @private
*