dynamic_bitset.hpp 9.7 KB

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