bit.hpp 24 KB

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