to_chars_integer_impl.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2020-2023 Junekey Jeon
  2. // Copyright 2022 Peter Dimov
  3. // Copyright 2023 Matt Borland
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_CHARCONV_DETAIL_TO_CHARS_INTEGER_IMPL_HPP
  7. #define BOOST_CHARCONV_DETAIL_TO_CHARS_INTEGER_IMPL_HPP
  8. #include <boost/charconv/detail/config.hpp>
  9. #include <boost/charconv/detail/memcpy.hpp>
  10. #include <boost/charconv/detail/to_chars_result.hpp>
  11. #include <boost/charconv/detail/integer_search_trees.hpp>
  12. #include <boost/charconv/detail/emulated128.hpp>
  13. #include <boost/charconv/detail/apply_sign.hpp>
  14. #include <limits>
  15. #include <system_error>
  16. #include <type_traits>
  17. #include <array>
  18. #include <limits>
  19. #include <utility>
  20. #include <cstring>
  21. #include <cstdio>
  22. #include <cerrno>
  23. #include <cstdint>
  24. #include <climits>
  25. #include <cmath>
  26. namespace boost { namespace charconv { namespace detail {
  27. static constexpr char radix_table[] = {
  28. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4',
  29. '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
  30. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
  31. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
  32. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4',
  33. '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  34. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
  35. '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
  36. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
  37. '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
  38. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4',
  39. '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  40. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4',
  41. '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
  42. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
  43. '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
  44. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4',
  45. '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  46. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4',
  47. '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
  48. };
  49. static constexpr char digit_table[] = {
  50. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  51. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  52. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  53. 'u', 'v', 'w', 'x', 'y', 'z'
  54. };
  55. // See: https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/
  56. // https://arxiv.org/abs/2101.11408
  57. BOOST_CHARCONV_CONSTEXPR char* decompose32(std::uint32_t value, char* buffer) noexcept
  58. {
  59. constexpr auto mask = (std::uint64_t(1) << 57) - 1;
  60. auto y = value * std::uint64_t(1441151881);
  61. for (std::size_t i {}; i < 10; i += 2)
  62. {
  63. boost::charconv::detail::memcpy(buffer + i, radix_table + static_cast<std::size_t>(y >> 57) * 2, 2);
  64. y &= mask;
  65. y *= 100;
  66. }
  67. return buffer + 10;
  68. }
  69. #ifdef BOOST_MSVC
  70. # pragma warning(push)
  71. # pragma warning(disable: 4127 4146)
  72. #endif
  73. template <typename Integer>
  74. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value) noexcept
  75. {
  76. using Unsigned_Integer = typename std::make_unsigned<Integer>::type;
  77. Unsigned_Integer unsigned_value {};
  78. char buffer[10] {};
  79. int converted_value_digits {};
  80. bool is_negative = false;
  81. if (first > last)
  82. {
  83. return {last, std::errc::invalid_argument};
  84. }
  85. // Strip the sign from the value and apply at the end after parsing if the type is signed
  86. BOOST_IF_CONSTEXPR (std::is_signed<Integer>::value)
  87. {
  88. if (value < 0)
  89. {
  90. is_negative = true;
  91. unsigned_value = apply_sign(value);
  92. }
  93. else
  94. {
  95. unsigned_value = static_cast<Unsigned_Integer>(value);
  96. }
  97. }
  98. else
  99. {
  100. unsigned_value = static_cast<Unsigned_Integer>(value);
  101. }
  102. const std::ptrdiff_t user_buffer_size = last - first - static_cast<std::ptrdiff_t>(is_negative);
  103. // If the type is less than 32 bits we can use this without change
  104. // If the type is greater than 32 bits we use a binary search tree to figure out how many digits
  105. // are present and then decompose the value into two (or more) std::uint32_t of known length so that we
  106. // don't have the issue of removing leading zeros from the least significant digits
  107. // Yields: warning C4127: conditional expression is constant because first half of the expression is constant,
  108. // but we need to short circuit to avoid UB on the second half
  109. if (std::numeric_limits<Integer>::digits <= std::numeric_limits<std::uint32_t>::digits ||
  110. unsigned_value <= static_cast<Unsigned_Integer>((std::numeric_limits<std::uint32_t>::max)()))
  111. {
  112. const auto converted_value = static_cast<std::uint32_t>(unsigned_value);
  113. converted_value_digits = num_digits(converted_value);
  114. if (converted_value_digits > user_buffer_size)
  115. {
  116. return {last, std::errc::value_too_large};
  117. }
  118. decompose32(converted_value, buffer);
  119. if (is_negative)
  120. {
  121. *first++ = '-';
  122. }
  123. boost::charconv::detail::memcpy(first, buffer + (sizeof(buffer) - static_cast<unsigned>(converted_value_digits)),
  124. static_cast<std::size_t>(converted_value_digits));
  125. }
  126. else if (std::numeric_limits<Integer>::digits <= std::numeric_limits<std::uint64_t>::digits ||
  127. static_cast<std::uint64_t>(unsigned_value) <= (std::numeric_limits<std::uint64_t>::max)())
  128. {
  129. auto converted_value = static_cast<std::uint64_t>(unsigned_value);
  130. converted_value_digits = num_digits(converted_value);
  131. if (converted_value_digits > user_buffer_size)
  132. {
  133. return {last, std::errc::value_too_large};
  134. }
  135. if (is_negative)
  136. {
  137. *first++ = '-';
  138. }
  139. // Only store 9 digits in each to avoid overflow
  140. if (num_digits(converted_value) <= 18)
  141. {
  142. const auto x = static_cast<std::uint32_t>(converted_value / UINT64_C(1000000000));
  143. const auto y = static_cast<std::uint32_t>(converted_value % UINT64_C(1000000000));
  144. const int first_value_chars = num_digits(x);
  145. decompose32(x, buffer);
  146. boost::charconv::detail::memcpy(first, buffer + (sizeof(buffer) - static_cast<unsigned>(first_value_chars)),
  147. static_cast<std::size_t>(first_value_chars));
  148. decompose32(y, buffer);
  149. boost::charconv::detail::memcpy(first + first_value_chars, buffer + 1, sizeof(buffer) - 1);
  150. }
  151. else
  152. {
  153. const auto x = static_cast<std::uint32_t>(converted_value / UINT64_C(100000000000));
  154. converted_value -= x * UINT64_C(100000000000);
  155. const auto y = static_cast<std::uint32_t>(converted_value / UINT64_C(100));
  156. const auto z = static_cast<std::uint32_t>(converted_value % UINT64_C(100));
  157. if (converted_value_digits == 19)
  158. {
  159. decompose32(x, buffer);
  160. boost::charconv::detail::memcpy(first, buffer + 2, sizeof(buffer) - 2);
  161. decompose32(y, buffer);
  162. boost::charconv::detail::memcpy(first + 8, buffer + 1, sizeof(buffer) - 1);
  163. // Always prints 2 digits last
  164. boost::charconv::detail::memcpy(first + 17, radix_table + z * 2, 2);
  165. }
  166. else // 20
  167. {
  168. decompose32(x, buffer);
  169. boost::charconv::detail::memcpy(first, buffer + 1, sizeof(buffer) - 1);
  170. decompose32(y, buffer);
  171. boost::charconv::detail::memcpy(first + 9, buffer + 1, sizeof(buffer) - 1);
  172. // Always prints 2 digits last
  173. boost::charconv::detail::memcpy(first + 18, radix_table + z * 2, 2);
  174. }
  175. }
  176. }
  177. return {first + converted_value_digits, std::errc()};
  178. }
  179. // Prior to GCC 10.3 std::numeric_limits was not specialized for __int128 which breaks the above control flow
  180. // Here we find if the 128-bit type will fit into a 64-bit type and use the above, or we use string manipulation
  181. // to extract the digits
  182. //
  183. // See: https://quuxplusone.github.io/blog/2019/02/28/is-int128-integral/
  184. template <typename Integer>
  185. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, char* last, Integer value) noexcept
  186. {
  187. #ifdef BOOST_CHARCONV_HAS_INT128
  188. using Unsigned_Integer = boost::uint128_type;
  189. #else
  190. using Unsigned_Integer = uint128;
  191. #endif
  192. Unsigned_Integer unsigned_value {};
  193. const std::ptrdiff_t user_buffer_size = last - first;
  194. BOOST_ATTRIBUTE_UNUSED bool is_negative = false;
  195. if (first > last)
  196. {
  197. return {last, std::errc::invalid_argument};
  198. }
  199. // Strip the sign from the value and apply at the end after parsing if the type is signed
  200. #ifdef BOOST_CHARCONV_HAS_INT128
  201. BOOST_IF_CONSTEXPR (std::is_same<boost::int128_type, Integer>::value)
  202. {
  203. if (value < 0)
  204. {
  205. is_negative = true;
  206. unsigned_value = -(static_cast<Unsigned_Integer>(value));
  207. }
  208. else
  209. {
  210. unsigned_value = static_cast<Unsigned_Integer>(value);
  211. }
  212. }
  213. else
  214. #endif
  215. {
  216. unsigned_value = static_cast<Unsigned_Integer>(value);
  217. }
  218. auto converted_value = static_cast<Unsigned_Integer>(unsigned_value);
  219. const int converted_value_digits = num_digits(converted_value);
  220. if (converted_value_digits > user_buffer_size)
  221. {
  222. return {last, std::errc::value_too_large};
  223. }
  224. if (is_negative)
  225. {
  226. *first++ = '-';
  227. }
  228. // If the value fits into 64 bits use the other method of processing
  229. if (converted_value < (std::numeric_limits<std::uint64_t>::max)())
  230. {
  231. return to_chars_integer_impl(first, last, static_cast<std::uint64_t>(value));
  232. }
  233. constexpr std::uint32_t ten_9 = UINT32_C(1000000000);
  234. char buffer[5][10] {};
  235. int num_chars[5] {};
  236. int i = 0;
  237. while (converted_value != 0)
  238. {
  239. auto digits = static_cast<std::uint32_t>(converted_value % ten_9);
  240. num_chars[i] = num_digits(digits);
  241. decompose32(digits, buffer[i]); // Always returns 10 digits (to include leading 0s) which we want
  242. converted_value = (converted_value - digits) / ten_9;
  243. ++i;
  244. }
  245. --i;
  246. auto offset = static_cast<std::size_t>(num_chars[i]);
  247. boost::charconv::detail::memcpy(first, buffer[i] + 10 - offset, offset);
  248. while (i > 0)
  249. {
  250. --i;
  251. boost::charconv::detail::memcpy(first + offset, buffer[i] + 1, 9);
  252. offset += 9;
  253. }
  254. return {first + converted_value_digits, std::errc()};
  255. }
  256. // Conversion warning from shift operators with unsigned char
  257. #if defined(__GNUC__) && __GNUC__ >= 5
  258. # pragma GCC diagnostic push
  259. # pragma GCC diagnostic ignored "-Wconversion"
  260. #elif defined(__clang__)
  261. # pragma clang diagnostic push
  262. # pragma clang diagnostic ignored "-Wconversion"
  263. #endif
  264. // All other bases
  265. // Use a simple lookup table to put together the Integer in character form
  266. template <typename Integer, typename Unsigned_Integer>
  267. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value, int base) noexcept
  268. {
  269. if (!((first <= last) && (base >= 2 && base <= 36)))
  270. {
  271. return {last, std::errc::invalid_argument};
  272. }
  273. if (value == 0)
  274. {
  275. *first++ = '0';
  276. return {first, std::errc()};
  277. }
  278. Unsigned_Integer unsigned_value {};
  279. const auto unsigned_base = static_cast<Unsigned_Integer>(base);
  280. BOOST_IF_CONSTEXPR (std::is_signed<Integer>::value)
  281. {
  282. if (value < 0)
  283. {
  284. *first++ = '-';
  285. unsigned_value = static_cast<Unsigned_Integer>(detail::apply_sign(value));
  286. }
  287. else
  288. {
  289. unsigned_value = static_cast<Unsigned_Integer>(value);
  290. }
  291. }
  292. else
  293. {
  294. unsigned_value = static_cast<Unsigned_Integer>(value);
  295. }
  296. const std::ptrdiff_t output_length = last - first;
  297. constexpr Unsigned_Integer zero = 48U; // Char for '0'
  298. constexpr auto buffer_size = sizeof(Unsigned_Integer) * CHAR_BIT;
  299. char buffer[buffer_size] {};
  300. const char* buffer_end = buffer + buffer_size;
  301. char* end = buffer + buffer_size - 1;
  302. // Work from LSB to MSB
  303. switch (base)
  304. {
  305. case 2:
  306. while (unsigned_value != 0)
  307. {
  308. *end-- = static_cast<char>(zero + (unsigned_value & 1U)); // 1<<1 - 1
  309. unsigned_value >>= static_cast<Unsigned_Integer>(1);
  310. }
  311. break;
  312. case 4:
  313. while (unsigned_value != 0)
  314. {
  315. *end-- = static_cast<char>(zero + (unsigned_value & 3U)); // 1<<2 - 1
  316. unsigned_value >>= static_cast<Unsigned_Integer>(2);
  317. }
  318. break;
  319. case 8:
  320. while (unsigned_value != 0)
  321. {
  322. *end-- = static_cast<char>(zero + (unsigned_value & 7U)); // 1<<3 - 1
  323. unsigned_value >>= static_cast<Unsigned_Integer>(3);
  324. }
  325. break;
  326. case 16:
  327. while (unsigned_value != 0)
  328. {
  329. *end-- = digit_table[unsigned_value & 15U]; // 1<<4 - 1
  330. unsigned_value >>= static_cast<Unsigned_Integer>(4);
  331. }
  332. break;
  333. case 32:
  334. while (unsigned_value != 0)
  335. {
  336. *end-- = digit_table[unsigned_value & 31U]; // 1<<5 - 1
  337. unsigned_value >>= static_cast<Unsigned_Integer>(5);
  338. }
  339. break;
  340. default:
  341. while (unsigned_value != 0)
  342. {
  343. *end-- = digit_table[unsigned_value % unsigned_base];
  344. unsigned_value /= unsigned_base;
  345. }
  346. break;
  347. }
  348. const std::ptrdiff_t num_chars = buffer_end - end - 1;
  349. if (num_chars > output_length)
  350. {
  351. return {last, std::errc::value_too_large};
  352. }
  353. boost::charconv::detail::memcpy(first, buffer + (buffer_size - static_cast<unsigned long>(num_chars)),
  354. static_cast<std::size_t>(num_chars));
  355. return {first + num_chars, std::errc()};
  356. }
  357. #if defined(__GNUC__) && __GNUC__ >= 5
  358. # pragma GCC diagnostic pop
  359. #elif defined(__clang__)
  360. # pragma clang diagnostic pop
  361. #endif
  362. #ifdef BOOST_MSVC
  363. # pragma warning(pop)
  364. #endif
  365. template <typename Integer>
  366. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_int(char* first, char* last, Integer value, int base = 10) noexcept
  367. {
  368. using Unsigned_Integer = typename std::make_unsigned<Integer>::type;
  369. if (base == 10)
  370. {
  371. return to_chars_integer_impl(first, last, value);
  372. }
  373. return to_chars_integer_impl<Integer, Unsigned_Integer>(first, last, value, base);
  374. }
  375. #ifdef BOOST_CHARCONV_HAS_INT128
  376. template <typename Integer>
  377. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars128(char* first, char* last, Integer value, int base = 10) noexcept
  378. {
  379. if (base == 10)
  380. {
  381. return to_chars_128integer_impl(first, last, value);
  382. }
  383. return to_chars_integer_impl<Integer, boost::uint128_type>(first, last, value, base);
  384. }
  385. #endif
  386. }}} // Namespaces
  387. #endif //BOOST_CHARCONV_DETAIL_TO_CHARS_INTEGER_IMPL_HPP