bit.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. #include <cstdlib>
  20. #if defined(_MSC_VER)
  21. # include <intrin.h>
  22. # pragma intrinsic(_BitScanForward)
  23. # pragma intrinsic(_BitScanReverse)
  24. # if defined(_M_X64)
  25. # pragma intrinsic(_BitScanForward64)
  26. # pragma intrinsic(_BitScanReverse64)
  27. # endif
  28. # pragma warning(push)
  29. # pragma warning(disable: 4127) // conditional expression is constant
  30. # pragma warning(disable: 4244) // conversion from int to T
  31. #endif // defined(_MSC_VER)
  32. #if defined(BOOST_MSVC) && BOOST_MSVC >= 1925
  33. # define BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL
  34. #endif
  35. #if defined(__has_builtin)
  36. # if __has_builtin(__builtin_bit_cast)
  37. # define BOOST_CORE_HAS_BUILTIN_BIT_CAST
  38. # endif
  39. #endif
  40. #if defined(BOOST_MSVC) && BOOST_MSVC >= 1926
  41. # define BOOST_CORE_HAS_BUILTIN_BIT_CAST
  42. #endif
  43. namespace boost
  44. {
  45. namespace core
  46. {
  47. // bit_cast
  48. #if defined(BOOST_CORE_HAS_BUILTIN_BIT_CAST)
  49. template<class To, class From>
  50. BOOST_CONSTEXPR To bit_cast( From const & from ) BOOST_NOEXCEPT
  51. {
  52. return __builtin_bit_cast( To, from );
  53. }
  54. #else
  55. template<class To, class From>
  56. To bit_cast( From const & from ) BOOST_NOEXCEPT
  57. {
  58. BOOST_STATIC_ASSERT( sizeof(To) == sizeof(From) );
  59. To to;
  60. std::memcpy( &to, &from, sizeof(To) );
  61. return to;
  62. }
  63. #endif
  64. // countl
  65. #if defined(__GNUC__) || defined(__clang__)
  66. namespace detail
  67. {
  68. BOOST_CONSTEXPR inline int countl_impl( unsigned char x ) BOOST_NOEXCEPT
  69. {
  70. return x? __builtin_clz( x ) - ( std::numeric_limits<unsigned int>::digits - std::numeric_limits<unsigned char>::digits ): std::numeric_limits<unsigned char>::digits;
  71. }
  72. BOOST_CONSTEXPR inline int countl_impl( unsigned short x ) BOOST_NOEXCEPT
  73. {
  74. return x? __builtin_clz( x ) - ( std::numeric_limits<unsigned int>::digits - std::numeric_limits<unsigned short>::digits ): std::numeric_limits<unsigned short>::digits;
  75. }
  76. BOOST_CONSTEXPR inline int countl_impl( unsigned int x ) BOOST_NOEXCEPT
  77. {
  78. return x? __builtin_clz( x ): std::numeric_limits<unsigned int>::digits;
  79. }
  80. BOOST_CONSTEXPR inline int countl_impl( unsigned long x ) BOOST_NOEXCEPT
  81. {
  82. return x? __builtin_clzl( x ): std::numeric_limits<unsigned long>::digits;
  83. }
  84. BOOST_CONSTEXPR inline int countl_impl( boost::ulong_long_type x ) BOOST_NOEXCEPT
  85. {
  86. return x? __builtin_clzll( x ): std::numeric_limits<boost::ulong_long_type>::digits;
  87. }
  88. } // namespace detail
  89. template<class T>
  90. BOOST_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT
  91. {
  92. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  93. return boost::core::detail::countl_impl( x );
  94. }
  95. #else // defined(__GNUC__) || defined(__clang__)
  96. namespace detail
  97. {
  98. #if defined(_MSC_VER) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  99. BOOST_CXX14_CONSTEXPR inline int countl_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  100. {
  101. if( __builtin_is_constant_evaluated() )
  102. {
  103. constexpr unsigned char 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 };
  104. x |= x >> 1;
  105. x |= x >> 2;
  106. x |= x >> 4;
  107. x |= x >> 8;
  108. x |= x >> 16;
  109. return mod37[ x % 37 ];
  110. }
  111. else
  112. {
  113. unsigned long r;
  114. if( _BitScanReverse( &r, x ) )
  115. {
  116. return 31 - static_cast<int>( r );
  117. }
  118. else
  119. {
  120. return 32;
  121. }
  122. }
  123. }
  124. BOOST_CXX14_CONSTEXPR inline int countl_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  125. {
  126. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 24;
  127. }
  128. BOOST_CXX14_CONSTEXPR inline int countl_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  129. {
  130. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 16;
  131. }
  132. #elif defined(_MSC_VER)
  133. inline int countl_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  134. {
  135. unsigned long r;
  136. if( _BitScanReverse( &r, x ) )
  137. {
  138. return 31 - static_cast<int>( r );
  139. }
  140. else
  141. {
  142. return 32;
  143. }
  144. }
  145. inline int countl_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  146. {
  147. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 24;
  148. }
  149. inline int countl_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  150. {
  151. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 16;
  152. }
  153. #else
  154. inline int countl_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  155. {
  156. 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 };
  157. x |= x >> 1;
  158. x |= x >> 2;
  159. x |= x >> 4;
  160. x |= x >> 8;
  161. x |= x >> 16;
  162. return mod37[ x % 37 ];
  163. }
  164. inline int countl_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  165. {
  166. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 24;
  167. }
  168. inline int countl_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  169. {
  170. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) - 16;
  171. }
  172. #endif
  173. #if defined(_MSC_VER) && defined(_M_X64) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  174. BOOST_CXX14_CONSTEXPR inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  175. {
  176. if( __builtin_is_constant_evaluated() )
  177. {
  178. return static_cast<boost::uint32_t>( x >> 32 ) != 0?
  179. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x >> 32 ) ):
  180. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) + 32;
  181. }
  182. else
  183. {
  184. unsigned long r;
  185. if( _BitScanReverse64( &r, x ) )
  186. {
  187. return 63 - static_cast<int>( r );
  188. }
  189. else
  190. {
  191. return 64;
  192. }
  193. }
  194. }
  195. #elif defined(_MSC_VER) && defined(_M_X64)
  196. inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  197. {
  198. unsigned long r;
  199. if( _BitScanReverse64( &r, x ) )
  200. {
  201. return 63 - static_cast<int>( r );
  202. }
  203. else
  204. {
  205. return 64;
  206. }
  207. }
  208. #elif defined(_MSC_VER) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  209. BOOST_CXX14_CONSTEXPR inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  210. {
  211. return static_cast<boost::uint32_t>( x >> 32 ) != 0?
  212. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x >> 32 ) ):
  213. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) + 32;
  214. }
  215. #else
  216. inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  217. {
  218. return static_cast<boost::uint32_t>( x >> 32 ) != 0?
  219. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x >> 32 ) ):
  220. boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) ) + 32;
  221. }
  222. #endif
  223. } // namespace detail
  224. template<class T>
  225. BOOST_CXX14_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT
  226. {
  227. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  228. 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) );
  229. BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) )
  230. {
  231. return boost::core::detail::countl_impl( static_cast<boost::uint8_t>( x ) );
  232. }
  233. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint16_t) )
  234. {
  235. return boost::core::detail::countl_impl( static_cast<boost::uint16_t>( x ) );
  236. }
  237. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint32_t) )
  238. {
  239. return boost::core::detail::countl_impl( static_cast<boost::uint32_t>( x ) );
  240. }
  241. else
  242. {
  243. return boost::core::detail::countl_impl( static_cast<boost::uint64_t>( x ) );
  244. }
  245. }
  246. #endif // defined(__GNUC__) || defined(__clang__)
  247. template<class T>
  248. BOOST_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT
  249. {
  250. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  251. return boost::core::countl_zero( static_cast<T>( ~x ) );
  252. }
  253. // countr
  254. #if defined(__GNUC__) || defined(__clang__)
  255. namespace detail
  256. {
  257. BOOST_CONSTEXPR inline int countr_impl( unsigned char x ) BOOST_NOEXCEPT
  258. {
  259. return x? __builtin_ctz( x ): std::numeric_limits<unsigned char>::digits;
  260. }
  261. BOOST_CONSTEXPR inline int countr_impl( unsigned short x ) BOOST_NOEXCEPT
  262. {
  263. return x? __builtin_ctz( x ): std::numeric_limits<unsigned short>::digits;
  264. }
  265. BOOST_CONSTEXPR inline int countr_impl( unsigned int x ) BOOST_NOEXCEPT
  266. {
  267. return x? __builtin_ctz( x ): std::numeric_limits<unsigned int>::digits;
  268. }
  269. BOOST_CONSTEXPR inline int countr_impl( unsigned long x ) BOOST_NOEXCEPT
  270. {
  271. return x? __builtin_ctzl( x ): std::numeric_limits<unsigned long>::digits;
  272. }
  273. BOOST_CONSTEXPR inline int countr_impl( boost::ulong_long_type x ) BOOST_NOEXCEPT
  274. {
  275. return x? __builtin_ctzll( x ): std::numeric_limits<boost::ulong_long_type>::digits;
  276. }
  277. } // namespace detail
  278. template<class T>
  279. BOOST_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT
  280. {
  281. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  282. return boost::core::detail::countr_impl( x );
  283. }
  284. #else // defined(__GNUC__) || defined(__clang__)
  285. namespace detail
  286. {
  287. #if defined(_MSC_VER) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  288. BOOST_CXX14_CONSTEXPR inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  289. {
  290. if( __builtin_is_constant_evaluated() )
  291. {
  292. constexpr unsigned char 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 };
  293. return mod37[ ( -(boost::int32_t)x & x ) % 37 ];
  294. }
  295. else
  296. {
  297. unsigned long r;
  298. if( _BitScanForward( &r, x ) )
  299. {
  300. return static_cast<int>( r );
  301. }
  302. else
  303. {
  304. return 32;
  305. }
  306. }
  307. }
  308. BOOST_CXX14_CONSTEXPR inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  309. {
  310. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x100 );
  311. }
  312. BOOST_CXX14_CONSTEXPR inline int countr_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  313. {
  314. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x10000 );
  315. }
  316. #elif defined(_MSC_VER)
  317. inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  318. {
  319. unsigned long r;
  320. if( _BitScanForward( &r, x ) )
  321. {
  322. return static_cast<int>( r );
  323. }
  324. else
  325. {
  326. return 32;
  327. }
  328. }
  329. inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  330. {
  331. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x100 );
  332. }
  333. inline int countr_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  334. {
  335. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x10000 );
  336. }
  337. #else
  338. inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  339. {
  340. 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 };
  341. return mod37[ ( -(boost::int32_t)x & x ) % 37 ];
  342. }
  343. inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  344. {
  345. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x100 );
  346. }
  347. inline int countr_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  348. {
  349. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x10000 );
  350. }
  351. #endif
  352. #if defined(_MSC_VER) && defined(_M_X64) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  353. BOOST_CXX14_CONSTEXPR inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  354. {
  355. if( __builtin_is_constant_evaluated() )
  356. {
  357. return static_cast<boost::uint32_t>( x ) != 0?
  358. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) ):
  359. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x >> 32 ) ) + 32;
  360. }
  361. else
  362. {
  363. unsigned long r;
  364. if( _BitScanForward64( &r, x ) )
  365. {
  366. return static_cast<int>( r );
  367. }
  368. else
  369. {
  370. return 64;
  371. }
  372. }
  373. }
  374. #elif defined(_MSC_VER) && defined(_M_X64)
  375. inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  376. {
  377. unsigned long r;
  378. if( _BitScanForward64( &r, x ) )
  379. {
  380. return static_cast<int>( r );
  381. }
  382. else
  383. {
  384. return 64;
  385. }
  386. }
  387. #elif defined(_MSC_VER) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  388. BOOST_CXX14_CONSTEXPR inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  389. {
  390. return static_cast<boost::uint32_t>( x ) != 0?
  391. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) ):
  392. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x >> 32 ) ) + 32;
  393. }
  394. #else
  395. inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  396. {
  397. return static_cast<boost::uint32_t>( x ) != 0?
  398. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) ):
  399. boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x >> 32 ) ) + 32;
  400. }
  401. #endif
  402. } // namespace detail
  403. template<class T>
  404. BOOST_CXX14_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT
  405. {
  406. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  407. 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) );
  408. BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) )
  409. {
  410. return boost::core::detail::countr_impl( static_cast<boost::uint8_t>( x ) );
  411. }
  412. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint16_t) )
  413. {
  414. return boost::core::detail::countr_impl( static_cast<boost::uint16_t>( x ) );
  415. }
  416. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint32_t) )
  417. {
  418. return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) );
  419. }
  420. else
  421. {
  422. return boost::core::detail::countr_impl( static_cast<boost::uint64_t>( x ) );
  423. }
  424. }
  425. #endif // defined(__GNUC__) || defined(__clang__)
  426. template<class T>
  427. BOOST_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT
  428. {
  429. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  430. return boost::core::countr_zero( static_cast<T>( ~x ) );
  431. }
  432. // popcount
  433. #if defined(__GNUC__) || defined(__clang__)
  434. #if defined(__clang__) && __clang_major__ * 100 + __clang_minor__ < 304
  435. # define BOOST_CORE_POPCOUNT_CONSTEXPR
  436. #else
  437. # define BOOST_CORE_POPCOUNT_CONSTEXPR BOOST_CONSTEXPR
  438. #endif
  439. namespace detail
  440. {
  441. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned char x ) BOOST_NOEXCEPT
  442. {
  443. return __builtin_popcount( x );
  444. }
  445. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned short x ) BOOST_NOEXCEPT
  446. {
  447. return __builtin_popcount( x );
  448. }
  449. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned int x ) BOOST_NOEXCEPT
  450. {
  451. return __builtin_popcount( x );
  452. }
  453. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( unsigned long x ) BOOST_NOEXCEPT
  454. {
  455. return __builtin_popcountl( x );
  456. }
  457. BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( boost::ulong_long_type x ) BOOST_NOEXCEPT
  458. {
  459. return __builtin_popcountll( x );
  460. }
  461. } // namespace detail
  462. #undef BOOST_CORE_POPCOUNT_CONSTEXPR
  463. template<class T>
  464. BOOST_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
  465. {
  466. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  467. return boost::core::detail::popcount_impl( x );
  468. }
  469. #else // defined(__GNUC__) || defined(__clang__)
  470. namespace detail
  471. {
  472. BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  473. {
  474. x = x - ( ( x >> 1 ) & 0x55555555 );
  475. x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
  476. x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F;
  477. return static_cast<unsigned>( ( x * 0x01010101 ) >> 24 );
  478. }
  479. BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  480. {
  481. x = x - ( ( x >> 1 ) & 0x5555555555555555 );
  482. x = ( x & 0x3333333333333333 ) + ( ( x >> 2 ) & 0x3333333333333333 );
  483. x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F0F0F0F0F;
  484. return static_cast<unsigned>( ( x * 0x0101010101010101 ) >> 56 );
  485. }
  486. } // namespace detail
  487. template<class T>
  488. BOOST_CXX14_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT
  489. {
  490. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  491. BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) );
  492. BOOST_IF_CONSTEXPR ( sizeof(T) <= sizeof(boost::uint32_t) )
  493. {
  494. return boost::core::detail::popcount_impl( static_cast<boost::uint32_t>( x ) );
  495. }
  496. else
  497. {
  498. return boost::core::detail::popcount_impl( static_cast<boost::uint64_t>( x ) );
  499. }
  500. }
  501. #endif // defined(__GNUC__) || defined(__clang__)
  502. // rotating
  503. template<class T>
  504. BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT
  505. {
  506. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  507. unsigned const mask = std::numeric_limits<T>::digits - 1;
  508. return static_cast<T>( x << (static_cast<unsigned>( s ) & mask) | x >> (static_cast<unsigned>( -s ) & mask) );
  509. }
  510. template<class T>
  511. BOOST_CXX14_CONSTEXPR T rotr( T x, int s ) BOOST_NOEXCEPT
  512. {
  513. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  514. unsigned const mask = std::numeric_limits<T>::digits - 1;
  515. return static_cast<T>( x >> (static_cast<unsigned>( s ) & mask) | x << (static_cast<unsigned>( -s ) & mask) );
  516. }
  517. // integral powers of 2
  518. template<class T>
  519. BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT
  520. {
  521. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  522. return x != 0 && ( x & ( x - 1 ) ) == 0;
  523. }
  524. // bit_width returns `int` now, https://cplusplus.github.io/LWG/issue3656
  525. // has been applied to C++20 as a DR
  526. template<class T>
  527. BOOST_CONSTEXPR int bit_width( T x ) BOOST_NOEXCEPT
  528. {
  529. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  530. return std::numeric_limits<T>::digits - boost::core::countl_zero( x );
  531. }
  532. template<class T>
  533. BOOST_CONSTEXPR T bit_floor( T x ) BOOST_NOEXCEPT
  534. {
  535. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  536. return x == 0? T(0): static_cast<T>( T(1) << ( boost::core::bit_width( x ) - 1 ) );
  537. }
  538. namespace detail
  539. {
  540. BOOST_CXX14_CONSTEXPR inline boost::uint32_t bit_ceil_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  541. {
  542. if( x == 0 )
  543. {
  544. return 0;
  545. }
  546. --x;
  547. x |= x >> 1;
  548. x |= x >> 2;
  549. x |= x >> 4;
  550. x |= x >> 8;
  551. x |= x >> 16;
  552. ++x;
  553. return x;
  554. }
  555. BOOST_CXX14_CONSTEXPR inline boost::uint64_t bit_ceil_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  556. {
  557. if( x == 0 )
  558. {
  559. return 0;
  560. }
  561. --x;
  562. x |= x >> 1;
  563. x |= x >> 2;
  564. x |= x >> 4;
  565. x |= x >> 8;
  566. x |= x >> 16;
  567. x |= x >> 32;
  568. ++x;
  569. return x;
  570. }
  571. } // namespace detail
  572. template<class T>
  573. BOOST_CXX14_CONSTEXPR T bit_ceil( T x ) BOOST_NOEXCEPT
  574. {
  575. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed );
  576. BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) );
  577. BOOST_IF_CONSTEXPR ( sizeof(T) <= sizeof(boost::uint32_t) )
  578. {
  579. return static_cast<T>( boost::core::detail::bit_ceil_impl( static_cast<boost::uint32_t>( x ) ) );
  580. }
  581. else
  582. {
  583. return static_cast<T>( boost::core::detail::bit_ceil_impl( static_cast<boost::uint64_t>( x ) ) );
  584. }
  585. }
  586. // endian
  587. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  588. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  589. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  590. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =big
  591. #elif defined(__BYTE_ORDER__) && defined(__ORDER_PDP_ENDIAN__) && __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
  592. # define BOOST_CORE_BIT_NATIVE_INITIALIZER
  593. #elif defined(__LITTLE_ENDIAN__)
  594. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  595. #elif defined(__BIG_ENDIAN__)
  596. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =big
  597. #elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
  598. # define BOOST_CORE_BIT_NATIVE_INITIALIZER =little
  599. #else
  600. # define BOOST_CORE_BIT_NATIVE_INITIALIZER
  601. #endif
  602. #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  603. enum class endian
  604. {
  605. big,
  606. little,
  607. native BOOST_CORE_BIT_NATIVE_INITIALIZER
  608. };
  609. typedef endian endian_type;
  610. #else
  611. namespace endian
  612. {
  613. enum type
  614. {
  615. big,
  616. little,
  617. native BOOST_CORE_BIT_NATIVE_INITIALIZER
  618. };
  619. } // namespace endian
  620. typedef endian::type endian_type;
  621. #endif
  622. #undef BOOST_CORE_BIT_NATIVE_INITIALIZER
  623. // byteswap
  624. namespace detail
  625. {
  626. BOOST_CONSTEXPR inline boost::uint8_t byteswap_impl( boost::uint8_t x ) BOOST_NOEXCEPT
  627. {
  628. return x;
  629. }
  630. BOOST_CONSTEXPR inline boost::uint16_t byteswap_impl( boost::uint16_t x ) BOOST_NOEXCEPT
  631. {
  632. return static_cast<boost::uint16_t>( x << 8 | x >> 8 );
  633. }
  634. #if defined(__GNUC__) || defined(__clang__)
  635. BOOST_CXX14_CONSTEXPR inline boost::uint32_t byteswap_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  636. {
  637. return __builtin_bswap32( x );
  638. }
  639. BOOST_CXX14_CONSTEXPR inline boost::uint64_t byteswap_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  640. {
  641. return __builtin_bswap64( x );
  642. }
  643. #elif defined(_MSC_VER) && defined(BOOST_CORE_HAS_BUILTIN_ISCONSTEVAL)
  644. BOOST_CXX14_CONSTEXPR inline boost::uint32_t byteswap_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  645. {
  646. if( __builtin_is_constant_evaluated() )
  647. {
  648. boost::uint32_t step16 = x << 16 | x >> 16;
  649. return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
  650. }
  651. else
  652. {
  653. return _byteswap_ulong( x );
  654. }
  655. }
  656. BOOST_CXX14_CONSTEXPR inline boost::uint64_t byteswap_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  657. {
  658. if( __builtin_is_constant_evaluated() )
  659. {
  660. boost::uint64_t step32 = x << 32 | x >> 32;
  661. boost::uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  662. return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  663. }
  664. else
  665. {
  666. return _byteswap_uint64( x );
  667. }
  668. }
  669. #elif defined(_MSC_VER)
  670. inline boost::uint32_t byteswap_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  671. {
  672. return _byteswap_ulong( x );
  673. }
  674. inline boost::uint64_t byteswap_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  675. {
  676. return _byteswap_uint64( x );
  677. }
  678. #else
  679. BOOST_CXX14_CONSTEXPR inline boost::uint32_t byteswap_impl( boost::uint32_t x ) BOOST_NOEXCEPT
  680. {
  681. boost::uint32_t step16 = x << 16 | x >> 16;
  682. return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
  683. }
  684. BOOST_CXX14_CONSTEXPR inline boost::uint64_t byteswap_impl( boost::uint64_t x ) BOOST_NOEXCEPT
  685. {
  686. boost::uint64_t step32 = x << 32 | x >> 32;
  687. boost::uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  688. return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  689. }
  690. #endif
  691. } // namespace detail
  692. template<class T> BOOST_CXX14_CONSTEXPR T byteswap( T x ) BOOST_NOEXCEPT
  693. {
  694. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_integer );
  695. 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) );
  696. BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) )
  697. {
  698. return static_cast<T>( boost::core::detail::byteswap_impl( static_cast<boost::uint8_t>( x ) ) );
  699. }
  700. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint16_t) )
  701. {
  702. return static_cast<T>( boost::core::detail::byteswap_impl( static_cast<boost::uint16_t>( x ) ) );
  703. }
  704. else BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint32_t) )
  705. {
  706. return static_cast<T>( boost::core::detail::byteswap_impl( static_cast<boost::uint32_t>( x ) ) );
  707. }
  708. else
  709. {
  710. return static_cast<T>( boost::core::detail::byteswap_impl( static_cast<boost::uint64_t>( x ) ) );
  711. }
  712. }
  713. } // namespace core
  714. } // namespace boost
  715. #if defined(_MSC_VER)
  716. # pragma warning(pop)
  717. #endif
  718. #endif // #ifndef BOOST_CORE_BIT_HPP_INCLUDED