albit.h 5.2 KB

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