]> wimlib.net Git - wimlib/commitdiff
bitops: use builtins directly
authorEric Biggers <ebiggers3@gmail.com>
Thu, 30 Mar 2023 07:00:56 +0000 (00:00 -0700)
committerEric Biggers <ebiggers3@gmail.com>
Thu, 30 Mar 2023 07:16:51 +0000 (00:16 -0700)
The compiler_bs{r,f}{32,64} macros serve no real purpose.  In bitops.h,
just use the builtins directly.

include/wimlib/bitops.h
include/wimlib/compiler.h

index 7cca2f22debb0cff8bd4aa096df2a6ba2e8e6c64..e5c4872e40b4035cd987ad70bcc8e0026be73fbf 100644 (file)
 static forceinline unsigned
 bsr32(u32 v)
 {
-#ifdef compiler_bsr32
-       return compiler_bsr32(v);
-#else
-       unsigned bit = 0;
-       while ((v >>= 1) != 0)
-               bit++;
-       return bit;
-#endif
+       return 31 - __builtin_clz(v);
 }
 
 static forceinline unsigned
 bsr64(u64 v)
 {
-#ifdef compiler_bsr64
-       return compiler_bsr64(v);
-#else
-       unsigned bit = 0;
-       while ((v >>= 1) != 0)
-               bit++;
-       return bit;
-#endif
+       return 63 - __builtin_clzll(v);
 }
 
 static forceinline unsigned
@@ -82,27 +68,13 @@ bsrw(machine_word_t v)
 static forceinline unsigned
 bsf32(u32 v)
 {
-#ifdef compiler_bsf32
-       return compiler_bsf32(v);
-#else
-       unsigned bit;
-       for (bit = 0; !(v & 1); bit++, v >>= 1)
-               ;
-       return bit;
-#endif
+       return __builtin_ctz(v);
 }
 
 static forceinline unsigned
 bsf64(u64 v)
 {
-#ifdef compiler_bsf64
-       return compiler_bsf64(v);
-#else
-       unsigned bit;
-       for (bit = 0; !(v & 1); bit++, v >>= 1)
-               ;
-       return bit;
-#endif
+       return __builtin_ctzll(v);
 }
 
 static forceinline unsigned
index ddb7a0e6f3d7be7056e47897ed2e0c506a422750..7f48e4fe8ea7ca213963f09b4a5d10e2374f0209 100644 (file)
 #  define compiler_bswap64 __builtin_bswap64
 #endif
 
-/* (Optional) Find Last Set bit and Find First Set bit macros.  */
-#define compiler_bsr32(n)      (31 - __builtin_clz(n))
-#define compiler_bsr64(n)      (63 - __builtin_clzll(n))
-#define compiler_bsf32(n)      __builtin_ctz(n)
-#define compiler_bsf64(n)      __builtin_ctzll(n)
-
 /* Optional definitions for checking with 'sparse'.  */
 #ifdef __CHECKER__
 #  define _bitwise_attr        __attribute__((bitwise))