18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/export.h> 38c2ecf20Sopenharmony_ci#include <linux/bitops.h> 48c2ecf20Sopenharmony_ci#include <asm/types.h> 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci/** 78c2ecf20Sopenharmony_ci * hweightN - returns the hamming weight of a N-bit word 88c2ecf20Sopenharmony_ci * @x: the word to weigh 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * The Hamming Weight of a number is the total number of bits set in it. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciunsigned int __sw_hweight32(unsigned int w) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER 168c2ecf20Sopenharmony_ci w -= (w >> 1) & 0x55555555; 178c2ecf20Sopenharmony_ci w = (w & 0x33333333) + ((w >> 2) & 0x33333333); 188c2ecf20Sopenharmony_ci w = (w + (w >> 4)) & 0x0f0f0f0f; 198c2ecf20Sopenharmony_ci return (w * 0x01010101) >> 24; 208c2ecf20Sopenharmony_ci#else 218c2ecf20Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x55555555); 228c2ecf20Sopenharmony_ci res = (res & 0x33333333) + ((res >> 2) & 0x33333333); 238c2ecf20Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F0F0F; 248c2ecf20Sopenharmony_ci res = res + (res >> 8); 258c2ecf20Sopenharmony_ci return (res + (res >> 16)) & 0x000000FF; 268c2ecf20Sopenharmony_ci#endif 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight32); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciunsigned int __sw_hweight16(unsigned int w) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x5555); 338c2ecf20Sopenharmony_ci res = (res & 0x3333) + ((res >> 2) & 0x3333); 348c2ecf20Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F; 358c2ecf20Sopenharmony_ci return (res + (res >> 8)) & 0x00FF; 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight16); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciunsigned int __sw_hweight8(unsigned int w) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci unsigned int res = w - ((w >> 1) & 0x55); 428c2ecf20Sopenharmony_ci res = (res & 0x33) + ((res >> 2) & 0x33); 438c2ecf20Sopenharmony_ci return (res + (res >> 4)) & 0x0F; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight8); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciunsigned long __sw_hweight64(__u64 w) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 32 508c2ecf20Sopenharmony_ci return __sw_hweight32((unsigned int)(w >> 32)) + 518c2ecf20Sopenharmony_ci __sw_hweight32((unsigned int)w); 528c2ecf20Sopenharmony_ci#elif BITS_PER_LONG == 64 538c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER 548c2ecf20Sopenharmony_ci w -= (w >> 1) & 0x5555555555555555ul; 558c2ecf20Sopenharmony_ci w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul); 568c2ecf20Sopenharmony_ci w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful; 578c2ecf20Sopenharmony_ci return (w * 0x0101010101010101ul) >> 56; 588c2ecf20Sopenharmony_ci#else 598c2ecf20Sopenharmony_ci __u64 res = w - ((w >> 1) & 0x5555555555555555ul); 608c2ecf20Sopenharmony_ci res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul); 618c2ecf20Sopenharmony_ci res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful; 628c2ecf20Sopenharmony_ci res = res + (res >> 8); 638c2ecf20Sopenharmony_ci res = res + (res >> 16); 648c2ecf20Sopenharmony_ci return (res + (res >> 32)) & 0x00000000000000FFul; 658c2ecf20Sopenharmony_ci#endif 668c2ecf20Sopenharmony_ci#endif 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__sw_hweight64); 69