hweight.S 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/linkage.h>
  3. #include <asm/export.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. SYM_FUNC_START(__sw_hweight64)
  34. #ifdef CONFIG_X86_64
  35. pushq %rdi
  36. pushq %rdx
  37. movq %rdi, %rdx # w -> t
  38. movabsq $0x5555555555555555, %rax
  39. shrq %rdx # t >>= 1
  40. andq %rdx, %rax # t &= 0x5555555555555555
  41. movabsq $0x3333333333333333, %rdx
  42. subq %rax, %rdi # w -= t
  43. movq %rdi, %rax # w -> t
  44. shrq $2, %rdi # w_tmp >>= 2
  45. andq %rdx, %rax # t &= 0x3333333333333333
  46. andq %rdi, %rdx # w_tmp &= 0x3333333333333333
  47. addq %rdx, %rax # w = w_tmp + t
  48. movq %rax, %rdx # w -> t
  49. shrq $4, %rdx # t >>= 4
  50. addq %rdx, %rax # w_tmp += t
  51. movabsq $0x0f0f0f0f0f0f0f0f, %rdx
  52. andq %rdx, %rax # w_tmp &= 0x0f0f0f0f0f0f0f0f
  53. movabsq $0x0101010101010101, %rdx
  54. imulq %rdx, %rax # w_tmp *= 0x0101010101010101
  55. shrq $56, %rax # w = w_tmp >> 56
  56. popq %rdx
  57. popq %rdi
  58. RET
  59. #else /* CONFIG_X86_32 */
  60. /* We're getting an u64 arg in (%eax,%edx): unsigned long hweight64(__u64 w) */
  61. pushl %ecx
  62. call __sw_hweight32
  63. movl %eax, %ecx # stash away result
  64. movl %edx, %eax # second part of input
  65. call __sw_hweight32
  66. addl %ecx, %eax # result
  67. popl %ecx
  68. RET
  69. #endif
  70. SYM_FUNC_END(__sw_hweight64)
  71. EXPORT_SYMBOL(__sw_hweight64)