integer_search_trees.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2023 Matt Borland
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // https://www.boost.org/LICENSE_1_0.txt
  4. #ifndef BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP
  5. #define BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP
  6. // https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer?page=1&tab=scoredesc#tab-top
  7. // https://graphics.stanford.edu/~seander/bithacks.html
  8. #include <boost/charconv/detail/config.hpp>
  9. #include <boost/charconv/detail/emulated128.hpp>
  10. #include <limits>
  11. #include <array>
  12. #include <cstdint>
  13. namespace boost { namespace charconv { namespace detail {
  14. // Generic solution
  15. template <typename T>
  16. BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(T x) noexcept
  17. {
  18. int digits = 0;
  19. while (x)
  20. {
  21. x /= 10;
  22. ++digits;
  23. }
  24. return digits;
  25. }
  26. template <>
  27. BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(std::uint32_t x) noexcept
  28. {
  29. if (x >= UINT32_C(10000))
  30. {
  31. if (x >= UINT32_C(10000000))
  32. {
  33. if (x >= UINT32_C(100000000))
  34. {
  35. if (x >= UINT32_C(1000000000))
  36. {
  37. return 10;
  38. }
  39. return 9;
  40. }
  41. return 8;
  42. }
  43. else if (x >= UINT32_C(100000))
  44. {
  45. if (x >= UINT32_C(1000000))
  46. {
  47. return 7;
  48. }
  49. return 6;
  50. }
  51. return 5;
  52. }
  53. else if (x >= UINT32_C(100))
  54. {
  55. if (x >= UINT32_C(1000))
  56. {
  57. return 4;
  58. }
  59. return 3;
  60. }
  61. else if (x >= UINT32_C(10))
  62. {
  63. return 2;
  64. }
  65. return 1;
  66. }
  67. template <>
  68. BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(std::uint64_t x) noexcept
  69. {
  70. if (x >= UINT64_C(10000000000))
  71. {
  72. if (x >= UINT64_C(100000000000000))
  73. {
  74. if (x >= UINT64_C(10000000000000000))
  75. {
  76. if (x >= UINT64_C(100000000000000000))
  77. {
  78. if (x >= UINT64_C(1000000000000000000))
  79. {
  80. if (x >= UINT64_C(10000000000000000000))
  81. {
  82. return 20;
  83. }
  84. return 19;
  85. }
  86. return 18;
  87. }
  88. return 17;
  89. }
  90. else if (x >= UINT64_C(1000000000000000))
  91. {
  92. return 16;
  93. }
  94. return 15;
  95. }
  96. if (x >= UINT64_C(1000000000000))
  97. {
  98. if (x >= UINT64_C(10000000000000))
  99. {
  100. return 14;
  101. }
  102. return 13;
  103. }
  104. if (x >= UINT64_C(100000000000))
  105. {
  106. return 12;
  107. }
  108. return 11;
  109. }
  110. else if (x >= UINT64_C(100000))
  111. {
  112. if (x >= UINT64_C(10000000))
  113. {
  114. if (x >= UINT64_C(100000000))
  115. {
  116. if (x >= UINT64_C(1000000000))
  117. {
  118. return 10;
  119. }
  120. return 9;
  121. }
  122. return 8;
  123. }
  124. if (x >= UINT64_C(1000000))
  125. {
  126. return 7;
  127. }
  128. return 6;
  129. }
  130. if (x >= UINT64_C(100))
  131. {
  132. if (x >= UINT64_C(1000))
  133. {
  134. if (x >= UINT64_C(10000))
  135. {
  136. return 5;
  137. }
  138. return 4;
  139. }
  140. return 3;
  141. }
  142. if (x >= UINT64_C(10))
  143. {
  144. return 2;
  145. }
  146. return 1;
  147. }
  148. #ifdef BOOST_MSVC
  149. # pragma warning(push)
  150. # pragma warning(disable: 4307) // MSVC 14.1 warns of intergral constant overflow
  151. #endif
  152. BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(uint128 x) noexcept
  153. {
  154. if (x.high == 0)
  155. {
  156. return num_digits(x.low);
  157. }
  158. BOOST_CHARCONV_CXX14_CONSTEXPR_NO_INLINE uint128 digits_39 = static_cast<uint128>(UINT64_C(10000000000000000000)) *
  159. static_cast<uint128>(UINT64_C(10000000000000000000));
  160. uint128 current_power_of_10 = digits_39;
  161. for (int i = 39; i > 0; --i)
  162. {
  163. if (x >= current_power_of_10)
  164. {
  165. return i;
  166. }
  167. current_power_of_10 /= 10U;
  168. }
  169. return 1;
  170. }
  171. #ifdef BOOST_MSVC
  172. # pragma warning(pop)
  173. #endif
  174. #ifdef BOOST_CHARCONV_HAS_INT128
  175. static constexpr std::array<std::uint64_t, 20> powers_of_10 =
  176. {{
  177. UINT64_C(1), UINT64_C(10), UINT64_C(100), UINT64_C(1000), UINT64_C(10000), UINT64_C(100000), UINT64_C(1000000),
  178. UINT64_C(10000000), UINT64_C(100000000), UINT64_C(1000000000), UINT64_C(10000000000), UINT64_C(100000000000),
  179. UINT64_C(1000000000000), UINT64_C(10000000000000), UINT64_C(100000000000000), UINT64_C(1000000000000000),
  180. UINT64_C(10000000000000000), UINT64_C(100000000000000000), UINT64_C(1000000000000000000), UINT64_C(10000000000000000000)
  181. }};
  182. // Assume that if someone is using 128 bit ints they are favoring the top end of the range
  183. // Max value is 340,282,366,920,938,463,463,374,607,431,768,211,455 (39 digits)
  184. BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(boost::uint128_type x) noexcept
  185. {
  186. // There is no literal for boost::uint128_type, so we need to calculate them using the max value of the
  187. // std::uint64_t powers of 10
  188. constexpr boost::uint128_type digits_39 = static_cast<boost::uint128_type>(UINT64_C(10000000000000000000)) *
  189. static_cast<boost::uint128_type>(UINT64_C(10000000000000000000));
  190. constexpr boost::uint128_type digits_38 = digits_39 / 10;
  191. constexpr boost::uint128_type digits_37 = digits_38 / 10;
  192. constexpr boost::uint128_type digits_36 = digits_37 / 10;
  193. constexpr boost::uint128_type digits_35 = digits_36 / 10;
  194. constexpr boost::uint128_type digits_34 = digits_35 / 10;
  195. constexpr boost::uint128_type digits_33 = digits_34 / 10;
  196. constexpr boost::uint128_type digits_32 = digits_33 / 10;
  197. constexpr boost::uint128_type digits_31 = digits_32 / 10;
  198. constexpr boost::uint128_type digits_30 = digits_31 / 10;
  199. constexpr boost::uint128_type digits_29 = digits_30 / 10;
  200. constexpr boost::uint128_type digits_28 = digits_29 / 10;
  201. constexpr boost::uint128_type digits_27 = digits_28 / 10;
  202. constexpr boost::uint128_type digits_26 = digits_27 / 10;
  203. constexpr boost::uint128_type digits_25 = digits_26 / 10;
  204. constexpr boost::uint128_type digits_24 = digits_25 / 10;
  205. constexpr boost::uint128_type digits_23 = digits_24 / 10;
  206. constexpr boost::uint128_type digits_22 = digits_23 / 10;
  207. constexpr boost::uint128_type digits_21 = digits_22 / 10;
  208. return (x >= digits_39) ? 39 :
  209. (x >= digits_38) ? 38 :
  210. (x >= digits_37) ? 37 :
  211. (x >= digits_36) ? 36 :
  212. (x >= digits_35) ? 35 :
  213. (x >= digits_34) ? 34 :
  214. (x >= digits_33) ? 33 :
  215. (x >= digits_32) ? 32 :
  216. (x >= digits_31) ? 31 :
  217. (x >= digits_30) ? 30 :
  218. (x >= digits_29) ? 29 :
  219. (x >= digits_28) ? 28 :
  220. (x >= digits_27) ? 27 :
  221. (x >= digits_26) ? 26 :
  222. (x >= digits_25) ? 25 :
  223. (x >= digits_24) ? 24 :
  224. (x >= digits_23) ? 23 :
  225. (x >= digits_22) ? 22 :
  226. (x >= digits_21) ? 21 :
  227. (x >= powers_of_10[19]) ? 20 :
  228. (x >= powers_of_10[18]) ? 19 :
  229. (x >= powers_of_10[17]) ? 18 :
  230. (x >= powers_of_10[16]) ? 17 :
  231. (x >= powers_of_10[15]) ? 16 :
  232. (x >= powers_of_10[14]) ? 15 :
  233. (x >= powers_of_10[13]) ? 14 :
  234. (x >= powers_of_10[12]) ? 13 :
  235. (x >= powers_of_10[11]) ? 12 :
  236. (x >= powers_of_10[10]) ? 11 :
  237. (x >= powers_of_10[9]) ? 10 :
  238. (x >= powers_of_10[8]) ? 9 :
  239. (x >= powers_of_10[7]) ? 8 :
  240. (x >= powers_of_10[6]) ? 7 :
  241. (x >= powers_of_10[5]) ? 6 :
  242. (x >= powers_of_10[4]) ? 5 :
  243. (x >= powers_of_10[3]) ? 4 :
  244. (x >= powers_of_10[2]) ? 3 :
  245. (x >= powers_of_10[1]) ? 2 :
  246. (x >= powers_of_10[0]) ? 1 : 0;
  247. }
  248. #endif
  249. }}} // Namespace boost::charconv::detail
  250. #endif // BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP