162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#include <linux/export.h> 362306a36Sopenharmony_ci#include <linux/bitops.h> 462306a36Sopenharmony_ci#include <asm/types.h> 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/** 762306a36Sopenharmony_ci * hweightN - returns the hamming weight of a N-bit word 862306a36Sopenharmony_ci * @x: the word to weigh 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * The Hamming Weight of a number is the total number of bits set in it. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciunsigned int __sw_hweight32(unsigned int w) 1462306a36Sopenharmony_ci{ 1562306a36Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER 1662306a36Sopenharmony_ci w -= (w >> 1) & 0x55555555; 1762306a36Sopenharmony_ci w = (w & 0x33333333) + ((w >> 2) & 0x33333333); 1862306a36Sopenharmony_ci w = (w + (w >> 4)) & 0x0f0f0f0f; 1962306a36Sopenharmony_ci return (w * 0x01010101) >> 24; 2062306a36Sopenharmony_ci#else 2162306a36Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x55555555); 2262306a36Sopenharmony_ci res = (res & 0x33333333) + ((res >> 2) & 0x33333333); 2362306a36Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F0F0F; 2462306a36Sopenharmony_ci res = res + (res >> 8); 2562306a36Sopenharmony_ci return (res + (res >> 16)) & 0x000000FF; 2662306a36Sopenharmony_ci#endif 2762306a36Sopenharmony_ci} 2862306a36Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight32); 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciunsigned int __sw_hweight16(unsigned int w) 3162306a36Sopenharmony_ci{ 3262306a36Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x5555); 3362306a36Sopenharmony_ci res = (res & 0x3333) + ((res >> 2) & 0x3333); 3462306a36Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F; 3562306a36Sopenharmony_ci return (res + (res >> 8)) & 0x00FF; 3662306a36Sopenharmony_ci} 3762306a36Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight16); 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ciunsigned int __sw_hweight8(unsigned int w) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x55); 4262306a36Sopenharmony_ci res = (res & 0x33) + ((res >> 2) & 0x33); 4362306a36Sopenharmony_ci return (res + (res >> 4)) & 0x0F; 4462306a36Sopenharmony_ci} 4562306a36Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight8); 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciunsigned long __sw_hweight64(__u64 w) 4862306a36Sopenharmony_ci{ 4962306a36Sopenharmony_ci#if BITS_PER_LONG == 32 5062306a36Sopenharmony_ci return __sw_hweight32((unsigned int)(w >> 32)) + 5162306a36Sopenharmony_ci __sw_hweight32((unsigned int)w); 5262306a36Sopenharmony_ci#elif BITS_PER_LONG == 64 5362306a36Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER 5462306a36Sopenharmony_ci w -= (w >> 1) & 0x5555555555555555ul; 5562306a36Sopenharmony_ci w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul); 5662306a36Sopenharmony_ci w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful; 5762306a36Sopenharmony_ci return (w * 0x0101010101010101ul) >> 56; 5862306a36Sopenharmony_ci#else 5962306a36Sopenharmony_ci __u64 res = w - ((w >> 1) & 0x5555555555555555ul); 6062306a36Sopenharmony_ci res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul); 6162306a36Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful; 6262306a36Sopenharmony_ci res = res + (res >> 8); 6362306a36Sopenharmony_ci res = res + (res >> 16); 6462306a36Sopenharmony_ci return (res + (res >> 32)) & 0x00000000000000FFul; 6562306a36Sopenharmony_ci#endif 6662306a36Sopenharmony_ci#endif 6762306a36Sopenharmony_ci} 6862306a36Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight64); 69