Re: [PATCH 02/17] bitops: Add generic parity calculation for u64

Jiri Slaby <[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]>
On 25. 02. 25, 14:29, Kuan-Wei Chiu wrote:
>> +#define parity(val)					\
>> +({							\
>> +	u64 __v = (val);				\
>> +	int __ret;					\
>> +	switch (BITS_PER_TYPE(val)) {			\
>> +	case 64:					\
>> +		__v ^= __v >> 32;			\
>> +		fallthrough;				\
>> +	case 32:					\
>> +		__v ^= __v >> 16;			\
>> +		fallthrough;				\
>> +	case 16:					\
>> +		__v ^= __v >> 8;			\
>> +		fallthrough;				\
>> +	case 8:						\
>> +		__v ^= __v >> 4;			\
>> +		__ret =  (0x6996 >> (__v & 0xf)) & 1;	\
>> +		break;					\
>> +	default:					\
>> +		BUILD_BUG();				\
>> +	}						\
>> +	__ret;						\
>> +})
>> +
>> +#define parity8(val)	parity((u8)(val))
>> +#define parity32(val)	parity((u32)(val))
>> +#define parity64(val)	parity((u64)(val))
>>   
> What do you think about using these inline functions instead of macros?
> Except for parity8(), each function is a single line and follows the
> same logic. I find inline functions more readable, and coding-style.rst
> also recommends them over macros.

Not in cases where macros are inevitable. I mean, do we need parityXX() 
for XX in (8, 16, 32, 64) at all? Isn't the parity() above enough for 
everybody? And if not, you can have all those parityXX() as inlines as 
you suggest, but also provide a macro such as the above to call 
(optimized) parityXX() as per datatype len.

thanks,
-- 
js
suse labs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.