arch_hweight.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_HWEIGHT_H
  3. #define _ASM_X86_HWEIGHT_H
  4. #include <asm/cpufeatures.h>
  5. #ifdef CONFIG_64BIT
  6. #define REG_IN "D"
  7. #define REG_OUT "a"
  8. #else
  9. #define REG_IN "a"
  10. #define REG_OUT "a"
  11. #endif
  12. static __always_inline unsigned int __arch_hweight32(unsigned int w)
  13. {
  14. unsigned int res;
  15. asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
  16. : "="REG_OUT (res)
  17. : REG_IN (w));
  18. return res;
  19. }
  20. static inline unsigned int __arch_hweight16(unsigned int w)
  21. {
  22. return __arch_hweight32(w & 0xffff);
  23. }
  24. static inline unsigned int __arch_hweight8(unsigned int w)
  25. {
  26. return __arch_hweight32(w & 0xff);
  27. }
  28. #ifdef CONFIG_X86_32
  29. static inline unsigned long __arch_hweight64(__u64 w)
  30. {
  31. return __arch_hweight32((u32)w) +
  32. __arch_hweight32((u32)(w >> 32));
  33. }
  34. #else
  35. static __always_inline unsigned long __arch_hweight64(__u64 w)
  36. {
  37. unsigned long res;
  38. asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
  39. : "="REG_OUT (res)
  40. : REG_IN (w));
  41. return res;
  42. }
  43. #endif /* CONFIG_X86_32 */
  44. #endif