Skip to contentSkip to conversation

Introduce parity_odd() and refactor redundant parity code

PATCH In review Kuan-Wei Chiu 9 participants 43 messages Apr 9, 2025 – Apr 26, 2025

Subsystems (24)
lore
Share & tools
lore
#0 lore raw

v4 cover Introduce parity_odd() and refactor redundant parity code

Several parts of the kernel contain open-coded and redundant implementations of parity calculation. This patch series introduces a unified helper, parity_odd(), to simplify and standardize these cases.

The first patch renames parity8() to parity_odd(), changes its argument type from u8 to u64 for broader applicability, and updates its return type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

Subsequent patches refactor various kernel components to replace open-coded parity logic with the new helper, reducing code duplication and improving consistency.

---

To H. Peter:
I understand your preference for a parity8/16/32/64() style interface, and I agree that such a design would better accommodate potential arch-specific implementations. However, I suspect there are very few, if any, users who care about the performance of parity calculations enough to warrant such optimizations. So my inclination is to defer any arch-specific or optimized implementations until we see parity_odd() being used in hot paths.

Changes in v4:
- Rename parity8() to parity_odd().
- Change the argument type from u8 to u64.
- Use a single parity_odd() function.

Changes in v3:
- Avoid using __builtin_parity.
- Change return type to bool.
- Drop parity() macro.
- Change parityXX() << y to !!parityXX() << y.

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

v3: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/

Kuan-Wei Chiu (13):
bitops: Change parity8() to parity_odd() with u64 input and bool
return type
media: media/test_drivers: Replace open-coded parity calculation with
parity_odd()
media: pci: cx18-av-vbi: Replace open-coded parity calculation with
parity_odd()
media: saa7115: Replace open-coded parity calculation with
parity_odd()
serial: max3100: Replace open-coded parity calculation with
parity_odd()
lib/bch: Replace open-coded parity calculation with parity_odd()
Input: joystick - Replace open-coded parity calculation with
parity_odd()
net: ethernet: oa_tc6: Replace open-coded parity calculation with
parity_odd()
wifi: brcm80211: Replace open-coded parity calculation with
parity_odd()
drm/bridge: dw-hdmi: Replace open-coded parity calculation with
parity_odd()
mtd: ssfdc: Replace open-coded parity calculation with parity_odd()
fsi: i2cr: Replace open-coded parity calculation with parity_odd()
nfp: bpf: Replace open-coded parity calculation with parity_odd()

 arch/x86/kernel/bootflag.c                    |  4 +--
 drivers/fsi/fsi-master-i2cr.c                 | 20 +++------------
 .../drm/bridge/synopsys/dw-hdmi-ahb-audio.c   |  8 ++----
 drivers/hwmon/spd5118.c                       |  2 +-
 drivers/i3c/master/dw-i3c-master.c            |  2 +-
 drivers/i3c/master/i3c-master-cdns.c          |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c      |  2 +-
 drivers/input/joystick/grip_mp.c              | 17 ++-----------
 drivers/input/joystick/sidewinder.c           | 25 ++++---------------
 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                           | 20 +++------------
 drivers/net/ethernet/netronome/nfp/nfp_asm.c  |  7 +-----
 drivers/net/ethernet/oa_tc6.c                 | 19 +++-----------
 .../broadcom/brcm80211/brcmsmac/dma.c         | 18 ++-----------
 drivers/tty/serial/max3100.c                  |  3 ++-
 include/linux/bitops.h                        | 19 ++++++++------
 lib/bch.c                                     | 14 +----------
 19 files changed, 49 insertions(+), 165 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/13 bitops: Change parity8() to parity_odd() with u64 input and bool return type

Redesign the parity8() helper as parity_odd(), changing its input type from u8 to u64 to support broader use cases and its return type from int to bool to clearly reflect the function's binary output. The function now returns true for odd parity and false for even parity, making its behavior more intuitive based on the name.

Also mark the function with __attribute_const__ to enable better compiler optimization, as the result depends solely on its input and has no side effects.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

---
 arch/x86/kernel/bootflag.c               |  4 ++--
 drivers/hwmon/spd5118.c                  |  2 +-
 drivers/i3c/master/dw-i3c-master.c       |  2 +-
 drivers/i3c/master/i3c-master-cdns.c     |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c |  2 +-
 include/linux/bitops.h                   | 19 ++++++++++++-------
 6 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/kernel/bootflag.c +2 -2
index 73274d76ce16..86aae4b2bfd5 100644
@@ -26,7 +26,7 @@ static void __init sbf_write(u8 v)
 	unsigned long flags;
 
 	if (sbf_port != -1) {
-		if (!parity8(v))
+		if (!parity_odd(v))
 			v ^= SBF_PARITY;
 
 		printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n",
@@ -57,7 +57,7 @@ static bool __init sbf_value_valid(u8 v)
 {
 	if (v & SBF_RESERVED)		/* Reserved bits */
 		return false;
-	if (!parity8(v))
+	if (!parity_odd(v))
 		return false;
 
 	return true;
drivers/hwmon/spd5118.c +1 -1
index 358152868d96..15761f2ca4e9 100644
@@ -298,7 +298,7 @@ static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types typ
  */
 static bool spd5118_vendor_valid(u8 bank, u8 id)
 {
-	if (parity8(bank) == 0 || parity8(id) == 0)
+	if (!parity_odd(bank) || !parity_odd(id))
 		return false;
 
 	id &= 0x7f;
drivers/i3c/master/dw-i3c-master.c +1 -1
index 611c22b72c15..dc61d87fcd94 100644
@@ -867,7 +867,7 @@ static int dw_i3c_master_daa(struct i3c_master_controller *m)
 		master->devs[pos].addr = ret;
 		last_addr = ret;
 
-		ret |= parity8(ret) ? 0 : BIT(7);
+		ret |= parity_odd(ret) ? 0 : BIT(7);
 
 		writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret),
 		       master->regs +
drivers/i3c/master/i3c-master-cdns.c +1 -1
index fd3752cea654..df14f978a388 100644
@@ -889,7 +889,7 @@ static u32 prepare_rr0_dev_address(u32 addr)
 	ret |= (addr & GENMASK(9, 7)) << 6;
 
 	/* RR0[0] = ~XOR(addr[6:0]) */
-	ret |= parity8(addr & 0x7f) ? 0 : BIT(0);
+	ret |= parity_odd(addr & 0x7f) ? 0 : BIT(0);
 
 	return ret;
 }
drivers/i3c/master/mipi-i3c-hci/dat_v1.c +1 -1
index 85c4916972e4..d692a299607d 100644
@@ -114,7 +114,7 @@ static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,
 	dat_w0 = dat_w0_read(dat_idx);
 	dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY);
 	dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) |
-		  (parity8(address) ? 0 : DAT_0_DYNADDR_PARITY);
+		  (parity_odd(address) ? 0 : DAT_0_DYNADDR_PARITY);
 	dat_w0_write(dat_idx, dat_w0);
 }
 
include/linux/bitops.h +12 -7
index c1cb53cf2f0f..7c4c8afccef1 100644
@@ -230,35 +230,40 @@ static inline int get_count_order_long(unsigned long l)
 }
 
 /**
- * parity8 - get the parity of an u8 value
- * @value: the value to be examined
+ * parity_odd - get the parity of an u64 value
+ * @val: the value to be examined
  *
- * Determine the parity of the u8 argument.
+ * Determine the parity of the u64 argument.
  *
  * Returns:
- * 0 for even parity, 1 for odd parity
+ * false for even parity, true for odd parity
  *
  * Note: This function informs you about the current parity. Example to bail
  * out when parity is odd:
  *
- *	if (parity8(val) == 1)
+ *	if (parity_odd(val))
  *		return -EBADMSG;
  *
  * If you need to calculate a parity bit, you need to draw the conclusion from
  * this result yourself. Example to enforce odd parity, parity bit is bit 7:
  *
- *	if (parity8(val) == 0)
+ *	if (!parity_odd(val))
  *		val ^= BIT(7);
  */
-static inline int parity8(u8 val)
+#ifndef parity_odd
+static inline __attribute_const__ bool parity_odd(u64 val)
 {
 	/*
 	 * One explanation of this algorithm:
 	 * https://funloop.org/codex/problem/parity/README.html
 	 */
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
 	val ^= val >> 4;
 	return (0x6996 >> (val & 0xf)) & 1;
 }
+#endif
 
 /**
  * __ffs64 - find first set bit in a 64 bit word
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v4 01/13] bitops: Change parity8() to parity_odd() with u64 input and bool return type

Quoted 125 lines · Kuan-Wei Chiu

Redesign the parity8() helper as parity_odd(), changing its input type from u8 to u64 to support broader use cases and its return type from int to bool to clearly reflect the function's binary output. The function now returns true for odd parity and false for even parity, making its behavior more intuitive based on the name.

Also mark the function with __attribute_const__ to enable better compiler optimization, as the result depends solely on its input and has no side effects.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

---
 arch/x86/kernel/bootflag.c               |  4 ++--
 drivers/hwmon/spd5118.c                  |  2 +-
 drivers/i3c/master/dw-i3c-master.c       |  2 +-
 drivers/i3c/master/i3c-master-cdns.c     |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c |  2 +-
 include/linux/bitops.h                   | 19 ++++++++++++-------
 6 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/kernel/bootflag.c +2 -2
index 73274d76ce16..86aae4b2bfd5 100644
@@ -26,7 +26,7 @@ static void __init sbf_write(u8 v)
 	unsigned long flags;
 
 	if (sbf_port != -1) {
-		if (!parity8(v))
+		if (!parity_odd(v))
 			v ^= SBF_PARITY;
 
 		printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n",
@@ -57,7 +57,7 @@ static bool __init sbf_value_valid(u8 v)
 {
 	if (v & SBF_RESERVED)		/* Reserved bits */
 		return false;
-	if (!parity8(v))
+	if (!parity_odd(v))
 		return false;
 
 	return true;
drivers/hwmon/spd5118.c +1 -1
index 358152868d96..15761f2ca4e9 100644
@@ -298,7 +298,7 @@ static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types typ
  */
 static bool spd5118_vendor_valid(u8 bank, u8 id)
 {
-	if (parity8(bank) == 0 || parity8(id) == 0)
+	if (!parity_odd(bank) || !parity_odd(id))
 		return false;
 
 	id &= 0x7f;
drivers/i3c/master/dw-i3c-master.c +1 -1
index 611c22b72c15..dc61d87fcd94 100644
@@ -867,7 +867,7 @@ static int dw_i3c_master_daa(struct i3c_master_controller *m)
 		master->devs[pos].addr = ret;
 		last_addr = ret;
 
-		ret |= parity8(ret) ? 0 : BIT(7);
+		ret |= parity_odd(ret) ? 0 : BIT(7);
 
 		writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret),
 		       master->regs +
drivers/i3c/master/i3c-master-cdns.c +1 -1
index fd3752cea654..df14f978a388 100644
@@ -889,7 +889,7 @@ static u32 prepare_rr0_dev_address(u32 addr)
 	ret |= (addr & GENMASK(9, 7)) << 6;
 
 	/* RR0[0] = ~XOR(addr[6:0]) */
-	ret |= parity8(addr & 0x7f) ? 0 : BIT(0);
+	ret |= parity_odd(addr & 0x7f) ? 0 : BIT(0);
 
 	return ret;
 }
drivers/i3c/master/mipi-i3c-hci/dat_v1.c +1 -1
index 85c4916972e4..d692a299607d 100644
@@ -114,7 +114,7 @@ static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,
 	dat_w0 = dat_w0_read(dat_idx);
 	dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY);
 	dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) |
-		  (parity8(address) ? 0 : DAT_0_DYNADDR_PARITY);
+		  (parity_odd(address) ? 0 : DAT_0_DYNADDR_PARITY);
 	dat_w0_write(dat_idx, dat_w0);
 }
 
include/linux/bitops.h +5 -5
index c1cb53cf2f0f..7c4c8afccef1 100644
@@ -230,35 +230,40 @@ static inline int get_count_order_long(unsigned long l)
 }
 
 /**
- * parity8 - get the parity of an u8 value
- * @value: the value to be examined
+ * parity_odd - get the parity of an u64 value
+ * @val: the value to be examined
  *
- * Determine the parity of the u8 argument.
+ * Determine the parity of the u64 argument.
  *
  * Returns:
- * 0 for even parity, 1 for odd parity
+ * false for even parity, true for odd parity
  *
  * Note: This function informs you about the current parity. Example to bail
  * out when parity is odd:
  *
- *	if (parity8(val) == 1)
+ *	if (parity_odd(val))
  *		return -EBADMSG;

include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 253

Quoted 10 lines
include/linux/bitops.h +2 -2
  *
  * If you need to calculate a parity bit, you need to draw the conclusion from
  * this result yourself. Example to enforce odd parity, parity bit is bit 7:
  *
- *	if (parity8(val) == 0)
+ *	if (!parity_odd(val))
  *		val ^= BIT(7);
  */
-static inline int parity8(u8 val)
+#ifndef parity_odd

Please don't add this guard. We've got no any arch implementations so far, and this is a dead code. Those adding arch code will also add the ifdefery.

Quoted 18 lines
include/linux/bitops.h +5 -0
+static inline __attribute_const__ bool parity_odd(u64 val)
 {
 	/*
 	 * One explanation of this algorithm:
 	 * https://funloop.org/codex/problem/parity/README.html
 	 */
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
 	val ^= val >> 4;
 	return (0x6996 >> (val & 0xf)) & 1;
 }
+#endif
 
 /**
  * __ffs64 - find first set bit in a 64 bit word
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v4 01/13] bitops: Change parity8() to parity_odd() with u64 input and bool return type

Quoted 132 lines · Yury Norov
Quoted 131 lines · Kuan-Wei Chiu

Redesign the parity8() helper as parity_odd(), changing its input type from u8 to u64 to support broader use cases and its return type from int to bool to clearly reflect the function's binary output. The function now returns true for odd parity and false for even parity, making its behavior more intuitive based on the name.

Also mark the function with __attribute_const__ to enable better compiler optimization, as the result depends solely on its input and has no side effects.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

---
 arch/x86/kernel/bootflag.c               |  4 ++--
 drivers/hwmon/spd5118.c                  |  2 +-
 drivers/i3c/master/dw-i3c-master.c       |  2 +-
 drivers/i3c/master/i3c-master-cdns.c     |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c |  2 +-
 include/linux/bitops.h                   | 19 ++++++++++++-------
 6 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/kernel/bootflag.c +2 -2
index 73274d76ce16..86aae4b2bfd5 100644
@@ -26,7 +26,7 @@ static void __init sbf_write(u8 v)
 	unsigned long flags;
 
 	if (sbf_port != -1) {
-		if (!parity8(v))
+		if (!parity_odd(v))
 			v ^= SBF_PARITY;
 
 		printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n",
@@ -57,7 +57,7 @@ static bool __init sbf_value_valid(u8 v)
 {
 	if (v & SBF_RESERVED)		/* Reserved bits */
 		return false;
-	if (!parity8(v))
+	if (!parity_odd(v))
 		return false;
 
 	return true;
drivers/hwmon/spd5118.c +1 -1
index 358152868d96..15761f2ca4e9 100644
@@ -298,7 +298,7 @@ static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types typ
  */
 static bool spd5118_vendor_valid(u8 bank, u8 id)
 {
-	if (parity8(bank) == 0 || parity8(id) == 0)
+	if (!parity_odd(bank) || !parity_odd(id))
 		return false;
 
 	id &= 0x7f;
drivers/i3c/master/dw-i3c-master.c +1 -1
index 611c22b72c15..dc61d87fcd94 100644
@@ -867,7 +867,7 @@ static int dw_i3c_master_daa(struct i3c_master_controller *m)
 		master->devs[pos].addr = ret;
 		last_addr = ret;
 
-		ret |= parity8(ret) ? 0 : BIT(7);
+		ret |= parity_odd(ret) ? 0 : BIT(7);
 
 		writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret),
 		       master->regs +
drivers/i3c/master/i3c-master-cdns.c +1 -1
index fd3752cea654..df14f978a388 100644
@@ -889,7 +889,7 @@ static u32 prepare_rr0_dev_address(u32 addr)
 	ret |= (addr & GENMASK(9, 7)) << 6;
 
 	/* RR0[0] = ~XOR(addr[6:0]) */
-	ret |= parity8(addr & 0x7f) ? 0 : BIT(0);
+	ret |= parity_odd(addr & 0x7f) ? 0 : BIT(0);
 
 	return ret;
 }
drivers/i3c/master/mipi-i3c-hci/dat_v1.c +1 -1
index 85c4916972e4..d692a299607d 100644
@@ -114,7 +114,7 @@ static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,
 	dat_w0 = dat_w0_read(dat_idx);
 	dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY);
 	dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) |
-		  (parity8(address) ? 0 : DAT_0_DYNADDR_PARITY);
+		  (parity_odd(address) ? 0 : DAT_0_DYNADDR_PARITY);
 	dat_w0_write(dat_idx, dat_w0);
 }
 
include/linux/bitops.h +6 -6
index c1cb53cf2f0f..7c4c8afccef1 100644
@@ -230,35 +230,40 @@ static inline int get_count_order_long(unsigned long l)
 }
 
 /**
- * parity8 - get the parity of an u8 value
- * @value: the value to be examined
+ * parity_odd - get the parity of an u64 value
+ * @val: the value to be examined
  *
- * Determine the parity of the u8 argument.
+ * Determine the parity of the u64 argument.
  *
  * Returns:
- * 0 for even parity, 1 for odd parity
+ * false for even parity, true for odd parity
  *
  * Note: This function informs you about the current parity. Example to bail
  * out when parity is odd:
  *
- *	if (parity8(val) == 1)
+ *	if (parity_odd(val))
  *		return -EBADMSG;
  *
  * If you need to calculate a parity bit, you need to draw the conclusion from
  * this result yourself. Example to enforce odd parity, parity bit is bit 7:
  *
- *	if (parity8(val) == 0)
+ *	if (!parity_odd(val))
Quoted 9 lines

include/linux/bitops.h static inline int get_count_order_long(unsigned long l) · line 253

Quoted 4 lines
include/linux/bitops.h +1 -1
  *		val ^= BIT(7);
  */
-static inline int parity8(u8 val)
+#ifndef parity_odd

Please don't add this guard. We've got no any arch implementations so far, and this is a dead code. Those adding arch code will also add the ifdefery.

Ack.
Will do in next version.

Regards,
Kuan-Wei

Quoted 18 lines
Quoted 18 lines
include/linux/bitops.h +5 -0
+static inline __attribute_const__ bool parity_odd(u64 val)
 {
 	/*
 	 * One explanation of this algorithm:
 	 * https://funloop.org/codex/problem/parity/README.html
 	 */
+	val ^= val >> 32;
+	val ^= val >> 16;
+	val ^= val >> 8;
 	val ^= val >> 4;
 	return (0x6996 >> (val & 0xf)) & 1;
 }
+#endif
 
 /**
  * __ffs64 - find first set bit in a 64 bit word
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH v4 01/13] bitops: Change parity8() to parity_odd() with u64 input and bool return type

Quoted 34 lines · Kuan-Wei Chiu
Quoted 33 lines · Yury Norov
Quoted 32 lines · Kuan-Wei Chiu

Redesign the parity8() helper as parity_odd(), changing its input type from u8 to u64 to support broader use cases and its return type from int to bool to clearly reflect the function's binary output. The function now returns true for odd parity and false for even parity, making its behavior more intuitive based on the name.

Also mark the function with __attribute_const__ to enable better compiler optimization, as the result depends solely on its input and has no side effects.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

---
  arch/x86/kernel/bootflag.c               |  4 ++--
  drivers/hwmon/spd5118.c                  |  2 +-
  drivers/i3c/master/dw-i3c-master.c       |  2 +-
  drivers/i3c/master/i3c-master-cdns.c     |  2 +-
  drivers/i3c/master/mipi-i3c-hci/dat_v1.c |  2 +-
  include/linux/bitops.h                   | 19 ++++++++++++-------
  6 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/kernel/bootflag.c +0 -0
index 73274d76ce16..86aae4b2bfd5 100644
@@ -26,7 +26,7 @@ static void __init sbf_write(u8 v)
  	unsigned long flags;
 
Quoted 3 lines
Quoted 3 lines

arch/x86/kernel/bootflag.c static void __init sbf_write(u8 v) · line 29

Quoted 3 lines
arch/x86/kernel/bootflag.c +1 -1
  	if (sbf_port != -1) {
-		if (!parity8(v))
+		if (!parity_odd(v))

What is the benefit of this change all over the place instead of adding parity_odd() as new API and keeping the old one (just letting it call the new API) ?

A simple

static inline int parity8(u8 val)

{
	return parity_odd(val);
}

would have done the trick and be much less invasive.

Guenter

#5 lore raw

Re: [PATCH v4 01/13] bitops: Change parity8() to parity_odd() with u64 input and bool return type

Quoted 42 lines · Guenter Roeck
Quoted 27 lines · Kuan-Wei Chiu
Quoted 26 lines · Yury Norov
Quoted 25 lines · Kuan-Wei Chiu

Redesign the parity8() helper as parity_odd(), changing its input type from u8 to u64 to support broader use cases and its return type from int to bool to clearly reflect the function's binary output. The function now returns true for odd parity and false for even parity, making its behavior more intuitive based on the name.

Also mark the function with __attribute_const__ to enable better compiler optimization, as the result depends solely on its input and has no side effects.

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

---
  arch/x86/kernel/bootflag.c               |  4 ++--
  drivers/hwmon/spd5118.c                  |  2 +-
  drivers/i3c/master/dw-i3c-master.c       |  2 +-
  drivers/i3c/master/i3c-master-cdns.c     |  2 +-
  drivers/i3c/master/mipi-i3c-hci/dat_v1.c |  2 +-
  include/linux/bitops.h                   | 19 ++++++++++++-------
  6 files changed, 18 insertions(+), 13 deletions(-)
Quoted 9 lines
Quoted 9 lines

arch/x86/kernel/bootflag.c static void __init sbf_write(u8 v) · line 28

Quoted 9 lines
arch/x86/kernel/bootflag.c +1 -1
index 73274d76ce16..86aae4b2bfd5 100644
@@ -26,7 +26,7 @@ static void __init sbf_write(u8 v)
  	unsigned long flags;
  	if (sbf_port != -1) {
-		if (!parity8(v))
+		if (!parity_odd(v))

What is the benefit of this change all over the place instead of adding parity_odd() as new API and keeping the old one (just letting it call the new API) ?

Quoted 9 lines

A simple

static inline int parity8(u8 val)

{
	return parity_odd(val);
}

would have done the trick and be much less invasive.

Yury has previously mentioned that adding multiple fixed-type parity functions increases his maintenance burden. IIUC, he prefers having a single interface in bitops.h rather than multiple ones.

He were reluctant to add three more functions like:

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));
}

But instead, we ended up with:

static inline bool parity(u64 val)

{
    val ^= val >> 32;
	val ^= val >> 16;
	val ^= val >> 8;
	val ^= val >> 4;
	return (0x6996 >> (val & 0xf)) & 1;
}

static inline bool parity8(u8 val)

{
    return parity_odd(val);
}

But in the end, we introduced both parity(u64) and parity8(u8), which, IMHO, might be even more confusing than having consistent fixed-type helpers.

Regards,
Kuan-Wei

#6 lore raw

PATCH v4 2/13 media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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..5e1b7b1742e4 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 | (parity_odd(val) ? 0 : 0x80);
 }
 
 static void vivid_vbi_gen_set_time_of_day(u8 *packet)
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

Quoted 23 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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 +1 -0
index 70a4024d461e..5e1b7b1742e4 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>

drivers/media/test-drivers/vivid/vivid-vbi-gen.c static const u8 vivid_cc_sequence2[30] = { · line 169

Quoted 10 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +1 -6
@@ -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 | (parity_odd(val) ? 0 : 0x80);

So, if val == 0 than parity_odd(val) is also 0, and this can be simplified just to:

        return parity(val) ? 0 : 0x80;

Or I miss something?

Quoted 5 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +0 -0
 }
 
 static void vivid_vbi_gen_set_time_of_day(u8 *packet)
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

Quoted 37 lines · Yury Norov
Quoted 23 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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 +1 -0
index 70a4024d461e..5e1b7b1742e4 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>

drivers/media/test-drivers/vivid/vivid-vbi-gen.c static const u8 vivid_cc_sequence2[30] = { · line 169

Quoted 10 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +1 -6
@@ -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 | (parity_odd(val) ? 0 : 0x80);

So, if val == 0 than parity_odd(val) is also 0, and this can be simplified just to:

Quoted 3 lines
        return parity(val) ? 0 : 0x80;

Or I miss something?

If val == 0x01, the return value of calc_parity() will remain 0x01. If changed to return parity_odd(val) ? 0 : 0x80;, the return value will be changed to 0x00.

Regards,
Kuan-Wei

Quoted 5 lines
Quoted 5 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +0 -0
 }
 
 static void vivid_vbi_gen_set_time_of_day(u8 *packet)
Signature · 1 line
2.34.1
maintainer
#9 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

Quoted 41 lines · Kuan-Wei Chiu
Quoted 37 lines · Yury Norov
Quoted 23 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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 +1 -0
index 70a4024d461e..5e1b7b1742e4 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>

drivers/media/test-drivers/vivid/vivid-vbi-gen.c static const u8 vivid_cc_sequence2[30] = { · line 169

Quoted 10 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +1 -6
@@ -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 | (parity_odd(val) ? 0 : 0x80);

So, if val == 0 than parity_odd(val) is also 0, and this can be simplified just to:

Quoted 3 lines
        return parity(val) ? 0 : 0x80;

Or I miss something?

Quoted 3 lines

If val == 0x01, the return value of calc_parity() will remain 0x01. If changed to return parity_odd(val) ? 0 : 0x80;, the return value will be changed to 0x00.

Sorry, I meant

        return val ? 0 : 0x80;

This 'val | (parity_odd(val)' is only false when val == 0, right? When val != 0, compiler will return true immediately, not even calling parity().

I think we need a comment from authors.

#10 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

Quoted 46 lines · Yury Norov
Quoted 41 lines · Kuan-Wei Chiu
Quoted 37 lines · Yury Norov
Quoted 23 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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 +1 -0
index 70a4024d461e..5e1b7b1742e4 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>

drivers/media/test-drivers/vivid/vivid-vbi-gen.c static const u8 vivid_cc_sequence2[30] = { · line 169

Quoted 10 lines
drivers/media/test-drivers/vivid/vivid-vbi-gen.c +1 -6
@@ -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 | (parity_odd(val) ? 0 : 0x80);

So, if val == 0 than parity_odd(val) is also 0, and this can be simplified just to:

Quoted 3 lines
        return parity(val) ? 0 : 0x80;

Or I miss something?

Quoted 3 lines

If val == 0x01, the return value of calc_parity() will remain 0x01. If changed to return parity_odd(val) ? 0 : 0x80;, the return value will be changed to 0x00.

Quoted 7 lines

Sorry, I meant

        return val ? 0 : 0x80;

This 'val | (parity_odd(val)' is only false when val == 0, right? When val != 0, compiler will return true immediately, not even calling parity().

I'm still confused.

Maybe you're interpreting the code as:

	(val | parity(val)) ? 0 : 0x80

But what we're trying to do is:

	val | (parity(val) ? 0 : 0x80)

So, for example, if val == 0x06, the return value will be 0x86. Only returning 0 or 0x80 seems wrong to me.
Or did I misunderstand something?

Regards,
Kuan-Wei

Quoted 1 line

I think we need a comment from authors.

maintainer
#11 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

Quoted 27 lines · Kuan-Wei Chiu
Quoted 13 lines · Yury Norov
Quoted 11 lines · Kuan-Wei Chiu
Quoted 7 lines · Yury Norov

On Wed, Apr 09, 2025 at 11:43:45PM +0800, Kuan-Wei Chiu wrote:

So, if val == 0 than parity_odd(val) is also 0, and this can be simplified just to:

        return parity(val) ? 0 : 0x80;

Or I miss something?

If val == 0x01, the return value of calc_parity() will remain 0x01. If changed to return parity_odd(val) ? 0 : 0x80;, the return value will be changed to 0x00.

Quoted 7 lines

Sorry, I meant

        return val ? 0 : 0x80;

This 'val | (parity_odd(val)' is only false when val == 0, right? When val != 0, compiler will return true immediately, not even calling parity().

I'm still confused.

Maybe you're interpreting the code as:

	(val | parity(val)) ? 0 : 0x80
Quoted 7 lines

But what we're trying to do is:

	val | (parity(val) ? 0 : 0x80)

So, for example, if val == 0x06, the return value will be 0x86. Only returning 0 or 0x80 seems wrong to me.
Or did I misunderstand something?

I misread the whole construction. Sorry, you're right. Scratch this.

Reviewed-by on v4 2/13
#12 lore raw

Re: [PATCH v4 02/13] media: media/test_drivers: Replace open-coded parity calculation with parity_odd()

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

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

Regards,

Hans

Quoted 30 lines
---
 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..5e1b7b1742e4 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 | (parity_odd(val) ? 0 : 0x80);
 }
 
 static void vivid_vbi_gen_set_time_of_day(u8 *packet)
#13 lore raw

PATCH v4 3/13 media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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..15b515b95956 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 = !parity_odd(p[0]) || !parity_odd(p[1]);
 		break;
 	case 9:
 		sdid = V4L2_SLICED_VPS;
Signature · 1 line
2.34.1
maintainer
#14 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 14 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
  drivers/media/pci/cx18/cx18-av-vbi.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/pci/cx18/cx18-av-vbi.c +0 -0
index 65281d40c681..15b515b95956 100644

[...]

drivers/media/pci/cx18/cx18-av-vbi.c int cx18_av_decode_vbi_line(struct v4l2_subdev *sd, · line 273

Quoted 6 lines
drivers/media/pci/cx18/cx18-av-vbi.c +1 -1
@@ -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 = !parity_odd(p[0]) || !parity_odd(p[1]);

No need to call parity_odd() twice here. Instead you could do:

		err = !parity_odd(p[0] ^ p[1]);

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

		err = !parity_odd(*(u16 *)p);

Regards,
Arend

#15 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 27 lines · Arend van Spriel
Quoted 14 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
  drivers/media/pci/cx18/cx18-av-vbi.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/pci/cx18/cx18-av-vbi.c +0 -0
index 65281d40c681..15b515b95956 100644

[...]

drivers/media/pci/cx18/cx18-av-vbi.c int cx18_av_decode_vbi_line(struct v4l2_subdev *sd, · line 273

Quoted 6 lines
drivers/media/pci/cx18/cx18-av-vbi.c +1 -1
@@ -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 = !parity_odd(p[0]) || !parity_odd(p[1]);

No need to call parity_odd() twice here. Instead you could do:

Quoted 9 lines
		err = !parity_odd(p[0] ^ p[1]);

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

		err = !parity_odd(*(u16 *)p);

Thanks for the feedback!
Would you prefer this change to be part of the parity() conversion patch, or in a separate one?

Regards,
Kuan-Wei

Reviewed-by
#16 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 28 lines · Kuan-Wei Chiu
Quoted 27 lines · Arend van Spriel
Quoted 14 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
  drivers/media/pci/cx18/cx18-av-vbi.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/pci/cx18/cx18-av-vbi.c +0 -0
index 65281d40c681..15b515b95956 100644

[...]

drivers/media/pci/cx18/cx18-av-vbi.c int cx18_av_decode_vbi_line(struct v4l2_subdev *sd, · line 273

Quoted 6 lines
drivers/media/pci/cx18/cx18-av-vbi.c +1 -1
@@ -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 = !parity_odd(p[0]) || !parity_odd(p[1]);

No need to call parity_odd() twice here. Instead you could do:

Quoted 1 line
Quoted 1 line
		err = !parity_odd(p[0] ^ p[1]);

I prefer the original approach, it is easier to understand. Performance is not an issue here, clarity of the code is more important.

So for this patch (i.e. this v4 version):

Quoted 11 lines
Quoted 8 lines

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

		err = !parity_odd(*(u16 *)p);

Thanks for the feedback!
Would you prefer this change to be part of the parity() conversion patch, or in a separate one?

Just leave it as-is, as mentioned above.

Regards,

Hans

Quoted 3 lines

Regards,
Kuan-Wei

#17 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

In reply to Arend van Spriel
Quoted 7 lines · Arend van Spriel

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

		err = !parity_odd(*(u16 *)p);

Can it though? Need to be careful with alignment with that, I'd think.

johannes

maintainer
#18 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 9 lines · Johannes Berg
Quoted 6 lines · Arend van Spriel

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

err = !parity_odd(*(u16 *)p);

Can it though? Need to be careful with alignment with that, I'd think.

My bad. You are absolutely right.

Gr. AvS

Quoted 1 line
#19 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 8 lines · Arend Van Spriel
Quoted 6 lines · Johannes Berg
Quoted 5 lines · Arend van Spriel

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

err = !parity_odd(*(u16 *)p);

Can it though? Need to be careful with alignment with that, I'd think.

My bad. You are absolutely right.

Then maybe we can still go with:

	err = !parity_odd(p[0] ^ p[1]);

I believe this should still be a fairly safe approach?

Regards,
Kuan-Wei

maintainer
#20 lore raw

Re: [PATCH v4 03/13] media: pci: cx18-av-vbi: Replace open-coded parity calculation with parity_odd()

Quoted 11 lines · Kuan-Wei Chiu
Quoted 10 lines · Arend Van Spriel
Quoted 8 lines · Johannes Berg
Quoted 6 lines · Arend van Spriel

This is orthogonal to the change to parity_odd() though. More specific to the new parity_odd() you can now do following as parity_odd() argument is u64:

err = !parity_odd(*(u16 *)p);

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Can it though? Need to be careful with alignment with that, I'd think.

My bad. You are absolutely right.

Then maybe we can still go with:

err = !parity_odd(p[0] ^ p[1]);

I believe this should still be a fairly safe approach?

Yes. Or whatever the name will be ;-)

Regards,
Arend

#21 lore raw

PATCH v4 4/13 media: saa7115: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/media/i2c/saa7115.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/i2c/saa7115.c +2 -10
index a1c71187e773..a7886269dcfc 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 (!parity_odd(p[0]) || !parity_odd(p[1]))
 			return 0;
 		vbi->type = V4L2_SLICED_CAPTION_525;
 		break;
Signature · 1 line
2.34.1
Reviewed-by on v4 4/13
#22 lore raw

Re: [PATCH v4 04/13] media: saa7115: Replace open-coded parity calculation with parity_odd()

Quoted 6 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

Regards,

Hans

Quoted 41 lines
---
 drivers/media/i2c/saa7115.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/media/i2c/saa7115.c +2 -10
index a1c71187e773..a7886269dcfc 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 (!parity_odd(p[0]) || !parity_odd(p[1]))
 			return 0;
 		vbi->type = V4L2_SLICED_CAPTION_525;
 		break;
#23 lore raw

PATCH v4 5/13 serial: max3100: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/tty/serial/max3100.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/max3100.c +2 -1
index f2dd83692b2c..36ed41eef7b1 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 ^ parity_odd(c);
 	return parity;
 }
 
Signature · 1 line
2.34.1
maintainer
#24 lore raw

Re: [PATCH v4 05/13] serial: max3100: Replace open-coded parity calculation with parity_odd()

Quoted 23 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/tty/serial/max3100.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/max3100.c +1 -0
index f2dd83692b2c..36ed41eef7b1 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>

drivers/tty/serial/max3100.c static int max3100_do_parity(struct max3100_port *s, u16 c) · line 137

Quoted 5 lines
drivers/tty/serial/max3100.c +1 -1
@@ -133,7 +134,7 @@ static int max3100_do_parity(struct max3100_port *s, u16 c)
 	else
 		c &= 0xff;
 
-	parity = parity ^ (hweight8(c) & 1);
+	parity = parity ^ parity_odd(c);

This can be simplified for more unless I misunderstand something...

From: Yury Norov <[email protected]>
Date: Wed Apr 9 13:22:04 2025 -0400

serial: max3100: Replace open-coded parity

drivers/tty/serial/max3100.c +1 -9
index f2dd83692b2c..07d332b8e87d 100644
@@ -121,20 +121,12 @@ static DEFINE_MUTEX(max3100s_lock);		   /* race on probe */
 
 static int max3100_do_parity(struct max3100_port *s, u16 c)
 {
-	int parity;
-
-	if (s->parity & MAX3100_PARITY_ODD)
-		parity = 1;
-	else
-		parity = 0;
-
 	if (s->parity & MAX3100_7BIT)
 		c &= 0x7f;
 	else
 		c &= 0xff;
 
-	parity = parity ^ (hweight8(c) & 1);
-	return parity;
+	return s->parity & MAX3100_PARITY_ODD ? !parity(c) : parity(c);
 }
 
 static int max3100_check_parity(struct max3100_port *s, u16 c)
#25 lore raw

Re: [PATCH v4 05/13] serial: max3100: Replace open-coded parity calculation with parity_odd()

Quoted 27 lines · Yury Norov
Quoted 26 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/tty/serial/max3100.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/max3100.c +1 -0
index f2dd83692b2c..36ed41eef7b1 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;
 
Quoted 5 lines

drivers/tty/serial/max3100.c static int max3100_do_parity(struct max3100_port *s, u16 c) · line 137

Quoted 2 lines
drivers/tty/serial/max3100.c +1 -1
-	parity = parity ^ (hweight8(c) & 1);
+	parity = parity ^ parity_odd(c);

This can be simplified for more unless I misunderstand something...

I usually don't change the existing coding style since each subsystem may have its own preferred style.

But yeah, if this is the preferred way, I can make this change in the next version.

Regards,
Kuan-Wei

Quoted 31 lines

From: Yury Norov <[email protected]>
Date: Wed Apr 9 13:22:04 2025 -0400

serial: max3100: Replace open-coded parity

drivers/tty/serial/max3100.c +1 -9
index f2dd83692b2c..07d332b8e87d 100644
@@ -121,20 +121,12 @@ static DEFINE_MUTEX(max3100s_lock);		   /* race on probe */
 
 static int max3100_do_parity(struct max3100_port *s, u16 c)
 {
-	int parity;
-
-	if (s->parity & MAX3100_PARITY_ODD)
-		parity = 1;
-	else
-		parity = 0;
-
 	if (s->parity & MAX3100_7BIT)
 		c &= 0x7f;
 	else
 		c &= 0xff;
 
-	parity = parity ^ (hweight8(c) & 1);
-	return parity;
+	return s->parity & MAX3100_PARITY_ODD ? !parity(c) : parity(c);
 }
 
 static int max3100_check_parity(struct max3100_port *s, u16 c)
#26 lore raw

PATCH v4 6/13 lib/bch: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 lib/bch.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
lib/bch.c +1 -13
index 1c0cb07cdfeb..3f14d69f373e 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 |= parity_odd(mask) << (m-r);
 		}
 		sol[p] = tmp >> 1;
 	}
Signature · 1 line
2.34.1
#27 lore raw

PATCH v4 7/13 Input: joystick - Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/input/joystick/grip_mp.c    | 17 ++---------------
 drivers/input/joystick/sidewinder.c | 25 +++++--------------------
 2 files changed, 7 insertions(+), 35 deletions(-)
drivers/input/joystick/grip_mp.c +2 -15
index 5eadb5a3ca37..e7f60a83a89b 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 (parity_odd(pkt))
 		return IO_RESET;
 
 	/* Acknowledge packet receipt */
drivers/input/joystick/sidewinder.c +5 -20
index 3a5873e5fcb3..fb6f7004a485 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,22 +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 +301,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 (parity_odd(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 +318,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 (!parity_odd(GB(0,48)) || (hat = GB(42,4)) > 8)
 				return -1;
 
 			dev = sw->dev[0];
@@ -354,7 +339,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 (!parity_odd(GB(0,43)) || (hat = GB(28,4)) > 8)
 				return -1;
 
 			dev = sw->dev[0];
@@ -379,7 +364,7 @@ static int sw_parse(unsigned char *buf, struct sw *sw)
 
 		case SW_ID_FFW:
 
-			if (!sw_parity(GB(0,33)))
+			if (!parity_odd(GB(0,33)))
 				return -1;
 
 			dev = sw->dev[0];
Signature · 1 line
2.34.1
maintainer Acked-by on v4 7/13
#28 lore raw

Re: [PATCH v4 07/13] Input: joystick - Replace open-coded parity calculation with parity_odd()

Quoted 6 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

Thanks.

Signature · 1 line
Dmitry
#29 lore raw

PATCH v4 8/13 net: ethernet: oa_tc6: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/net/ethernet/oa_tc6.c | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)
drivers/net/ethernet/oa_tc6.c +3 -16
index db200e4ec284..6734b49357e7 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));
+			     !parity_odd(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, !parity_odd(header));
 
 	return cpu_to_be32(header);
 }
Signature · 1 line
2.34.1
#30 lore raw

PATCH v4 9/13 wifi: brcm80211: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 .../wireless/broadcom/brcm80211/brcmsmac/dma.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c +2 -16
index 80c35027787a..5d7500ee2d3b 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,24 +284,9 @@ 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);
+	return parity_odd(dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
 }
 
 /* descriptor bumping functions */
Signature · 1 line
2.34.1
reviewer
#31 lore raw

Re: [PATCH v4 09/13] wifi: brcm80211: Replace open-coded parity calculation with parity_odd()

Quoted 37 lines · Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 .../wireless/broadcom/brcm80211/brcmsmac/dma.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c +1 -10
index 80c35027787a..5d7500ee2d3b 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,24 +284,9 @@ 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;

drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c struct dma_info { · line 290

Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c +1 -6
-	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);
+	return parity_odd(dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
 }

parity32 expected a little-endian integer as it's argument while parity_odd expects a host byte order value.

I realise that the existing code just casts-away the endianness annotation, but this patch adds a Sparse warning.

.../brcmsmac/dma.c:289:66: warning: incorrect type in argument 1 (different base types)
.../brcmsmac/dma.c:289:66: expected unsigned long long [usertype] val
.../brcmsmac/dma.c:289:66: got restricted __le32

Quoted 6 lines
drivers/net/wireless/broadcom/brcm80211/brcmsmac/dma.c +0 -0
 /* descriptor bumping functions */
Signature · 1 line
2.34.1
#32 lore raw

PATCH v4 10/13 drm/bridge: dw-hdmi: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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..abf2c72be622 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 parity_odd(sample) << 27;
 }
 
 static void dw_hdmi_reformat_s24(struct snd_dw_hdmi *dw,
Signature · 1 line
2.34.1
#33 lore raw

PATCH v4 11/13 mtd: ssfdc: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/mtd/ssfdc.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
drivers/mtd/ssfdc.c +4 -16
index 46c01fa2ec46..0db4776424d0 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,24 +179,11 @@ 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)
 {
-	int block_address, parity;
+	int block_address;
+	bool parity;
 	int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */
 	int j;
 	int ok = 0;
@@ -215,7 +203,7 @@ static int get_logical_address(uint8_t *oob_buf)
 			block_address &= 0x7FF;
 			block_address >>= 1;
 
-			if (get_parity(block_address, 10) != parity) {
+			if (parity_odd(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
#34 lore raw

PATCH v4 12/13 fsi: i2cr: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 drivers/fsi/fsi-master-i2cr.c | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)
drivers/fsi/fsi-master-i2cr.c +3 -17
index 40f1f4d231e5..692d73e892e3 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,27 +39,12 @@ 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 != parity_odd(v);
 }
 
 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_odd(v) != I2CR_INITIAL_PARITY;
 }
 
 static u32 i2cr_get_command(u32 address, bool parity)
Signature · 1 line
2.34.1
#35 lore raw

PATCH v4 13/13 nfp: bpf: Replace open-coded parity calculation with parity_odd()

In reply to Kuan-Wei Chiu

Refactor parity calculations to use the standard parity_odd() helper. This change eliminates redundant implementations.

---
 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..2f8f78abb6f5 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 |= parity_odd(nfp_ustore_ecc_polynomials[i] & insn) << i;
 
 	return insn | (u64)ecc << NFP_USTORE_OP_BITS;
 }
Signature · 1 line
2.34.1
maintainer
#36 lore raw
In reply to Kuan-Wei Chiu
Quoted 6 lines · Kuan-Wei Chiu

Several parts of the kernel contain open-coded and redundant implementations of parity calculation. This patch series introduces a unified helper, parity_odd(), to simplify and standardize these cases.

The first patch renames parity8() to parity_odd(), changes its argument

Alright, if it's an extension of the area of applicability, it should be renamed to just parity(). I already shared a table that summarized the drivers authors' view on that, and they clearly prefer not to add the suffix - 13 vs 2. The __builtin_parity() doesn't care of suffix as well.

https://lore.kernel.org/all/Z9GtcNJie8TRKywZ@thinkpad/

Yes, the argument that boolean function should explain itself sounds correct, but in this case, comment on top of the function looks enough to me.

The existing codebase doesn't care about the suffix as well. If no strong preference, let's just pick a short and sweet name?

Quoted 4 lines

type from u8 to u64 for broader applicability, and updates its return type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

That's correct and nice, but can you support it with a bloat-o-meter's before/after and/or asm snippets? I also think it worth to be a separate patch, preferably the last patch in the series.

Quoted 82 lines

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

Subsequent patches refactor various kernel components to replace open-coded parity logic with the new helper, reducing code duplication and improving consistency.

---

To H. Peter:
I understand your preference for a parity8/16/32/64() style interface, and I agree that such a design would better accommodate potential arch-specific implementations. However, I suspect there are very few, if any, users who care about the performance of parity calculations enough to warrant such optimizations. So my inclination is to defer any arch-specific or optimized implementations until we see parity_odd() being used in hot paths.

Changes in v4:
- Rename parity8() to parity_odd().
- Change the argument type from u8 to u64.
- Use a single parity_odd() function.

Changes in v3:
- Avoid using __builtin_parity.
- Change return type to bool.
- Drop parity() macro.
- Change parityXX() << y to !!parityXX() << y.

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

v3: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/

Kuan-Wei Chiu (13):
bitops: Change parity8() to parity_odd() with u64 input and bool
return type
media: media/test_drivers: Replace open-coded parity calculation with
parity_odd()
media: pci: cx18-av-vbi: Replace open-coded parity calculation with
parity_odd()
media: saa7115: Replace open-coded parity calculation with
parity_odd()
serial: max3100: Replace open-coded parity calculation with
parity_odd()
lib/bch: Replace open-coded parity calculation with parity_odd()
Input: joystick - Replace open-coded parity calculation with
parity_odd()
net: ethernet: oa_tc6: Replace open-coded parity calculation with
parity_odd()
wifi: brcm80211: Replace open-coded parity calculation with
parity_odd()
drm/bridge: dw-hdmi: Replace open-coded parity calculation with
parity_odd()
mtd: ssfdc: Replace open-coded parity calculation with parity_odd()
fsi: i2cr: Replace open-coded parity calculation with parity_odd()
nfp: bpf: Replace open-coded parity calculation with parity_odd()

 arch/x86/kernel/bootflag.c                    |  4 +--
 drivers/fsi/fsi-master-i2cr.c                 | 20 +++------------
 .../drm/bridge/synopsys/dw-hdmi-ahb-audio.c   |  8 ++----
 drivers/hwmon/spd5118.c                       |  2 +-
 drivers/i3c/master/dw-i3c-master.c            |  2 +-
 drivers/i3c/master/i3c-master-cdns.c          |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c      |  2 +-
 drivers/input/joystick/grip_mp.c              | 17 ++-----------
 drivers/input/joystick/sidewinder.c           | 25 ++++---------------
 drivers/media/i2c/saa7115.c                   | 12 ++-------
 drivers/media/pci/cx18/cx18-av-vbi.c          | 12 ++-------
 .../media/test-drivers/vivid/vivid-vbi-gen.c  |  8 ++----
Quoted 8 lines
 drivers/mtd/ssfdc.c                           | 20 +++------------
 drivers/net/ethernet/netronome/nfp/nfp_asm.c  |  7 +-----
 drivers/net/ethernet/oa_tc6.c                 | 19 +++-----------
 .../broadcom/brcm80211/brcmsmac/dma.c         | 18 ++-----------
 drivers/tty/serial/max3100.c                  |  3 ++-
 include/linux/bitops.h                        | 19 ++++++++------
 lib/bch.c                                     | 14 +----------
 19 files changed, 49 insertions(+), 165 deletions(-)

OK, now it looks like a nice consolidation and simplification of code base. Thanks for the work.

Thanks,
Yury

#37 lore raw
Quoted 13 lines · Yury Norov
Quoted 6 lines · Kuan-Wei Chiu

Several parts of the kernel contain open-coded and redundant implementations of parity calculation. This patch series introduces a unified helper, parity_odd(), to simplify and standardize these cases.

The first patch renames parity8() to parity_odd(), changes its argument

Alright, if it's an extension of the area of applicability, it should be renamed to just parity(). I already shared a table that summarized the drivers authors' view on that, and they clearly prefer not to add the suffix - 13 vs 2. The __builtin_parity() doesn't care of suffix as well.

Quoted 9 lines

https://lore.kernel.org/all/Z9GtcNJie8TRKywZ@thinkpad/

Yes, the argument that boolean function should explain itself sounds correct, but in this case, comment on top of the function looks enough to me.

The existing codebase doesn't care about the suffix as well. If no strong preference, let's just pick a short and sweet name?

I don't have a strong preference for the name, but if I had to guess the return value from the function prototype, I would intuitively expect an int to return "0 for even and 1 for odd," and a bool to return "true for even, false for odd." I recall Jiri and Jacob shared similar thoughts, which is why I felt adding _odd could provide better clarity.

However, I agree that if the kernel doc comment is clear, it might not be a big issue. But David previously mentioned that he doesn't want to rely on checking the function's documentation every time while reading the code.

Regardless, I'm flexible as long as we all reach a consensus on the naming.

Quoted 9 lines
Quoted 4 lines

type from u8 to u64 for broader applicability, and updates its return type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

That's correct and nice, but can you support it with a bloat-o-meter's before/after and/or asm snippets? I also think it worth to be a separate patch, preferably the last patch in the series.

I quickly tested it with the x86 defconfig, and it appears that the generated code doesn't change. I forgot who requested the addition during the review process, but I initially thought it would either improve the generated code or leave it unchanged without significantly increasing the source code size.

However, if there's no actual difference in the generated code, maybe let's just remove it?

Regards,
Kuan-Wei

Quoted 96 lines
Quoted 82 lines

While more efficient implementations may exist, further optimization is postponed until a use case in performance-critical paths arises.

Subsequent patches refactor various kernel components to replace open-coded parity logic with the new helper, reducing code duplication and improving consistency.

---

To H. Peter:
I understand your preference for a parity8/16/32/64() style interface, and I agree that such a design would better accommodate potential arch-specific implementations. However, I suspect there are very few, if any, users who care about the performance of parity calculations enough to warrant such optimizations. So my inclination is to defer any arch-specific or optimized implementations until we see parity_odd() being used in hot paths.

Changes in v4:
- Rename parity8() to parity_odd().
- Change the argument type from u8 to u64.
- Use a single parity_odd() function.

Changes in v3:
- Avoid using __builtin_parity.
- Change return type to bool.
- Drop parity() macro.
- Change parityXX() << y to !!parityXX() << y.

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

v3: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/

Kuan-Wei Chiu (13):
bitops: Change parity8() to parity_odd() with u64 input and bool
return type
media: media/test_drivers: Replace open-coded parity calculation with
parity_odd()
media: pci: cx18-av-vbi: Replace open-coded parity calculation with
parity_odd()
media: saa7115: Replace open-coded parity calculation with
parity_odd()
serial: max3100: Replace open-coded parity calculation with
parity_odd()
lib/bch: Replace open-coded parity calculation with parity_odd()
Input: joystick - Replace open-coded parity calculation with
parity_odd()
net: ethernet: oa_tc6: Replace open-coded parity calculation with
parity_odd()
wifi: brcm80211: Replace open-coded parity calculation with
parity_odd()
drm/bridge: dw-hdmi: Replace open-coded parity calculation with
parity_odd()
mtd: ssfdc: Replace open-coded parity calculation with parity_odd()
fsi: i2cr: Replace open-coded parity calculation with parity_odd()
nfp: bpf: Replace open-coded parity calculation with parity_odd()

 arch/x86/kernel/bootflag.c                    |  4 +--
 drivers/fsi/fsi-master-i2cr.c                 | 20 +++------------
 .../drm/bridge/synopsys/dw-hdmi-ahb-audio.c   |  8 ++----
 drivers/hwmon/spd5118.c                       |  2 +-
 drivers/i3c/master/dw-i3c-master.c            |  2 +-
 drivers/i3c/master/i3c-master-cdns.c          |  2 +-
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c      |  2 +-
 drivers/input/joystick/grip_mp.c              | 17 ++-----------
 drivers/input/joystick/sidewinder.c           | 25 ++++---------------
 drivers/media/i2c/saa7115.c                   | 12 ++-------
 drivers/media/pci/cx18/cx18-av-vbi.c          | 12 ++-------
 .../media/test-drivers/vivid/vivid-vbi-gen.c  |  8 ++----
Quoted 8 lines
 drivers/mtd/ssfdc.c                           | 20 +++------------
 drivers/net/ethernet/netronome/nfp/nfp_asm.c  |  7 +-----
 drivers/net/ethernet/oa_tc6.c                 | 19 +++-----------
 .../broadcom/brcm80211/brcmsmac/dma.c         | 18 ++-----------
 drivers/tty/serial/max3100.c                  |  3 ++-
 include/linux/bitops.h                        | 19 ++++++++------
 lib/bch.c                                     | 14 +----------
 19 files changed, 49 insertions(+), 165 deletions(-)

OK, now it looks like a nice consolidation and simplification of code base. Thanks for the work.

Thanks,
Yury

maintainer
#38 lore raw
Quoted 23 lines · Kuan-Wei Chiu
Quoted 13 lines · Yury Norov
Quoted 6 lines · Kuan-Wei Chiu

Several parts of the kernel contain open-coded and redundant implementations of parity calculation. This patch series introduces a unified helper, parity_odd(), to simplify and standardize these cases.

The first patch renames parity8() to parity_odd(), changes its argument

Alright, if it's an extension of the area of applicability, it should be renamed to just parity(). I already shared a table that summarized the drivers authors' view on that, and they clearly prefer not to add the suffix - 13 vs 2. The __builtin_parity() doesn't care of suffix as well.

Quoted 9 lines

https://lore.kernel.org/all/Z9GtcNJie8TRKywZ@thinkpad/

Yes, the argument that boolean function should explain itself sounds correct, but in this case, comment on top of the function looks enough to me.

The existing codebase doesn't care about the suffix as well. If no strong preference, let's just pick a short and sweet name?

Quoted 6 lines

I don't have a strong preference for the name, but if I had to guess the return value from the function prototype, I would intuitively expect an int to return "0 for even and 1 for odd," and a bool to return "true for even, false for odd." I recall Jiri and Jacob shared similar thoughts, which is why I felt adding _odd could provide better clarity.

I think they said they are convinced that parity should return 1 for odd because of folding and __builtin_parity() arguments.

Quoted 4 lines

However, I agree that if the kernel doc comment is clear, it might not be a big issue. But David previously mentioned that he doesn't want to rely on checking the function's documentation every time while reading the code.

He's wrong. Kernel engineers _must_ read documentation, regardless.

Quoted 12 lines

Regardless, I'm flexible as long as we all reach a consensus on the naming.

Quoted 9 lines
Quoted 4 lines

type from u8 to u64 for broader applicability, and updates its return type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

That's correct and nice, but can you support it with a bloat-o-meter's before/after and/or asm snippets? I also think it worth to be a separate patch, preferably the last patch in the series.

Quoted 5 lines

I quickly tested it with the x86 defconfig, and it appears that the generated code doesn't change. I forgot who requested the addition during the review process, but I initially thought it would either improve the generated code or leave it unchanged without significantly increasing the source code size.

That's what I actually expected, but was shy to guess openly. :). It's hard to imagine how compiler may improve code generation in this case...

This attribute is used when there's an asm block, or some non-trivial function call. In this case, the function is self-consistent and makes no calls. And you see, const annotation raises more questions than solves problems. Let's drop it.

Thanks,
Yury

#39 lore raw
Quoted 11 lines · Yury Norov
Quoted 7 lines
Quoted 1 line

I don't have a strong preference for the name, but if I had to guess the return value from the function prototype, I would intuitively expect an int to return "0 for even and 1 for odd," and a bool to return "true for even, false for odd." I recall Jiri and Jacob shared similar thoughts, which is why I felt adding _odd could provide better clarity.

I think they said they are convinced that parity should return 1 for odd because of folding and __builtin_parity() arguments.

And for bool, 0 == false, and 1 == true. In fact, the *definitions* for false and true in C (but not C++) is:

<stdbool.h>:
typedef _Bool bool;
#define false 0
#define true 1

If someone wants to make more clear, it would be better to put "typedef bool bit_t" in a common header, but that personally seems ridiculous to me.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

type from u8 to u64 for broader applicability, and updates its

return

Quoted 14 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

That's correct and nice, but can you support it with a bloat-o-meter's before/after and/or asm snippets? I also think it worth to be a separate patch, preferably the last patch in the series.

Quoted 5 lines

I quickly tested it with the x86 defconfig, and it appears that the generated code doesn't change. I forgot who requested the addition during the review process, but I initially thought it would either improve the generated code or leave it unchanged without significantly increasing the source code size.

Quoted 7 lines

That's what I actually expected, but was shy to guess openly. :). It's hard to imagine how compiler may improve code generation in this case...

This attribute is used when there's an asm block, or some non-trivial function call. In this case, the function is self-consistent and makes no calls. And you see, const annotation raises more questions than solves problems. Let's drop it.

Ah yes; one of the quirks about gcc asm is that an asm is implicitly assumed "const" (with no memory operands) or "pure" (with memory operands) unless declared volatile or given an explicit "memory" clobber.

So yes, the compiler can most definitely derive the constness from the form of the function even in the variable case.

I would still like to see __builtin_parity() being used as an architecture opt-in; it can, of course, also be unconditionally used in the constant case.

So in the end one of these two become my preferred implementation, and I really don't think it is very complicated:

#ifndef use_builtin_parity
#define use_builtin_parity(x) __builtin_constant_p(x) #endif

static inline bool parity8(u8 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	val ^= val >> 4;
	return (0x6996 >> (val & 0xf)) & 1;
}

static inline bool parity16(u16 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	return parity8(val ^ (val >> 8));
}

static inline bool parity32(u32 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	return parity16(val ^ (val >> 16));
}

static inline bool parity64(u64 val)

{
	if (use_builtin_parity(val))
		return __builtin_parityll(val);
	return parity32(val ^ (val >> 32));
}

This means that an architecture -- in particular, x86 -- can still ask to use __builtin_parity*() directly. It means that architectures on which __builtin_parity*() produces bad code should either complain to the gcc/clang team and have it fixed, or we can add additional mechanism for them to override the implementation at that time.

The alternative is to stop worrying about overengineering, and just do it once and for all:

#ifndef parity8
static inline bool parity8(u8 val)

{
	val ^= val >> 4;
	return (0x6996 >> (val & 0xf)) & 1;
}

#endif

#ifndef parity16
static inline bool parity16(u16 val)

{
	return parity8(val ^ (val >> 8));
}

#endif

#ifndef parity32
static inline bool parity32(u32 val)

{
	return parity16(val ^ (val >> 16));
}

#endif

#ifndef parity64
static inline bool parity64(u64 val)

{
	return parity32(val ^ (val >> 32));
}

#endif

In either case, instead of packing the cascade into one function, make good use of it.

In the latter case, __builtin_constant_p() isn't necessary as it puts the onus on the architecture to separate out const and non-const cases, if it matters -- which it doesn't if the architecture simply wants to use __builtin_parity:

#define parity8(x) ((bool) __builtin_parity((u8)(x))) #define parity16(x) ((bool) __builtin_parity((u16)(x))) #define parity32(x) ((bool) __builtin_parity((u32)(x))) #define parity64(x) ((bool) __builtin_parityll((u64)(x)))

As stated before, I don't really see that the parity function itself would be very suitable for a generic helper, but if it were to, then using the "standard" macro construct for it would seem to be the better option.

(And I would be very much in favor of not open-coding the helper everywhere but to macroize it; effectively creating a C++ template equivalent. It is out of scope for this project, though.)

-hpa

#40 lore raw
Quoted 144 lines · H. Peter Anvin
Quoted 11 lines · Yury Norov
Quoted 7 lines
Quoted 1 line

I don't have a strong preference for the name, but if I had to guess the return value from the function prototype, I would intuitively expect an int to return "0 for even and 1 for odd," and a bool to return "true for even, false for odd." I recall Jiri and Jacob shared similar thoughts, which is why I felt adding _odd could provide better clarity.

I think they said they are convinced that parity should return 1 for odd because of folding and __builtin_parity() arguments.

And for bool, 0 == false, and 1 == true. In fact, the *definitions* for false and true in C (but not C++) is:

<stdbool.h>:
typedef _Bool bool;
#define false 0
#define true 1

If someone wants to make more clear, it would be better to put "typedef bool bit_t" in a common header, but that personally seems ridiculous to me.

Quoted 15 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

type from u8 to u64 for broader applicability, and updates its return type from int to bool to make its usage and return semantics more intuitive-returning true for odd parity and false for even parity. It also adds __attribute_const__ to enable compiler optimizations.

That's correct and nice, but can you support it with a bloat-o-meter's before/after and/or asm snippets? I also think it worth to be a separate patch, preferably the last patch in the series.

Quoted 5 lines

I quickly tested it with the x86 defconfig, and it appears that the generated code doesn't change. I forgot who requested the addition during the review process, but I initially thought it would either improve the generated code or leave it unchanged without significantly increasing the source code size.

Quoted 7 lines

That's what I actually expected, but was shy to guess openly. :). It's hard to imagine how compiler may improve code generation in this case...

This attribute is used when there's an asm block, or some non-trivial function call. In this case, the function is self-consistent and makes no calls. And you see, const annotation raises more questions than solves problems. Let's drop it.

Ah yes; one of the quirks about gcc asm is that an asm is implicitly assumed "const" (with no memory operands) or "pure" (with memory operands) unless declared volatile or given an explicit "memory" clobber.

So yes, the compiler can most definitely derive the constness from the form of the function even in the variable case.

I would still like to see __builtin_parity() being used as an architecture opt-in; it can, of course, also be unconditionally used in the constant case.

So in the end one of these two become my preferred implementation, and I really don't think it is very complicated:

#ifndef use_builtin_parity
#define use_builtin_parity(x) __builtin_constant_p(x) #endif

static inline bool parity8(u8 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	val ^= val >> 4;
	return (0x6996 >> (val & 0xf)) & 1;
}

static inline bool parity16(u16 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	return parity8(val ^ (val >> 8));
}

static inline bool parity32(u32 val)

{
	if (use_builtin_parity(val))
		return __builtin_parity(val);
	return parity16(val ^ (val >> 16));
}

static inline bool parity64(u64 val)

{
	if (use_builtin_parity(val))
		return __builtin_parityll(val);
	return parity32(val ^ (val >> 32));
}

This means that an architecture -- in particular, x86 -- can still ask to use __builtin_parity*() directly. It means that architectures on which __builtin_parity*() produces bad code should either complain to the gcc/clang team and have it fixed, or we can add additional mechanism for them to override the implementation at that time.

The alternative is to stop worrying about overengineering, and just do it once and for all:

#ifndef parity8
static inline bool parity8(u8 val)

{
	val ^= val >> 4;
	return (0x6996 >> (val & 0xf)) & 1;
}

#endif

#ifndef parity16
static inline bool parity16(u16 val)

{
	return parity8(val ^ (val >> 8));
}

#endif

#ifndef parity32
static inline bool parity32(u32 val)

{
	return parity16(val ^ (val >> 16));
}

#endif

#ifndef parity64
static inline bool parity64(u64 val)

{
	return parity32(val ^ (val >> 32));
}

#endif

In either case, instead of packing the cascade into one function, make good use of it.

In the latter case, __builtin_constant_p() isn't necessary as it puts the onus on the architecture to separate out const and non-const cases, if it matters -- which it doesn't if the architecture simply wants to use __builtin_parity:

#define parity8(x) ((bool) __builtin_parity((u8)(x))) #define parity16(x) ((bool) __builtin_parity((u16)(x))) #define parity32(x) ((bool) __builtin_parity((u32)(x))) #define parity64(x) ((bool) __builtin_parityll((u64)(x)))

Quoted 8 lines

As stated before, I don't really see that the parity function itself would be very suitable for a generic helper, but if it were to, then using the "standard" macro construct for it would seem to be the better option.

(And I would be very much in favor of not open-coding the helper everywhere but to macroize it; effectively creating a C++ template equivalent. It is out of scope for this project, though.)

IIUC, you prefer using the parity8/16/32/64() interface with __builtin_parity(), regardless of whether there are users on the hot path?

If the maintainer has no concerns about maintenance burden, I also lean toward this interface. While I don't think the implementation is particularly complex, I'm not the maintainer, so I'd rather not argue about the maintenance aspect. Also, to be clear, I don't think I'm the right person to provide evidence that performance or code generation matters to any user.

OTOH, If we do end up going with one of the two approaches, since bitops.h is included (directly or indirectly) by many files while parity is only used in fewer than 20, perhaps we should move the parity-related code to a separate parity.h. It doesn't necessarily have to be maintained by Yury - it could be someone else, or me.

Regards,
Kuan-Wei

#41 lore raw
Quoted 18 lines · Kuan-Wei Chiu
Quoted 18 lines

In either case, instead of packing the cascade into one function, make good use of it.

In the latter case, __builtin_constant_p() isn't necessary as it puts the onus on the architecture to separate out const and non-const cases, if it matters -- which it doesn't if the architecture simply wants to use __builtin_parity:

#define parity8(x) ((bool) __builtin_parity((u8)(x))) #define parity16(x) ((bool) __builtin_parity((u16)(x))) #define parity32(x) ((bool) __builtin_parity((u32)(x))) #define parity64(x) ((bool) __builtin_parityll((u64)(x)))

As stated before, I don't really see that the parity function itself would be very suitable for a generic helper, but if it were to, then using the "standard" macro construct for it would seem to be the better option.

Quoted 7 lines
Quoted 4 lines

(And I would be very much in favor of not open-coding the helper everywhere but to macroize it; effectively creating a C++ template equivalent. It is out of scope for this project, though.)

IIUC, you prefer using the parity8/16/32/64() interface with __builtin_parity(), regardless of whether there are users on the hot path?

As a per-architecture opt-in, yes.

-hpa

#42 lore raw
Quoted 23 lines · H. Peter Anvin
Quoted 22 lines · Kuan-Wei Chiu
Quoted 22 lines

In either case, instead of packing the cascade into one function, make good use of it.

In the latter case, __builtin_constant_p() isn't necessary as it puts the onus on the architecture to separate out const and non-const cases, if it matters -- which it doesn't if the architecture simply wants to use __builtin_parity:

#define parity8(x) ((bool) __builtin_parity((u8)(x))) #define parity16(x) ((bool) __builtin_parity((u16)(x))) #define parity32(x) ((bool) __builtin_parity((u32)(x))) #define parity64(x) ((bool) __builtin_parityll((u64)(x)))

As stated before, I don't really see that the parity function itself would be very suitable for a generic helper, but if it were to, then using the "standard" macro construct for it would seem to be the better option.

(And I would be very much in favor of not open-coding the helper everywhere but to macroize it; effectively creating a C++ template equivalent. It is out of scope for this project, though.)

Quoted 6 lines
Quoted 3 lines

IIUC, you prefer using the parity8/16/32/64() interface with __builtin_parity(), regardless of whether there are users on the hot path?

As a per-architecture opt-in, yes.

I'd prefer to see Yury agree first, otherwise there's a high risk of a maintainer NAK after the next submission.

Regards,
Kuan-Wei