Skip to contentSkip to conversation

bitfield.h: Ensure FIELD_PREP_CONST() is constant

PATCH In review [email protected] 2 participants 7 messages Apr 10 – Apr 13 Bitmap API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitfield.h: Ensure FIELD_PREP_CONST() is constant

From: David Laight <[email protected]>

Some versions of gcc report that some expressions involving __builtin_ffsll(1ull << 63) are not integer constant expressions.

Rework FIELD_PREP_CONST() to avoid the issue.

---

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

 include/linux/bitfield.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/bitfield.h +4 -5
index 54aeeef1f0ec..f21765851d5c 100644
@@ -45,6 +45,7 @@
  */
 
 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_low_bit(mask) ((mask) & (~(mask) + 1))
 
 #define __scalar_type_to_unsigned_cases(type)				\
 		unsigned type:	(unsigned type)0,			\
@@ -138,8 +139,6 @@
 		__FIELD_PREP(_mask, _val, "FIELD_PREP: ");		\
 	})
 
-#define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
-
 /**
  * FIELD_PREP_CONST() - prepare a constant bitfield element
  * @_mask: shifted mask defining the field's length and position
@@ -157,11 +156,11 @@
 		/* mask must be non-zero */				\
 		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
 		/* check if value fits */				\
-		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		BUILD_BUG_ON_ZERO(~((_mask) / __bf_low_bit(_mask)) & (_val)) + \
 		/* check if mask is contiguous */			\
-		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
+		BUILD_BUG_ON_ZERO((_mask) & ((_mask) + __bf_low_bit(_mask))) + \
 		/* and create the value */				\
-		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
+		(((_val) * __bf_low_bit(_mask)) & (_mask))		\
 	)
 
 /**
Signature · 1 line
2.39.5
#1 lore raw

+ Matt Coster

Quoted 8 lines · On Fri, Apr 10, 2026 at 10:09:27AM +0100, [email protected] wrote:

From: David Laight <[email protected]>

Some versions of gcc report that some expressions involving __builtin_ffsll(1ull << 63) are not integer constant expressions.

Rework FIELD_PREP_CONST() to avoid the issue.

Thanks, David.

So, the original patch that spotted the problem is:

https://lore.kernel.org/all/[email protected]/

Let's keep Matt in the loop, at least.

Please keep the details from the original patch. "Some versions of some expressions" style is too uncertain.

Quoted 5 lines

---

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

Thanks,
Yury

Quoted 41 lines
 include/linux/bitfield.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/bitfield.h +4 -5
index 54aeeef1f0ec..f21765851d5c 100644
@@ -45,6 +45,7 @@
  */
 
 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_low_bit(mask) ((mask) & (~(mask) + 1))
 
 #define __scalar_type_to_unsigned_cases(type)				\
 		unsigned type:	(unsigned type)0,			\
@@ -138,8 +139,6 @@
 		__FIELD_PREP(_mask, _val, "FIELD_PREP: ");		\
 	})
 
-#define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
-
 /**
  * FIELD_PREP_CONST() - prepare a constant bitfield element
  * @_mask: shifted mask defining the field's length and position
@@ -157,11 +156,11 @@
 		/* mask must be non-zero */				\
 		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
 		/* check if value fits */				\
-		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		BUILD_BUG_ON_ZERO(~((_mask) / __bf_low_bit(_mask)) & (_val)) + \
 		/* check if mask is contiguous */			\
-		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
+		BUILD_BUG_ON_ZERO((_mask) & ((_mask) + __bf_low_bit(_mask))) + \
 		/* and create the value */				\
-		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
+		(((_val) * __bf_low_bit(_mask)) & (_mask))		\
 	)
 
 /**
Signature · 1 line
2.39.5
#2 lore raw
Quoted 10 lines · On Fri, 10 Apr 2026 12:55:25 -0400 Yury Norov <[email protected]> wrote:

+ Matt Coster

Quoted 7 lines · On Fri, Apr 10, 2026 at 10:09:27AM +0100, [email protected] wrote:

From: David Laight <[email protected]>

Some versions of gcc report that some expressions involving __builtin_ffsll(1ull << 63) are not integer constant expressions.

Rework FIELD_PREP_CONST() to avoid the issue.

Quoted 7 lines
Quoted 1 line

Thanks, David.

So, the original patch that spotted the problem is:

https://lore.kernel.org/all/[email protected]/

That isn't the patch that spotted the problem.
That is a suggested fix.
I've not seen the code (or patch) that actually exhibited the issue.

I think someone changed (drivers/gpu/drm/imagination/pvr_device.h):

#define PVR_PACKED_BVNC(b, v, n, c) \
	((((u64)(b) & GENMASK_ULL(15, 0)) << 48) | \
	 (((u64)(v) & GENMASK_ULL(15, 0)) << 32) | \
	 (((u64)(n) & GENMASK_ULL(15, 0)) << 16) | \
	 (((u64)(c) & GENMASK_ULL(15, 0)) <<  0))

to use FIELD_PREP_CONST().
The top line might be FIELD_PREP_CONST(GENMASK_ULL(63, 48), b)) which has the MSB set on the call to __builtin_ffsll() which seems to be necessary (but not sufficient) to fail.

The expansion of that is used (pvr_device.c) in:
static enum pvr_gpu_support_level pvr_gpu_support_level(const struct pvr_gpu_id *gpu_id)

{
	switch (pvr_gpu_id_to_packed_bvnc(gpu_id)) {
	case PVR_PACKED_BVNC(33, 15, 11, 3):
	case PVR_PACKED_BVNC(36, 53, 104, 796):
		return PVR_GPU_SUPPORTED;

	case PVR_PACKED_BVNC(36, 52, 104, 182):
		return PVR_GPU_EXPERIMENTAL;

	default:
		return PVR_GPU_UNKNOWN;
	}
}
Quoted 2 lines

Let's keep Matt in the loop, at least.

I thought I'd typed his address in - somewhere it got lost.

Quoted 2 lines

Please keep the details from the original patch. "Some versions of some expressions" style is too uncertain.

But that is pretty much what it is.
I could give a failing test case, but it all reminds me of a bug in ash where:

	x=$(command args)

could lose the last byte of output.
Fiddling with the shell script would fix it (never mind changing the compiler options for the shell itself).
(Basically is was scanning the on-stack temporary buffer when removing trailing '\n' and could access buffer[-1], if that happened to be 0xa then the length would be decremented. Only likely to happen on BE.)

In this case minor tweaks to the expression change the behaviour. This one fails:

	int t : (__builtin_ffsll((1ull << 63)) < 3u) ? 1 : 2;

change the 3u to 3 or the 63 to 62 and it is ok.
Until someone supporting gcc actually identifies the underlying bug there really isn't much to say.

Quoted 9 lines
Quoted 6 lines

---

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

I think the multiply instruction will have a higher latency than the shift. So you are talking about a very small number of clocks if the expression is in the critical register dependency path.
However FIELD_GET() would need to use a divide - and that would be a lot worse.

Having written that, ISTR that 'mask' is required to be a constant. So the compiler may use a shift anyway - if the divide is unsigned. But for non-constant mask you definitely want a 'shift right'.

While you might think that it only makes sense to use unsigned values, I've found one piece of code (IIRC in the x86 fault handler) that passes a signed value to FIELD_GET() and needs the result sign extended. So, unless that is changed, FIELD_GET() must use an explicit right shift. (Of course, right shift of negative values is probably UB...)

David

Quoted 45 lines

Thanks,
Yury

Quoted 41 lines
 include/linux/bitfield.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/bitfield.h +4 -5
index 54aeeef1f0ec..f21765851d5c 100644
@@ -45,6 +45,7 @@
  */
 
 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_low_bit(mask) ((mask) & (~(mask) + 1))
 
 #define __scalar_type_to_unsigned_cases(type)				\
 		unsigned type:	(unsigned type)0,			\
@@ -138,8 +139,6 @@
 		__FIELD_PREP(_mask, _val, "FIELD_PREP: ");		\
 	})
 
-#define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
-
 /**
  * FIELD_PREP_CONST() - prepare a constant bitfield element
  * @_mask: shifted mask defining the field's length and position
@@ -157,11 +156,11 @@
 		/* mask must be non-zero */				\
 		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
 		/* check if value fits */				\
-		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		BUILD_BUG_ON_ZERO(~((_mask) / __bf_low_bit(_mask)) & (_val)) + \
 		/* check if mask is contiguous */			\
-		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
+		BUILD_BUG_ON_ZERO((_mask) & ((_mask) + __bf_low_bit(_mask))) + \
 		/* and create the value */				\
-		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
+		(((_val) * __bf_low_bit(_mask)) & (_mask))		\
 	)
 
 /**
Signature · 1 line
2.39.5  
#3 lore raw
Quoted 2 lines · David Laight

On Fri, 10 Apr 2026 12:55:25 -0400
Yury Norov <[email protected]> wrote:

...

Quoted 13 lines
Quoted 6 lines
Quoted 3 lines

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

I think the multiply instruction will have a higher latency than the shift. So you are talking about a very small number of clocks if the expression is in the critical register dependency path.
However FIELD_GET() would need to use a divide - and that would be a lot worse.

Quoted 3 lines

Having written that, ISTR that 'mask' is required to be a constant. So the compiler may use a shift anyway - if the divide is unsigned. But for non-constant mask you definitely want a 'shift right'.

Non-constant masks are handled with __field_get(), which doesn't use __bf_shf().

Quoted 5 lines

While you might think that it only makes sense to use unsigned values, I've found one piece of code (IIRC in the x86 fault handler) that passes a signed value to FIELD_GET() and needs the result sign extended. So, unless that is changed, FIELD_GET() must use an explicit right shift. (Of course, right shift of negative values is probably UB...)

FIELD_GET() is quite fine with the change:

 #define __FIELD_GET(mask, reg, pfx)                                    \
         ({                                                              \
                 __BF_FIELD_CHECK_MASK(mask, 0U, pfx);                   \
 -               (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask));     \
 +               (typeof(mask))(((reg) & (mask)) / __bf_low_bit(mask));  \
         })

void my_test(void)

{
        f3 0f 1e fa             endbr64
        48 83 ec 08             sub    $0x8,%rsp
        volatile int i = -1;

        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        48 c7 c7 13 e3 51 82    mov    $0xffffffff8251e313,%rdi
        volatile int i = -1;
        c7 44 24 04 ff ff ff    movl   $0xffffffff,0x4(%rsp)
        ff 
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        8b 74 24 04             mov    0x4(%rsp),%esi

}

        48 83 c4 08             add    $0x8,%rsp
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        81 e6 e0 07 00 00       and    $0x7e0,%esi
        48 c1 ee 05             shr    $0x5,%rsi
        e9 32 aa b9 ff          jmp    <_printk>

Thanks,
Yury

#4 lore raw
Quoted 55 lines · On Sat, 11 Apr 2026 00:24:28 -0400 Yury Norov <[email protected]> wrote:
Quoted 2 lines · David Laight

On Fri, 10 Apr 2026 12:55:25 -0400
Yury Norov <[email protected]> wrote:

...

Quoted 13 lines
Quoted 6 lines
Quoted 3 lines

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

I think the multiply instruction will have a higher latency than the shift. So you are talking about a very small number of clocks if the expression is in the critical register dependency path.
However FIELD_GET() would need to use a divide - and that would be a lot worse.

Quoted 3 lines

Having written that, ISTR that 'mask' is required to be a constant. So the compiler may use a shift anyway - if the divide is unsigned. But for non-constant mask you definitely want a 'shift right'.

Non-constant masks are handled with __field_get(), which doesn't use __bf_shf().

Quoted 5 lines

While you might think that it only makes sense to use unsigned values, I've found one piece of code (IIRC in the x86 fault handler) that passes a signed value to FIELD_GET() and needs the result sign extended. So, unless that is changed, FIELD_GET() must use an explicit right shift. (Of course, right shift of negative values is probably UB...)

FIELD_GET() is quite fine with the change:

 #define __FIELD_GET(mask, reg, pfx)                                    \
         ({                                                              \
                 __BF_FIELD_CHECK_MASK(mask, 0U, pfx);                   \
 -               (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask));     \
 +               (typeof(mask))(((reg) & (mask)) / __bf_low_bit(mask));  \
         })

void my_test(void)

{
        f3 0f 1e fa             endbr64
        48 83 ec 08             sub    $0x8,%rsp
        volatile int i = -1;

        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        48 c7 c7 13 e3 51 82    mov    $0xffffffff8251e313,%rdi
        volatile int i = -1;
        c7 44 24 04 ff ff ff    movl   $0xffffffff,0x4(%rsp)
        ff 
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        8b 74 24 04             mov    0x4(%rsp),%esi
Quoted 6 lines

}

        48 83 c4 08             add    $0x8,%rsp
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        81 e6 e0 07 00 00       and    $0x7e0,%esi
        48 c1 ee 05             shr    $0x5,%rsi
        e9 32 aa b9 ff          jmp    <_printk>

There is a subtle difference between (https://www.godbolt.org/z/KM7MesPWM):

int a(int x)

{
    return x >> __bf_shf(0xf0u);
}

int b(int x)

{
    return x / __bf_low_bit(0xf0);
}

int c(int x)

{
    return x / __bf_low_bit(0xf0u);
}

a:
movl %edi, %eax
sarl $4, %eax
ret
b:

        testl   %edi, %edi
        leal    15(%rdi), %eax
        cmovns  %edi, %eax
        sarl    $4, %eax

ret
c:
movl %edi, %eax
shrl $4, %eax
ret

A while ago I did a compile-test for negative values and found one place that requires the sign-replicating right shift.

So you'd need that check and to fixup the caller.

David

Quoted 3 lines

Thanks,
Yury

#5 lore raw
Quoted 91 lines · David Laight
Quoted 55 lines · On Sat, 11 Apr 2026 00:24:28 -0400 Yury Norov <[email protected]> wrote:
Quoted 2 lines · David Laight

On Fri, 10 Apr 2026 12:55:25 -0400
Yury Norov <[email protected]> wrote:

...

Quoted 13 lines
Quoted 6 lines
Quoted 3 lines

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

I think the multiply instruction will have a higher latency than the shift. So you are talking about a very small number of clocks if the expression is in the critical register dependency path.
However FIELD_GET() would need to use a divide - and that would be a lot worse.

Quoted 3 lines

Having written that, ISTR that 'mask' is required to be a constant. So the compiler may use a shift anyway - if the divide is unsigned. But for non-constant mask you definitely want a 'shift right'.

Non-constant masks are handled with __field_get(), which doesn't use __bf_shf().

Quoted 5 lines

While you might think that it only makes sense to use unsigned values, I've found one piece of code (IIRC in the x86 fault handler) that passes a signed value to FIELD_GET() and needs the result sign extended. So, unless that is changed, FIELD_GET() must use an explicit right shift. (Of course, right shift of negative values is probably UB...)

FIELD_GET() is quite fine with the change:

 #define __FIELD_GET(mask, reg, pfx)                                    \
         ({                                                              \
                 __BF_FIELD_CHECK_MASK(mask, 0U, pfx);                   \
 -               (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask));     \
 +               (typeof(mask))(((reg) & (mask)) / __bf_low_bit(mask));  \
         })

void my_test(void)

{
        f3 0f 1e fa             endbr64
        48 83 ec 08             sub    $0x8,%rsp
        volatile int i = -1;

        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        48 c7 c7 13 e3 51 82    mov    $0xffffffff8251e313,%rdi
        volatile int i = -1;
        c7 44 24 04 ff ff ff    movl   $0xffffffff,0x4(%rsp)
        ff 
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        8b 74 24 04             mov    0x4(%rsp),%esi
Quoted 6 lines

}

        48 83 c4 08             add    $0x8,%rsp
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        81 e6 e0 07 00 00       and    $0x7e0,%esi
        48 c1 ee 05             shr    $0x5,%rsi
        e9 32 aa b9 ff          jmp    <_printk>

There is a subtle difference between (https://www.godbolt.org/z/KM7MesPWM):

int a(int x)

{
    return x >> __bf_shf(0xf0u);
}

int b(int x)

{
    return x / __bf_low_bit(0xf0);
}

int c(int x)

{
    return x / __bf_low_bit(0xf0u);
}

a:
movl %edi, %eax
sarl $4, %eax
ret
b:

        testl   %edi, %edi
        leal    15(%rdi), %eax
        cmovns  %edi, %eax
        sarl    $4, %eax
Quoted 8 lines

ret
c:
movl %edi, %eax
shrl $4, %eax
ret

A while ago I did a compile-test for negative values and found one place that requires the sign-replicating right shift.

Again, please be more certain. When? Which compiler did you use? Which place have you found? Does that place still exist?

Quoted 2 lines

So you'd need that check and to fixup the caller.

None of them use DIV or MUL expensive instructions, which was your original concern. If you're concerned about code generation in (b), you can typecast it to an unsigned with __bf_cast_unsigned(). And I also think that __bf_low_bit() is a bad name - low bit is always #0. Maybe ffs_mask(), lsb_mask() or more wordy least_set_bit_mask()?

Altogether, IMO this would be:

#define ffs_mask(val) (__bf_cast_unsigned(val, val) &            \
                            (~(__bf_cast_unsigned(val, val)) + 1)

--

Regardless of __bf_low_bit() discussion, __bf_shf() needs to get fixed for gcc <= 14, because it's a public API and has over 100 users in the kernel. So, Matt, you're very welcome to submit v2.

Thanks,
Yury

#6 lore raw
Quoted 98 lines · On Mon, 13 Apr 2026 12:40:11 -0400 Yury Norov <[email protected]> wrote:
Quoted 97 lines · David Laight
Quoted 55 lines · On Sat, 11 Apr 2026 00:24:28 -0400 Yury Norov <[email protected]> wrote:
Quoted 2 lines · David Laight

On Fri, 10 Apr 2026 12:55:25 -0400
Yury Norov <[email protected]> wrote:

...

Quoted 13 lines
Quoted 6 lines
Quoted 3 lines

Note that when 'val' is a variable 'val << constant' is likely to execute faster than 'val * (1 << constant)'.
So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent when possible.

I think the multiply instruction will have a higher latency than the shift. So you are talking about a very small number of clocks if the expression is in the critical register dependency path.
However FIELD_GET() would need to use a divide - and that would be a lot worse.

Quoted 3 lines

Having written that, ISTR that 'mask' is required to be a constant. So the compiler may use a shift anyway - if the divide is unsigned. But for non-constant mask you definitely want a 'shift right'.

Non-constant masks are handled with __field_get(), which doesn't use __bf_shf().

Quoted 5 lines

While you might think that it only makes sense to use unsigned values, I've found one piece of code (IIRC in the x86 fault handler) that passes a signed value to FIELD_GET() and needs the result sign extended. So, unless that is changed, FIELD_GET() must use an explicit right shift. (Of course, right shift of negative values is probably UB...)

FIELD_GET() is quite fine with the change:

 #define __FIELD_GET(mask, reg, pfx)                                    \
         ({                                                              \
                 __BF_FIELD_CHECK_MASK(mask, 0U, pfx);                   \
 -               (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask));     \
 +               (typeof(mask))(((reg) & (mask)) / __bf_low_bit(mask));  \
         })

void my_test(void)

{
        f3 0f 1e fa             endbr64
        48 83 ec 08             sub    $0x8,%rsp
        volatile int i = -1;

        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        48 c7 c7 13 e3 51 82    mov    $0xffffffff8251e313,%rdi
        volatile int i = -1;
        c7 44 24 04 ff ff ff    movl   $0xffffffff,0x4(%rsp)
        ff 
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        8b 74 24 04             mov    0x4(%rsp),%esi
Quoted 6 lines

}

        48 83 c4 08             add    $0x8,%rsp
        pr_err("%lx\n", FIELD_GET(GENMASK(10,5), i));
        81 e6 e0 07 00 00       and    $0x7e0,%esi
        48 c1 ee 05             shr    $0x5,%rsi
        e9 32 aa b9 ff          jmp    <_printk>  

There is a subtle difference between (https://www.godbolt.org/z/KM7MesPWM):

int a(int x)

{
    return x >> __bf_shf(0xf0u);
}

int b(int x)

{
    return x / __bf_low_bit(0xf0);
}

int c(int x)

{
    return x / __bf_low_bit(0xf0u);
}

a:
movl %edi, %eax
sarl $4, %eax
ret
b:

        testl   %edi, %edi
        leal    15(%rdi), %eax
        cmovns  %edi, %eax
        sarl    $4, %eax

ret
c:
movl %edi, %eax
shrl $4, %eax
ret

Quoted 5 lines
Quoted 2 lines

A while ago I did a compile-test for negative values and found one place that requires the sign-replicating right shift.

Again, please be more certain. When? Which compiler did you use? Which place have you found? Does that place still exist?

Basically the compiler doesn't matter and the code is in arch/x866/mm_extable.c Read the comment at the top of arch/x86/include/asm/extable_fixup_flags.h. I've just done a build that uses a union and C bitfields in struct exception_table_entry, actually compiles to fewer bytes - but there are 4 memory reads instead of one so it may be slower.

Quoted 7 lines
Quoted 3 lines

So you'd need that check and to fixup the caller.

None of them use DIV or MUL expensive instructions, which was your original concern. If you're concerned about code generation in (b), you can typecast it to an unsigned with __bf_cast_unsigned().

FFS - that is only of those unnecessary and bloat-worthy defines that makes FIELD_GET(GENMASK(),) expand to kb of source. I've got another new plan for __BF_FIELD_CHECK_REG().

Quoted 8 lines

And I
also think that __bf_low_bit() is a bad name - low bit is always #0. Maybe ffs_mask(), lsb_mask() or more wordy least_set_bit_mask()?

Altogether, IMO this would be:

#define ffs_mask(val) (__bf_cast_unsigned(val, val) &            \
                            (~(__bf_cast_unsigned(val, val)) + 1)

Nope, the most you need is:

	((val) & ((~val) + 1)) + 0u + 0ul + 0ull)

That will zero-extend the value and the compiler will throw away all the spare high zeros.
There is another annoyance that if val is '(int)(1u << 31)' you get a warning about integer overflow.

Quoted 6 lines

--

Regardless of __bf_low_bit() discussion, __bf_shf() needs to get fixed for gcc <= 14, because it's a public API and has over 100 users in the kernel.

I wonder how that happened! the leading __ really ought to have indicated that is was internal.
In any case none of those rely on the result being an integer constant expressions (and set the high bit) or there'd have need earlier errors.

For this error it really is best to just revert the change to the file that is generating the error - it is a new change to that file. Better change it to something sensible like:

	BUILD_BUG_ON_ZERO(((a) | (b) | (c) | (d)) > 0xffffu) +
	(u64)a << 48 | (u64)b << 32 | c << 16 | d

David

So, Matt, you're very welcome to submit v2.

Quoted 3 lines

Thanks,
Yury