albit.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef AL_BIT_H
  2. #define AL_BIT_H
  3. #include <limits>
  4. #include <type_traits>
  5. #if !defined(__GNUC__) && (defined(_WIN32) || defined(_WIN64))
  6. #include <intrin.h>
  7. #include "opthelpers.h"
  8. #endif
  9. namespace al {
  10. #ifdef __BYTE_ORDER__
  11. enum class endian {
  12. little = __ORDER_LITTLE_ENDIAN__,
  13. big = __ORDER_BIG_ENDIAN__,
  14. native = __BYTE_ORDER__
  15. };
  16. #else
  17. /* This doesn't support mixed-endian. */
  18. namespace detail_ {
  19. constexpr inline bool EndianTest() noexcept
  20. {
  21. static_assert(sizeof(char) < sizeof(int), "char is too big");
  22. constexpr int test_val{1};
  23. return static_cast<const char&>(test_val);
  24. }
  25. } // namespace detail_
  26. enum class endian {
  27. little = 0,
  28. big = 1,
  29. native = detail_::EndianTest() ? little : big
  30. };
  31. #endif
  32. /* Define popcount (population count/count 1 bits) and countr_zero (count
  33. * trailing zero bits, starting from the lsb) methods, for various integer
  34. * types.
  35. */
  36. #ifdef __GNUC__
  37. namespace detail_ {
  38. inline int popcount(unsigned long long val) noexcept { return __builtin_popcountll(val); }
  39. inline int popcount(unsigned long val) noexcept { return __builtin_popcountl(val); }
  40. inline int popcount(unsigned int val) noexcept { return __builtin_popcount(val); }
  41. inline int countr_zero(unsigned long long val) noexcept { return __builtin_ctzll(val); }
  42. inline int countr_zero(unsigned long val) noexcept { return __builtin_ctzl(val); }
  43. inline int countr_zero(unsigned int val) noexcept { return __builtin_ctz(val); }
  44. } // namespace detail_
  45. template<typename T>
  46. inline std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  47. int> popcount(T v) noexcept { return detail_::popcount(v); }
  48. template<typename T>
  49. inline std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  50. int> countr_zero(T val) noexcept
  51. { return val ? detail_::countr_zero(val) : std::numeric_limits<T>::digits; }
  52. #else
  53. /* There be black magics here. The popcount method is derived from
  54. * https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  55. * while the ctz-utilizing-popcount algorithm is shown here
  56. * http://www.hackersdelight.org/hdcodetxt/ntz.c.txt
  57. * as the ntz2 variant. These likely aren't the most efficient methods, but
  58. * they're good enough if the GCC built-ins aren't available.
  59. */
  60. namespace detail_ {
  61. template<typename T>
  62. constexpr T repbits(unsigned char bits) noexcept
  63. {
  64. T ret{bits};
  65. for(size_t i{1};i < sizeof(T);++i)
  66. ret = (ret<<8) | bits;
  67. return ret;
  68. }
  69. } // namespace detail_
  70. template<typename T>
  71. constexpr std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  72. int> popcount(T v) noexcept
  73. {
  74. constexpr T m55{detail_::repbits<T>(0x55)};
  75. constexpr T m33{detail_::repbits<T>(0x33)};
  76. constexpr T m0f{detail_::repbits<T>(0x0f)};
  77. constexpr T m01{detail_::repbits<T>(0x01)};
  78. v = v - ((v >> 1) & m55);
  79. v = (v & m33) + ((v >> 2) & m33);
  80. v = (v + (v >> 4)) & m0f;
  81. return static_cast<int>((v * m01) >> ((sizeof(T)-1)*8));
  82. }
  83. #if defined(_WIN64)
  84. template<typename T>
  85. inline std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  86. int> countr_zero(T v)
  87. {
  88. unsigned long idx{std::numeric_limits<T>::digits};
  89. if_constexpr(std::numeric_limits<T>::digits <= 32)
  90. _BitScanForward(&idx, static_cast<uint32_t>(v));
  91. else // std::numeric_limits<T>::digits > 32
  92. _BitScanForward64(&idx, v);
  93. return static_cast<int>(idx);
  94. }
  95. #elif defined(_WIN32)
  96. template<typename T>
  97. inline std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  98. int> countr_zero(T v)
  99. {
  100. unsigned long idx{std::numeric_limits<T>::digits};
  101. if_constexpr(std::numeric_limits<T>::digits <= 32)
  102. _BitScanForward(&idx, static_cast<uint32_t>(v));
  103. else if(!_BitScanForward(&idx, static_cast<uint32_t>(v)))
  104. {
  105. if(_BitScanForward(&idx, static_cast<uint32_t>(v>>32)))
  106. idx += 32;
  107. }
  108. return static_cast<int>(idx);
  109. }
  110. #else
  111. template<typename T>
  112. constexpr std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
  113. int> countr_zero(T value)
  114. { return popcount(static_cast<T>(~value & (value - 1))); }
  115. #endif
  116. #endif
  117. } // namespace al
  118. #endif /* AL_BIT_H */