hweight.S 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <linux/linkage.h>
  2. #include <asm/asm.h>
  3. /*
  4. * unsigned int __sw_hweight32(unsigned int w)
  5. * %rdi: w
  6. */
  7. ENTRY(__sw_hweight32)
  8. #ifdef CONFIG_X86_64
  9. movl %edi, %eax # w
  10. #endif
  11. __ASM_SIZE(push,) %__ASM_REG(dx)
  12. movl %eax, %edx # w -> t
  13. shrl %edx # t >>= 1
  14. andl $0x55555555, %edx # t &= 0x55555555
  15. subl %edx, %eax # w -= t
  16. movl %eax, %edx # w -> t
  17. shrl $2, %eax # w_tmp >>= 2
  18. andl $0x33333333, %edx # t &= 0x33333333
  19. andl $0x33333333, %eax # w_tmp &= 0x33333333
  20. addl %edx, %eax # w = w_tmp + t
  21. movl %eax, %edx # w -> t
  22. shrl $4, %edx # t >>= 4
  23. addl %edx, %eax # w_tmp += t
  24. andl $0x0f0f0f0f, %eax # w_tmp &= 0x0f0f0f0f
  25. imull $0x01010101, %eax, %eax # w_tmp *= 0x01010101
  26. shrl $24, %eax # w = w_tmp >> 24
  27. __ASM_SIZE(pop,) %__ASM_REG(dx)
  28. ret
  29. ENDPROC(__sw_hweight32)
  30. ENTRY(__sw_hweight64)
  31. #ifdef CONFIG_X86_64
  32. pushq %rdi
  33. pushq %rdx
  34. movq %rdi, %rdx # w -> t
  35. movabsq $0x5555555555555555, %rax
  36. shrq %rdx # t >>= 1
  37. andq %rdx, %rax # t &= 0x5555555555555555
  38. movabsq $0x3333333333333333, %rdx
  39. subq %rax, %rdi # w -= t
  40. movq %rdi, %rax # w -> t
  41. shrq $2, %rdi # w_tmp >>= 2
  42. andq %rdx, %rax # t &= 0x3333333333333333
  43. andq %rdi, %rdx # w_tmp &= 0x3333333333333333
  44. addq %rdx, %rax # w = w_tmp + t
  45. movq %rax, %rdx # w -> t
  46. shrq $4, %rdx # t >>= 4
  47. addq %rdx, %rax # w_tmp += t
  48. movabsq $0x0f0f0f0f0f0f0f0f, %rdx
  49. andq %rdx, %rax # w_tmp &= 0x0f0f0f0f0f0f0f0f
  50. movabsq $0x0101010101010101, %rdx
  51. imulq %rdx, %rax # w_tmp *= 0x0101010101010101
  52. shrq $56, %rax # w = w_tmp >> 56
  53. popq %rdx
  54. popq %rdi
  55. ret
  56. #else /* CONFIG_X86_32 */
  57. /* We're getting an u64 arg in (%eax,%edx): unsigned long hweight64(__u64 w) */
  58. pushl %ecx
  59. call __sw_hweight32
  60. movl %eax, %ecx # stash away result
  61. movl %edx, %eax # second part of input
  62. call __sw_hweight32
  63. addl %ecx, %eax # result
  64. popl %ecx
  65. ret
  66. #endif
  67. ENDPROC(__sw_hweight64)