Skip to contentSkip to conversation

Introduce and use generic parity16/32/64 helper

PATCH Superseded Kuan-Wei Chiu 5 participants 34 messages Mar 1, 2025 – Mar 5, 2025

Subsystems (19)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Introduce and use generic parity16/32/64 helper

Several parts of the kernel contain redundant implementations of parity calculations for 16/32/64-bit values. Introduces generic parity16/32/64() 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.

---

This patch series is based on next-20250228.

Changes in v2:
- Provide fallback functions for __builtin_parity() when the compiler decides not to inline it
- Use __builtin_parity() when no architecture-specific implementation is available
- Optimize for constant folding when val is a compile-time constant
- Add a generic parity() macro
- Drop the x86 bootflag conversion patch since it has been merged into the tip tree

Kuan-Wei Chiu (18):
lib/parity: Add __builtin_parity() fallback implementations
bitops: Optimize parity8() using __builtin_parity()
bitops: Add parity16(), parity32(), and parity64() helpers
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()
  drm/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()
bitops: Add parity() macro for automatic type-based selection

 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                        | 115 ++++++++++++++++--
 lib/Makefile                                  |   2 +-
 lib/bch.c                                     |  14 +--
 lib/parity.c                                  |  48 ++++++++
 16 files changed, 185 insertions(+), 155 deletions(-)
 create mode 100644 lib/parity.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/18 lib/parity: Add __builtin_parity() fallback implementations

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 44 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +6 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>

lib/parity.c line 16

Quoted 10 lines
lib/parity.c new file +10 -0
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 22 lines
lib/parity.c new file +22 -0
+ */
+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);
+
+int __weak __paritydi2(u64 val);
+int __weak __paritydi2(u64 val)
+{
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
+	val ^= val >> 4;
+	return (0x6996 >> (val & 0xf)) & 1;
+}
+EXPORT_SYMBOL(__paritydi2);
+

lib/parity.c line 48

Quoted 10 lines
lib/parity.c new file +10 -0
+int __weak __parityti2(u64 val);
+int __weak __parityti2(u64 val)
+{
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
+	val ^= val >> 4;
+	return (0x6996 >> (val & 0xf)) & 1;
+}
+EXPORT_SYMBOL(__parityti2);

OK, it seems I wasn't clear enough on the previous round, so I'll try to be very straightforward now.

To begin with, the difference between __parityti2 and __paritydi2 doesn't exist. I'm seriously. I put them side by side, and there's no difference at all.

Next, this all is clearly an overengineering. You bake all those weak, const and (ironically, missing) high-efficient arch implementations. But you show no evidence that:
- it improves on code generation,
- the drivers care about parity()'s performance, and
- show no perf tests at all.

So you end up with +185/-155 LOCs.

Those +30 lines add no new functionality. You copy-paste the same algorithm again and again in very core kernel files. This is a no-go for a nice consolidation series.

In the previous round reviewers gave you quite a few nice suggestions. H. Peter Anvin suggested to switch the function to return a boolean, I suggested to make it a macro and even sent you a patch, Jiri and David also spent their time trying to help you, and became ignored.

Nevertheless. NAK for the series. Whatever you end up, if it comes to v3, please make it simple, avoid code duplication and run checkpatch.

Thanks,
Yury

#3 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html

I already asked you not to spread this link. Is there any reason to ignore it?

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Quoted 53 lines
Quoted 22 lines
lib/parity.c new file +22 -0
+ */
+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);
+
+int __weak __paritydi2(u64 val);
+int __weak __paritydi2(u64 val)
+{
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
+	val ^= val >> 4;
+	return (0x6996 >> (val & 0xf)) & 1;
+}
+EXPORT_SYMBOL(__paritydi2);
+

lib/parity.c line 48

Quoted 10 lines
lib/parity.c new file +10 -0
+int __weak __parityti2(u64 val);
+int __weak __parityti2(u64 val)
+{
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
+	val ^= val >> 4;
+	return (0x6996 >> (val & 0xf)) & 1;
+}
+EXPORT_SYMBOL(__parityti2);

OK, it seems I wasn't clear enough on the previous round, so I'll try to be very straightforward now.

To begin with, the difference between __parityti2 and __paritydi2 doesn't exist. I'm seriously. I put them side by side, and there's no difference at all.

Next, this all is clearly an overengineering. You bake all those weak, const and (ironically, missing) high-efficient arch implementations. But you show no evidence that:
- it improves on code generation,
- the drivers care about parity()'s performance, and
- show no perf tests at all.

So you end up with +185/-155 LOCs.

Those +30 lines add no new functionality. You copy-paste the same algorithm again and again in very core kernel files. This is a no-go for a nice consolidation series.

Quoted 8 lines

In the previous round reviewers gave you quite a few nice suggestions. H. Peter Anvin suggested to switch the function to return a boolean, I suggested to make it a macro and even sent you a patch, Jiri and David also spent their time trying to help you, and became ignored.

Nevertheless. NAK for the series. Whatever you end up, if it comes to v3, please make it simple, avoid code duplication and run checkpatch.

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +37 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}
+
+#define parity(val)			\
+({					\
+	bool __ret;			\
+	switch (BITS_PER_TYPE(val)) {	\
+	case 64:			\
+		__ret = parity64(val);	\
+		break;			\
+	case 32:			\
+		__ret = parity32(val);	\
+		break;			\
+	case 16:			\
+		__ret = parity16(val);	\
+		break;			\
+	case 8:				\
+		__ret = parity8(val);	\
+		break;			\
+	default:			\
+		BUILD_BUG();		\
+	}				\
+	__ret;				\
+})
+
 /**
  * __ffs64 - find first set bit in a 64 bit word
  * @word: The 64 bit word
maintainer
#4 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 12 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

Quoted 8 lines

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

Quoted 25 lines
include/linux/bitops.h +22 -0
+#define parity(val)			\
+({					\
+	bool __ret;			\
+	switch (BITS_PER_TYPE(val)) {	\
+	case 64:			\
+		__ret = parity64(val);	\
+		break;			\
+	case 32:			\
+		__ret = parity32(val);	\
+		break;			\
+	case 16:			\
+		__ret = parity16(val);	\
+		break;			\
+	case 8:				\
+		__ret = parity8(val);	\
+		break;			\
+	default:			\
+		BUILD_BUG();		\
+	}				\
+	__ret;				\
+})
+
 /**
  * __ffs64 - find first set bit in a 64 bit word
  * @word: The 64 bit word
#5 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Hi Yury,

Quoted 103 lines · Yury Norov
Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 17 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

Quoted 8 lines
Quoted 3 lines

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

For example, the following warning:

ERROR: space required after that ',' (ctx:VxV) #84: FILE: drivers/input/joystick/sidewinder.c:368:
+ if (!parity64(GB(0,33)))

                                          ^

This issue already existed before this series, and I'm keeping its style unchanged for now.

Quoted 28 lines
Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}
Quoted 8 lines

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

So, I should add the following parity() macro in v3, remove parity8(), and update all current parity8() users to use this macro, right?

I changed u64 to __auto_type and applied David's suggestion to replace the >> 32 with >> 16 >> 16 to avoid compiler warnings.

Regards,
Kuan-Wei

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		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;						\
})
#6 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 184 lines · On Mon, 3 Mar 2025 01:29:19 +0800 Kuan-Wei Chiu <[email protected]> wrote:

Hi Yury,

Quoted 103 lines · Yury Norov
Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html  

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 17 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

Quoted 8 lines
Quoted 3 lines

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

For example, the following warning:

ERROR: space required after that ',' (ctx:VxV) #84: FILE: drivers/input/joystick/sidewinder.c:368:
+ if (!parity64(GB(0,33)))

                                          ^

This issue already existed before this series, and I'm keeping its style unchanged for now.

Quoted 28 lines
Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}  
Quoted 8 lines

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

So, I should add the following parity() macro in v3, remove parity8(), and update all current parity8() users to use this macro, right?

I changed u64 to __auto_type and applied David's suggestion to replace the >> 32 with >> 16 >> 16 to avoid compiler warnings.

Regards,
Kuan-Wei

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

David

#7 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 209 lines · David Laight
Quoted 184 lines · On Mon, 3 Mar 2025 01:29:19 +0800 Kuan-Wei Chiu <[email protected]> wrote:

Hi Yury,

Quoted 103 lines · Yury Norov
Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html  

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 17 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

Quoted 8 lines
Quoted 3 lines

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

For example, the following warning:

ERROR: space required after that ',' (ctx:VxV) #84: FILE: drivers/input/joystick/sidewinder.c:368:
+ if (!parity64(GB(0,33)))

                                          ^

This issue already existed before this series, and I'm keeping its style unchanged for now.

Quoted 28 lines
Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}  
Quoted 8 lines

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

So, I should add the following parity() macro in v3, remove parity8(), and update all current parity8() users to use this macro, right?

I changed u64 to __auto_type and applied David's suggestion to replace the >> 32 with >> 16 >> 16 to avoid compiler warnings.

Regards,
Kuan-Wei

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\
Quoted 6 lines

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

I'm happy to make this change, though I'm a bit confused about how much we care about the code generated by gcc. So this is the macro expected in v3:

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	u32 __ret = val;				\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
                __ret ^= __v >> 16 >> 16;		\
		fallthrough;				\
	case 32:					\
		__ret ^= __ret >> 16;			\
		fallthrough;				\
	case 16:					\
		__ret ^= __ret >> 8;			\
		fallthrough;				\
	case 8:						\
		__ret ^= __ret >> 4;			\
		__ret = (0x6996 >> (__ret & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})
#8 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 6 lines · On Mon, 3 Mar 2025 10:47:20 +0800 Kuan-Wei Chiu <[email protected]> wrote:
Quoted 5 lines · David Laight
Quoted 2 lines · On Mon, 3 Mar 2025 01:29:19 +0800 Kuan-Wei Chiu <[email protected]> wrote:

Hi Yury,

...

Quoted 43 lines
Quoted 37 lines
Quoted 15 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})  

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\
Quoted 6 lines

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

Quoted 3 lines

I'm happy to make this change, though I'm a bit confused about how much we care about the code generated by gcc. So this is the macro expected in v3:

There is 'good', 'bad' and 'ugly' - it was in the 'bad' to 'ugly' area.

Quoted 16 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	u32 __ret = val;				\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
                __ret ^= __v >> 16 >> 16;		\
		fallthrough;				\
	case 32:					\
		__ret ^= __ret >> 16;			\
		fallthrough;				\
	case 16:					\
		__ret ^= __ret >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__ret ^= __ret >> 4;			\
		__ret = (0x6996 >> (__ret & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})

That looks like it will avoid double-register shifts on 32bit archs. arm64 can do slightly better (a couple of instructions) because of its barrel shifter.
x86 can do a lot better because of the cpu 'parity' flag. But maybe it is never used anywhere that really matters.

David

maintainer
#9 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

In reply to Kuan-Wei Chiu
Quoted 43 lines · Kuan-Wei Chiu
Quoted 37 lines
Quoted 15 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\
Quoted 6 lines

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

Quoted 3 lines

I'm happy to make this change, though I'm a bit confused about how much we care about the code generated by gcc. So this is the macro expected in v3:

We do care about code generated by any compiler. But we don't spread hacks here and there just to make GCC happy. This is entirely broken strategy. Things should work the other way: compiler people should collect real-life examples and learn from them.

I'm not happy even with this 'v >> 16 >> 16' hack, I just think that disabling Wshift-count-overflow is the worse option. Hacking the macro to optimize parity64() on 32-bit arch case doesn't worth it entirely.

In your patchset, you have only 3 drivers using parity64(). For each of them, please in the commit message refer that calling generic parity() with 64-bit argument may lead to sub-optimal code generation with a certain compiler against 32-bit arches. If you'll get a feedback that it's a real problem for somebody, we'll think about mitigating it.

Quoted 23 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	u32 __ret = val;				\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
                __ret ^= __v >> 16 >> 16;		\
		fallthrough;				\
	case 32:					\
		__ret ^= __ret >> 16;			\
		fallthrough;				\
	case 16:					\
		__ret ^= __ret >> 8;			\
		fallthrough;				\
	case 8:						\
		__ret ^= __ret >> 4;			\
		__ret = (0x6996 >> (__ret & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})
#10 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 57 lines · Yury Norov
Quoted 43 lines · Kuan-Wei Chiu
Quoted 37 lines
Quoted 15 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\
Quoted 6 lines

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

Quoted 3 lines

I'm happy to make this change, though I'm a bit confused about how much we care about the code generated by gcc. So this is the macro expected in v3:

We do care about code generated by any compiler. But we don't spread hacks here and there just to make GCC happy. This is entirely broken strategy. Things should work the other way: compiler people should collect real-life examples and learn from them.

I'm not happy even with this 'v >> 16 >> 16' hack, I just think that disabling Wshift-count-overflow is the worse option. Hacking the macro to optimize parity64() on 32-bit arch case doesn't worth it entirely.

Quoted 7 lines

In your patchset, you have only 3 drivers using parity64(). For each of them, please in the commit message refer that calling generic parity() with 64-bit argument may lead to sub-optimal code generation with a certain compiler against 32-bit arches. If you'll get a feedback that it's a real problem for somebody, we'll think about mitigating it.

How about reconsidering using parity8/16/32/64() instead of adding a parity() macro? They allow compiler to generate correct code without any hacks, and each implementation is simple and just one line. Jiri also agreed in the previous thread that we need parity8() in cases like the i3c driver. I think this might be the easiest solution to satisfy most people?

Regards,
Kuan-Wei

maintainer NAK
#11 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

In reply to David Laight
Quoted 24 lines · David Laight
Quoted 15 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\
	case 32:					\
		__v ^= __v >> 16;			\
		fallthrough;				\
	case 16:					\
		__v ^= __v >> 8;			\
		fallthrough;				\
	case 8:						\
Quoted 8 lines
		__v ^= __v >> 4;			\
		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
		break;					\
	default:					\
		BUILD_BUG();				\
	}						\
	__ret;						\
})
Quoted 2 lines

I'm seeing double-register shifts for 64bit values on 32bit systems. And gcc is doing 64bit double-register maths all the way down.

If you don't like GCC code generation why don't you discuss it in GCC maillist?

Quoted 10 lines

That is fixed by changing the top of the define to
#define parity(val) \

({							\
	unsigned int __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= val >> 16 >> 16;			\
		fallthrough;				\
Quoted 5 lines

But it's need changing to only expand 'val' once.
Perhaps:

	auto_type _val = (val);
	u32 __ret = val;

and (mostly) s/__v/__ret/g

You suggest another complication to mitigate a problem that most likely doesn't exist. I looked through the series and found that parity64() is used for I2C, joystick and a netronome ethernet card.

For I2C and joystick performance is definitely not a problem. For ethernet - maybe. But I feel like you didn't compile that driver for any 32-bit arch, and didn't test with a real hardware. So your concern is a pure speculation.

NAK.

maintainer
#12 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

In reply to Kuan-Wei Chiu
Quoted 160 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 103 lines · Yury Norov
Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 17 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

Quoted 8 lines
Quoted 3 lines

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

For example, the following warning:

ERROR: space required after that ',' (ctx:VxV) #84: FILE: drivers/input/joystick/sidewinder.c:368:
+ if (!parity64(GB(0,33)))

                                          ^

This issue already existed before this series, and I'm keeping its style unchanged for now.

Quoted 28 lines
Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}
Quoted 8 lines

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

Quoted 2 lines

So, I should add the following parity() macro in v3, remove parity8(), and update all current parity8() users to use this macro, right?

If you go with macro, please apply my patch and modify it in-place with this __auto_type thing and GCC hack. Feel free to add your co-developed-by, or tested, or whatever.

Quoted 6 lines

I changed u64 to __auto_type and applied David's suggestion to replace the >> 32 with >> 16 >> 16 to avoid compiler warnings.

Regards,
Kuan-Wei

Quoted 8 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\

This hack should be GCC-only, and well documented. For clang it should be
__v ^= __v >> 32; \

Quoted 15 lines
	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;						\
})
#13 lore raw

Re: [PATCH v2 01/18] lib/parity: Add __builtin_parity() fallback implementations

Quoted 183 lines · On Mon, 3 Mar 2025 10:15:41 -0500 Yury Norov <[email protected]> wrote:
Quoted 160 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 103 lines · Yury Norov
Quoted 62 lines · Kuan-Wei Chiu

Hi Yury,

Quoted 50 lines · Yury Norov
Quoted 49 lines · Kuan-Wei Chiu

Add generic C implementations of __paritysi2(), __paritydi2(), and __parityti2() as fallback functions in lib/parity.c. These functions compute the parity of a given integer using a bitwise approach and are marked with __weak, allowing architecture-specific implementations to override them.

This patch serves as preparation for using __builtin_parity() by ensuring a fallback mechanism is available when the compiler does not inline the __builtin_parity().

---
 lib/Makefile |  2 +-
 lib/parity.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 lib/parity.c
lib/Makefile +1 -1
index 7bab71e59019..45affad85ee4 100644
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o bitmap-str.o
+	 generic-radix-tree.o bitmap-str.o parity.o
 obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
lib/parity.c new file +11 -0
new file mode 100644
index 000000000000..a83ff8d96778
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * lib/parity.c
+ *
+ * Copyright (C) 2025 Kuan-Wei Chiu <[email protected]>
+ * Copyright (C) 2025 Yu-Chun Lin <[email protected]>
+ *
+ * __parity[sdt]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/export.h>
Quoted 9 lines

lib/parity.c line 16

Quoted 5 lines
lib/parity.c new file +5 -0
+#include <linux/kernel.h>
+
+/*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html  

I already asked you not to spread this link. Is there any reason to ignore it?

Quoted 2 lines

In v2, this algorithm was removed from bitops.h, so I moved the link here instead. I'm sorry if it seemed like I ignored your comment.

Yes, it is.

Quoted 4 lines

In v1, I used the same approach as parity8() because I couldn't justify the performance impact in a specific driver or subsystem. However, multiple people commented on using __builtin_parity or an x86 assembly implementation. I'm not ignoring their feedback-I want to address these

Please ask those multiple people: are they ready to maintain all that zoo of macros, weak implementations, arch implementations and stubs for no clear benefit? Performance is always worth it, but again I see not even a hint that the drivers care about performance. You don't measure it, so don't care as well. Right?

Quoted 4 lines

comments. Before submitting, I sent an email explaining my current
approach: using David's suggested method along with __builtin_parity, but no one responded. So, I decided to submit v2 for discussion instead.

For discussion use tag RFC.

Quoted 17 lines

To avoid mistakes in v3, I want to confirm the following changes before sending it:

(a) Change the return type from int to bool.
(b) Avoid __builtin_parity and use the same approach as parity8().
(c) Implement parity16/32/64() as single-line inline functions that call the next smaller variant after xor.
(d) Add a parity() macro that selects the appropriate parityXX() based on type size.
(e) Update users to use this parity() macro.

However, (d) may require a patch affecting multiple subsystems at once since some places that already include bitops.h have functions named parity(), causing conflicts. Unless we decide not to add this macro in the end.

Quoted 8 lines
Quoted 3 lines

As for checkpatch.pl warnings, they are mostly pre-existing coding style issues in this series. I've kept them as-is, but if preferred, I'm fine with fixing them.

Checkpatch only complains on new lines. Particularly this patch should trigger checkpatch warning because it adds a new file but doesn't touch MAINTAINERS.

For example, the following warning:

ERROR: space required after that ',' (ctx:VxV) #84: FILE: drivers/input/joystick/sidewinder.c:368:
+ if (!parity64(GB(0,33)))

                                          ^

This issue already existed before this series, and I'm keeping its style unchanged for now.

Quoted 28 lines
Quoted 17 lines

If anything is incorrect or if there are concerns, please let me know.

Regards,
Kuan-Wei

include/linux/bitops.h +4 -0
index c1cb53cf2f0f..47b7eca8d3b7 100644
@@ -260,6 +260,43 @@ static inline int parity8(u8 val)
 	return (0x6996 >> (val & 0xf)) & 1;
 }
 
+static inline bool parity16(u16 val)
+{
+	return parity8(val ^ (val >> 8));
+}

include/linux/bitops.h static inline int parity8(u8 val) · line 276

Quoted 10 lines
include/linux/bitops.h +10 -0
+
+static inline bool parity32(u32 val)
+{
+	return parity16(val ^ (val >> 16));
+}
+
+static inline bool parity64(u64 val)
+{
+	return parity32(val ^ (val >> 32));
+}  
Quoted 8 lines

That was discussed between Jiri and me in v2. Fixed types functions are needed only in a few very specific cases. With the exception of I3C driver (which doesn't look good for both Jiri and me), all the drivers have the type of variable passed to the parityXX() matching the actual variable length. It means that fixed-type versions of the parity() are simply not needed. So if we don't need them, please don't introduce it.

Quoted 2 lines

So, I should add the following parity() macro in v3, remove parity8(), and update all current parity8() users to use this macro, right?

If you go with macro, please apply my patch and modify it in-place with this __auto_type thing and GCC hack. Feel free to add your co-developed-by, or tested, or whatever.

Quoted 6 lines

I changed u64 to __auto_type and applied David's suggestion to replace the >> 32 with >> 16 >> 16 to avoid compiler warnings.

Regards,
Kuan-Wei

Quoted 8 lines

#define parity(val) \

({							\
	__auto_type __v = (val);			\
	bool __ret;					\
	switch (BITS_PER_TYPE(val)) {			\
	case 64:					\
		__v ^= __v >> 16 >> 16;			\
		fallthrough;				\  
Quoted 3 lines

This hack should be GCC-only, and well documented. For clang it should be
__v ^= __v >> 32; \

There is no point doing a conditional - it just obscures things.

Quoted 16 lines
Quoted 16 lines
	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;						\
})  
#14 lore raw

PATCH v2 2/18 bitops: Optimize parity8() using __builtin_parity()

In reply to Kuan-Wei Chiu

Refactor parity8() to use __builtin_parity() when no architecture-specific implementation is available. If the input is a compile-time constant, expand it using the _parity_const() macro to enable constant folding, allowing the compiler to optimize it at compile time.

Additionally, mark parity8() with __attribute_const__ to indicate its pure nature, ensuring better optimization opportunities.

This change improves efficiency by leveraging compiler intrinsics while maintaining a fallback mechanism for architectures without specific implementations.

---
 include/linux/bitops.h | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
include/linux/bitops.h +23 -7
index c1cb53cf2f0f..4c307d9c1545 100644
@@ -229,6 +229,27 @@ static inline int get_count_order_long(unsigned long l)
 	return (int)fls_long(--l);
 }
 
+#define _parity_const(val)	\
+({				\
+	u64 __v = (val);	\
+	int __ret;		\
+	__v ^= __v >> 32;	\
+	__v ^= __v >> 16;	\
+	__v ^= __v >> 8;	\
+	__v ^= __v >> 4;	\
+	__v ^= __v >> 2;	\
+	__v ^= __v >> 1;	\
+	__ret = __v & 1;	\
+	__ret;			\
+})
+
+#ifndef _parity8
+static inline __attribute_const__ int _parity8(u8 val)
+{
+	return __builtin_parity(val);
+}
+#endif
+
 /**
  * parity8 - get the parity of an u8 value
  * @value: the value to be examined
@@ -250,14 +271,9 @@ static inline int get_count_order_long(unsigned long l)
  *	if (parity8(val) == 0)
  *		val ^= BIT(7);
  */
-static inline int parity8(u8 val)
+static inline __attribute_const__ 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;
+	return __builtin_constant_p(val) ? _parity_const(val) : _parity8(val);
 }
 
 /**
Signature · 1 line
2.34.1
#15 lore raw

PATCH v2 3/18 bitops: Add parity16(), parity32(), and parity64() helpers

In reply to Kuan-Wei Chiu

Introduce parity16(), parity32(), and parity64() functions for computing parity on 16-bit, 32-bit, and 64-bit integers, respectively. These functions use __builtin_parity() or __builtin_parityll() when available, ensuring efficient computation. If the input is a compile-time constant, they expand using the _parity_const() macro to allow constant folding.

These additions provide parity computation helpers for larger integer types, ensuring consistency and performance across different bit-widths.

---
 include/linux/bitops.h | 63 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
include/linux/bitops.h +63 -0
index 4c307d9c1545..41e9e7fb894b 100644
@@ -276,6 +276,69 @@ static inline __attribute_const__ int parity8(u8 val)
 	return __builtin_constant_p(val) ? _parity_const(val) : _parity8(val);
 }
 
+#ifndef _parity16
+static inline __attribute_const__ int _parity16(u16 val)
+{
+	return __builtin_parity(val);
+}
+#endif
+
+/**
+ * parity16 - get the parity of an u16 value
+ * @value: the value to be examined
+ *
+ * Determine the parity of the u16 argument.
+ *
+ * Returns:
+ * 0 for even parity, 1 for odd parity
+ */
+static inline __attribute_const__ int parity16(u16 val)
+{
+	return __builtin_constant_p(val) ? _parity_const(val) : _parity16(val);
+}
+
+#ifndef _parity32
+static inline __attribute_const__ int _parity32(u32 val)
+{
+	return __builtin_parity(val);
+}
+#endif
+
+/**
+ * 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 __attribute_const__ int parity32(u32 val)
+{
+	return __builtin_constant_p(val) ? _parity_const(val) : _parity32(val);
+}
+
+#ifndef _parity64
+static inline __attribute_const__ int _parity64(u64 val)
+{
+	return __builtin_parityll(val);
+}
+#endif
+
+/**
+ * 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 __attribute_const__ int parity64(u64 val)
+{
+	return __builtin_constant_p(val) ? _parity_const(val) : _parity64(val);
+}
+
 /**
  * __ffs64 - find first set bit in a 64 bit word
  * @word: The 64 bit word
Signature · 1 line
2.34.1
reviewer
#16 lore raw

Re: [PATCH v2 03/18] bitops: Add parity16(), parity32(), and parity64() helpers

Quoted 26 lines · Kuan-Wei Chiu

Introduce parity16(), parity32(), and parity64() functions for computing parity on 16-bit, 32-bit, and 64-bit integers, respectively. These functions use __builtin_parity() or __builtin_parityll() when available, ensuring efficient computation. If the input is a compile-time constant, they expand using the _parity_const() macro to allow constant folding.

These additions provide parity computation helpers for larger integer types, ensuring consistency and performance across different bit-widths.

---
 include/linux/bitops.h | 63 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
include/linux/bitops.h +0 -0
index 4c307d9c1545..41e9e7fb894b 100644
@@ -276,6 +276,69 @@ static inline __attribute_const__ int parity8(u8 val)
 	return __builtin_constant_p(val) ? _parity_const(val) : _parity8(val);
 }
 

include/linux/bitops.h static inline __attribute_const__ int parity8(u8 val) · line 288

Quoted 10 lines
include/linux/bitops.h +10 -0
+#ifndef _parity16
+static inline __attribute_const__ int _parity16(u16 val)
+{
+	return __builtin_parity(val);
+}
+#endif
+
+/**
+ * parity16 - get the parity of an u16 value
+ * @value: the value to be examined

nit: This really ought to be @val to match the function signature.
Likewise for parity8, parity32, and parity64.

Quoted 10 lines
include/linux/bitops.h +10 -0
+ *
+ * Determine the parity of the u16 argument.
+ *
+ * Returns:
+ * 0 for even parity, 1 for odd parity
+ */
+static inline __attribute_const__ int parity16(u16 val)
+{
+	return __builtin_constant_p(val) ? _parity_const(val) : _parity16(val);
+}

...

#17 lore raw

PATCH v2 4/18 media: media/test_drivers: Replace open-coded parity calculation with parity8()

In reply to Kuan-Wei Chiu

Refactor 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
index 70a4024d461e..e0f4151bda18 100644
@@ -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
#18 lore raw

PATCH v2 5/18 media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity8()

In reply to Kuan-Wei Chiu

Refactor 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
index 65281d40c681..1a113aad9cd4 100644
@@ -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
#19 lore raw

PATCH v2 6/18 media: saa7115: Replace open-coded parity calculation with parity8()

In reply to Kuan-Wei Chiu

Refactor 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
index a1c71187e773..b8b8f206ec3a 100644
@@ -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
#20 lore raw

PATCH v2 7/18 serial: max3100: Replace open-coded parity calculation with parity8()

In reply to Kuan-Wei Chiu

Refactor 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
index cde5f1c86353..3b05ed113a67 100644
@@ -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
#21 lore raw

PATCH v2 8/18 lib/bch: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index 1c0cb07cdfeb..769459749982 100644
@@ -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
#22 lore raw

PATCH v2 9/18 Input: joystick - Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index 5eadb5a3ca37..897ce13753dc 100644
@@ -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, 5
 
 static 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
#23 lore raw

PATCH v2 10/18 net: ethernet: oa_tc6: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index db200e4ec284..f02dba7b89a1 100644
@@ -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
#24 lore raw

PATCH v2 11/18 wifi: brcm80211: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index 80c35027787a..d1a1ecd97d42 100644
@@ -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
#25 lore raw

PATCH v2 12/18 drm/bridge: dw-hdmi: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index cf1f66b7b192..833e65f33483 100644
@@ -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
#26 lore raw

PATCH v2 13/18 mtd: ssfdc: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index 46c01fa2ec46..e7f9e73da644 100644
@@ -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
#27 lore raw

PATCH v2 14/18 fsi: i2cr: Replace open-coded parity calculation with parity32()

In reply to Kuan-Wei Chiu

Refactor 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
index 40f1f4d231e5..8212b99ab2f9 100644
@@ -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
#28 lore raw

PATCH v2 15/18 fsi: i2cr: Replace open-coded parity calculation with parity64()

In reply to Kuan-Wei Chiu

Refactor 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
index 8212b99ab2f9..8f558b7c6dbc 100644
@@ -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
#29 lore raw

PATCH v2 16/18 Input: joystick - Replace open-coded parity calculation with parity64()

In reply to Kuan-Wei Chiu

Refactor 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
index 3a5873e5fcb3..9fe980096f70 100644
@@ -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
#30 lore raw

PATCH v2 17/18 nfp: bpf: Replace open-coded parity calculation with parity64()

In reply to Kuan-Wei Chiu

Refactor 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
index 154399c5453f..3646f84a6e8c 100644
@@ -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
#31 lore raw

PATCH v2 18/18 bitops: Add parity() macro for automatic type-based selection

In reply to Kuan-Wei Chiu

Introduce the parity() macro, which selects the appropriate parity function (parity8(), parity16(), parity32(), or parity64()) based on the size of the input type. This improves usability by allowing a generic parity calculation without requiring explicit function selection.

If the input type does not match the supported sizes, BUILD_BUG() is triggered to catch invalid usage at compile time.

---
Place this patch last in the series to avoid compilation errors.

 include/linux/bitops.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
include/linux/bitops.h +22 -0
index 41e9e7fb894b..fa4e45741dff 100644
@@ -339,6 +339,28 @@ static inline __attribute_const__ int parity64(u64 val)
 	return __builtin_constant_p(val) ? _parity_const(val) : _parity64(val);
 }
 
+#define parity(val)			\
+({					\
+	int __ret;			\
+	switch (BITS_PER_TYPE(val)) {	\
+	case 64:			\
+		__ret = parity64(val);	\
+		break;			\
+	case 32:			\
+		__ret = parity32(val);	\
+		break;			\
+	case 16:			\
+		__ret = parity16(val);	\
+		break;			\
+	case 8:				\
+		__ret = parity8(val);	\
+		break;			\
+	default:			\
+		BUILD_BUG();		\
+	}				\
+	__ret;				\
+})
+
 /**
  * __ffs64 - find first set bit in a 64 bit word
  * @word: The 64 bit word
Signature · 1 line
2.34.1
#32 lore raw

Re: [PATCH v2 18/18] bitops: Add parity() macro for automatic type-based selection

kernel test robot noticed the following build errors:

Hi Kuan-Wei,

kernel test robot noticed the following build errors:

[auto build test ERROR on next-20250228]
[cannot apply to akpm-mm/mm-nonmm-unstable dtor-input/next dtor-input/for-linus tty/tty-testing tty/tty-next tty/tty-linus wireless-next/main wireless/main mtd/mtd/next mtd/mtd/fixes linus/master v6.14-rc4 v6.14-rc3 v6.14-rc2 v6.14-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuan-Wei-Chiu/lib-parity-Add-__builtin_parity-fallback-implementations/20250301-222957
base:   next-20250228
patch link:    https://lore.kernel.org/r/20250301142409.2513835-19-visitorckw%40gmail.com
patch subject: [PATCH v2 18/18] bitops: Add parity() macro for automatic type-based selection
config: x86_64-buildonly-randconfig-001-20250302 (https://download.01.org/0day-ci/archive/20250302/[email protected]/config)
compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250302/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:13:
In file included from include/drm/bridge/dw_hdmi.h:9:
In file included from include/sound/hdmi-codec.h:16:
In file included from include/sound/soc.h:26:
In file included from include/sound/ac97_codec.h:17:
In file included from include/sound/pcm.h:15:
In file included from include/linux/mm.h:2287:
include/linux/vmstat.h:493:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     493 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     494 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:500:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     500 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     501 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:507:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     507 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:513:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     513 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     514 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:173:12: error: expected identifier or '('
     173 | static u32 parity(u32 sample)
         |            ^
   include/linux/bitops.h:343:2: note: expanded from macro 'parity'
     343 | ({                                      \
         |  ^
>> drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:173:12: error: expected ')'
   include/linux/bitops.h:343:2: note: expanded from macro 'parity'
     343 | ({                                      \
         |  ^
   drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:173:12: note: to match this '('
   include/linux/bitops.h:342:23: note: expanded from macro 'parity'
     342 | #define parity(val)                     \
         |                                         ^

4 warnings and 2 errors generated.

vim +173 drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c

7ed6c665e19d4c drivers/gpu/drm/bridge/dw_hdmi-ahb-audio.c          Russell King  2013-11-07  172  
7ed6c665e19d4c drivers/gpu/drm/bridge/dw_hdmi-ahb-audio.c          Russell King  2013-11-07 @173  static u32 parity(u32 sample)
7ed6c665e19d4c drivers/gpu/drm/bridge/dw_hdmi-ahb-audio.c          Russell King  2013-11-07  174  {
251332255cc5da drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c Kuan-Wei Chiu 2025-03-01  175  	return parity32(sample) << 27;
7ed6c665e19d4c drivers/gpu/drm/bridge/dw_hdmi-ahb-audio.c          Russell King  2013-11-07  176  }
7ed6c665e19d4c drivers/gpu/drm/bridge/dw_hdmi-ahb-audio.c          Russell King  2013-11-07  177  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#33 lore raw

Re: [PATCH v2 18/18] bitops: Add parity() macro for automatic type-based selection

In reply to Kuan-Wei Chiu
kernel test robot noticed the following build errors:

Hi Kuan-Wei,

kernel test robot noticed the following build errors:

[auto build test ERROR on next-20250228]
[cannot apply to akpm-mm/mm-nonmm-unstable dtor-input/next dtor-input/for-linus tty/tty-testing tty/tty-next tty/tty-linus wireless-next/main wireless/main mtd/mtd/next mtd/mtd/fixes linus/master v6.14-rc4 v6.14-rc3 v6.14-rc2 v6.14-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuan-Wei-Chiu/lib-parity-Add-__builtin_parity-fallback-implementations/20250301-222957
base:   next-20250228
patch link:    https://lore.kernel.org/r/20250301142409.2513835-19-visitorckw%40gmail.com
patch subject: [PATCH v2 18/18] bitops: Add parity() macro for automatic type-based selection
config: powerpc64-randconfig-001-20250302 (https://download.01.org/0day-ci/archive/20250302/[email protected]/config)
compiler: powerpc64-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250302/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:7:

>> include/linux/bitops.h:343:2: error: expected identifier or '(' before '{' token
     343 | ({                                      \
         |  ^
   drivers/gpu/drm/bridge/synopsys/dw-hdmi-ahb-audio.c:173:12: note: in expansion of macro 'parity'
     173 | static u32 parity(u32 sample)
         |            ^~~~~~
--
   In file included from dw-hdmi-ahb-audio.c:7:
>> include/linux/bitops.h:343:2: error: expected identifier or '(' before '{' token
     343 | ({                                      \
         |  ^
   dw-hdmi-ahb-audio.c:173:12: note: in expansion of macro 'parity'
     173 | static u32 parity(u32 sample)
         |            ^~~~~~

vim +343 include/linux/bitops.h

341
342 #define parity(val) \

 > 343	({					\

344 int __ret; \
345 switch (BITS_PER_TYPE(val)) { \
346 case 64: \
347 __ret = parity64(val); \
348 break; \
349 case 32: \
350 __ret = parity32(val); \
351 break; \
352 case 16: \
353 __ret = parity16(val); \
354 break; \
355 case 8: \
356 __ret = parity8(val); \
357 break; \
358 default: \
359 BUILD_BUG(); \
360 } \
361 __ret; \
362 })
363

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki