[PATCH v3 02/16] bitops: Add parity16(), parity32(), and parity64() helpers
Kuan-Wei Chiu <[email protected]>
| Newsgroups | dev.linux.lists.brcm80211,org.freedesktop.lists.dri-devel,org.infradead.lists.linux-mtd,org.kernel.vger.bpf,org.kernel.vger.linux-input,org.kernel.vger.linux-kernel,org.kernel.vger.linux-media,org.kernel.vger.linux-serial,org.kernel.vger.linux-wireless,org.kernel.vger.netdev |
|---|---|
| Message-ID | <[email protected]> |
Add parity16(), parity32(), and parity64() to compute the parity of 16-bit, 32-bit, and 64-bit values, respectively. Each function extends parity8() by XOR-ing upper and lower halves, reducing the input size progressively. Co-developed-by: Yu-Chun Lin <[email protected]> Signed-off-by: Yu-Chun Lin <[email protected]> Signed-off-by: Kuan-Wei Chiu <[email protected]> --- include/linux/bitops.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 44e5765b8bec..906757e1ddf8 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -260,6 +260,48 @@ static inline __attribute_const__ bool parity8(u8 val) return (0x6996 >> (val & 0xf)) & 1; } +/** + * parity16 - get the parity of an u16 value + * @val: the value to be examined + * + * Determine the parity of the u16 argument. + * + * Returns: + * false for even parity, true for odd parity + */ +static inline __attribute_const__ bool parity16(u16 val) +{ + return parity8(val ^ (val >> 8)); +} + +/** + * parity32 - get the parity of an u32 value + * @val: the value to be examined + * + * Determine the parity of the u32 argument. + * + * Returns: + * false for even parity, true for odd parity + */ +static inline __attribute_const__ bool parity32(u32 val) +{ + return parity16(val ^ (val >> 16)); +} + +/** + * parity64 - get the parity of an u64 value + * @val: the value to be examined + * + * Determine the parity of the u64 argument. + * + * Returns: + * false for even parity, true for odd parity + */ +static inline __attribute_const__ bool parity64(u64 val) +{ + return parity32(val ^ (val >> 32)); +} + /** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word -- 2.34.1