From 97ac78dfb802599862e9b4ba15f02babd6093903 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Thu, 30 Mar 2023 00:00:56 -0700 Subject: [PATCH] bitops: use builtins directly The compiler_bs{r,f}{32,64} macros serve no real purpose. In bitops.h, just use the builtins directly. --- include/wimlib/bitops.h | 36 ++++-------------------------------- include/wimlib/compiler.h | 6 ------ 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/include/wimlib/bitops.h b/include/wimlib/bitops.h index 7cca2f22..e5c4872e 100644 --- a/include/wimlib/bitops.h +++ b/include/wimlib/bitops.h @@ -40,27 +40,13 @@ 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 diff --git a/include/wimlib/compiler.h b/include/wimlib/compiler.h index ddb7a0e6..7f48e4fe 100644 --- a/include/wimlib/compiler.h +++ b/include/wimlib/compiler.h @@ -168,12 +168,6 @@ # 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)) -- 2.43.0