dynamic_bitset.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // -----------------------------------------------------------
  2. //
  3. // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
  4. // Copyright (c) 2003-2006, 2008 Gennaro Prota
  5. // Copyright (c) 2014 Glen Joseph Fernandes
  6. // (glenjofe@gmail.com)
  7. // Copyright (c) 2018 Evgeny Shulgin
  8. // Copyright (c) 2019 Andrey Semashev
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. // (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. // -----------------------------------------------------------
  15. #ifndef BOOST_DETAIL_DYNAMIC_BITSET_HPP
  16. #define BOOST_DETAIL_DYNAMIC_BITSET_HPP
  17. #include <memory>
  18. #include <cstddef>
  19. #include "boost/config.hpp"
  20. #include "boost/detail/workaround.hpp"
  21. #if ((defined(BOOST_MSVC) && (BOOST_MSVC >= 1600)) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
  22. #include <intrin.h>
  23. #endif
  24. namespace boost {
  25. namespace detail {
  26. namespace dynamic_bitset_impl {
  27. template<class T>
  28. struct max_limit {
  29. BOOST_STATIC_CONSTEXPR T value = static_cast<T>(-1);
  30. };
  31. template<class T>
  32. BOOST_CONSTEXPR_OR_CONST T max_limit<T>::value;
  33. // Gives (read-)access to the object representation
  34. // of an object of type T (3.9p4). CANNOT be used
  35. // on a base sub-object
  36. //
  37. template <typename T>
  38. inline const unsigned char * object_representation (T* p)
  39. {
  40. return static_cast<const unsigned char *>(static_cast<const void *>(p));
  41. }
  42. template<typename T, int amount, int width /* = default */>
  43. struct shifter
  44. {
  45. static void left_shift(T & v) {
  46. amount >= width ? (v = 0)
  47. : (v >>= BOOST_DYNAMIC_BITSET_WRAP_CONSTANT(amount));
  48. }
  49. };
  50. // ------- count function implementation --------------
  51. typedef unsigned char byte_type;
  52. // These two entities
  53. //
  54. // enum mode { access_by_bytes, access_by_blocks };
  55. // template <mode> struct mode_to_type {};
  56. //
  57. // were removed, since the regression logs (as of 24 Aug 2008)
  58. // showed that several compilers had troubles with recognizing
  59. //
  60. // const mode m = access_by_bytes
  61. //
  62. // as a constant expression
  63. //
  64. // * So, we'll use bool, instead of enum *.
  65. //
  66. template <bool value>
  67. struct value_to_type
  68. {
  69. value_to_type() {}
  70. };
  71. const bool access_by_bytes = true;
  72. const bool access_by_blocks = false;
  73. // the table: wrapped in a class template, so
  74. // that it is only instantiated if/when needed
  75. //
  76. template <bool dummy_name = true>
  77. struct count_table { static const byte_type table[]; };
  78. template <>
  79. struct count_table<false> { /* no table */ };
  80. const unsigned int table_width = 8;
  81. template <bool b>
  82. const byte_type count_table<b>::table[] =
  83. {
  84. // Automatically generated by GPTableGen.exe v.1.0
  85. //
  86. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  87. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  88. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  89. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  90. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  91. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  92. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  93. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  94. };
  95. // Some platforms have fast popcount operation, that allow us to implement
  96. // counting bits much more efficiently
  97. //
  98. template <typename ValueType>
  99. BOOST_FORCEINLINE std::size_t popcount(ValueType value) BOOST_NOEXCEPT
  100. {
  101. std::size_t num = 0u;
  102. while (value) {
  103. num += count_table<>::table[value & ((1u<<table_width) - 1)];
  104. value >>= table_width;
  105. }
  106. return num;
  107. }
  108. #if (((defined(BOOST_MSVC) && (BOOST_MSVC >= 1600)) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))) \
  109. && (defined(__POPCNT__) || defined(__AVX__))
  110. template <>
  111. BOOST_FORCEINLINE std::size_t popcount<unsigned short>(unsigned short value) BOOST_NOEXCEPT
  112. {
  113. return static_cast<std::size_t>(__popcnt16(value));
  114. }
  115. template <>
  116. BOOST_FORCEINLINE std::size_t popcount<unsigned int>(unsigned int value) BOOST_NOEXCEPT
  117. {
  118. return static_cast<std::size_t>(__popcnt(value));
  119. }
  120. template <>
  121. BOOST_FORCEINLINE std::size_t popcount<unsigned __int64>(unsigned __int64 value) BOOST_NOEXCEPT
  122. {
  123. #if defined(_M_X64)
  124. return static_cast<std::size_t>(__popcnt64(value));
  125. #else
  126. return static_cast<std::size_t>(__popcnt(static_cast< unsigned int >(value))) + static_cast<std::size_t>(__popcnt(static_cast< unsigned int >(value >> 32)));
  127. #endif
  128. }
  129. #elif defined(BOOST_GCC) || defined(__clang__) || (defined(BOOST_INTEL) && defined(__GNUC__))
  130. // Note: gcc builtins are implemented by compiler runtime when the target CPU may not support the necessary instructions
  131. template <>
  132. BOOST_FORCEINLINE std::size_t popcount<unsigned short>(unsigned short value) BOOST_NOEXCEPT
  133. {
  134. return static_cast<unsigned int>(__builtin_popcount(static_cast<unsigned int>(value)));
  135. }
  136. template <>
  137. BOOST_FORCEINLINE std::size_t popcount<unsigned int>(unsigned int value) BOOST_NOEXCEPT
  138. {
  139. return static_cast<unsigned int>(__builtin_popcount(value));
  140. }
  141. template <>
  142. BOOST_FORCEINLINE std::size_t popcount<unsigned long>(unsigned long value) BOOST_NOEXCEPT
  143. {
  144. return static_cast<unsigned int>(__builtin_popcountl(value));
  145. }
  146. template <>
  147. BOOST_FORCEINLINE std::size_t popcount<boost::ulong_long_type>(boost::ulong_long_type value) BOOST_NOEXCEPT
  148. {
  149. return static_cast<unsigned int>(__builtin_popcountll(value));
  150. }
  151. #endif
  152. // overload for access by blocks
  153. //
  154. template <typename Iterator, typename ValueType>
  155. inline std::size_t do_count(Iterator first, std::size_t length, ValueType,
  156. value_to_type<access_by_blocks>*)
  157. {
  158. std::size_t num1 = 0u, num2 = 0u;
  159. while (length >= 2u) {
  160. num1 += popcount<ValueType>(*first);
  161. ++first;
  162. num2 += popcount<ValueType>(*first);
  163. ++first;
  164. length -= 2u;
  165. }
  166. if (length > 0u)
  167. num1 += popcount<ValueType>(*first);
  168. return num1 + num2;
  169. }
  170. // overload for access by bytes
  171. //
  172. template <typename Iterator>
  173. inline std::size_t do_count(Iterator first, std::size_t length,
  174. int /*dummy param*/,
  175. value_to_type<access_by_bytes>*)
  176. {
  177. if (length > 0u) {
  178. const byte_type* p = object_representation(&*first);
  179. length *= sizeof(*first);
  180. return do_count(p, length, static_cast<byte_type>(0u),
  181. static_cast< value_to_type<access_by_blocks>* >(0));
  182. }
  183. return 0u;
  184. }
  185. // -------------------------------------------------------
  186. // Some library implementations simply return a dummy
  187. // value such as
  188. //
  189. // size_type(-1) / sizeof(T)
  190. //
  191. // from vector<>::max_size. This tries to get more
  192. // meaningful info.
  193. //
  194. template <typename T>
  195. inline typename T::size_type vector_max_size_workaround(const T & v)
  196. BOOST_NOEXCEPT
  197. {
  198. typedef typename T::allocator_type allocator_type;
  199. const allocator_type& alloc = v.get_allocator();
  200. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  201. typedef std::allocator_traits<allocator_type> allocator_traits;
  202. const typename allocator_traits::size_type alloc_max =
  203. allocator_traits::max_size(alloc);
  204. #else
  205. const typename allocator_type::size_type alloc_max = alloc.max_size();
  206. #endif
  207. const typename T::size_type container_max = v.max_size();
  208. return alloc_max < container_max ? alloc_max : container_max;
  209. }
  210. // for static_asserts
  211. template <typename T>
  212. struct allowed_block_type {
  213. enum { value = T(-1) > 0 }; // ensure T has no sign
  214. };
  215. template <>
  216. struct allowed_block_type<bool> {
  217. enum { value = false };
  218. };
  219. template <typename T>
  220. struct is_numeric {
  221. enum { value = false };
  222. };
  223. # define BOOST_dynamic_bitset_is_numeric(x) \
  224. template<> \
  225. struct is_numeric< x > { \
  226. enum { value = true }; \
  227. } /**/
  228. BOOST_dynamic_bitset_is_numeric(bool);
  229. BOOST_dynamic_bitset_is_numeric(char);
  230. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  231. BOOST_dynamic_bitset_is_numeric(wchar_t);
  232. #endif
  233. BOOST_dynamic_bitset_is_numeric(signed char);
  234. BOOST_dynamic_bitset_is_numeric(short int);
  235. BOOST_dynamic_bitset_is_numeric(int);
  236. BOOST_dynamic_bitset_is_numeric(long int);
  237. BOOST_dynamic_bitset_is_numeric(unsigned char);
  238. BOOST_dynamic_bitset_is_numeric(unsigned short);
  239. BOOST_dynamic_bitset_is_numeric(unsigned int);
  240. BOOST_dynamic_bitset_is_numeric(unsigned long);
  241. #if defined(BOOST_HAS_LONG_LONG)
  242. BOOST_dynamic_bitset_is_numeric(::boost::long_long_type);
  243. BOOST_dynamic_bitset_is_numeric(::boost::ulong_long_type);
  244. #endif
  245. // intentionally omitted
  246. //BOOST_dynamic_bitset_is_numeric(float);
  247. //BOOST_dynamic_bitset_is_numeric(double);
  248. //BOOST_dynamic_bitset_is_numeric(long double);
  249. #undef BOOST_dynamic_bitset_is_numeric
  250. } // dynamic_bitset_impl
  251. } // namespace detail
  252. } // namespace boost
  253. #endif // include guard