From: Arran Cudbard-Bell Date: Sat, 28 Mar 2020 04:04:23 +0000 (-0600) Subject: misc: We want the position of the highest bit, not the highest bit itself X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1491fc650d35afdb0abae0637fecd12dae84d973;p=thirdparty%2Ffreeradius-server.git misc: We want the position of the highest bit, not the highest bit itself --- diff --git a/src/lib/util/misc.h b/src/lib/util/misc.h index db1633f3a61..df0ac806b81 100644 --- a/src/lib/util/misc.h +++ b/src/lib/util/misc.h @@ -170,19 +170,17 @@ static inline bool is_zero(char const *value) /** Find the highest order bit in an unsigned 64 bit integer * + * @return 0-64 indicating the position of the highest bit. */ -static inline uint8_t fr_high_bit_uint64(uint64_t num) +static inline uint8_t fr_high_bit_pos_uint64(uint64_t num) { #ifndef HAVE_BUILTIN_CLZLL return (64 - __builtin_clzll(num)); #else - num |= (num >> 1); - num |= (num >> 2); - num |= (num >> 4); - num |= (num >> 8); - num |= (num >> 16); - num |= (num >> 32); - return num - (num >> 1); + uint8_t ret = 1; + if (num == 0) return 0; + while (num >>= 1) ret++; + return ret; #endif }