bit_count.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. * History
  19. * -------
  20. * 2010-04-26 Initial version (Miklos)
  21. */
  22. /* Implements the bit counting function:
  23. * int bit_count(unsigned int u)
  24. * Returns the number of bits in u.
  25. */
  26. #ifndef _BIT_COUNT_H
  27. #define _BIT_COUNT_H
  28. /* fix __CPU_i386 -> __CPU_x86 */
  29. #if defined __CPU_i386 && ! defined __CPU_x86
  30. #define __CPU_x86
  31. #endif
  32. #ifdef CC_GCC_LIKE_ASM
  33. #if defined __CPU_x86 || defined __CPU_x86_64
  34. #ifdef __SSE4_2__
  35. /* popcnt requires SSE4.2 support,
  36. * see http://en.wikipedia.org/wiki/SSE4 */
  37. #define BIT_COUNT_ASM
  38. #endif
  39. #endif
  40. #endif
  41. #ifdef BIT_COUNT_ASM
  42. /* Returns the number of 1 bits in u. */
  43. static inline int bit_count(unsigned int u)
  44. {
  45. int v;
  46. asm volatile(" popcnt %1, %0 " : "=r" (v) : "rm" (u));
  47. return v;
  48. }
  49. #else /* BIT_COUNT_ASM */
  50. /* Returns the number of 1 bits in u.
  51. * source: http://en.wikipedia.org/wiki/Hamming_weight
  52. */
  53. #if 0
  54. static inline int bit_count(unsigned int u)
  55. {
  56. int count;
  57. /* It is likely to have only few
  58. * bits set to 1, so there will be only
  59. * few iterations */
  60. for (count=0; u; count++)
  61. u &= u-1;
  62. return count;
  63. }
  64. #endif
  65. static inline int bit_count(unsigned int i)
  66. {
  67. i = i - ((i >> 1) & 0x55555555);
  68. i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
  69. return (((i + (i >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
  70. }
  71. #if 0
  72. /* number of bits in a byte.
  73. * (Only slightly faster then the above version,
  74. * It is not worth the extra memory usage.)
  75. */
  76. extern int bits_in_char[256];
  77. static inline int bit_count(unsigned int u)
  78. {
  79. return bits_in_char [u & 0xffu]
  80. + bits_in_char [(u >> 8 ) & 0xffu]
  81. + bits_in_char [(u >> 16) & 0xffu]
  82. + bits_in_char [(u >> 24) & 0xffu];
  83. }
  84. #endif
  85. #endif /* BIT_COUNT_ASM */
  86. #endif /* _BIT_COUNT_H */