bitfield: add FIELD_GET_SIGNED()
Subsystems (9)
cover bitfield: add FIELD_GET_SIGNED()
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);
It's error-prone because it relies on user to provide the correct index of the most significant bit.
This series adds a signed version of FIELD_GET(), which is the more convenient and compiles (on x86_64) to just a couple instructions:
shl and sar.
Patch #1 adds FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.
Yury Norov (9):
bitfield: add FIELD_GET_SIGNED()
x86/extable: switch to using FIELD_GET_SIGNED()
iio: intel_dc_ti_adc: switch to using
iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()
iio: pressure: bmp280: switch to using
iio: mcp9600: switch to using FIELD_GET_SIGNED()
wifi: rtw89: switch to using FIELD_GET_SIGNED()
rtc: rv3032: switch to using FIELD_GET_SIGNED()
ptp: switch to using FIELD_GET_SIGNED()
arch/x86/include/asm/extable_fixup_types.h | 13 ++++--------- arch/x86/mm/extable.c | 2 +- drivers/iio/adc/intel_dc_ti_adc.c | 4 ++-- drivers/iio/magnetometer/yamaha-yas530.c | 12 ++++++------ drivers/iio/pressure/bmp280-core.c | 2 +- drivers/iio/temperature/mcp9600.c | 2 +- .../net/wireless/realtek/rtw89/rtw8852a_rfk.c | 4 ++-- .../net/wireless/realtek/rtw89/rtw8852b_common.c | 4 ++-- .../net/wireless/realtek/rtw89/rtw8852b_rfk.c | 4 ++-- drivers/net/wireless/realtek/rtw89/rtw8852c.c | 4 ++-- drivers/ptp/ptp_fc3.c | 4 ++-- drivers/rtc/rtc-rv3032.c | 2 +- include/linux/bitfield.h | 16 ++++++++++++++++ 13 files changed, 42 insertions(+), 31 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/9 bitfield: add FIELD_GET_SIGNED()
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);
It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a FIELD_GET_SIGNED() macro, which is the more convenient and compiles (on x86_64) to just a couple instructions:
shl and sar.
--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/bitfield.h +16 -0
@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ }) +/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +* +* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\ + }) +/** * FIELD_MODIFY() - modify a bitfield element * @_mask: shifted mask defining the field's length and position
Signature · 1 line
2.51.0
Quoted 15 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.
...
include/linux/bitfield.h line 194
Quoted 7 lines
include/linux/bitfield.h +6
-0+#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof (mask))((long long )(reg) << \ +__builtin_clzll (mask) >> (__builtin_clzll (mask) + \ +__builtin_ctzll (mask))));\
I would re-indent these lines as
((__signed_scalar_typeof(mask)) ((long long)(reg) << __builtin_clzll(mask) >> \ (__builtin_clzll(mask) + __builtin_ctzll(mask)))); \
Quoted 1 line
include/linux/bitfield.h +1
-0+ })
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 33 lines · On Fri, 17 Apr 2026 13:36:12 -0400 Yury Norov <[email protected]> wrote:
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Signed-off-by Yury Norov <[email protected]>--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/bitfield.h +4
-0@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ }) +/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield
include/linux/bitfield.h line 194
Quoted 10 lines
include/linux/bitfield.h +10
-0+* +* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\
Have you looked at what that generates on a typical 32bit architecture?
It really a bad idea to use __signed_scalar_typeof() on anything that isn't a simple variable.
The bloat from all this when 'mask' is an expansion of GENMASK() is horrid. Indeed both signed_scalar_typeof() and unsigned_scalar_typeof() should really not be used - there are generally much better ways.
In this case you can just write:
({
auto _mask = mask;
unsigned int __sl = __builtin_clzll(_mask);
unsigned int __sr = __sl + __builtin_ctzll(_mask);
__builtin_chose_expr(sizeof(_mask) <= 4,
(int)(reg) << __sl - 32 >> __sr - 32,
((long long)(reg) << __sl >> __sr)
})and let the compiler do any more integer promotions (etc).
I'm also not convinced that the checks __BF_FIELD_CHECK() does on 'reg' are in any sense worth the effort.
I have tried some simpler alternatives, eg:
!__builtin_constant_p(reg) && statically_true((reg & mask) == 0)
however that throws up some false positives due to some of weird ways people have used FIELD_GET() where it is nothing like the simplest (or most obvious) way to do things.
That might have been the code that split a 32bit value into bytes in a printf with:
FIELD_GET(GENMASK(7, 0), val), FIELD_GET(GENMASK(15, 8), val), FIELD_GET(GENMASK(23, 16), val), FIELD_GET(GENMASK(31, 24), val), David
Quoted 5 lines
include/linux/bitfield.h +2
-0+ }) +/** * FIELD_MODIFY() - modify a bitfield element * @_mask: shifted mask defining the field's length and position
Quoted 32 lines · David Laight
Quoted 29 lines · On Fri, 17 Apr 2026 13:36:12 -0400 Yury Norov <[email protected]> wrote:
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Signed-off-by Yury Norov <[email protected]>--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/bitfield.h +0
-0@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ })
Quoted 16 lines
Quoted 4 lines
include/linux/bitfield.h +4
-0+/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield include/linux/bitfield.h line 194
Quoted 10 lines
include/linux/bitfield.h +10
-0+* +* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\Have you looked at what that generates on a typical 32bit architecture?
Yes, for arm32:
#define FIELD_GET_SIGNED(mask, reg) \
((long long)(reg) << \
__builtin_clzll(mask) >> (__builtin_clzll(mask) + \
__builtin_ctzll(mask))) long long foo(long long reg)
{
return FIELD_GET_SIGNED(0x00f00000ULL, reg);
}generates:
foo(long long):
lsls r1, r0, #8
asrs r0, r1, #28
asrs r1, r1, #31
bx lrJust as good as x86_64.
https://godbolt.org/z/eMnKrnocq
Quoted 5 lines
It really a bad idea to use
__signed_scalar_typeof()on anything that isn't a simple variable.
The bloat from all this when 'mask' is an expansion ofGENMASK()is horrid. Indeed bothsigned_scalar_typeof()andunsigned_scalar_typeof()should really not be used - there are generally much better ways.
David, it's not the first time you're throwing "bad idea, horrid bloat, really not be used"-like rant with absolutely no evidence that people do something wrong. Today I became another random victim of your style of communication, and I don't think there's any benefit to tolerate it for me or anybody else.
I encourage you to change your attitude, and use professional and specific communication style in the kernel mailing list.
Starting from now, I'm not a free tester for your ideas anymore. If you think that my patch is wrong, please prove it yourself. If you think that 32-bit or whatever code generation is bad - please send an example. If you believe that your implementation is any better - please bother yourself to convince me.
I will continue receiving patches from you in my tree, but if your patch is claimed to improve code generation, performance of any sort, or similar things, and doesn't provide any numbers - I'll not waste my time on it.
Thanks,
Yury
Quoted 31 lines
In this case you can just write:
({ auto _mask = mask; unsigned int __sl = __builtin_clzll(_mask); unsigned int __sr = __sl + __builtin_ctzll(_mask); __builtin_chose_expr(sizeof(_mask) <= 4, (int)(reg) << __sl - 32 >> __sr - 32, ((long long)(reg) << __sl >> __sr) })and let the compiler do any more integer promotions (etc).
I'm also not convinced that the checks
__BF_FIELD_CHECK()does on 'reg' are in any sense worth the effort.I have tried some simpler alternatives, eg:
!__builtin_constant_p(reg) && statically_true((reg & mask) == 0)however that throws up some false positives due to some of weird ways people have used
FIELD_GET()where it is nothing like the simplest (or most obvious) way to do things.
That might have been the code that split a 32bit value into bytes in a printf with:FIELD_GET(GENMASK(7, 0), val), FIELD_GET(GENMASK(15, 8), val), FIELD_GET(GENMASK(23, 16), val), FIELD_GET(GENMASK(31, 24), val), DavidQuoted 5 lines
include/linux/bitfield.h +2
-0+ }) +/** * FIELD_MODIFY() - modify a bitfield element * @_mask: shifted mask defining the field's length and position
Quoted 8 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);
That's indeed pretty awful...
Quoted 2 lines
+#define
FIELD_GET_SIGNED(mask, reg)\
[...]
I (personally) tend to prefer the "__MAKE_OP" versions (*_get_bits() etc.), in particular because WiFi and firmware interfaces deal a lot with fixed endian fields.
Any chance it'd be simple to generate u32_get_bits_signed() etc.? Could be especially useful for le32_get_bits_signed() for example, to have the endian conversion built-in unlike FIELD_GET_SIGNED().
johannes
On Mon, 20 Apr 2026 10:43:08 +0200
Johannes Berg <[email protected]> wrote:
...
Quoted 7 lines
I (personally) tend to prefer the "
__MAKE_OP" versions (*_get_bits()etc.), in particular because WiFi and firmware interfaces deal a lot with fixed endian fields.Any chance it'd be simple to generate
u32_get_bits_signed()etc.? Could be especially useful forle32_get_bits_signed()for example, to have the endian conversion built-in unlikeFIELD_GET_SIGNED().
The problem is that the number of options explodes.
You need PREP/GET/GET_SIGNED functions x_64() x_64le() x_64be() x_32() x_32le() x_32be() x_16le() and x_16be().
I make that 24 functions.
For UPDATE you also need x_16() and x_8() for another 10.
So at least 34 definitions - it is all getting silly.
(And I've excluded the pointless 8/16 bit functions where the result is promoted to signed int.)
Then you have the problem that some (common) architectures don't have byteswap instructions, but do have byteswapping memory accesses (maybe not 16bit). Which means that is it generally better to byteswap the value before passing to FIELD_GET() and after building the full 'word' using FIELD_PREP() (and just | for single bits).
(Except it is probably always better to byteswap constants.)
It is also worth nothing that FIELD_PREP() will reject requests to fill signed fields with negative constants.
David
Quoted 3 lines
johannes
Quoted 13 lines · Johannes Berg
Quoted 8 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);That's indeed pretty awful...
Quoted 8 lines
Quoted 2 lines
+#define
FIELD_GET_SIGNED(mask, reg)\[...]
I (personally) tend to prefer the "
__MAKE_OP" versions (*_get_bits()etc.), in particular because WiFi and firmware interfaces deal a lot with fixed endian fields.
I don't like that __MAKE_OP magic because whatever it generates is not greppable. And because we disable strict type checks for kernel, but this API claims to typecheck the parameters for the user. So, the following compiles well:
u64 val = 0;
ret = le16_get_bits(val, GENMASK(15, 10));I don't like autogeneration in general. We generate, for example, be32_get_bits(), but never use it. We don't even know the level of the bloat.
Quoted 3 lines
Any chance it'd be simple to generate
u32_get_bits_signed()etc.? Could be especially useful forle32_get_bits_signed()for example, to have the endian conversion built-in unlikeFIELD_GET_SIGNED().
Maybe this:
FIELD_GET_SIGNED(mask, le32_to_cpu(reg))
Thanks,
Yury
On Fri, 24 Apr 2026 12:35:51 -0400
Yury Norov <[email protected]> wrote:
...
Quoted 7 lines
Quoted 3 lines
Any chance it'd be simple to generate
u32_get_bits_signed()etc.? Could be especially useful forle32_get_bits_signed()for example, to have the endian conversion built-in unlikeFIELD_GET_SIGNED().Maybe this:
x = FIELD_GET_SIGNED(mask, le32_to_cpu(reg))
But if you are going to follow it by:
x1 = FIELD_GET_SIGNED(mask1, le32_to_cpu(reg))
you really want to to the byteswap once, best as:
reg = le32_to_cpu(struct->member); David
Quoted 4 lines
Thanks,
Yury
Quoted 9 lines · Yury Norov
Quoted 3 lines
I (personally) tend to prefer the "
__MAKE_OP" versions (*_get_bits()etc.), in particular because WiFi and firmware interfaces deal a lot with fixed endian fields.I don't like that
__MAKE_OPmagic because whatever it generates is not greppable. And because we disable strict type checks for kernel, but this API claims to typecheck the parameters for the user. So, the following compiles well:
Quoted 5 lines
u64 val = 0; ret = le16_get_bits(val, GENMASK(15, 10));I don't like autogeneration in general. We generate, for example,
be32_get_bits(), but never use it.
That's a lot of "I don't like", but whatever.
Quoted 1 line
We don't even know the level of the bloat.
These are static inlines so there's no binary cost, and given that you're complaining about them being generated you can't really *also* complain about too much code...
Quoted 7 lines
Quoted 3 lines
Any chance it'd be simple to generate
u32_get_bits_signed()etc.? Could be especially useful forle32_get_bits_signed()for example, to have the endian conversion built-in unlikeFIELD_GET_SIGNED().Maybe this:
FIELD_GET_SIGNED(mask, le32_to_cpu(reg))
Awful. "I don't like". But we rarely deal with bit-packed signed values anyway.
johannes
Quoted 16 lines · On Mon, 27 Apr 2026 10:29:21 +0200 Johannes Berg <[email protected]>…
Quoted 9 lines · Yury Norov
Quoted 3 lines
I (personally) tend to prefer the "
__MAKE_OP" versions (*_get_bits()etc.), in particular because WiFi and firmware interfaces deal a lot with fixed endian fields.I don't like that
__MAKE_OPmagic because whatever it generates is not greppable. And because we disable strict type checks for kernel, but this API claims to typecheck the parameters for the user. So, the following compiles well:Quoted 5 lines
u64 val = 0; ret = le16_get_bits(val, GENMASK(15, 10));I don't like autogeneration in general. We generate, for example,
be32_get_bits(), but never use it.
Quoted 8 lines
That's a lot of "I don't like", but whatever.
Quoted 1 line
We don't even know the level of the bloat.
These are static inlines so there's no binary cost, and given that you're complaining about them being generated you can't really *also* complain about too much code...
There is a measurable compile-time cost for processing the definitions of static inlines even when they aren't used.
While processing the definitions of #defines is cheap, processing the expansions can be measurable. Particularly when expansions get nested as often happens when GENMASK() is used as an argument to FIELD_xxx().
Even trivial #defines can affect compile times.
The 'size' check in READ_ONCE() (etc) costs about 1%.
(1% may not sound much, but find 10 of them and it becomes significant.) I suspect a lot of that is adding the extra external function to hold the error message.
David
Quoted 14 lines
Quoted 8 lines
Quoted 4 lines
Any chance it'd be simple to generate
u32_get_bits_signed()etc.? Could be especially useful forle32_get_bits_signed()for example, to have the endian conversion built-in unlikeFIELD_GET_SIGNED().Maybe this:
FIELD_GET_SIGNED(mask, le32_to_cpu(reg))Awful. "I don't like". But we rarely deal with bit-packed signed values anyway.
johannes
Peter Zijlstra
maintainerQuoted 34 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Signed-off-by Yury Norov <[email protected]>--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/bitfield.h +5
-0@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ }) +/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +*
include/linux/bitfield.h line 195
Quoted 10 lines
include/linux/bitfield.h +10
-0+* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\ + })
IIRC clz is count-leading-zeros and ctz is count-trailing-zeros. Most of the other FIELD things use __bf_shf() which is defined in terms of ffs - 1 (which is another way of writing ctz).
So how about you start by redefining __bf_shf() in ctz, and then add another helper for the clz and write the thing something like:
((long long)(reg) << __bf_clz(mask)) >> (__bf_clz(mask) + __bf_shf(mask));
Also, since the order of the shifts is rather important, I think it makes sense to add this extra pair of (), even when not strictly needed, just to make it easier to read.
Quoted 50 lines · Peter Zijlstra
Quoted 34 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Signed-off-by Yury Norov <[email protected]>--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/bitfield.h +5
-0@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ }) +/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +* include/linux/bitfield.h line 195
Quoted 10 lines
include/linux/bitfield.h +10
-0+* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\ + })IIRC clz is count-leading-zeros and ctz is count-trailing-zeros. Most of the other FIELD things use
__bf_shf()which is defined in terms of ffs - 1 (which is another way of writing ctz).
Quoted 4 lines
So how about you start by redefining
__bf_shf()in ctz, and then add another helper for the clz and write the thing something like:((long long)(reg) << __bf_clz(mask)) >> (__bf_clz(mask) + __bf_shf(mask));
So...
I like the shorter form, but whatever we add in the bitfield.h - we'll have to support it.
For example, __bf_shf() wasn't intended to be used outsize of the header, thus double underscored. But there's over 100 external users now. And to make it worse, it's broken for GCC 14 and earlier:
https://lore.kernel.org/all/[email protected]/
So needs to get fixed.
The bitfield.h has two __bf macros: __bf_shf() and __bf_cast_unsigned(). They are thin wrappers, but after all do something with the corresponding builtins output. The __bf_cls() would be a pure renaming. I'm OK with that, but some people don't:
https://lore.kernel.org/all/[email protected]/
That's why I didn't make FIELD_GET_SIGNED() implementation looking nicer. If you strongly prefer the shorter version, I can do that in v2.
Quoted 3 lines
Also, since the order of the shifts is rather important, I think it makes sense to add this extra pair of (), even when not strictly needed, just to make it easier to read.
Sure, will do.
Quoted 56 lines · On Mon, 20 Apr 2026 13:54:57 -0400 Yury Norov <[email protected]> wrote:
Quoted 50 lines · Peter Zijlstra
Quoted 34 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit and proper 32 vs 64 function flavor.
Thus, introduce a
FIELD_GET_SIGNED()macro, which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Signed-off-by Yury Norov <[email protected]>--- include/linux/bitfield.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/bitfield.h +5
-0@@ -178,6 +178,22 @@__FIELD_GET (_mask, _reg,"FIELD_GET: " ); \ }) +/** +* FIELD_GET_SIGNED() - extract a signed bitfield element +* @mask: shifted mask defining the field's length and position +* @reg: value of entire bitfield +* include/linux/bitfield.h line 195
Quoted 10 lines
include/linux/bitfield.h +10
-0+* Returns the sign-extended field specified by @_mask from the +* bitfield passed in as @_reg by masking and shifting it down. +*/ +#define FIELD_GET_SIGNED (mask, reg) \ + ({ \ + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ + ((__signed_scalar_typeof(mask))((long long)(reg) << \ + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ + __builtin_ctzll(mask))));\ + })IIRC clz is count-leading-zeros and ctz is count-trailing-zeros. Most of the other FIELD things use
__bf_shf()which is defined in terms of ffs - 1 (which is another way of writing ctz).Quoted 4 lines
So how about you start by redefining
__bf_shf()in ctz, and then add another helper for the clz and write the thing something like:((long long)(reg) << __bf_clz(mask)) >> (__bf_clz(mask) + __bf_shf(mask));
Quoted 8 lines
So...
I like the shorter form, but whatever we add in the
bitfield.h- we'll have to support it.For example,
__bf_shf()wasn't intended to be used outsize of the header, thus double underscored. But there's over 100 external users now. And to make it worse, it's broken for GCC 14 and earlier:
For anyone who hasn't followed the gory details it isn't 'very broken'. Basically __builtin_ffsll() doesn't always generate an 'integer constant expression' from constant input so you can get a compile fail.
Quoted 6 lines
https://lore.kernel.org/all/[email protected]/
So needs to get fixed.
The
bitfield.hhas two__bfmacros:__bf_shf()and__bf_cast_unsigned(). They are thin wrappers,
__bf_cast_unsigned() isn't exactly thin.
David
Quoted 15 lines
but after all do something with the corresponding
builtins output. The__bf_cls()would be a pure renaming. I'm OK with that, but some people don't:https://lore.kernel.org/all/[email protected]/
That's why I didn't make
FIELD_GET_SIGNED()implementation looking nicer. If you strongly prefer the shorter version, I can do that in v2.Quoted 3 lines
Also, since the order of the shifts is rather important, I think it makes sense to add this extra pair of (), even when not strictly needed, just to make it easier to read.
Sure, will do.
PATCH 2/9 x86/extable: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovThe EX_DATA register is laid out such that EX_DATA_IMM occupied MSB. It's done to make sure that FIELD_GET() will sign-extend the IMM field during extraction.
To enforce that, all EX_DATA masks are made signed integers. This works, but relies on the particular implementation of FIELD_GET(), i.e. masking then shifting, not vice versa; and the particular placement of the fields in the register.
Switch to using the dedicated FIELD_GET_SIGNED(), and relax those limitations.
--- arch/x86/include/asm/extable_fixup_types.h | 13 ++++--------- arch/x86/mm/extable.c | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-)
arch/x86/include/asm/extable_fixup_types.h +4 -9
@@ -2,15 +2,10 @@#ifndef _ASM_X86_EXTABLE_FIXUP_TYPES_H#define _ASM_X86_EXTABLE_FIXUP_TYPES_H -/* -* Our IMM is signed, as such it must live at the top end of the word. Also, -* since C99 hex constants are of ambiguous type, force cast the mask to 'int' -* so that FIELD_GET() will DTRT and sign extend the value when it extracts it. -*/ -#define EX_DATA_TYPE_MASK ((int)0x000000FF) -#define EX_DATA_REG_MASK ((int)0x00000F00) -#define EX_DATA_FLAG_MASK ((int)0x0000F000) -#define EX_DATA_IMM_MASK ((int)0xFFFF0000) +#define EX_DATA_TYPE_MASK (0x000000FF) +#define EX_DATA_REG_MASK (0x00000F00) +#define EX_DATA_FLAG_MASK (0x0000F000) +#define EX_DATA_IMM_MASK (0xFFFF0000)#define EX_DATA_REG_SHIFT 8#define EX_DATA_FLAG_SHIFT 12
arch/x86/mm/extable.c +1 -1
@@ -322,7 +322,7 @@ int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code, type =FIELD_GET (EX_DATA_TYPE_MASK , e->data); reg =FIELD_GET (EX_DATA_REG_MASK , e->data); - imm =FIELD_GET (EX_DATA_IMM_MASK , e->data); + imm =FIELD_GET_SIGNED (EX_DATA_IMM_MASK , e->data);switch (type) {case EX_TYPE_DEFAULT :
Signature · 1 line
2.51.0
Peter Zijlstra
maintainerRe: [PATCH 2/9] x86/extable: switch to using FIELD_GET_SIGNED()
Quoted 8 lines · Yury Norov
The
EX_DATAregister is laid out such thatEX_DATA_IMMoccupied MSB. It's done to make sure thatFIELD_GET()will sign-extend the IMM field during extraction.To enforce that, all
EX_DATAmasks are made signed integers. This works, but relies on the particular implementation ofFIELD_GET(), i.e. masking then shifting, not vice versa; and the particular placement of the fields in the register.
I don't think the order of the mask and shift matters in this case. If we were to first shift down and then mask, it would still work (after all, the mask would also need to be shifted and would also get sign extended, effectively ending up as -1).
But yes, this very much depends on the signed field being the topmost field and including the MSB.
Re: [PATCH 2/9] x86/extable: switch to using FIELD_GET_SIGNED()
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Yury Norov
The
EX_DATAregister is laid out such thatEX_DATA_IMMoccupied MSB. It's done to make sure thatFIELD_GET()will sign-extend the IMM field during extraction.To enforce that, all
EX_DATAmasks are made signed integers. This works, but relies on the particular implementation ofFIELD_GET(), i.e. masking then shifting, not vice versa; and the particular placement of the fields in the register.
Quoted 4 lines
I don't think the order of the mask and shift matters in this case. If we were to first shift down and then mask, it would still work (after all, the mask would also need to be shifted and would also get sign extended, effectively ending up as -1).
FIELD_GET() doesn't require mask to be signed when a reg is signed, so shifting mask may become zero-extended in an alternative implementation:
(reg >> __bf_shf(mask)) & (mask >> __bf_shf(mask)
This all is hypothetical, anyways.
Quoted 2 lines
But yes, this very much depends on the signed field being the topmost field and including the MSB.
This is the part I dislike mostly. This would look just like undefined behavior for the API user: depending on fields placement or type of the inputs, sometimes FIELD_GET() sign-extendeds the field, and sometimes not.
We could likely force FIELD_GET() to treat both reg and mask as unsigned types, and state that explicitly in the documentation.
Re: [PATCH 2/9] x86/extable: switch to using FIELD_GET_SIGNED()
Quoted 26 lines · On Mon, 20 Apr 2026 13:18:47 -0400 Yury Norov <[email protected]> wrote:
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Yury Norov
The
EX_DATAregister is laid out such thatEX_DATA_IMMoccupied MSB. It's done to make sure thatFIELD_GET()will sign-extend the IMM field during extraction.To enforce that, all
EX_DATAmasks are made signed integers. This works, but relies on the particular implementation ofFIELD_GET(), i.e. masking then shifting, not vice versa; and the particular placement of the fields in the register.Quoted 4 lines
I don't think the order of the mask and shift matters in this case. If we were to first shift down and then mask, it would still work (after all, the mask would also need to be shifted and would also get sign extended, effectively ending up as -1).
FIELD_GET()doesn't require mask to be signed when a reg is signed, so shifting mask may become zero-extended in an alternative implementation:(reg >> __bf_shf(mask)) & (mask >> __bf_shf(mask)This all is hypothetical, anyways.
Quoted 2 lines
But yes, this very much depends on the signed field being the topmost field and including the MSB.
Quoted 8 lines
This is the part I dislike mostly. This would look just like undefined behavior for the API user: depending on fields placement or type of the inputs, sometimes
FIELD_GET()sign-extendeds the field, and sometimes not.We could likely force
FIELD_GET()to treat both reg and mask as unsigned types, and state that explicitly in the documentation.
There is already a BUILD_BUG_ON((_mask) == 0), changing it to >= 0 will detect negative masks.
I think the only one is the x86 exception table.FIELD_GET() casts the result to typeof(_mask) so the sign of 'reg' shouldn't matter.
I just tried building with a compile-time check for reg being negative. But there are too many false positives from FIELD_GET(mask, readl(addr)) and FIELD_GET(mask, READ_ONCE(var)).
The pre-processor expansions of those don't bear thinking about.
It's late now, but I will check how __unsigned_scalar_typeof() handles variables with const or volatile qualifiers.
I think they do though the 'default' the same at bitfields.
David
PATCH 3/9 iio: intel_dc_ti_adc: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't provide the fields length explicitly.
--- drivers/iio/adc/intel_dc_ti_adc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/adc/intel_dc_ti_adc.c +2 -2
@@ -290,8 +290,8 @@ static int dc_ti_adc_probe(struct platform_device *pdev)if (ret)return ret; - info->vbat_zse =sign_extend32 (FIELD_GET (DC_TI_VBAT_ZSE , val),3 ); - info->vbat_ge =sign_extend32 (FIELD_GET (DC_TI_VBAT_GE , val),3 ); + info->vbat_zse =FIELD_GET_SIGNED (DC_TI_VBAT_ZSE , val); + info->vbat_ge =FIELD_GET_SIGNED (DC_TI_VBAT_GE , val);dev_dbg (dev,"vbat-zse %d vbat-ge %d\n" , info->vbat_zse, info->vbat_ge);
Signature · 1 line
2.51.0
Re: [PATCH 3/9] iio: intel_dc_ti_adc: switch to using FIELD_GET_SIGNED()
Quoted 4 lines · On Fri, 17 Apr 2026 13:36:14 -0400 Yury Norov <[email protected]> wrote:
Switch from sign_extend32(FIELD_GET()) to the dedicated
FIELD_GET_SIGNED()and don't provide the fields length explicitly.Signed-off-by Yury Norov <[email protected]>
Assuming any remaining discussion on the implementation of the macro shakes out, this looks like a good little cleanup to me.
Not sure how you want to merge this but if it's going through another tree.
Quoted 19 lines
--- drivers/iio/adc/intel_dc_ti_adc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/adc/intel_dc_ti_adc.c +2
-2@@ -290,8 +290,8 @@ static int dc_ti_adc_probe(struct platform_device *pdev)if (ret)return ret; - info->vbat_zse =sign_extend32 (FIELD_GET (DC_TI_VBAT_ZSE , val),3 ); - info->vbat_ge =sign_extend32 (FIELD_GET (DC_TI_VBAT_GE , val),3 ); + info->vbat_zse =FIELD_GET_SIGNED (DC_TI_VBAT_ZSE , val); + info->vbat_ge =FIELD_GET_SIGNED (DC_TI_VBAT_GE , val);dev_dbg (dev,"vbat-zse %d vbat-ge %d\n" , info->vbat_zse, info->vbat_ge);
PATCH 4/9 iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/iio/magnetometer/yamaha-yas530.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iio/magnetometer/yamaha-yas530.c +6 -6
@@ -859,9 +859,9 @@ static int yas530_get_calibration_data(struct yas5xx *yas5xx) c->f[0 ] =FIELD_GET (GENMASK (22 ,21 ), val); c->f[1 ] =FIELD_GET (GENMASK (14 ,13 ), val); c->f[2 ] =FIELD_GET (GENMASK (6 ,5 ), val); - c->r[0 ] =sign_extend32 (FIELD_GET (GENMASK (28 ,23 ), val),5 ); - c->r[1 ] =sign_extend32 (FIELD_GET (GENMASK (20 ,15 ), val),5 ); - c->r[2 ] =sign_extend32 (FIELD_GET (GENMASK (12 ,7 ), val),5 ); + c->r[0 ] =FIELD_GET_SIGNED (GENMASK (28 ,23 ), val); + c->r[1 ] =FIELD_GET_SIGNED (GENMASK (20 ,15 ), val); + c->r[2 ] =FIELD_GET_SIGNED (GENMASK (12 ,7 ), val);return 0 ; }
@@ -914,9 +914,9 @@ static int yas532_get_calibration_data(struct yas5xx *yas5xx) c->f[0 ] =FIELD_GET (GENMASK (24 ,23 ), val); c->f[1 ] =FIELD_GET (GENMASK (16 ,15 ), val); c->f[2 ] =FIELD_GET (GENMASK (8 ,7 ), val); - c->r[0 ] =sign_extend32 (FIELD_GET (GENMASK (30 ,25 ), val),5 ); - c->r[1 ] =sign_extend32 (FIELD_GET (GENMASK (22 ,17 ), val),5 ); - c->r[2 ] =sign_extend32 (FIELD_GET (GENMASK (14 ,7 ), val),5 ); + c->r[0 ] =FIELD_GET_SIGNED (GENMASK (30 ,25 ), val); + c->r[1 ] =FIELD_GET_SIGNED (GENMASK (22 ,17 ), val); + c->r[2 ] =FIELD_GET_SIGNED (GENMASK (14 ,7 ), val);return 0 ; }
Signature · 1 line
2.51.0
Re: [PATCH 4/9] iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()
Quoted 4 lines · On Fri, 17 Apr 2026 13:36:15 -0400 Yury Norov <[email protected]> wrote:
Switch from sign_extend32(FIELD_GET()) to the dedicated
FIELD_GET_SIGNED()and don't calculate the fields length explicitly.Signed-off-by Yury Norov <[email protected]>
Re: [PATCH 4/9] iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovQuoted 4 lines · Yury Norov
Switch from sign_extend32(FIELD_GET()) to the dedicated
FIELD_GET_SIGNED()and don't calculate the fields length explicitly.Signed-off-by Yury Norov <[email protected]>
Very nice,
Yours,
Linus Walleij
PATCH 5/9 iio: pressure: bmp280: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/iio/pressure/bmp280-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/pressure/bmp280-core.c +1 -1
@@ -392,7 +392,7 @@ static int bme280_read_calib(struct bmp280_data *data) h4_lower =FIELD_GET (BME280_COMP_H4_MASK_LOW , tmp_1); calib->H4 =sign_extend32 (h4_upper | h4_lower,11 ); tmp_3 =get_unaligned_le16 (&data->bme280_humid_cal_buf[H5 ]); - calib->H5 =sign_extend32 (FIELD_GET (BME280_COMP_H5_MASK , tmp_3),11 ); + calib->H5 =FIELD_GET_SIGNED (BME280_COMP_H5_MASK , tmp_3); calib->H6 = data->bme280_humid_cal_buf[H6 ];return 0 ;
Signature · 1 line
2.51.0
Re: [PATCH 5/9] iio: pressure: bmp280: switch to using FIELD_GET_SIGNED()
Quoted 4 lines · On Fri, 17 Apr 2026 13:36:16 -0400 Yury Norov <[email protected]> wrote:
Switch from sign_extend32(FIELD_GET()) to the dedicated
FIELD_GET_SIGNED()and don't calculate the fields length explicitly.Signed-off-by Yury Norov <[email protected]>
PATCH 6/9 iio: mcp9600: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/iio/temperature/mcp9600.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +1 -1
@@ -297,7 +297,7 @@ static int mcp9600_read_thresh(struct iio_dev *indio_dev,* Temperature is stored in two’s complement format in * bits(15:2), LSB is 0.25 degree celsius. */ - *val =sign_extend32 (FIELD_GET (MCP9600_ALERT_LIMIT_MASK , ret),13 ); + *val =FIELD_GET_SIGNED (MCP9600_ALERT_LIMIT_MASK , ret); *val2 =4 ;return IIO_VAL_FRACTIONAL ;case IIO_EV_INFO_HYSTERESIS :
Signature · 1 line
2.51.0
Re: [PATCH 6/9] iio: mcp9600: switch to using FIELD_GET_SIGNED()
Quoted 4 lines · On Fri, 17 Apr 2026 13:36:17 -0400 Yury Norov <[email protected]> wrote:
Switch from sign_extend32(FIELD_GET()) to the dedicated
FIELD_GET_SIGNED()and don't calculate the fields length explicitly.Signed-off-by Yury Norov <[email protected]>
PATCH 7/9 wifi: rtw89: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/net/wireless/realtek/rtw89/rtw8852a_rfk.c | 4 ++-- drivers/net/wireless/realtek/rtw89/rtw8852b_common.c | 4 ++-- drivers/net/wireless/realtek/rtw89/rtw8852b_rfk.c | 4 ++-- drivers/net/wireless/realtek/rtw89/rtw8852c.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
drivers/net/wireless/realtek/rtw89/rtw8852a_rfk.c +2 -2
@@ -334,8 +334,8 @@ static void _check_addc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)for (i =0 ; i <ADDC_T_AVG ; i++) { tmp =rtw89_phy_read32_mask (rtwdev,R_DBG32_D ,MASKDWORD ); - dc_re +=sign_extend32 (FIELD_GET (0xfff000 , tmp),11 ); - dc_im +=sign_extend32 (FIELD_GET (0xfff , tmp),11 ); + dc_re +=FIELD_GET_SIGNED (0xfff000 , tmp); + dc_im +=FIELD_GET_SIGNED (0xfff , tmp); } dc_re /=ADDC_T_AVG ;
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c +2 -2
@@ -206,9 +206,9 @@ static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev,static bool _decode_efuse_gain (u8 data,s8 *high,s8 *low) {if (high) - *high =sign_extend32 (FIELD_GET (GENMASK (7 ,4 ), data),3 ); + *high =FIELD_GET_SIGNED (GENMASK (7 ,4 ), data);if (low) - *low =sign_extend32 (FIELD_GET (GENMASK (3 ,0 ), data),3 ); + *low =FIELD_GET (GENMASK (3 ,0 ), data);return data !=0xff ; }
drivers/net/wireless/realtek/rtw89/rtw8852b_rfk.c +2 -2
@@ -497,8 +497,8 @@ static void _check_addc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)for (i =0 ; i <ADDC_T_AVG ; i++) { tmp =rtw89_phy_read32_mask (rtwdev,R_DBG32_D ,MASKDWORD ); - dc_re +=sign_extend32 (FIELD_GET (0xfff000 , tmp),11 ); - dc_im +=sign_extend32 (FIELD_GET (0xfff , tmp),11 ); + dc_re +=FIELD_GET_SIGNED (0xfff000 , tmp); + dc_im +=FIELD_GET_SIGNED (0xfff , tmp); } dc_re /=ADDC_T_AVG ;
drivers/net/wireless/realtek/rtw89/rtw8852c.c +2 -2
@@ -517,9 +517,9 @@ static void rtw8852c_efuse_parsing_tssi(struct rtw89_dev *rtwdev,static bool _decode_efuse_gain (u8 data,s8 *high,s8 *low) {if (high) - *high =sign_extend32 (FIELD_GET (GENMASK (7 ,4 ), data),3 ); + *high =FIELD_GET_SIGNED (GENMASK (7 ,4 ), data);if (low) - *low =sign_extend32 (FIELD_GET (GENMASK (3 ,0 ), data),3 ); + *low =FIELD_GET_SIGNED (GENMASK (3 ,0 ), data);return data !=0xff ; }
Signature · 1 line
2.51.0
Ping-Ke Shih
maintainerRE: [PATCH 7/9] wifi: rtw89: switch to using FIELD_GET_SIGNED()
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev, · line 211
Quoted 11 lines · Yury Norov
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c +2
-2@@ -206,9 +206,9 @@ static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev,static bool _decode_efuse_gain (u8 data,s8 *high,s8 *low) {if (high) - *high =sign_extend32 (FIELD_GET (GENMASK (7 ,4 ), data),3 ); + *high =FIELD_GET_SIGNED (GENMASK (7 ,4 ), data);if (low) - *low =sign_extend32 (FIELD_GET (GENMASK (3 ,0 ), data),3 ); + *low =FIELD_GET (GENMASK (3 ,0 ), data);
FIELD_GET_SIGNED()?
Quoted 3 lines
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c +0
-0return data !=0xff ; }
Re: [PATCH 7/9] wifi: rtw89: switch to using FIELD_GET_SIGNED()
Quoted 18 lines · Ping-Ke Shih
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev, · line 211
Quoted 11 lines · Yury Norov
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c +2
-2@@ -206,9 +206,9 @@ static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev,static bool _decode_efuse_gain (u8 data,s8 *high,s8 *low) {if (high) - *high =sign_extend32 (FIELD_GET (GENMASK (7 ,4 ), data),3 ); + *high =FIELD_GET_SIGNED (GENMASK (7 ,4 ), data);if (low) - *low =sign_extend32 (FIELD_GET (GENMASK (3 ,0 ), data),3 ); + *low =FIELD_GET (GENMASK (3 ,0 ), data);
FIELD_GET_SIGNED()?Quoted 3 lines
drivers/net/wireless/realtek/rtw89/rtw8852b_common.c +0
-0return data !=0xff ; }
Ah sorry. Will fix in v2
PATCH 8/9 rtc: rv3032: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/rtc/rtc-rv3032.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/rtc-rv3032.c +1 -1
@@ -376,7 +376,7 @@ static int rv3032_read_offset(struct device *dev, long *offset)if (ret <0 )return ret; - steps =sign_extend32 (FIELD_GET (RV3032_OFFSET_MSK , value),5 ); + steps =FIELD_GET_SIGNED (RV3032_OFFSET_MSK , value); *offset =DIV_ROUND_CLOSEST (steps *OFFSET_STEP_PPT ,1000 );
Signature · 1 line
2.51.0
PATCH 9/9 ptp: switch to using FIELD_GET_SIGNED()
In reply to Yury NorovSwitch from sign_extend32(FIELD_GET()) to the dedicated FIELD_GET_SIGNED() and don't calculate the fields length explicitly.
--- drivers/ptp/ptp_fc3.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ptp/ptp_fc3.c +2 -2
@@ -55,8 +55,8 @@ static s64 tdc_meas2offset(struct idtfc3 *idtfc3, u64 meas_read) {s64 coarse, fine; - fine =sign_extend64 (FIELD_GET (FINE_MEAS_MASK , meas_read),12 ); - coarse =sign_extend64 (FIELD_GET (COARSE_MEAS_MASK , meas_read), (39 -13 )); + fine =FIELD_GET_SIGNED (FINE_MEAS_MASK , meas_read); + coarse =FIELD_GET_SIGNED (COARSE_MEAS_MASK , meas_read); fine =div64_s64 (fine *NSEC_PER_SEC , idtfc3->tdc_apll_freq *62LL ); coarse =div64_s64 (coarse *NSEC_PER_SEC , idtfc3->time_ref_freq);
Signature · 1 line
2.51.0
Quoted 12 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit.
Quoted 6 lines
This series adds a signed version of
FIELD_GET(), which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Patch #1 adds
FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.
Here the example is missing.
Nevertheless, I looked at the implementation a bit and wondering how would it work for 64-bit mask of say GENMASK_ULL(63, 60)? Wouldn't it give an overflow?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 13 lines · Andy Shevchenko
Quoted 12 lines · Yury Norov
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit.
Quoted 8 lines
Quoted 6 lines
This series adds a signed version of
FIELD_GET(), which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Patch #1 adds
FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.Here the example is missing.
This series is full of examples... I'll add one here if you prefer, if it comes to v2.
Quoted 2 lines
Nevertheless, I looked at the implementation a bit and wondering how would it work for 64-bit mask of say
GENMASK_ULL(63, 60)? Wouldn't it give an overflow?
In that case, the '<< __builtin_clzll(mask)' part becomes a NOP, and the compiler only emits a single sar:
long long foo(long long reg)
{
10: f3 0f 1e fa endbr64
return FIELD_GET_SIGNED(GENMASK_ULL(63, 60), reg);
14: 48 89 f8 mov %rdi,%rax
17: 48 c1 f8 3c sar $0x3c,%rax
}Just tested it with a real kernel build with gcc-15.2, and it works as intended.
Jonathan Cameron
maintainerQuoted 12 lines · On Fri, 17 Apr 2026 13:36:11 -0400 Yury Norov <[email protected]> wrote:
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit.
Quoted 7 lines
This series adds a signed version of
FIELD_GET(), which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Patch #1 adds
FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.
Just a quick heads up that I'm beginning to assume that this series will land in some form. If it does can we do it as an immutable branch as I'm suggesting it gets used in some other patches in that should land in the new cycle.
Thanks,
Jonathan
Quoted 26 lines
Yury Norov (9):
bitfield: addFIELD_GET_SIGNED()
x86/extable: switch to usingFIELD_GET_SIGNED()
iio:intel_dc_ti_adc: switch to using
iio: magnetometer: yas530: switch to usingFIELD_GET_SIGNED()
iio: pressure: bmp280: switch to using
iio: mcp9600: switch to usingFIELD_GET_SIGNED()
wifi: rtw89: switch to usingFIELD_GET_SIGNED()
rtc: rv3032: switch to usingFIELD_GET_SIGNED()
ptp: switch to usingFIELD_GET_SIGNED()arch/x86/include/asm/extable_fixup_types.h | 13 ++++--------- arch/x86/mm/extable.c | 2 +- drivers/iio/adc/intel_dc_ti_adc.c | 4 ++-- drivers/iio/magnetometer/yamaha-yas530.c | 12 ++++++------ drivers/iio/pressure/bmp280-core.c | 2 +- drivers/iio/temperature/mcp9600.c | 2 +- .../net/wireless/realtek/rtw89/rtw8852a_rfk.c | 4 ++-- .../net/wireless/realtek/rtw89/rtw8852b_common.c | 4 ++-- .../net/wireless/realtek/rtw89/rtw8852b_rfk.c | 4 ++-- drivers/net/wireless/realtek/rtw89/rtw8852c.c | 4 ++-- drivers/ptp/ptp_fc3.c | 4 ++-- drivers/rtc/rtc-rv3032.c | 2 +- include/linux/bitfield.h | 16 ++++++++++++++++ 13 files changed, 42 insertions(+), 31 deletions(-)
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Fri, 17 Apr 2026 13:36:11 -0400 Yury Norov <[email protected]> wrote:
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit.
This series adds a signed version of
FIELD_GET(), which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.
Quoted 8 lines
Quoted 3 lines
Patch #1 adds
FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.Just a quick heads up that I'm beginning to assume that this series will land in some form. If it does can we do it as an immutable branch as I'm suggesting it gets used in some other patches in that should land in the new cycle.
I'm going to submit v2 soon, as seemingly the discussion is boiled down, and then will likely merge it with my tree. I'll create an immutable branch for you before the end of day.
Thanks,
Yury
Quoted 24 lines · Yury Norov
Quoted 23 lines · Jonathan Cameron
Quoted 19 lines · On Fri, 17 Apr 2026 13:36:11 -0400 Yury Norov <[email protected]> wrote:
The bitfields are designed in assumption that fields contain unsigned integer values, thus extracting the values from the field implies zero-extending.
Some drivers need to sign-extend their fields, and currently do it like:
dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11);It's error-prone because it relies on user to provide the correct index of the most significant bit.
This series adds a signed version of
FIELD_GET(), which is the more convenient and compiles (onx86_64) to just a couple instructions:
shl and sar.Patch #1 adds
FIELD_GET_SIGNED(), and the rest of the series applies it tree-wide.
Quoted 8 lines
Quoted 4 lines
Just a quick heads up that I'm beginning to assume that this series will land in some form. If it does can we do it as an immutable branch as I'm suggesting it gets used in some other patches in that should land in the new cycle.
I'm going to submit v2 soon, as seemingly the discussion is boiled down, and then will likely merge it with my tree. I'll create an immutable branch for you before the end of day.
Here it is:
https://github.com/norov/linux/pull/new/fgsv2
It builds well for me, but I'll wait for a while for robots feedback before making it 'officially' immutable.
Thanks,
Yury