albit.h 5.5 KB

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