bit.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #ifndef BOOST_CORE_BIT_HPP_INCLUDED
  2. #define BOOST_CORE_BIT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // boost/core/bit.hpp
  8. //
  9. // A portable version of the C++20 standard header <bit>
  10. //
  11. // Copyright 2020 Peter Dimov
  12. // Distributed under the Boost Software License, Version 1.0.
  13. // https://www.boost.org/LICENSE_1_0.txt
  14. #include <boost/config.hpp>
  15. #include <boost/static_assert.hpp>
  16. #include <boost/cstdint.hpp>
  17. #include <limits>
  18. #include <cstring>
  19. #if defined(_MSC_VER)
  20. # include <intrin.h>
  21. # pragma intrinsic(_BitScanForward)
  22. # pragma intrinsic(_BitScanReverse)
  23. # if defined(_M_X64)
  24. # pragma intrinsic(_BitScanForward64)
  25. # pragma intrinsic(_BitScanReverse64)
  26. # endif
  27. #endif // defined(_MSC_VER)
  28. namespace boost
  29. {
  30. namespace core
  31. {
  32. // bit_cast
  33. template<class To, class From>
  34. To bit_cast( From const & from ) BOOST_NOEXCEPT
  35. {
  36. BOOST_STATIC_ASSERT( sizeof(To) == sizeof(From) );
  37. To to;
  38. std::memcpy( &to, &from, sizeof(To) );
  39. return to;
  40. }
  41. // countl
  42. #if defined(__GNUC__) || defined(__clang__)
  43. namespace detail
  44. {
  45. BOOST_CONSTEXPR inline int countl_impl( unsigned char x ) BOOST_NOEXCEPT
  46. {
  47. return x? __builtin_clz( x ) - ( std::numeric_limits<unsigned int>::digits - std::numeric_limits<unsigned char>::digits ): std::numeric_limits<unsigned char>::digits;
  48. }
  49. BOOST_CONSTEXPR inline int countl_impl( unsigned short x ) BOOST_NOEXCEPT
  50. {
  51. return x? __builtin_clz( x ) - ( std::numeric_limits<unsigned int>::digits - std::numeric_limits<unsigned short>::digits ): std::numeric_limits<unsigned short>::digits;
  52. }
  53. BOOST_CONSTEXPR inline int countl_impl( unsigned int x ) BOOST_NOEXCEPT
  54. {
  55. return x? __builtin_clz( x ): std::numeric_limits<unsigned int>::digits;
  56. }
  57. BOOST_CONSTEXPR inline int countl_impl( unsigned long x ) BOOST_NOEXCEPT
  58. {
  59. return x? __builtin_clzl( x ): std::numeric_limits<unsigned long>::digits;
  60. }
  61. BOOST_CONSTEXPR inline int countl_impl( unsigned long long x ) BOOST_NOEXCEPT
  62. {
  63. return x? __builtin_clzll( x ): std::numeric_limits<unsigned long long>::digits;
  64. }
  65. } // namespace detail
  66. template<class T>
  67. BOOST_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT
  68. {
  69. return boost::core::detail::countl_impl( x );
  70. }
  71. #else // defined(__GNUC__) || defined(__clang__)
  72. namespace detail
  73. {
  74. inline int countl_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  75. {
  76. #if defined(_MSC_VER)
  77. unsigned long r;
  78. if( _BitScanReverse( &r, x ) )
  79. {
  80. return 31 - static_cast<int>( r );
  81. }
  82. else
  83. {
  84. return 32;
  85. }
  86. #else
  87. static unsigned char const mod37[ 37 ] = { 32, 31, 6, 30, 9, 5, 0, 29, 16, 8, 2, 4, 21, 0, 19, 28, 25, 15, 0, 7, 10, 1, 17, 3, 22, 20, 26, 0, 11, 18, 23, 27, 12, 24, 13, 14, 0 };
  88. x |= x >> 1;
  89. x |= x >> 2;
  90. x |= x >> 4;
  91. x |= x >> 8;
  92. x |= x >> 16;
  93. return mod37[ x % 37 ];
  94. #endif
  95. }
  96. inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  97. {
  98. #if defined(_MSC_VER) && defined(_M_X64)
  99. unsigned long r;
  100. if( _BitScanReverse64( &r, x ) )
  101. {
  102. return 63 - static_cast<int>( r );
  103. }
  104. else
  105. {
  106. return 64;
  107. }
  108. #else
  109. return static_cast<boost::uint32_t>( x >> 32 ) != 0?
  110. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x >> 32 ) ):
  111. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) + 32;
  112. #endif
  113. }
  114. inline int countl_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  115. {
  116. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 24;
  117. }
  118. inline int countl_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  119. {
  120. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 16;
  121. }
  122. } // namespace detail
  123. template<class T>
  124. int countl_zero( T x ) BOOST_NOEXCEPT
  125. {
  126. BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) );
  127. if( sizeof(T) == sizeof(boost::uint8_t) )
  128. {
  129. return boost::core::detail::countl_impl( static_cast<boost::uint8_t>( x ) );
  130. }
  131. else if( sizeof(T) == sizeof(boost::uint16_t) )
  132. {
  133. return boost::core::detail::countl_impl( static_cast<boost::uint16_t>( x ) );
  134. }
  135. else if( sizeof(T) == sizeof(boost::uint32_t) )
  136. {
  137. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) );
  138. }
  139. else
  140. {
  141. return boost::core::detail::countl_impl( static_cast<boost::uint64_t>( x ) );
  142. }
  143. }
  144. #endif // defined(__GNUC__) || defined(__clang__)
  145. template<class T>
  146. BOOST_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT
  147. {
  148. return boost::core::countl_zero( static_cast<T>( ~x ) );
  149. }
  150. // countr
  151. #if defined(__GNUC__) || defined(__clang__)
  152. namespace detail
  153. {
  154. BOOST_CONSTEXPR inline int countr_impl( unsigned char x ) BOOST_NOEXCEPT
  155. {
  156. return x? __builtin_ctz( x ): std::numeric_limits<unsigned char>::digits;
  157. }
  158. BOOST_CONSTEXPR inline int countr_impl( unsigned short x ) BOOST_NOEXCEPT
  159. {
  160. return x? __builtin_ctz( x ): std::numeric_limits<unsigned short>::digits;
  161. }
  162. BOOST_CONSTEXPR inline int countr_impl( unsigned int x ) BOOST_NOEXCEPT
  163. {
  164. return x? __builtin_ctz( x ): std::numeric_limits<unsigned int>::digits;
  165. }
  166. BOOST_CONSTEXPR inline int countr_impl( unsigned long x ) BOOST_NOEXCEPT
  167. {
  168. return x? __builtin_ctzl( x ): std::numeric_limits<unsigned long>::digits;
  169. }
  170. BOOST_CONSTEXPR inline int countr_impl( unsigned long long x ) BOOST_NOEXCEPT
  171. {
  172. return x? __builtin_ctzll( x ): std::numeric_limits<unsigned long long>::digits;
  173. }
  174. } // namespace detail
  175. template<class T>
  176. BOOST_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT
  177. {
  178. return boost::core::detail::countr_impl( x );
  179. }
  180. #else // defined(__GNUC__) || defined(__clang__)
  181. namespace detail
  182. {
  183. inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  184. {
  185. #if defined(_MSC_VER)
  186. unsigned long r;
  187. if( _BitScanForward( &r, x ) )
  188. {
  189. return static_cast<int>( r );
  190. }
  191. else
  192. {
  193. return 32;
  194. }
  195. #else
  196. static unsigned char const mod37[ 37 ] = { 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 };
  197. return mod37[ ( -(boost::int32_t)x & x ) % 37 ];
  198. #endif
  199. }
  200. inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  201. {
  202. #if defined(_MSC_VER) && defined(_M_X64)
  203. unsigned long r;
  204. if( _BitScanForward64( &r, x ) )
  205. {
  206. return static_cast<int>( r );
  207. }
  208. else
  209. {
  210. return 64;
  211. }
  212. #else
  213. return static_cast<boost::uint32_t>( x ) != 0?
  214. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) ):
  215. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x >> 32 ) ) + 32;
  216. #endif
  217. }
  218. inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  219. {
  220. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x100 );
  221. }
  222. inline int countr_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  223. {
  224. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x10000 );
  225. }
  226. } // namespace detail
  227. template<class T>
  228. int countr_zero( T x ) BOOST_NOEXCEPT
  229. {
  230. BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) );
  231. if( sizeof(T) == sizeof(boost::uint8_t) )
  232. {
  233. return boost::core::detail::countr_impl( static_cast<boost::uint8_t>( x ) );
  234. }
  235. else if( sizeof(T) == sizeof(boost::uint16_t) )
  236. {
  237. return boost::core::detail::countr_impl( static_cast<boost::uint16_t>( x ) );
  238. }
  239. else if( sizeof(T) == sizeof(boost::uint32_t) )
  240. {
  241. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) );
  242. }
  243. else
  244. {
  245. return boost::core::detail::countr_impl( static_cast<boost::uint64_t>( x ) );
  246. }
  247. }
  248. #endif // defined(__GNUC__) || defined(__clang__)
  249. template<class T>
  250. BOOST_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT
  251. {
  252. return boost::core::countr_zero( static_cast<T>( ~x ) );
  253. }
  254. // popcount
  255. #if defined(__GNUC__) || defined(__clang__)
  256. #if defined(__clang__) && __clang_major__ * 100 + __clang_minor__ < 304
  257. # define BOOST_CORE_POPCOUNT_CONSTEXPR
  258. #else
  259. # define BOOST_CORE_POPCOUNT_CONSTEXPR BOOST_CONSTEXPR
  260. #endif
  261. namespace detail
  262. {
  263. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned char x ) BOOST_NOEXCEPT
  264. {
  265. return __builtin_popcount( x );
  266. }
  267. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned short x ) BOOST_NOEXCEPT
  268. {
  269. return __builtin_popcount( x );
  270. }
  271. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned int x ) BOOST_NOEXCEPT
  272. {
  273. return __builtin_popcount( x );
  274. }
  275. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned long x ) BOOST_NOEXCEPT
  276. {
  277. return __builtin_popcountl( x );
  278. }
  279. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned long long x ) BOOST_NOEXCEPT
  280. {
  281. return __builtin_popcountll( x );
  282. }
  283. } // namespace detail
  284. #undef BOOST_CORE_POPCOUNT_CONSTEXPR
  285. template<class T>
  286. BOOST_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
  287. {
  288. return boost::core::detail::popcount_impl( x );
  289. }
  290. #else // defined(__GNUC__) || defined(__clang__)
  291. namespace detail
  292. {
  293. BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  294. {
  295. x = x - ( ( x >> 1 ) & 0x55555555 );
  296. x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
  297. x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F;
  298. return static_cast<unsigned>( ( x * 0x01010101 ) >> 24 );
  299. }
  300. BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  301. {
  302. x = x - ( ( x >> 1 ) & 0x5555555555555555 );
  303. x = ( x & 0x3333333333333333 ) + ( ( x >> 2 ) & 0x3333333333333333 );
  304. x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F0F0F0F0F;
  305. return static_cast<unsigned>( ( x * 0x0101010101010101 ) >> 56 );
  306. }
  307. } // namespace detail
  308. template<class T>
  309. BOOST_CXX14_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
  310. {
  311. BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) );
  312. if( sizeof(T) <= sizeof(boost::uint32_t) )
  313. {
  314. return boost::core::detail::popcount_impl( static_cast<boost::uint32_t>( x ) );
  315. }
  316. else
  317. {
  318. return boost::core::detail::popcount_impl( static_cast<boost::uint64_t>( x ) );
  319. }
  320. }
  321. #endif // defined(__GNUC__) || defined(__clang__)
  322. // rotating
  323. template<class T>
  324. BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT
  325. {
  326. unsigned const mask = std::numeric_limits<T>::digits - 1;
  327. return x << (s & mask) | x >> ((-s) & mask);
  328. }
  329. template<class T>
  330. BOOST_CXX14_CONSTEXPR T rotr( T x, int s ) BOOST_NOEXCEPT
  331. {
  332. unsigned const mask = std::numeric_limits<T>::digits - 1;
  333. return x >> (s & mask) | x << ((-s) & mask);
  334. }
  335. // integral powers of 2
  336. template<class T>
  337. BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT
  338. {
  339. return x != 0 && ( x & ( x - 1 ) ) == 0;
  340. }
  341. template<class T>
  342. BOOST_CONSTEXPR T bit_width( T x ) BOOST_NOEXCEPT
  343. {
  344. return std::numeric_limits<T>::digits - boost::core::countl_zero( x );
  345. }
  346. template<class T>
  347. BOOST_CONSTEXPR T bit_floor( T x ) BOOST_NOEXCEPT
  348. {
  349. return x == 0? 0: T(1) << ( boost::core::bit_width( x ) - 1 );
  350. }
  351. namespace detail
  352. {
  353. BOOST_CXX14_CONSTEXPR inline boost::uint32_t bit_ceil_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  354. {
  355. if( x == 0 )
  356. {
  357. return 0;
  358. }
  359. --x;
  360. x |= x >> 1;
  361. x |= x >> 2;
  362. x |= x >> 4;
  363. x |= x >> 8;
  364. x |= x >> 16;
  365. ++x;
  366. return x;
  367. }
  368. BOOST_CXX14_CONSTEXPR inline boost::uint64_t bit_ceil_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  369. {
  370. if( x == 0 )
  371. {
  372. return 0;
  373. }
  374. --x;
  375. x |= x >> 1;
  376. x |= x >> 2;
  377. x |= x >> 4;
  378. x |= x >> 8;
  379. x |= x >> 16;
  380. x |= x >> 32;
  381. ++x;
  382. return x;
  383. }
  384. } // namespace detail
  385. template<class T>
  386. BOOST_CXX14_CONSTEXPR T bit_ceil( T x ) BOOST_NOEXCEPT
  387. {
  388. BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) );
  389. if( sizeof(T) <= sizeof(boost::uint32_t) )
  390. {
  391. return static_cast<T>( boost::core::detail::bit_ceil_impl( static_cast<boost::uint32_t>( x ) ) );
  392. }
  393. else
  394. {
  395. return static_cast<T>( boost::core::detail::bit_ceil_impl( static_cast<boost::uint64_t>( x ) ) );
  396. }
  397. }
  398. // endian
  399. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  400. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  401. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  402. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =big
  403. #elif defined(__BYTE_ORDER__) && defined(__ORDER_PDP_ENDIAN__) && __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
  404. # define BOOST_CORE_BIT_NATIVE_INITIALIZER
  405. #elif defined(__LITTLE_ENDIAN__)
  406. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  407. #elif defined(__BIG_ENDIAN__)
  408. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =big
  409. #elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
  410. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  411. #else
  412. # define BOOST_CORE_BIT_NATIVE_INITIALIZER
  413. #endif
  414. #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  415. enum class endian
  416. {
  417. big,
  418. little,
  419. native BOOST_CORE_BIT_NATIVE_INITIALIZER
  420. };
  421. typedef endian endian_type;
  422. #else
  423. namespace endian
  424. {
  425. enum type
  426. {
  427. big,
  428. little,
  429. native BOOST_CORE_BIT_NATIVE_INITIALIZER
  430. };
  431. } // namespace endian
  432. typedef endian::type endian_type;
  433. #endif
  434. #undef BOOST_CORE_BIT_NATIVE_INITIALIZER
  435. } // namespace core
  436. } // namespace boost
  437. #endif // #ifndef BOOST_CORE_BIT_HPP_INCLUDED