Skip to contentSkip to conversation

bitfield: wire __bf_shf to __builtin_ctzll

PATCH In review Yury Norov 2 participants 2 messages Apr 27 – Apr 28 Bitmap API

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 bitfield: wire __bf_shf to __builtin_ctzll

__bf_shf() is currently based on built-in ffsll. It's more straightforward to wire it to __builtin_ctzll, which makes it a pure rename.

Worth to notice that __builtin_ffsll() is buggy on GCC before 14.1:

  int main() {
      sizeof(struct {
          int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
      });
  }

test.c: In function 'main':
test.c:3:21: error: bit-field 't' width not an integer constant

      3 |                 int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
        |                     ^

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699

---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitfield.h +1 -1
index cd44013281c7..ddf6d1a70473 100644
@@ -44,7 +44,7 @@
  *  FIELD_MODIFY(REG_FIELD_C, &reg, c);
  */
 
-#define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_shf __builtin_ctzll
 
 #define __scalar_type_to_unsigned_cases(type)				\
 		unsigned type:	(unsigned type)0,			\
Signature · 1 line
2.51.0
#1 lore raw
Quoted 31 lines · On Mon, 27 Apr 2026 18:57:05 -0400 Yury Norov <[email protected]> wrote:

__bf_shf() is currently based on built-in ffsll. It's more straightforward to wire it to __builtin_ctzll, which makes it a pure rename.

Worth to notice that __builtin_ffsll() is buggy on GCC before 14.1:

  int main() {
      sizeof(struct {
          int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
      });
  }

test.c: In function 'main':
test.c:3:21: error: bit-field 't' width not an integer constant

      3 |                 int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
        |                     ^

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699

---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitfield.h +0 -0
index cd44013281c7..ddf6d1a70473 100644

include/linux/bitfield.h line 47

Quoted 5 lines
include/linux/bitfield.h +1 -1
@@ -44,7 +44,7 @@
  *  FIELD_MODIFY(REG_FIELD_C, &reg, c);
  */
 
-#define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_shf __builtin_ctzll

I'd leave in the (x).

David

Quoted 3 lines
include/linux/bitfield.h +0 -0
 #define __scalar_type_to_unsigned_cases(type)				\
 		unsigned type:	(unsigned type)0,			\