18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_HWEIGHT_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_HWEIGHT_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/cpufeatures.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
88c2ecf20Sopenharmony_ci#define REG_IN "D"
98c2ecf20Sopenharmony_ci#define REG_OUT "a"
108c2ecf20Sopenharmony_ci#else
118c2ecf20Sopenharmony_ci#define REG_IN "a"
128c2ecf20Sopenharmony_ci#define REG_OUT "a"
138c2ecf20Sopenharmony_ci#endif
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic __always_inline unsigned int __arch_hweight32(unsigned int w)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	unsigned int res;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
208c2ecf20Sopenharmony_ci			 : "="REG_OUT (res)
218c2ecf20Sopenharmony_ci			 : REG_IN (w));
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	return res;
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic inline unsigned int __arch_hweight16(unsigned int w)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	return __arch_hweight32(w & 0xffff);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic inline unsigned int __arch_hweight8(unsigned int w)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	return __arch_hweight32(w & 0xff);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
378c2ecf20Sopenharmony_cistatic inline unsigned long __arch_hweight64(__u64 w)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	return  __arch_hweight32((u32)w) +
408c2ecf20Sopenharmony_ci		__arch_hweight32((u32)(w >> 32));
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci#else
438c2ecf20Sopenharmony_cistatic __always_inline unsigned long __arch_hweight64(__u64 w)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	unsigned long res;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
488c2ecf20Sopenharmony_ci			 : "="REG_OUT (res)
498c2ecf20Sopenharmony_ci			 : REG_IN (w));
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return res;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci#endif /* CONFIG_X86_32 */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#endif
56