dynamic_bitset.hpp 9.7 KB

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