]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
misc: We want the position of the highest bit, not the highest bit itself
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 28 Mar 2020 04:04:23 +0000 (22:04 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 28 Mar 2020 04:04:23 +0000 (22:04 -0600)
src/lib/util/misc.h

index db1633f3a61d704f43332deed730231e20efd5a0..df0ac806b81a17e7900b80fa777a19ef8c3224f6 100644 (file)
@@ -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
 }