Introduce and use generic parity32/64 helper
Subsystems (20)
cover Introduce and use generic parity32/64 helper
Several parts of the kernel contain redundant implementations of parity calculations for 32-bit and 64-bit values. Introduces generic parity32() and parity64() helpers in bitops.h, providing a standardized and optimized implementation.
Subsequent patches refactor various kernel components to replace open-coded parity calculations with the new helpers, reducing code duplication and improving maintainability.
Kuan-Wei Chiu (17):
bitops: Add generic parity calculation for u32
bitops: Add generic parity calculation for u64
x86: Replace open-coded parity calculation with parity8()
media: media/test_drivers: Replace open-coded parity calculation with
parity8()
media: pci: cx18-av-vbi: Replace open-coded parity calculation with
parity8()
media: saa7115: Replace open-coded parity calculation with parity8()
serial: max3100: Replace open-coded parity calculation with parity8()
lib/bch: Replace open-coded parity calculation with parity32()
Input: joystick - Replace open-coded parity calculation with
parity32()
net: ethernet: oa_tc6: Replace open-coded parity calculation with
parity32()
wifi: brcm80211: Replace open-coded parity calculation with parity32()
rm/bridge: dw-hdmi: Replace open-coded parity calculation with
parity32()
mtd: ssfdc: Replace open-coded parity calculation with parity32()
fsi: i2cr: Replace open-coded parity calculation with parity32()
fsi: i2cr: Replace open-coded parity calculation with parity64()
Input: joystick - Replace open-coded parity calculation with
parity64() nfp: bpf: Replace open-coded parity calculation with parity64()
arch/x86/kernel/bootflag.c | 18 ++------ drivers/fsi/fsi-master-i2cr.c | 18 ++------ .../drm/bridge/synopsys/dw-hdmi-ahb-audio.c | 8 +--- drivers/input/joystick/grip_mp.c | 17 +------- drivers/input/joystick/sidewinder.c | 24 +++-------- drivers/media/i2c/saa7115.c | 12 +----- drivers/media/pci/cx18/cx18-av-vbi.c | 12 +----- .../media/test-drivers/vivid/vivid-vbi-gen.c | 8 +--- drivers/mtd/ssfdc.c | 17 +------- drivers/net/ethernet/netronome/nfp/nfp_asm.c | 7 +-- drivers/net/ethernet/oa_tc6.c | 19 ++------ .../broadcom/brcm80211/brcmsmac/dma.c | 16 +------ drivers/tty/serial/max3100.c | 3 +- include/linux/bitops.h | 43 +++++++++++++++++++ lib/bch.c | 14 +----- 15 files changed, 74 insertions(+), 162 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/17 bitops: Add generic parity calculation for u32
Several parts of the kernel open-code parity calculations using different methods. Add a generic parity32() helper implemented with the same efficient approach as parity8().
--- include/linux/bitops.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
include/linux/bitops.h +21 -0
@@ -260,6 +260,27 @@ static inline int parity8(u8 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity32 - get the parity of an u32 value +* @value: the value to be examined +* +* Determine the parity of the u32 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity32 (u32 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Signature · 1 line
2.34.1
PATCH 2/17 bitops: Add generic parity calculation for u64
In reply to Kuan-Wei ChiuSeveral parts of the kernel open-code parity calculations using different methods. Add a generic parity64() helper implemented with the same efficient approach as parity8().
--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/linux/bitops.h +22 -0
@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ; + val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Signature · 1 line
2.34.1
Jiri Slaby
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 25 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +6
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +*
include/linux/bitops.h static inline int parity32(u32 val) · line 299
Quoted 10 lines
include/linux/bitops.h +10
-0+* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ;
Do we need all these implementations? Can't we simply use parity64() for any 8, 16 and 32-bit values too? I.e. have one parity().
Quoted 9 lines
include/linux/bitops.h +6
-0+ val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Signature · 2 lines
js suse labs
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 31 lines · On Mon, 24 Feb 2025 08:09:43 +0100 Jiri Slaby <[email protected]> wrote:
Quoted 30 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +11
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{
Quoted 8 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 299
Quoted 5 lines
include/linux/bitops.h +5
-0+/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ;Do we need all these implementations? Can't we simply use
parity64()for any 8, 16 and 32-bit values too? I.e. have oneparity().
I'm not sure you can guarantee that the compiler will optimise away the unnecessary operations.
But:
static inline int parity64(u64 val)
{
return parity32(val ^ (val >> 32))
}should be ok.
It will also work on x86-32 where parity32() can just check the parity flag. Although you are unlikely to manage to use the the PF the xor sets.
David
Quoted 12 lines
Quoted 10 lines
include/linux/bitops.h +6
-0+ val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 43 lines · David Laight
Quoted 31 lines · On Mon, 24 Feb 2025 08:09:43 +0100 Jiri Slaby <[email protected]> wrote:
Quoted 30 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +11
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{Quoted 8 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 299
Quoted 5 lines
include/linux/bitops.h +5
-0+/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ;Do we need all these implementations? Can't we simply use
parity64()for any 8, 16 and 32-bit values too? I.e. have oneparity().
Quoted 2 lines
I'm not sure you can guarantee that the compiler will optimise away the unnecessary operations.
Hi Jiri and David,
Unless we can be certain about the compiler's optimization behavior, we prefer to follow an approach similar to hweight, distinguishing implementations based on different bit sizes.
Quoted 8 lines
But:
static inline int parity64(u64 val){ return parity32(val ^ (val >> 32)) }should be ok.
We will adopt this approach, as it is indeed more concise.
Thank you all for your feedback.
Best regards,
Yu-Chun Lin
Quoted 18 lines
It will also work on x86-32 where
parity32()can just check the parity flag. Although you are unlikely to manage to use the the PF the xor sets.David
Quoted 12 lines
Quoted 10 lines
include/linux/bitops.h +6
-0+ val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
H. Peter Anvin
reviewerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to David LaightQuoted 52 lines · David Laight
Quoted 31 lines · On Mon, 24 Feb 2025 08:09:43 +0100 Jiri Slaby <[email protected]> wrote:
Quoted 30 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +11
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{Quoted 8 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 299
Quoted 5 lines
include/linux/bitops.h +5
-0+/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ;Do we need all these implementations? Can't we simply use
parity64()for any 8, 16 and 32-bit values too? I.e. have oneparity().I'm not sure you can guarantee that the compiler will optimise away the unnecessary operations.
But:
static inline int parity64(u64 val){ return parity32(val ^ (val >> 32)) }
Quoted 19 lines
should be ok.
It will also work on x86-32 whereparity32()can just check the parity flag. Although you are unlikely to manage to use the the PF the xor sets.David
Quoted 12 lines
Quoted 10 lines
include/linux/bitops.h +6
-0+ val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Sure you can; you do need an 8- and a 16-bit arch implementation though (the 16 bit one being xor %rh,%rl)
H. Peter Anvin
reviewerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to David LaightQuoted 52 lines · David Laight
Quoted 31 lines · On Mon, 24 Feb 2025 08:09:43 +0100 Jiri Slaby <[email protected]> wrote:
Quoted 30 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +11
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* +* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{Quoted 8 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 299
Quoted 5 lines
include/linux/bitops.h +5
-0+/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html +*/ + val ^= val >>32 ;Do we need all these implementations? Can't we simply use
parity64()for any 8, 16 and 32-bit values too? I.e. have oneparity().I'm not sure you can guarantee that the compiler will optimise away the unnecessary operations.
But:
static inline int parity64(u64 val){ return parity32(val ^ (val >> 32)) }
Quoted 19 lines
should be ok.
It will also work on x86-32 whereparity32()can just check the parity flag. Although you are unlikely to manage to use the the PF the xor sets.David
Quoted 12 lines
Quoted 10 lines
include/linux/bitops.h +6
-0+ val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ; +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Incidentally, in all of this, didn't anyone notice __builtin_parity()?
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 1 line
Incidentally, in all of this, didn't anyone notice
__builtin_parity()?
Yes. It it has done sane for a decade on x86, yet does things such as emitting a library call on other architectures.
https://godbolt.org/z/6qG3noebq
~Andrew
H. Peter Anvin
reviewerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 8 lines · Andrew Cooper
Quoted 1 line
Incidentally, in all of this, didn't anyone notice
__builtin_parity()?Yes. It it has done sane for a decade on x86, yet does things such as emitting a library call on other architectures.
https://godbolt.org/z/6qG3noebq
~Andrew
And not even a smart one at that.
Yury Norov
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to Kuan-Wei ChiuQuoted 3 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().
No reason to add parity32() and parity64() in separate patches
Quoted 19 lines
Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +4
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; } +/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +*
include/linux/bitops.h static inline int parity32(u32 val) · line 297
Quoted 10 lines
include/linux/bitops.h +10
-0+* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html
This is already referenced in sources. No need to spread it for more.
include/linux/bitops.h static inline int parity32(u32 val) · line 303
Quoted 6 lines
include/linux/bitops.h +6
-0+*/ + val ^= val >>32 ; + val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ;
It's better to avoid duplicating the same logic again and again.
include/linux/bitops.h static inline int parity32(u32 val) · line 305
Quoted 2 lines
include/linux/bitops.h +2
-0+} +
So maybe make it a macro?
From f17a28ae3429f49825d65ebc0f7717c6a191a3e2 Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
Date: Mon, 24 Feb 2025 14:14:27 -0500
Subject: [PATCH] bitops: generalize parity8()
The generic parity calculation approach may be easily generalized for other standard types. Do that and drop sub-optimal implementation of parity calculation in x86 code.
--- arch/x86/kernel/bootflag.c | 14 +----------- include/linux/bitops.h | 47 +++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 26 deletions(-)
arch/x86/kernel/bootflag.c +1 -13
@@ -2,6 +2,7 @@/* * Implement 'Simple Boot Flag Specification 2.0' */ +#include <linux/bitops.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/init.h>
@@ -20,19 +21,6 @@int sbf_port__initdata =-1 ;/* set via acpi_boot_init() */ -static int __init parity (u8 v) -{ -int x =0 ; -int i; - -for (i =0 ; i <8 ; i++) { - x ^= (v &1 ); - v >>=1 ; - } - -return x; -} -static void __init sbf_write (u8 v) {unsigned long flags;
include/linux/bitops.h +34 -13
68 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Hi Yury,
Quoted 6 lines · Yury Norov
Quoted 3 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().No reason to add
parity32()andparity64()in separate patches
Ack.
Quoted 16 lines
Quoted 16 lines
Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +0
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; }
Quoted 16 lines
Quoted 4 lines
include/linux/bitops.h +4
-0+/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* include/linux/bitops.h static inline int parity32(u32 val) · line 297
Quoted 10 lines
include/linux/bitops.h +10
-0+* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html This is already referenced in sources. No need to spread it for more.
Ack.
Quoted 9 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 303
Quoted 7 lines
include/linux/bitops.h +6
-0+*/ + val ^= val >>32 ; + val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ;It's better to avoid duplicating the same logic again and again.
Ack.
Quoted 116 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 305
Quoted 3 lines
include/linux/bitops.h +2
-0+} +So maybe make it a macro?
From
f17a28ae3429f49825d65ebc0f7717c6a191a3e2Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
Date: Mon, 24 Feb 2025 14:14:27 -0500
Subject: [PATCH] bitops: generalizeparity8()The generic parity calculation approach may be easily generalized for other standard types. Do that and drop sub-optimal implementation of parity calculation in x86 code.
Signed-off-by Yury Norov [NVIDIA] <[email protected]>--- arch/x86/kernel/bootflag.c | 14 +----------- include/linux/bitops.h | 47 +++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 26 deletions(-)arch/x86/kernel/bootflag.c +1
-13@@ -2,6 +2,7 @@/* * Implement 'Simple Boot Flag Specification 2.0' */ +#include <linux/bitops.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/init.h> @@ -20,19 +21,6 @@int sbf_port__initdata =-1 ;/* set via acpi_boot_init() */ -static int __init parity (u8 v) -{ -int x =0 ; -int i; - -for (i =0 ; i <8 ; i++) { - x ^= (v &1 ); - v >>=1 ; - } - -return x; -} -static void __init sbf_write (u8 v) {unsigned long flags;include/linux/bitops.h +25
-13@@ -230,10 +230,10 @@ static inline int get_count_order_long(unsigned long l) }/** -* parity8 - get the parity of an u8 value +* parity - get the parity of a value * @value: the value to be examined * -* Determine the parity of the u8 argument. +* Determine parity of the argument. * * Returns: * 0 for even parity, 1 for odd parity @@ -241,24 +241,45 @@ static inline int get_count_order_long(unsigned long l)* Note: This function informs you about the current parity. Example to bail * out when parity is odd: * -* if (parity8(val) == 1) +* if (parity(val) == 1) * return -EBADMSG; * * If you need to calculate a parity bit, you need to draw the conclusion from * this result yourself. Example to enforce odd parity, parity bit is bit 7: * -* if (parity8(val) == 0) +* if (parity(val) == 0) * val ^= BIT(7); +* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html */ -static inline int parity8 (u8 val) -{ -/* -* One explanation of this algorithm: -* https://funloop.org/codex/problem/parity/README.html -*/ - val ^= val >>4 ; -return (0x6996 >> (val &0xf )) &1 ; -} +#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \
include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 283
Quoted 10 lines
include/linux/bitops.h +9
-0+default : \ +BUILD_BUG (); \ + } \ + __ret; \ +}) + +#define parity8 (val) parity((u8)(val)) +#define parity32 (val) parity((u32)(val)) +#define parity64 (val) parity((u64)(val))
What do you think about using these inline functions instead of macros? Except for parity8(), each function is a single line and follows the same logic. I find inline functions more readable, and coding-style.rst also recommends them over macros.
Regards,
Kuan-Wei
include/linux/bitops.h +20 -0
@@ -260,6 +260,26 @@ static inline int parity8(u8 val)return (0x6996 >> (val &0xf )) &1 ; } +static inline parity16(u16 val) +{ +return parity8 (val ^ (val >>8 )); +} + +static inline parity16(u16 val) +{ +return parity8 (val ^ (val >>8 )); +} + +static inline parity32 (u32) +{ +return parity16 (val ^ (val >>16 )); +} + +static inline parity64 (u64) +{ +return parity32 (val ^ (val >>32 )); +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Quoted 5 lines
include/linux/bitops.h +0
-0/** * __ffs64 - find first set bit in a 64 bit word Signature · 1 line
2.43.0
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 216 lines · Kuan-Wei Chiu
Hi Yury,
Quoted 6 lines · Yury Norov
Quoted 3 lines · Kuan-Wei Chiu
Several parts of the kernel open-code parity calculations using different methods. Add a generic
parity64()helper implemented with the same efficient approach asparity8().No reason to add
parity32()andparity64()in separate patchesAck.
Quoted 16 lines
Quoted 16 lines
Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- include/linux/bitops.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/bitops.h +0
-0@@ -281,6 +281,28 @@ static inline int parity32(u32 val)return (0x6996 >> (val &0xf )) &1 ; }Quoted 16 lines
Quoted 4 lines
include/linux/bitops.h +4
-0+/** +* parity64 - get the parity of an u64 value +* @value: the value to be examined +* include/linux/bitops.h static inline int parity32(u32 val) · line 297
Quoted 10 lines
include/linux/bitops.h +10
-0+* Determine the parity of the u64 argument. +* +* Returns: +* 0 for even parity, 1 for odd parity +*/ +static inline int parity64 (u64 val) +{ +/* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html This is already referenced in sources. No need to spread it for more.
Ack.
Quoted 9 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 303
Quoted 7 lines
include/linux/bitops.h +6
-0+*/ + val ^= val >>32 ; + val ^= val >>16 ; + val ^= val >>8 ; + val ^= val >>4 ; +return (0x6996 >> (val &0xf )) &1 ;It's better to avoid duplicating the same logic again and again.
Ack.
Quoted 116 lines
include/linux/bitops.h static inline int parity32(u32 val) · line 305
Quoted 3 lines
include/linux/bitops.h +2
-0+} +So maybe make it a macro?
From
f17a28ae3429f49825d65ebc0f7717c6a191a3e2Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
Date: Mon, 24 Feb 2025 14:14:27 -0500
Subject: [PATCH] bitops: generalizeparity8()The generic parity calculation approach may be easily generalized for other standard types. Do that and drop sub-optimal implementation of parity calculation in x86 code.
Signed-off-by Yury Norov [NVIDIA] <[email protected]>--- arch/x86/kernel/bootflag.c | 14 +----------- include/linux/bitops.h | 47 +++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 26 deletions(-)arch/x86/kernel/bootflag.c +1
-13@@ -2,6 +2,7 @@/* * Implement 'Simple Boot Flag Specification 2.0' */ +#include <linux/bitops.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/init.h> @@ -20,19 +21,6 @@int sbf_port__initdata =-1 ;/* set via acpi_boot_init() */ -static int __init parity (u8 v) -{ -int x =0 ; -int i; - -for (i =0 ; i <8 ; i++) { - x ^= (v &1 ); - v >>=1 ; - } - -return x; -} -static void __init sbf_write (u8 v) {unsigned long flags;include/linux/bitops.h +25
-13@@ -230,10 +230,10 @@ static inline int get_count_order_long(unsigned long l) }/** -* parity8 - get the parity of an u8 value +* parity - get the parity of a value * @value: the value to be examined * -* Determine the parity of the u8 argument. +* Determine parity of the argument. * * Returns: * 0 for even parity, 1 for odd parity @@ -241,24 +241,45 @@ static inline int get_count_order_long(unsigned long l)* Note: This function informs you about the current parity. Example to bail * out when parity is odd: * -* if (parity8(val) == 1) +* if (parity(val) == 1) * return -EBADMSG; * * If you need to calculate a parity bit, you need to draw the conclusion from * this result yourself. Example to enforce odd parity, parity bit is bit 7: * -* if (parity8(val) == 0) +* if (parity(val) == 0) * val ^= BIT(7); +* +* One explanation of this algorithm: +* https://funloop.org/codex/problem/parity/README.html */ -static inline int parity8 (u8 val) -{ -/* -* One explanation of this algorithm: -* https://funloop.org/codex/problem/parity/README.html -*/ - val ^= val >>4 ; -return (0x6996 >> (val &0xf )) &1 ; -} +#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 283
Quoted 10 lines
include/linux/bitops.h +9
-0+default : \ +BUILD_BUG (); \ + } \ + __ret; \ +}) + +#define parity8 (val) parity((u8)(val)) +#define parity32 (val) parity((u32)(val)) +#define parity64 (val) parity((u64)(val))What do you think about using these inline functions instead of macros? Except for
parity8(), each function is a single line and follows the same logic. I find inline functions more readable, andcoding-style.rstalso recommends them over macros.Regards,
Kuan-Weiinclude/linux/bitops.h +13
-0@@ -260,6 +260,26 @@ static inline int parity8(u8 val)return (0x6996 >> (val &0xf )) &1 ; } +static inline parity16(u16 val) +{ +return parity8 (val ^ (val >>8 )); +} + +static inline parity16(u16 val) +{ +return parity8 (val ^ (val >>8 )); +} + +static inline parity32 (u32) +{ +return parity16 (val ^ (val >>16 ));
include/linux/bitops.h static inline int parity8(u8 val) · line 285
Quoted 10 lines
include/linux/bitops.h +7
-0+} + +static inline parity64 (u64) +{ +return parity32 (val ^ (val >>32 )); +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Oops... I made a lot of fat-finger mistakes. Here's the correct one.
include/linux/bitops.h +15 -0
@@ -260,6 +260,21 @@ static inline int parity8(u8 val)return (0x6996 >> (val &0xf )) &1 ; } +static inline int parity16 (u16 val) +{ +return parity8 (val ^ (val >>8 )); +} + +static inline int parity32 (u32 val) +{ +return parity16 (val ^ (val >>16 )); +} + +static inline int parity64 (u64 val) +{ +return parity32 (val ^ (val >>32 )); +} +/** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word
Quoted 5 lines
Quoted 5 lines
include/linux/bitops.h +0
-0/** * __ffs64 - find first set bit in a 64 bit word Signature · 1 line
2.43.0
Jiri Slaby
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to Kuan-Wei ChiuQuoted 24 lines · Kuan-Wei Chiu
Quoted 24 lines
(quoted diff) +24
-0+#define parity(val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \ + } \ + __ret; \ +}) +
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +3
-0+#define parity8(val) parity((u8)(val)) +#define parity32(val) parity((u32)(val)) +#define parity64(val) parity((u64)(val))What do you think about using these inline functions instead of macros? Except for
parity8(), each function is a single line and follows the same logic. I find inline functions more readable, andcoding-style.rstalso recommends them over macros.
Not in cases where macros are inevitable. I mean, do we need parityXX() for XX in (8, 16, 32, 64) at all? Isn't the parity() above enough for everybody? And if not, you can have all those parityXX() as inlines as you suggest, but also provide a macro such as the above to call (optimized) parityXX() as per datatype len.
thanks,
Signature · 2 lines
js suse labs
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Hi Jiri,
Quoted 33 lines · Jiri Slaby
Quoted 23 lines · Kuan-Wei Chiu
Quoted 23 lines
(quoted diff) +23
-0+#define parity(val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \ + } \ + __ret; \ +})Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ +#define parity8(val) parity((u8)(val)) +#define parity32(val) parity((u32)(val)) +#define parity64(val) parity((u64)(val))What do you think about using these inline functions instead of macros? Except for
parity8(), each function is a single line and follows the same logic. I find inline functions more readable, andcoding-style.rstalso recommends them over macros.
Quoted 6 lines
Not in cases where macros are inevitable. I mean, do we need
parityXX()for XX in (8, 16, 32, 64) at all? Isn't theparity()above enough for everybody? And if not, you can have all thoseparityXX()as inlines as you suggest, but also provide a macro such as the above to call (optimized)parityXX()as per datatype len.
I agree that we can add a macro to call parity8/16/32/64 based on the data type size. However, I think we should still keep parity8/16/32/64. As Peter and David discussed, the x86-specific implementations of parity8() and parity16() might use different instructions instead of just XORing and calling another function, as in the generic version.
My current idea is to follow David's suggestion and use __builtin_parity when there is no architecture-specific implementation. In lib/, we can provide a generic weak function implementation of __parity[sdt]i2.
Any comments or suggestions are welcome!
Regards,
Kuan-Wei
static inline parity32(u32 val)
{
return __builtin_const_p(val) ? _parity_const(val) : _parity32(val);
}#ifndef _parity32
static inline _parity32(u32 val)
{
return __builtin_parity(val);
}#endif
int __weak __paritysi2(u32 val);
int __weak __paritysi2(u32 val)
{
val ^= val >> 16;
val ^= val >> 8;
val ^= val >> 4;
return (0x6996 >> (val & 0xf)) & 1;
}EXPORT_SYMBOL(__paritysi2);
Yury Norov
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to Jiri SlabyQuoted 33 lines · Jiri Slaby
Quoted 23 lines · Kuan-Wei Chiu
Quoted 23 lines
(quoted diff) +23
-0+#define parity(val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \ + } \ + __ret; \ +})Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ +#define parity8(val) parity((u8)(val)) +#define parity32(val) parity((u32)(val)) +#define parity64(val) parity((u64)(val))What do you think about using these inline functions instead of macros? Except for
parity8(), each function is a single line and follows the same logic. I find inline functions more readable, andcoding-style.rstalso recommends them over macros.
Quoted 2 lines
Not in cases where macros are inevitable. I mean, do we need
parityXX()for XX in (8, 16, 32, 64) at all? Isn't theparity()above enough for everybody?
The existing codebase has something like:
int ret;
ret = i3c_master_get_free_addr(m, last_addr + 1);
ret |= parity8(ret) ? 0 : BIT(7)So if we'll switch it to a macro like one above, it will become a 32-bit parity. It wouldn't be an error because i3c_master_get_free_addr() returns an u8 or -ENOMEM, and the error code is checked explicitly.
But if we decide to go with parity() only, some users will have to call it like parity((u8)val) explicitly. Which is not bad actually.
Quoted 3 lines
And if not, you can have all those
parityXX()as inlines as you suggest, but also provide a macro such as the above to call (optimized)parityXX()as per datatype len.
Yes, if we need fixed-type parity's, they should all be one-liners calling the same macro. Macros or inline functions - no preference for me.
Thanks,
Yury
Jiri Slaby
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 10 lines · Yury Norov
Quoted 2 lines
Not in cases where macros are inevitable. I mean, do we need
parityXX()for XX in (8, 16, 32, 64) at all? Isn't theparity()above enough for everybody?The existing codebase has something like:
int ret; ret = i3c_master_get_free_addr(m, last_addr + 1); ret |= parity8(ret) ? 0 : BIT(7)
Quoted 6 lines
So if we'll switch it to a macro like one above, it will become a 32-bit parity. It wouldn't be an error because
i3c_master_get_free_addr()returns an u8 or-ENOMEM, and the error code is checked explicitly.But if we decide to go with
parity()only, some users will have to call it like parity((u8)val) explicitly. Which is not bad actually.
That cast looks ugly -- we apparently need parityXX(). (In this particular case we could do parity8(last_addr), but I assume there are more cases like this.) Thanks for looking up the case for this.
Quoted 7 lines
Quoted 3 lines
And if not, you can have all those
parityXX()as inlines as you suggest, but also provide a macro such as the above to call (optimized)parityXX()as per datatype len.Yes, if we need fixed-type parity's, they should all be one-liners calling the same macro. Macros or inline functions - no preference for me.
Signature · 2 lines
js suse labs
Yury Norov
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 15 lines · Jiri Slaby
Quoted 14 lines · Yury Norov
Quoted 2 lines
Not in cases where macros are inevitable. I mean, do we need
parityXX()for XX in (8, 16, 32, 64) at all? Isn't theparity()above enough for everybody?The existing codebase has something like:
int ret; ret = i3c_master_get_free_addr(m, last_addr + 1); ret |= parity8(ret) ? 0 : BIT(7)So if we'll switch it to a macro like one above, it will become a 32-bit parity. It wouldn't be an error because
i3c_master_get_free_addr()returns an u8 or-ENOMEM, and the error code is checked explicitly.
Quoted 6 lines
Quoted 2 lines
But if we decide to go with
parity()only, some users will have to call it like parity((u8)val) explicitly. Which is not bad actually.That cast looks ugly -- we apparently need
parityXX(). (In this particular case we could doparity8(last_addr), but I assume there are more cases like this.) Thanks for looking up the case for this.
This parity8() is used in just 2 drivers - i3c and hwmon/spd5118. The hwmon driver looks good. I3C, yeah, makes this implied typecast, which is nasty regardless.
This is the new code, and I think if we all agree that generic parity() would be a better API, it's a good time to convert existing users now.
Thanks,
Yury
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to Yury NorovOn Mon, 24 Feb 2025 14:27:03 -0500
Yury Norov <[email protected]> wrote:
....
Quoted 14 lines
include/linux/bitops.h +14
-0+#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \
include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 279
Quoted 10 lines
include/linux/bitops.h +10
-0+case 8 : \ + __v ^= __v >>4 ; \ + __ret = (0x6996 >> (__v &0xf )) &1 ; \ +break ; \ +default : \ +BUILD_BUG (); \ + } \ + __ret; \ +}) +
You really don't want to do that!
gcc makes a right hash of it for x86 (32bit).
See https://www.godbolt.org/z/jG8dv3cvs
You do better using a __v32 after the 64bit xor.
Even the 64bit version is probably sub-optimal (both gcc and clang). The whole lot ends up being a bit single register dependency chain. You want to do:
mov %eax, %edx
shrl $n, %eax
xor %edx, %eax
so that the 'mov' and 'shrl' can happen in the same clock (without relying on the register-register move being optimised out).
I dropped in the arm64 for an example of where the magic shift of 6996 just adds an extra instruction.
David
Yury Norov
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 23 lines · David Laight
On Mon, 24 Feb 2025 14:27:03 -0500
Yury Norov <[email protected]> wrote:
....Quoted 20 lines
include/linux/bitops.h +20
-0+#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \
Quoted 8 lines
include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 279
Quoted 4 lines
include/linux/bitops.h +4
-0+ } \ + __ret; \ +}) +You really don't want to do that!
gcc makes a right hash of it for x86 (32bit).
See https://www.godbolt.org/z/jG8dv3cvs
GCC fails to even understand this. Of course, the __v should be an __auto_type. But that way GCC fails to understand that case 64 is a dead code for all smaller type and throws a false-positive Wshift-count-overflow. This is a known issue, unfixed for 25 years!
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=4210
Quoted 1 line
You do better using a
__v32after the 64bit xor.
It should be an __auto_type. I already mentioned. So because of that, we can either do something like this:
#define parity(val) \
({ \
#ifdef CLANG \
__auto_type __v = (val); \
#else /* GCC; because of this and that */ \
u64 __v = (val); \
#endif \
int __ret; \Or simply disable Wshift-count-overflow for GCC.
Quoted 3 lines
Even the 64bit version is probably sub-optimal (both gcc and clang). The whole lot ends up being a bit single register dependency chain. You want to do:
No, I don't. I want to have a sane compiler that does it for me.
Quoted 8 lines
mov %eax, %edx
shrl $n, %eax
xor %edx, %eax
so that the 'mov' and 'shrl' can happen in the same clock (without relying on the register-register move being optimised out).I dropped in the arm64 for an example of where the magic shift of 6996 just adds an extra instruction.
It's still unclear to me that this parity thing is used in hot paths. If that holds, it's unclear that your hand-made version is better than what's generated by GCC.
Do you have any perf test?
Thanks,
Yury
Re: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 33 lines · On Thu, 27 Feb 2025 13:05:29 -0500 Yury Norov <[email protected]> wrote:
Quoted 23 lines · David Laight
On Mon, 24 Feb 2025 14:27:03 -0500
Yury Norov <[email protected]> wrote:
....Quoted 20 lines
include/linux/bitops.h +20
-0+#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \Quoted 8 lines
include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 279
Quoted 4 lines
include/linux/bitops.h +4
-0+ } \ + __ret; \ +}) +You really don't want to do that!
gcc makes a right hash of it for x86 (32bit).
See https://www.godbolt.org/z/jG8dv3cvs
Quoted 4 lines
GCC fails to even understand this. Of course, the
__vshould be an__auto_type. But that way GCC fails to understand that case 64 is a dead code for all smaller type and throws a false-positive Wshift-count-overflow. This is a known issue, unfixed for 25 years!
Just do __v ^= __v >> 16 >> 16
Quoted 17 lines
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=4210
Quoted 1 line
You do better using a
__v32after the 64bit xor.It should be an
__auto_type. I already mentioned. So because of that, we can either do something like this:#define parity(val) \ ({ \ #ifdef CLANG \ __auto_type __v = (val); \ #else /* GCC; because of this and that */ \ u64 __v = (val); \ #endif \ int __ret; \
Quoted 1 line
Or simply disable Wshift-count-overflow for GCC.
For 64bit values on 32bit it is probably better to do:
int p32(unsigned long long x)
{
unsigned int lo = x;
lo ^= x >> 32;
lo ^= lo >> 16;
lo ^= lo >> 8;
lo ^= lo >> 4;
return (0x6996 >> (lo & 0xf)) & 1;
}That stops the compiler doing 64bit shifts (ok on x86, but probably not elsewhere). It is likely to be reasonably optimal for most 64bit cpu as well.
(For x86-64 it probably removes a load of REX prefix.)
(It adds an extra instruction to arm because if its barrel shifter.)
Quoted 13 lines
Quoted 4 lines
Even the 64bit version is probably sub-optimal (both gcc and clang). The whole lot ends up being a bit single register dependency chain. You want to do:
No, I don't. I want to have a sane compiler that does it for me.
Quoted 6 lines
mov %eax, %edx
shrl $n, %eax
xor %edx, %eax
so that the 'mov' and 'shrl' can happen in the same clock (without relying on the register-register move being optimised out).
Quoted 6 lines
Quoted 2 lines
I dropped in the arm64 for an example of where the magic shift of 6996 just adds an extra instruction.
It's still unclear to me that this parity thing is used in hot paths. If that holds, it's unclear that your hand-made version is better than what's generated by GCC.
I wasn't seriously considering doing that optimisation. Perhaps just hoping is might make a compiler person think :-)
David
Quoted 5 lines
Do you have any perf test?
Thanks,
Yury
H. Peter Anvin
reviewerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
Quoted 94 lines · David Laight
Quoted 33 lines · On Thu, 27 Feb 2025 13:05:29 -0500 Yury Norov <[email protected]> wrote:
Quoted 23 lines · David Laight
On Mon, 24 Feb 2025 14:27:03 -0500
Yury Norov <[email protected]> wrote:
....Quoted 20 lines
include/linux/bitops.h +20
-0+#define parity (val) \ +({ \ + u64 __v = (val); \ + int __ret; \ + switch (BITS_PER_TYPE(val)) { \ + case 64: \ + __v ^= __v >> 32; \ + fallthrough; \ + case 32: \ + __v ^= __v >> 16; \ + fallthrough; \ + case 16: \ + __v ^= __v >> 8; \ + fallthrough; \ + case 8: \ + __v ^= __v >> 4; \ + __ret = (0x6996 >> (__v & 0xf)) & 1; \ + break; \ + default: \ + BUILD_BUG(); \Quoted 8 lines
include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 279
Quoted 4 lines
include/linux/bitops.h +4
-0+ } \ + __ret; \ +}) +You really don't want to do that!
gcc makes a right hash of it for x86 (32bit).
See https://www.godbolt.org/z/jG8dv3cvsQuoted 4 lines
GCC fails to even understand this. Of course, the
__vshould be an__auto_type. But that way GCC fails to understand that case 64 is a dead code for all smaller type and throws a false-positive Wshift-count-overflow. This is a known issue, unfixed for 25 years!Just do
__v^=__v>> 16 >> 16Quoted 17 lines
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=4210
Quoted 1 line
You do better using a
__v32after the 64bit xor.It should be an
__auto_type. I already mentioned. So because of that, we can either do something like this:#define parity(val) \ ({ \ #ifdef CLANG \ __auto_type __v = (val); \ #else /* GCC; because of this and that */ \ u64 __v = (val); \ #endif \ int __ret; \Quoted 1 line
Or simply disable Wshift-count-overflow for GCC.
For 64bit values on 32bit it is probably better to do:
int p32(unsigned long long x){ unsigned int lo = x; lo ^= x >> 32; lo ^= lo >> 16; lo ^= lo >> 8; lo ^= lo >> 4; return (0x6996 >> (lo & 0xf)) & 1; }That stops the compiler doing 64bit shifts (ok on x86, but probably not elsewhere). It is likely to be reasonably optimal for most 64bit cpu as well.
(For x86-64 it probably removes a load of REX prefix.)
(It adds an extra instruction to arm because if its barrel shifter.)Quoted 16 lines
Quoted 4 lines
Even the 64bit version is probably sub-optimal (both gcc and clang). The whole lot ends up being a bit single register dependency chain. You want to do:
No, I don't. I want to have a sane compiler that does it for me.
Quoted 8 lines
mov %eax, %edx
shrl $n, %eax
xor %edx, %eax
so that the 'mov' and 'shrl' can happen in the same clock (without relying on the register-register move being optimised out).I dropped in the arm64 for an example of where the magic shift of 6996 just adds an extra instruction.
Quoted 15 lines
Quoted 3 lines
It's still unclear to me that this parity thing is used in hot paths. If that holds, it's unclear that your hand-made version is better than what's generated by GCC.
I wasn't seriously considering doing that optimisation. Perhaps just hoping is might make a compiler person think :-)
David
Quoted 5 lines
Do you have any perf test?
Thanks,
Yury
What the compiler people need to do is to not make __builtin_parity*() generate crap.
Yury Norov
maintainerRe: [PATCH 02/17] bitops: Add generic parity calculation for u64
In reply to David LaightQuoted 6 lines · David Laight
Quoted 3 lines
It's still unclear to me that this parity thing is used in hot paths. If that holds, it's unclear that your hand-made version is better than what's generated by GCC.
I wasn't seriously considering doing that optimisation. Perhaps just hoping is might make a compiler person think :-)
David, can you suggest only things you're seriously considered to do? Random suggestions distract my contributors and make them doing unneeded work and experiments.
In the other thread you asked I Hsin to try your approach to GENMASK() macro, saying you're lazy. I don't think this is the right way to communicate, not mentioning that if you're lazy to try your own approach, it doesn't sound nice to ask someone else to try it.
Thanks for understanding,
Yury
PATCH 3/17 x86: Replace open-coded parity calculation with parity8()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity8() helper. This change eliminates redundant implementations and improves code efficiency.
--- arch/x86/kernel/bootflag.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-)
arch/x86/kernel/bootflag.c +3 -15
@@ -8,6 +8,7 @@#include <linux/string.h> #include <linux/spinlock.h> #include <linux/acpi.h> +#include <linux/bitops.h> #include <asm/io.h> #include <linux/mc146818rtc.h>
@@ -20,26 +21,13 @@int sbf_port__initdata =-1 ;/* set via acpi_boot_init() */ -static int __init parity (u8 v) -{ -int x =0 ; -int i; - -for (i =0 ; i <8 ; i++) { - x ^= (v &1 ); - v >>=1 ; - } - -return x; -} -static void __init sbf_write (u8 v) {unsigned long flags;if (sbf_port !=-1 ) { v &= ~SBF_PARITY ; -if (!parity (v)) +if (!parity8 (v)) v |=SBF_PARITY ; printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n",
@@ -70,7 +58,7 @@ static int __init sbf_value_valid(u8 v) {if (v &SBF_RESERVED )/* Reserved bits */ return 0 ; -if (!parity (v)) +if (!parity8 (v))return 0 ;return 1 ;
Signature · 1 line
2.34.1
Re: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.
The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil 68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5>
76:
to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <sbf_init+0xbc>
68:
which is faster and smaller (-10 bytes) code.
Thanks,
Uros.
Quoted 57 lines
Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>--- arch/x86/kernel/bootflag.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-)arch/x86/kernel/bootflag.c +3
-15@@ -8,6 +8,7 @@#include <linux/string.h> #include <linux/spinlock.h> #include <linux/acpi.h> +#include <linux/bitops.h> #include <asm/io.h> #include <linux/mc146818rtc.h> @@ -20,26 +21,13 @@int sbf_port__initdata =-1 ;/* set via acpi_boot_init() */ -static int __init parity (u8 v) -{ -int x =0 ; -int i; - -for (i =0 ; i <8 ; i++) { - x ^= (v &1 ); - v >>=1 ; - } - -return x; -} -static void __init sbf_write (u8 v) {unsigned long flags;if (sbf_port !=-1 ) { v &= ~SBF_PARITY ; -if (!parity (v)) +if (!parity8 (v)) v |=SBF_PARITY ; printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n",@@ -70,7 +58,7 @@ static int __init sbf_value_valid(u8 v) {if (v &SBF_RESERVED )/* Reserved bits */ return 0 ; -if (!parity (v)) +if (!parity8 (v))return 0 ;return 1 ;
H. Peter Anvin
reviewerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 34 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil
68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5> 76:to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <
sbf_init+0xbc> 68:
Quoted 2 lines
which is faster and smaller (-10 bytes) code.
Of course, on x86, parity8() and parity16() can be implemented very simply:
(Also, the parity functions really ought to return bool, and be flagged __attribute_const__.)
static inline __attribute_const__ bool _arch_parity8(u8 val)
{
bool parity;
asm("and %0,%0" : "=@ccnp" (parity) : "q" (val));
return parity;
}static inline __attribute_const__ bool _arch_parity16(u16 val)
{
bool parity;
asm("xor %h0,%b0" : "=@ccnp" (parity), "+Q" (val));
return parity;
}In the generic algorithm, you probably should implement parity16() in terms of parity8(), parity32() in terms of parity16() and so on:
static inline __attribute_const__ bool parity16(u16 val) {
#ifdef ARCH_HAS_PARITY16 if (!__builtin_const_p(val)) return _arch_parity16(val); #endif return parity8(val ^ (val >> 8)); }
This picks up the architectural versions when available.
Furthermore, if a popcnt instruction is known to exist, then the parity is simply popcnt(x) & 1.
-hpa
Re: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 41 lines · H. Peter Anvin
Quoted 32 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil 68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5>76:
to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <sbf_init+0xbc>Quoted 4 lines
68:
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:
Quoted 7 lines
(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)static inline
__attribute_const__bool_arch_parity8(u8 val){ bool parity; asm("and %0,%0" : "=@ccnp" (parity) : "q" (val));
asm("test %0,%0" : "=@ccnp" (parity) : "q" (val));
because we are interested only in flags.
Uros.
H. Peter Anvin
reviewerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 50 lines · Uros Bizjak
Quoted 41 lines · H. Peter Anvin
Quoted 32 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil 68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5>76:
to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <sbf_init+0xbc>Quoted 4 lines
68:
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:Quoted 7 lines
(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)static inline
__attribute_const__bool_arch_parity8(u8 val){ bool parity; asm("and %0,%0" : "=@ccnp" (parity) : "q" (val));
Quoted 6 lines
asm("test %0,%0" : "=@ccnp" (parity) : "q" (val));because we are interested only in flags.
Uros.
Same thing, really, but yes, using test is cleaner.
H. Peter Anvin
reviewerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
In reply to Uros BizjakQuoted 45 lines · Uros Bizjak
Quoted 44 lines · H. Peter Anvin
Quoted 34 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil 68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5> 76:to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <sbf_init+0xbc> 68:Quoted 2 lines
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)
Quoted 9 lines
Quoted 4 lines
static inline
__attribute_const__bool_arch_parity8(u8 val){ bool parity; asm("and %0,%0" : "=@ccnp" (parity) : "q" (val));
asm("test %0,%0" : "=@ccnp" (parity) : "q" (val));because we are interested only in flags.
Also, needs to be %1,%1 (my mistake, thought flags outputs didn't count.)
Finally, this is kind of an obvious improvement:
static void __init sbf_write(u8 v)
{
unsigned long flags;
if (sbf_port != -1) {
- v &= ~SBF_PARITY;
if (!parity(v))- v |= SBF_PARITY;
+ v ^= SBF_PARITY;
-hpa
Yury Norov
maintainerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
In reply to H. Peter AnvinQuoted 35 lines · H. Peter Anvin
Quoted 34 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil
68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5> 76:to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <
sbf_init+0xbc> 68:
Quoted 7 lines
Quoted 2 lines
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)
There was a discussion regarding return type when parity8() was added. The integer type was taken over bool with a sort of consideration that bool should be returned as an answer to some question, like parity_odd().
To me it's not a big deal. We can switch to boolean and describe in comment what the 'true' means for the parity() function.
H. Peter Anvin
reviewerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 44 lines · Yury Norov
Quoted 35 lines · H. Peter Anvin
Quoted 34 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil
68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5> 76:to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <
sbf_init+0xbc> 68:Quoted 7 lines
Quoted 2 lines
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)
Quoted 6 lines
There was a discussion regarding return type when
parity8()was added. The integer type was taken over bool with a sort of consideration that bool should be returned as an answer to some question, likeparity_odd().To me it's not a big deal. We can switch to boolean and describe in comment what the 'true' means for the
parity()function.
Bool is really the single-bit type, and gives the compiler more information. You could argue that the function really should be called parity_odd*() in general, but that's kind of excessive IMO.
Yury Norov
maintainerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 45 lines · H. Peter Anvin
Quoted 44 lines · Yury Norov
Quoted 42 lines · H. Peter Anvin
Quoted 34 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.The patch improves parity assembly code in bootflag.o from:
58: 89 de mov %ebx,%esi 5a: b9 08 00 00 00 mov $0x8,%ecx 5f: 31 d2 xor %edx,%edx 61: 89 f0 mov %esi,%eax 63: 89 d7 mov %edx,%edi 65: 40 d0 ee shr %sil
68: 83 e0 01 and $0x1,%eax 6b: 31 c2 xor %eax,%edx 6d: 83 e9 01 sub $0x1,%ecx 70: 75 ef jne 61 <sbf_init+0x51> 72: 39 c7 cmp %eax,%edi 74: 74 7f je f5 <sbf_init+0xe5> 76:to:
54: 89 d8 mov %ebx,%eax 56: ba 96 69 00 00 mov $0x6996,%edx 5b: c0 e8 04 shr $0x4,%al 5e: 31 d8 xor %ebx,%eax 60: 83 e0 0f and $0xf,%eax 63: 0f a3 c2 bt %eax,%edx 66: 73 64 jae cc <
sbf_init+0xbc> 68:Quoted 2 lines
which is faster and smaller (-10 bytes) code.
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)
Quoted 8 lines
Quoted 6 lines
There was a discussion regarding return type when
parity8()was added. The integer type was taken over bool with a sort of consideration that bool should be returned as an answer to some question, likeparity_odd().To me it's not a big deal. We can switch to boolean and describe in comment what the 'true' means for the
parity()function.Bool is really the single-bit type, and gives the compiler more information. You could argue that the function really should be called parity_odd*() in general, but that's kind of excessive IMO.
Yes, I could, but I will not. :) I also feel like bool looks more natural here.
Re: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
In reply to H. Peter AnvinQuoted 7 lines · On Mon, 24 Feb 2025 13:55:28 -0800 "H. Peter Anvin" <[email protected]> wrote:
Quoted 6 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency.
...
Quoted 12 lines
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)static inline
__attribute_const__bool_arch_parity8(u8 val){ bool parity; asm("and %0,%0" : "=@ccnp" (parity) : "q" (val)); return parity; }
Quoted 6 lines
static inline
__attribute_const__bool_arch_parity16(u16 val){ bool parity; asm("xor %h0,%b0" : "=@ccnp" (parity), "+Q" (val)); return parity; }
The same (with fixes) can be done for parity64() on 32bit.
Quoted 6 lines
In the generic algorithm, you probably should implement
parity16()in terms ofparity8(),parity32()in terms ofparity16()and so on:static inline
__attribute_const__bool parity16(u16 val) {
Quoted 8 lines
#ifdef ARCH_HAS_PARITY16 if (!__builtin_const_p(val)) return _arch_parity16(val); #endif return parity8(val ^ (val >> 8)); }This picks up the architectural versions when available.
Not the best way to do that.
Make the name in the #ifdef the same as the function and define a default one if the architecture doesn't define one. So:
static inline parity16(u16 val)
{
return __builtin_const_p(val) ? _parity_const(val) : _parity16(val);
}#ifndef _parity16
static inline _parity16(u15 val)
{
return _parity8(val ^ (val >> 8));
}#endif
You only need one _parity_const().
Quoted 3 lines
Furthermore, if a popcnt instruction is known to exist, then the parity is simply popcnt(x) & 1.
Beware that some popcnt instructions are slow.
David
Quoted 4 lines
-hpa
H. Peter Anvin
reviewerRe: [PATCH 03/17] x86: Replace open-coded parity calculation with parity8()
Quoted 67 lines · David Laight
Quoted 7 lines · On Mon, 24 Feb 2025 13:55:28 -0800 "H. Peter Anvin" <[email protected]> wrote:
Quoted 6 lines · Uros Bizjak
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity8()helper. This change eliminates redundant implementations and improves code efficiency....
Quoted 12 lines
Of course, on x86,
parity8()andparity16()can be implemented very simply:(Also, the parity functions really ought to return bool, and be flagged
__attribute_const__.)static inline
__attribute_const__bool_arch_parity8(u8 val){ bool parity; asm("and %0,%0" : "=@ccnp" (parity) : "q" (val)); return parity; }Quoted 6 lines
static inline
__attribute_const__bool_arch_parity16(u16 val){ bool parity; asm("xor %h0,%b0" : "=@ccnp" (parity), "+Q" (val)); return parity; }The same (with fixes) can be done for
parity64()on 32bit.Quoted 6 lines
In the generic algorithm, you probably should implement
parity16()in terms ofparity8(),parity32()in terms ofparity16()and so on:static inline
__attribute_const__bool parity16(u16 val) {Quoted 8 lines
#ifdef ARCH_HAS_PARITY16 if (!__builtin_const_p(val)) return _arch_parity16(val); #endif return parity8(val ^ (val >> 8)); }This picks up the architectural versions when available.
Not the best way to do that.
Make the name in the #ifdef the same as the function and define a default one if the architecture doesn't define one. So:static inline parity16(u16 val)
{ return __builtin_const_p(val) ? _parity_const(val) : _parity16(val); }#ifndef
_parity16
static inline_parity16(u15 val){ return _parity8(val ^ (val >> 8)); }#endif
You only need one
_parity_const().Quoted 1 line
Quoted 12 lines
Quoted 2 lines
Furthermore, if a popcnt instruction is known to exist, then the parity is simply popcnt(x) & 1.
Beware that some popcnt instructions are slow.
David
Quoted 4 lines
-hpa
Seems more verbose than just #ifdef _arch_parity8 et al since the const and generic code cases are the same (which they aren't always.)
But that part is a good idea, especially since on at least *some* architectures like x86 doing:
#define _arch_parity8(x) __builtin_parity(x)
... etc is entirely reasonable and lets gcc use an already available parity flag should one be available.
The inline wrapper, of course, takes care of the type mangling.
PATCH 4/17 media: media/test_drivers: Replace open-coded parity calculation with parity8()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity8() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/media/test-drivers/vivid/vivid-vbi-gen.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +2 -6
@@ -5,6 +5,7 @@* Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. */ +#include <linux/bitops.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/ktime.h>
@@ -165,12 +166,7 @@ static const u8 vivid_cc_sequence2[30] = {static u8 calc_parity (u8 val) { -unsigned i; -unsigned tot =0 ; - -for (i =0 ; i <7 ; i++) - tot += (val & (1 << i)) ?1 :0 ; -return val | ((tot &1 ) ?0 :0x80 ); +return val | ((parity8 (val)) ?0 :0x80 ); }static void vivid_vbi_gen_set_time_of_day (u8 *packet)
Signature · 1 line
2.34.1
PATCH 5/17 media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity8()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity8() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/media/pci/cx18/cx18-av-vbi.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/pci/cx18/cx18-av-vbi.c +2 -10
@@ -8,6 +8,7 @@*/ +#include <linux/bitops.h> #include "cx18-driver.h" /*
@@ -56,15 +57,6 @@ struct vbi_anc_data {/* u8 fill[]; Variable number of fill bytes */ }; -static int odd_parity (u8 c) -{ - c ^= (c >>4 ); - c ^= (c >>2 ); - c ^= (c >>1 ); - -return c &1 ; -} -static int decode_vps (u8 *dst,u8 *p) {static const u8 biphase_tbl[] = {
@@ -278,7 +270,7 @@ int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,break ;case 6 : sdid =V4L2_SLICED_CAPTION_525 ; - err = !odd_parity (p[0 ]) || !odd_parity (p[1 ]); + err = !parity8 (p[0 ]) || !parity8 (p[1 ]);break ;case 9 : sdid =V4L2_SLICED_VPS ;
Signature · 1 line
2.34.1
PATCH 6/17 media: saa7115: Replace open-coded parity calculation with parity8()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity8() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/media/i2c/saa7115.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/i2c/saa7115.c +2 -10
@@ -25,6 +25,7 @@#include "saa711x_regs.h" +#include <linux/bitops.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h>
@@ -664,15 +665,6 @@ static const unsigned char saa7115_init_misc[] = {0x00 ,0x00 }; -static int saa711x_odd_parity (u8 c) -{ - c ^= (c >>4 ); - c ^= (c >>2 ); - c ^= (c >>1 ); - -return c &1 ; -} -static int saa711x_decode_vps (u8 *dst,u8 *p) { static const u8 biphase_tbl[] = {
@@ -1227,7 +1219,7 @@ static int saa711x_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vb vbi->type =V4L2_SLICED_TELETEXT_B ;break ;case 4 : -if (!saa711x_odd_parity (p[0 ]) || !saa711x_odd_parity (p[1 ])) +if (!parity8 (p[0 ]) || !parity8 (p[1 ]))return 0 ; vbi->type =V4L2_SLICED_CAPTION_525 ;break ;
Signature · 1 line
2.34.1
PATCH 7/17 serial: max3100: Replace open-coded parity calculation with parity8()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity8() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/tty/serial/max3100.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/max3100.c +2 -1
@@ -16,6 +16,7 @@/* 4 MAX3100s should be enough for everyone */ #define MAX_MAX3100 4 +#include <linux/bitops.h> #include <linux/container_of.h> #include <linux/delay.h> #include <linux/device.h>
@@ -133,7 +134,7 @@ static int max3100_do_parity(struct max3100_port *s, u16 c)else c &=0xff ; - parity = parity ^ (hweight8 (c) &1 ); + parity = parity ^ (parity8 (c));return parity; }
Signature · 1 line
2.34.1
Jiri Slaby
maintainerRe: [PATCH 07/17] serial: max3100: Replace open-coded parity calculation with parity8()
Quoted 11 lines · Kuan-Wei Chiu
drivers/tty/serial/max3100.c +1
-0@@ -16,6 +16,7 @@/* 4 MAX3100s should be enough for everyone */ #define MAX_MAX3100 4 +#include <linux/bitops.h> #include <linux/container_of.h> #include <linux/delay.h> #include <linux/device.h>
drivers/tty/serial/max3100.c static int max3100_do_parity(struct max3100_port *s, u16 c) · line 138
Quoted 6 lines
drivers/tty/serial/max3100.c +1
-1@@ -133,7 +134,7 @@ static int max3100_do_parity(struct max3100_port *s, u16 c)else c &=0xff ; - parity = parity ^ (hweight8 (c) &1 ); + parity = parity ^ (parity8 (c));return parity;
So all this should be simply:
return parity ^ parity8(c);
drivers/tty/serial/max3100.c static int max3100_do_parity(struct max3100_port *s, u16 c) · line 140
Quoted 2 lines
drivers/tty/serial/max3100.c +0
-0}
thanks,
Signature · 2 lines
js suse labs
PATCH 8/17 lib/bch: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- lib/bch.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
lib/bch.c +1 -13
@@ -311,18 +311,6 @@ static inline int deg(unsigned int poly)return fls (poly)-1 ; } -static inline int parity (unsigned int x) -{ -/* -* public domain code snippet, lifted from -* http://www-graphics.stanford.edu/~seander/bithacks.html -*/ - x ^= x >>1 ; - x ^= x >>2 ; - x = (x &0x11111111U ) *0x11111111U ; -return (x >>28 ) &1 ; -} -/* Galois field basic operations: multiply, divide, inverse, etc. */ static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a,
@@ -524,7 +512,7 @@ static int solve_linear_system(struct bch_control *bch, unsigned int *rows, tmp =0 ;for (r = m-1 ; r >=0 ; r--) { mask = rows[r] & (tmp|1 ); - tmp |=parity (mask) << (m-r); + tmp |=parity32 (mask) << (m-r); } sol[p] = tmp >>1 ; }
Signature · 1 line
2.34.1
PATCH 9/17 Input: joystick - Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/input/joystick/grip_mp.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/input/joystick/grip_mp.c +2 -15
@@ -18,6 +18,7 @@#include <linux/delay.h> #include <linux/proc_fs.h> #include <linux/jiffies.h> +#include <linux/bitops.h> #define DRIVER_DESC "Gravis Grip Multiport driver"
@@ -112,20 +113,6 @@ static const int axis_map[] = { 5, 9, 1, 5, 6, 10, 2, 6, 4, 8, 0, 4, 5, 9, 1, 5static int register_slot (int i,struct grip_mp *grip); -/* -* Returns whether an odd or even number of bits are on in pkt. -*/ - -static int bit_parity (u32 pkt) -{ -int x = pkt ^ (pkt >>16 ); - x ^= x >>8 ; - x ^= x >>4 ; - x ^= x >>2 ; - x ^= x >>1 ; -return x &1 ; -} -/* * Poll gameport; return true if all bits set in 'onbits' are on and * all bits set in 'offbits' are off.
@@ -236,7 +223,7 @@ static int mp_io(struct gameport* gameport, int sendflags, int sendcode, u32 *pa pkt = (pkt >>2 ) |0xf0000000 ; } -if (bit_parity (pkt) ==1 ) +if (parity32 (pkt) ==1 )return IO_RESET ;/* Acknowledge packet receipt */
Signature · 1 line
2.34.1
PATCH 10/17 net: ethernet: oa_tc6: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/net/ethernet/oa_tc6.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)
drivers/net/ethernet/oa_tc6.c +3 -16
@@ -6,6 +6,7 @@*/ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/iopoll.h> #include <linux/mdio.h> #include <linux/phy.h>
@@ -177,19 +178,6 @@ static int oa_tc6_spi_transfer(struct oa_tc6 *tc6,return spi_sync (tc6->spi, &msg); } -static int oa_tc6_get_parity (u32 p) -{ -/* Public domain code snippet, lifted from -* http://www-graphics.stanford.edu/~seander/bithacks.html -*/ - p ^= p >>1 ; - p ^= p >>2 ; - p = (p &0x11111111U ) *0x11111111U ; - -/* Odd parity is used here */ -return !((p >>28 ) &1 ); -} -static __be32 oa_tc6_prepare_ctrl_header (u32 addr,u8 length,enum oa_tc6_register_op reg_op) {
@@ -202,7 +190,7 @@ static __be32 oa_tc6_prepare_ctrl_header(u32 addr, u8 length,FIELD_PREP (OA_TC6_CTRL_HEADER_ADDR , addr) |FIELD_PREP (OA_TC6_CTRL_HEADER_LENGTH , length -1 ); header |=FIELD_PREP (OA_TC6_CTRL_HEADER_PARITY , -oa_tc6_get_parity (header)); + !parity32 (header));return cpu_to_be32 (header); }
@@ -940,8 +928,7 @@ static __be32 oa_tc6_prepare_data_header(bool data_valid, bool start_valid,FIELD_PREP (OA_TC6_DATA_HEADER_END_BYTE_OFFSET , end_byte_offset); - header |=FIELD_PREP (OA_TC6_DATA_HEADER_PARITY , -oa_tc6_get_parity (header)); + header |=FIELD_PREP (OA_TC6_DATA_HEADER_PARITY , !parity32 (header));return cpu_to_be32 (header); }
Signature · 1 line
2.34.1
PATCH 11/17 wifi: brcm80211: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- .../wireless/broadcom/brcm80211/brcmsmac/dma.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c +1 -15
@@ -17,6 +17,7 @@#include <linux/slab.h> #include <linux/delay.h> #include <linux/pci.h> +#include <linux/bitops.h> #include <net/cfg80211.h> #include <net/mac80211.h>
@@ -283,21 +284,6 @@ struct dma_info {bool aligndesc_4k; }; -/* Check for odd number of 1's */ -static u32 parity32 (__le32 data) -{ -/* no swap needed for counting 1's */ -u32 par_data = *(u32 *)&data; - - par_data ^= par_data >>16 ; - par_data ^= par_data >>8 ; - par_data ^= par_data >>4 ; - par_data ^= par_data >>2 ; - par_data ^= par_data >>1 ; - -return par_data &1 ; -} -static bool dma64_dd_parity (struct dma64desc *dd) {return parity32 (dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
Signature · 1 line
2.34.1
Re: [PATCH 11/17] wifi: brcm80211: Replace open-coded parity calculation with parity32()
Quoted 3 lines · Kuan-Wei Chiu
Refactor parity calculations to use the standard
parity32()helper. This change eliminates redundant implementations and improves code efficiency.
While the dust settles on the exact implementation from driver perspective looks fine to me so...
Quoted 7 lines
Co-developed-by Yu-Chun Lin <[email protected]>Signed-off-by Yu-Chun Lin <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>---
.../wireless/broadcom/brcm80211/brcmsmac/dma.c| 16 +---------------1 file changed, 1 insertion(+), 15 deletions(-)
PATCH 12/17 drm/bridge: dw-hdmi: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c +2 -6
@@ -4,6 +4,7 @@* * Written and tested against the Designware HDMI Tx found in iMX6. */ +#include <linux/bitops.h> #include <linux/io.h> #include <linux/interrupt.h> #include <linux/module.h>
@@ -171,12 +172,7 @@ static void dw_hdmi_reformat_iec958(struct snd_dw_hdmi *dw,static u32 parity (u32 sample) { - sample ^= sample >>16 ; - sample ^= sample >>8 ; - sample ^= sample >>4 ; - sample ^= sample >>2 ; - sample ^= sample >>1 ; -return (sample &1 ) <<27 ; +return parity32 (sample) <<27 ; } static void dw_hdmi_reformat_s24(struct snd_dw_hdmi *dw,
Signature · 1 line
2.34.1
PATCH 13/17 mtd: ssfdc: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/mtd/ssfdc.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/mtd/ssfdc.c +2 -15
@@ -7,6 +7,7 @@* Based on NTFL and MTDBLOCK_RO drivers */ +#include <linux/bitops.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h>
@@ -178,20 +179,6 @@ static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf)return 0 ; } -/* Parity calculator on a word of n bit size */ -static int get_parity (int number,int size) -{ -int k; -int parity; - - parity =1 ; -for (k =0 ; k < size; k++) { - parity += (number >> k); - parity &=1 ; - } -return parity; -} -/* Read and validate the logical block address field stored in the OOB */ static int get_logical_address (uint8_t *oob_buf) {
@@ -215,7 +202,7 @@ static int get_logical_address(uint8_t *oob_buf) block_address &=0x7FF ; block_address >>=1 ; -if (get_parity (block_address,10 ) != parity) { +if (parity32 (block_address &0x3ff ) == parity) {pr_debug ("SSFDC_RO: logical address field%d" "parity error(0x%04X)\n" , j+1 , block_address);
Signature · 1 line
2.34.1
PATCH 14/17 fsi: i2cr: Replace open-coded parity calculation with parity32()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity32() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/fsi/fsi-master-i2cr.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/fsi/fsi-master-i2cr.c +2 -8
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) IBM Corporation 2023 */ +#include <linux/bitops.h> #include <linux/device.h> #include <linux/fsi.h> #include <linux/i2c.h>
@@ -38,14 +39,7 @@ static const u8 i2cr_cfam[] = {static bool i2cr_check_parity32 (u32 v,bool parity) { -u32 i; - -for (i =0 ; i <32 ; ++i) { -if (v & (1u << i)) - parity = !parity; - } - -return parity; +return parity ^parity32 (v); }static bool i2cr_check_parity64 (u64 v)
Signature · 1 line
2.34.1
PATCH 15/17 fsi: i2cr: Replace open-coded parity calculation with parity64()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity64() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/fsi/fsi-master-i2cr.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/fsi/fsi-master-i2cr.c +1 -7
@@ -44,15 +44,9 @@ static bool i2cr_check_parity32(u32 v, bool parity)static bool i2cr_check_parity64 (u64 v) { -u32 i;bool parity =I2CR_INITIAL_PARITY ; -for (i =0 ; i <64 ; ++i) { -if (v & (1llu << i)) - parity = !parity; - } - -return parity; +return parity ^parity64 (v); }static u32 i2cr_get_command (u32 address,bool parity)
Signature · 1 line
2.34.1
PATCH 16/17 Input: joystick - Replace open-coded parity calculation with parity64()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity64() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/input/joystick/sidewinder.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-)
drivers/input/joystick/sidewinder.c +5 -19
@@ -7,6 +7,7 @@* Microsoft SideWinder joystick family driver for Linux */ +#include <linux/bitops.h> #include <linux/delay.h> #include <linux/kernel.h> #include <linux/module.h>
@@ -240,21 +241,6 @@ static void sw_init_digital(struct gameport *gameport)local_irq_restore (flags); } -/* -* sw_parity() computes parity of __u64 -*/ - -static int sw_parity (__u64 t) -{ -int x = t ^ (t >>32 ); - - x ^= x >>16 ; - x ^= x >>8 ; - x ^= x >>4 ; - x ^= x >>2 ; - x ^= x >>1 ; -return x &1 ; -}/* * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
@@ -316,7 +302,7 @@ static int sw_parse(unsigned char *buf, struct sw *sw)for (i =0 ; i < sw->number; i ++) { -if (sw_parity (GB (i*15 ,15 ))) +if (parity64 (GB (i*15 ,15 )))return -1 ;input_report_abs (sw->dev[i],ABS_X ,GB (i*15 +3 ,1 ) -GB (i*15 +2 ,1 ));
@@ -333,7 +319,7 @@ static int sw_parse(unsigned char *buf, struct sw *sw)case SW_ID_PP :case SW_ID_FFP : -if (!sw_parity (GB (0 ,48 )) || (hat =GB (42 ,4 )) >8 ) +if (!parity64 (GB (0 ,48 )) || (hat =GB (42 ,4 )) >8 )return -1 ; dev = sw->dev[0 ];
@@ -354,7 +340,7 @@ static int sw_parse(unsigned char *buf, struct sw *sw)case SW_ID_FSP : -if (!sw_parity (GB (0 ,43 )) || (hat =GB (28 ,4 )) >8 ) +if (!parity64 (GB (0 ,43 )) || (hat =GB (28 ,4 )) >8 )return -1 ; dev = sw->dev[0 ];
@@ -379,7 +365,7 @@ static int sw_parse(unsigned char *buf, struct sw *sw)case SW_ID_FFW : -if (!sw_parity (GB (0 ,33 ))) +if (!parity64 (GB (0 ,33 )))return -1 ; dev = sw->dev[0 ];
Signature · 1 line
2.34.1
PATCH 17/17 nfp: bpf: Replace open-coded parity calculation with parity64()
In reply to Kuan-Wei ChiuRefactor parity calculations to use the standard parity64() helper. This change eliminates redundant implementations and improves code efficiency.
--- drivers/net/ethernet/netronome/nfp/nfp_asm.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/ethernet/netronome/nfp/nfp_asm.c +1 -6
@@ -295,11 +295,6 @@ static const u64 nfp_ustore_ecc_polynomials[NFP_USTORE_ECC_POLY_WORDS] = { 0x0daf69a46910ULL, }; -static bool parity (u64 value) -{ -return hweight64 (value) &1 ; -} -int nfp_ustore_check_valid_no_ecc (u64 insn) {if (insn & ~GENMASK_ULL (NFP_USTORE_OP_BITS ,0 ))
@@ -314,7 +309,7 @@ u64 nfp_ustore_calc_ecc_insn(u64 insn)int i;for (i =0 ; i <NFP_USTORE_ECC_POLY_WORDS ; i++) - ecc |=parity (nfp_ustore_ecc_polynomials[i] & insn) << i; + ecc |=parity64 (nfp_ustore_ecc_polynomials[i] & insn) << i;return insn | (u64 )ecc <<NFP_USTORE_OP_BITS ; }
Signature · 1 line
2.34.1
Quoted 8 lines · Kuan-Wei Chiu
Several parts of the kernel contain redundant implementations of parity calculations for 32-bit and 64-bit values. Introduces generic
parity32()andparity64()helpers inbitops.h, providing a standardized and optimized implementation.Subsequent patches refactor various kernel components to replace open-coded parity calculations with the new helpers, reducing code duplication and improving maintainability.
Please note that GCC (and clang) provide __builtin_parity{,l,ll}() family of builtin functions. Recently, I have tried to use this builtin in a couple of places [1], [2], but I had to retract the patches, because __builtin functions aren't strictly required to be inlined and can generate a library call [3].
As explained in [2], the compilers are able to emit optimized target-dependent code (also automatically using popcnt insn when avaialble), so ideally the generic parity64() and parity32() would be implemented using __builtin_parity(), where the generic library would provide a fallback __paritydi2() and __paritysi2() functions, otherwise provided by the compiler support library.
For x86, we would like to exercise the hardware parity calculation or optimized code sequences involving HW parity calculation, as shown in [1] and [2].
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/linux-mm/CAKbZUD0N7bkuw_Le3Pr9o1V2BjjcY_YiLm8a8DPceubTdZ00GQ@mail.gmail.com/
Thanks,
Uros.
Quoted 18 lines · Uros Bizjak
Please note that GCC (and clang) provide __builtin_parity{,l,ll}() family of builtin functions. Recently, I have tried to use this builtin in a couple of places [1], [2], but I had to retract the patches, because
__builtinfunctions aren't strictly required to be inlined and can generate a library call [3].As explained in [2], the compilers are able to emit optimized target-dependent code (also automatically using popcnt insn when avaialble), so ideally the generic
parity64()andparity32()would be implemented using__builtin_parity(), where the generic library would provide a fallback__paritydi2()and__paritysi2()functions, otherwise provided by the compiler support library.For x86, we would like to exercise the hardware parity calculation or optimized code sequences involving HW parity calculation, as shown in [1] and [2].
Quoted 5 lines
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/linux-mm/CAKbZUD0N7bkuw_Le3Pr9o1V2BjjcY_YiLm8a8DPceubTdZ00GQ@mail.gmail.com/
Hi Uros,
Thanks for your information.
We originally planned to implement hardware optimizations after this patch series. However, for V2, We will incorporate __builtin_parity(), while keeping our current implementation as the fallback function.
Best regards,
Yu-Chun Lin
Hi Kuan-Wei,
Quoted 4 lines
Several parts of the kernel contain redundant implementations of parity calculations for 32-bit and 64-bit values. Introduces generic
parity32()andparity64()helpers inbitops.h, providing a standardized and optimized implementation.
More so than __builtin_parity() ?
I'm all for reducing the duplication, but the compiler may well have a better parity approach than the xor-folding implementation here. Looks like we can get this to two instructions on powerpc64, for example.
Cheers,
Jeremy
Quoted 5 lines · Jeremy Kerr
More so than
__builtin_parity()?I'm all for reducing the duplication, but the compiler may well have a better parity approach than the xor-folding implementation here. Looks like we can get this to two instructions on powerpc64, for example.
Hi Jeremy,
Thank for your input. We will do that in V2.
Best regards,
Yu-Chun Lin