Path: blob/master/arch/riscv/include/asm/arch_hweight.h
26471 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Based on arch/x86/include/asm/arch_hweight.h3*/45#ifndef _ASM_RISCV_HWEIGHT_H6#define _ASM_RISCV_HWEIGHT_H78#include <asm/alternative-macros.h>9#include <asm/hwcap.h>1011#if (BITS_PER_LONG == 64)12#define CPOPW "cpopw "13#elif (BITS_PER_LONG == 32)14#define CPOPW "cpop "15#else16#error "Unexpected BITS_PER_LONG"17#endif1819static __always_inline unsigned int __arch_hweight32(unsigned int w)20{21#if defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB)22asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,23RISCV_ISA_EXT_ZBB, 1)24: : : : legacy);2526asm (".option push\n"27".option arch,+zbb\n"28CPOPW "%0, %1\n"29".option pop\n"30: "=r" (w) : "r" (w) :);3132return w;3334legacy:35#endif36return __sw_hweight32(w);37}3839static inline unsigned int __arch_hweight16(unsigned int w)40{41return __arch_hweight32(w & 0xffff);42}4344static inline unsigned int __arch_hweight8(unsigned int w)45{46return __arch_hweight32(w & 0xff);47}4849#if BITS_PER_LONG == 6450static __always_inline unsigned long __arch_hweight64(__u64 w)51{52#if defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB)53asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,54RISCV_ISA_EXT_ZBB, 1)55: : : : legacy);5657asm (".option push\n"58".option arch,+zbb\n"59"cpop %0, %1\n"60".option pop\n"61: "=r" (w) : "r" (w) :);6263return w;6465legacy:66#endif67return __sw_hweight64(w);68}69#else /* BITS_PER_LONG == 64 */70static inline unsigned long __arch_hweight64(__u64 w)71{72return __arch_hweight32((u32)w) +73__arch_hweight32((u32)(w >> 32));74}75#endif /* !(BITS_PER_LONG == 64) */7677#endif /* _ASM_RISCV_HWEIGHT_H */787980