hweight.S 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/export.h>
  3. #include <linux/linkage.h>
  4. #include <asm/asm.h>
  5. /*
  6. * unsigned int __sw_hweight32(unsigned int w)
  7. * %rdi: w
  8. */
  9. SYM_FUNC_START(__sw_hweight32)
  10. #ifdef CONFIG_X86_64
  11. movl %edi, %eax # w
  12. #endif
  13. __ASM_SIZE(push,) %__ASM_REG(dx)
  14. movl %eax, %edx # w -> t
  15. shrl %edx # t >>= 1
  16. andl $0x55555555, %edx # t &= 0x55555555
  17. subl %edx, %eax # w -= t
  18. movl %eax, %edx # w -> t
  19. shrl $2, %eax # w_tmp >>= 2
  20. andl $0x33333333, %edx # t &= 0x33333333
  21. andl $0x33333333, %eax # w_tmp &= 0x33333333
  22. addl %edx, %eax # w = w_tmp + t
  23. movl %eax, %edx # w -> t
  24. shrl $4, %edx # t >>= 4
  25. addl %edx, %eax # w_tmp += t
  26. andl $0x0f0f0f0f, %eax # w_tmp &= 0x0f0f0f0f
  27. imull $0x01010101, %eax, %eax # w_tmp *= 0x01010101
  28. shrl $24, %eax # w = w_tmp >> 24
  29. __ASM_SIZE(pop,) %__ASM_REG(dx)
  30. RET
  31. SYM_FUNC_END(__sw_hweight32)
  32. EXPORT_SYMBOL(__sw_hweight32)
  33. /*
  34. * No 32-bit variant, because it's implemented as an inline wrapper
  35. * on top of __arch_hweight32():
  36. */
  37. #ifdef CONFIG_X86_64
  38. SYM_FUNC_START(__sw_hweight64)
  39. pushq %rdi
  40. pushq %rdx
  41. movq %rdi, %rdx # w -> t
  42. movabsq $0x5555555555555555, %rax
  43. shrq %rdx # t >>= 1
  44. andq %rdx, %rax # t &= 0x5555555555555555
  45. movabsq $0x3333333333333333, %rdx
  46. subq %rax, %rdi # w -= t
  47. movq %rdi, %rax # w -> t
  48. shrq $2, %rdi # w_tmp >>= 2
  49. andq %rdx, %rax # t &= 0x3333333333333333
  50. andq %rdi, %rdx # w_tmp &= 0x3333333333333333
  51. addq %rdx, %rax # w = w_tmp + t
  52. movq %rax, %rdx # w -> t
  53. shrq $4, %rdx # t >>= 4
  54. addq %rdx, %rax # w_tmp += t
  55. movabsq $0x0f0f0f0f0f0f0f0f, %rdx
  56. andq %rdx, %rax # w_tmp &= 0x0f0f0f0f0f0f0f0f
  57. movabsq $0x0101010101010101, %rdx
  58. imulq %rdx, %rax # w_tmp *= 0x0101010101010101
  59. shrq $56, %rax # w = w_tmp >> 56
  60. popq %rdx
  61. popq %rdi
  62. RET
  63. SYM_FUNC_END(__sw_hweight64)
  64. EXPORT_SYMBOL(__sw_hweight64)
  65. #endif