[PATCH v2 18/18] bitops: Add parity() macro for automatic type-based selection
Kuan-Wei Chiu
visitorckw at gmail.com
Sat Mar 1 14:24:09 UTC 2025
Introduce the parity() macro, which selects the appropriate parity
function (parity8(), parity16(), parity32(), or parity64()) based on
the size of the input type. This improves usability by allowing a
generic parity calculation without requiring explicit function
selection.
If the input type does not match the supported sizes, BUILD_BUG() is
triggered to catch invalid usage at compile time.
Co-developed-by: Yu-Chun Lin <eleanor15x at gmail.com>
Signed-off-by: Yu-Chun Lin <eleanor15x at gmail.com>
Signed-off-by: Kuan-Wei Chiu <visitorckw at gmail.com>
---
Place this patch last in the series to avoid compilation errors.
include/linux/bitops.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 41e9e7fb894b..fa4e45741dff 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -339,6 +339,28 @@ static inline __attribute_const__ int parity64(u64 val)
return __builtin_constant_p(val) ? _parity_const(val) : _parity64(val);
}
+#define parity(val) \
+({ \
+ int __ret; \
+ switch (BITS_PER_TYPE(val)) { \
+ case 64: \
+ __ret = parity64(val); \
+ break; \
+ case 32: \
+ __ret = parity32(val); \
+ break; \
+ case 16: \
+ __ret = parity16(val); \
+ break; \
+ case 8: \
+ __ret = parity8(val); \
+ break; \
+ default: \
+ BUILD_BUG(); \
+ } \
+ __ret; \
+})
+
/**
* __ffs64 - find first set bit in a 64 bit word
* @word: The 64 bit word
--
2.34.1
More information about the dri-devel
mailing list