Skip to content

bitfield: add FIELD_GET_SIGNED()

Yury Norov linux-iio linux-rtc linux-wireless lkml netdev 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest9 patches 7 repliesIn review
  2. v19 patches 27 repliesSuperseded

Changes in v2 from the cover letter

- more examples of the new API and code generation (Andy, David);
- fix #7 FIELD_GET() / FIELD_GET_SIGNED() typo (Ping-Ke);
- re-indent the macro (Andy, Peter);

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9bitfield: add FIELD_GET_SIGNED()include/linux/bitfield.h+16 -00 replies
2/9x86/extable: switch to using FIELD_GET_SIGNED()arch/x86/include/asm/extable_fixup_types.h arch/x86/mm/extable.c+5 -102 replies
3/9iio: intel_dc_ti_adc: switch to using FIELD_GET_SIGNED()drivers/iio/adc/intel_dc_ti_adc.c+2 -20 replies
4/9iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()drivers/iio/magnetometer/yamaha-yas530.c+6 -60 replies
5/9iio: pressure: bmp280: switch to using FIELD_GET_SIGNED()drivers/iio/pressure/bmp280-core.c+1 -10 replies
6/9iio: mcp9600: switch to using FIELD_GET_SIGNED()drivers/iio/temperature/mcp9600.c+1 -10 replies
7/9wifi: rtw89: switch to using FIELD_GET_SIGNED()drivers/net/wireless/realtek/rtw89/rtw8852a_rfk.c drivers/net/wireless/realtek/rtw89/rtw8852b_common.c drivers/net/wireless/realtek/rtw89/rtw8852b_rfk.c +1 more+8 -84 replies
8/9rtc: rv3032: switch to using FIELD_GET_SIGNED()drivers/rtc/rtc-rv3032.c+1 -11 reply
9/9ptp: switch to using FIELD_GET_SIGNED()drivers/ptp/ptp_fc3.c+2 -20 replies

Reviews trailers per patch

7 of 9 patches have a Reviewed-by or Acked-by.

PatchJonathan CameronLinus WalleijPeter Zijlstra (Intel)Alexandre BelloniPing-Ke Shih
1/9
2/9A-b
3/9A-b
4/9A-b R-b
5/9A-b
6/9A-b
7/9A-b
8/9A-b
9/9

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.

Read the v2 thread

Cover letter [PATCH v2 0/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(). With the new API, the above snippet turns into the more convenient:

	dc_re += FIELD_GET_SIGNED(0xfff000, tmp);
	dc_im += FIELD_GET_SIGNED(0xfff, tmp);

It compiles (on x86_64) into just a couple instructions: shl and sar. When the mask includes MSB, 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
  }

32-bit code generation is equally well. On arm32:

  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      lr

Immutable branch:

https://github.com/norov/linux/pull/new/fgsv2

v1: https://lore.kernel.org/all/[email protected]/
v2:
- more examples of the new API and code generation (Andy, David);
- fix #7 FIELD_GET() / FIELD_GET_SIGNED() typo (Ping-Ke);
- re-indent the macro (Andy, Peter);

Yury Norov (9):
bitfield: add FIELD_GET_SIGNED()
x86/extable: switch to using FIELD_GET_SIGNED()
iio: intel_dc_ti_adc: switch to using FIELD_GET_SIGNED()
iio: magnetometer: yas530: switch to using FIELD_GET_SIGNED()
iio: pressure: bmp280: switch to using FIELD_GET_SIGNED()
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