]> wimlib.net Git - wimlib/blobdiff - include/wimlib/bitops.h
Introduce ilog2_ceil()
[wimlib] / include / wimlib / bitops.h
index 70e6c611a888c68feb76ddbb627c57a4d2b89c54..d964374d63e0def7112e83ea5532d7d1d7f91832 100644 (file)
@@ -55,8 +55,8 @@ fls64(u64 v)
 static inline unsigned
 flsw(machine_word_t v)
 {
-       STATIC_ASSERT(WORDSIZE == 4 || WORDSIZE == 8);
-       if (WORDSIZE == 4)
+       STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
+       if (WORDBITS == 32)
                return fls32(v);
        else
                return fls64(v);
@@ -93,21 +93,27 @@ ffs64(u64 v)
 static inline unsigned
 ffsw(machine_word_t v)
 {
-       STATIC_ASSERT(WORDSIZE == 4 || WORDSIZE == 8);
-       if (WORDSIZE == 4)
+       STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
+       if (WORDBITS == 32)
                return ffs32(v);
        else
                return ffs64(v);
 }
 
-/* Round up to nearest power of 2  */
+/* Return the log base 2 of 'n', rounded up to the nearest integer. */
+static inline unsigned
+ilog2_ceil(size_t n)
+{
+        if (n <= 1)
+                return 0;
+        return 1 + flsw(n - 1);
+}
 
+/* Round 'n' up to the nearest power of 2 */
 static inline size_t
 roundup_pow_of_2(size_t n)
 {
-       if (n <= 1)
-               return 1;
-       return (size_t)1 << (1 + flsw(n - 1));
+       return (size_t)1 << ilog2_ceil(n);
 }
 
 #endif /* _WIMLIB_BITOPS_H */