to_chars_integer_impl.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #ifdef BOOST_CHARCONV_HAS_INT128
  185. template <typename Integer, typename Unsigned_Integer = boost::uint128_type>
  186. #else
  187. template <typename Integer, typename Unsigned_Integer = uint128>
  188. #endif
  189. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, char* last, Integer value) noexcept
  190. {
  191. Unsigned_Integer unsigned_value {};
  192. const std::ptrdiff_t user_buffer_size = last - first;
  193. BOOST_ATTRIBUTE_UNUSED bool is_negative = false;
  194. if (first > last)
  195. {
  196. return {last, std::errc::invalid_argument};
  197. }
  198. // Strip the sign from the value and apply at the end after parsing if the type is signed
  199. BOOST_IF_CONSTEXPR (std::numeric_limits<Integer>::is_signed
  200. #ifdef BOOST_CHARCONV_HAS_INT128
  201. || std::is_same<boost::int128_type, Integer>::value
  202. #endif
  203. )
  204. {
  205. if (value < 0)
  206. {
  207. is_negative = true;
  208. unsigned_value = -(static_cast<Unsigned_Integer>(value));
  209. }
  210. else
  211. {
  212. unsigned_value = static_cast<Unsigned_Integer>(value);
  213. }
  214. }
  215. else
  216. {
  217. unsigned_value = static_cast<Unsigned_Integer>(value);
  218. }
  219. auto converted_value = static_cast<Unsigned_Integer>(unsigned_value);
  220. const int converted_value_digits = num_digits(converted_value);
  221. if (converted_value_digits > user_buffer_size)
  222. {
  223. return {last, std::errc::value_too_large};
  224. }
  225. if (is_negative)
  226. {
  227. *first++ = '-';
  228. }
  229. // If the value fits into 64 bits use the other method of processing
  230. if (converted_value < (std::numeric_limits<std::uint64_t>::max)())
  231. {
  232. return to_chars_integer_impl(first, last, static_cast<std::uint64_t>(value));
  233. }
  234. constexpr std::uint32_t ten_9 = UINT32_C(1000000000);
  235. char buffer[5][10] {};
  236. int num_chars[5] {};
  237. int i = 0;
  238. while (converted_value != 0)
  239. {
  240. auto digits = static_cast<std::uint32_t>(converted_value % ten_9);
  241. num_chars[i] = num_digits(digits);
  242. decompose32(digits, buffer[i]); // Always returns 10 digits (to include leading 0s) which we want
  243. converted_value = (converted_value - digits) / ten_9;
  244. ++i;
  245. }
  246. --i;
  247. auto offset = static_cast<std::size_t>(num_chars[i]);
  248. boost::charconv::detail::memcpy(first, buffer[i] + 10 - offset, offset);
  249. while (i > 0)
  250. {
  251. --i;
  252. boost::charconv::detail::memcpy(first + offset, buffer[i] + 1, 9);
  253. offset += 9;
  254. }
  255. return {first + converted_value_digits, std::errc()};
  256. }
  257. // Conversion warning from shift operators with unsigned char
  258. #if defined(__GNUC__) && __GNUC__ >= 5
  259. # pragma GCC diagnostic push
  260. # pragma GCC diagnostic ignored "-Wconversion"
  261. #elif defined(__clang__)
  262. # pragma clang diagnostic push
  263. # pragma clang diagnostic ignored "-Wconversion"
  264. #endif
  265. // All other bases
  266. // Use a simple lookup table to put together the Integer in character form
  267. template <typename Integer, typename Unsigned_Integer>
  268. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value, int base) noexcept
  269. {
  270. if (!((first <= last) && (base >= 2 && base <= 36)))
  271. {
  272. return {last, std::errc::invalid_argument};
  273. }
  274. if (value == 0)
  275. {
  276. *first++ = '0';
  277. return {first, std::errc()};
  278. }
  279. Unsigned_Integer unsigned_value {};
  280. const auto unsigned_base = static_cast<Unsigned_Integer>(base);
  281. BOOST_IF_CONSTEXPR (std::is_signed<Integer>::value)
  282. {
  283. if (value < 0)
  284. {
  285. *first++ = '-';
  286. unsigned_value = static_cast<Unsigned_Integer>(detail::apply_sign(value));
  287. }
  288. else
  289. {
  290. unsigned_value = static_cast<Unsigned_Integer>(value);
  291. }
  292. }
  293. else
  294. {
  295. unsigned_value = static_cast<Unsigned_Integer>(value);
  296. }
  297. const std::ptrdiff_t output_length = last - first;
  298. constexpr Unsigned_Integer zero = 48U; // Char for '0'
  299. constexpr auto buffer_size = sizeof(Unsigned_Integer) * CHAR_BIT;
  300. char buffer[buffer_size] {};
  301. const char* buffer_end = buffer + buffer_size;
  302. char* end = buffer + buffer_size - 1;
  303. // Work from LSB to MSB
  304. switch (base)
  305. {
  306. case 2:
  307. while (unsigned_value != 0)
  308. {
  309. *end-- = static_cast<char>(zero + (unsigned_value & 1U)); // 1<<1 - 1
  310. unsigned_value >>= static_cast<Unsigned_Integer>(1);
  311. }
  312. break;
  313. case 4:
  314. while (unsigned_value != 0)
  315. {
  316. *end-- = static_cast<char>(zero + (unsigned_value & 3U)); // 1<<2 - 1
  317. unsigned_value >>= static_cast<Unsigned_Integer>(2);
  318. }
  319. break;
  320. case 8:
  321. while (unsigned_value != 0)
  322. {
  323. *end-- = static_cast<char>(zero + (unsigned_value & 7U)); // 1<<3 - 1
  324. unsigned_value >>= static_cast<Unsigned_Integer>(3);
  325. }
  326. break;
  327. case 16:
  328. while (unsigned_value != 0)
  329. {
  330. *end-- = digit_table[static_cast<std::size_t>(unsigned_value & 15U)]; // 1<<4 - 1
  331. unsigned_value >>= static_cast<Unsigned_Integer>(4);
  332. }
  333. break;
  334. case 32:
  335. while (unsigned_value != 0)
  336. {
  337. *end-- = digit_table[static_cast<std::size_t>(unsigned_value & 31U)]; // 1<<5 - 1
  338. unsigned_value >>= static_cast<Unsigned_Integer>(5);
  339. }
  340. break;
  341. default:
  342. while (unsigned_value != 0)
  343. {
  344. *end-- = digit_table[static_cast<std::size_t>(unsigned_value % unsigned_base)];
  345. unsigned_value /= unsigned_base;
  346. }
  347. break;
  348. }
  349. const std::ptrdiff_t num_chars = buffer_end - end - 1;
  350. if (num_chars > output_length)
  351. {
  352. return {last, std::errc::value_too_large};
  353. }
  354. boost::charconv::detail::memcpy(first, buffer + (buffer_size - static_cast<unsigned long>(num_chars)),
  355. static_cast<std::size_t>(num_chars));
  356. return {first + num_chars, std::errc()};
  357. }
  358. #if defined(__GNUC__) && __GNUC__ >= 5
  359. # pragma GCC diagnostic pop
  360. #elif defined(__clang__)
  361. # pragma clang diagnostic pop
  362. #endif
  363. #ifdef BOOST_MSVC
  364. # pragma warning(pop)
  365. #endif
  366. template <typename Integer>
  367. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_int(char* first, char* last, Integer value, int base = 10) noexcept
  368. {
  369. using Unsigned_Integer = typename std::make_unsigned<Integer>::type;
  370. if (base == 10)
  371. {
  372. return to_chars_integer_impl(first, last, value);
  373. }
  374. return to_chars_integer_impl<Integer, Unsigned_Integer>(first, last, value, base);
  375. }
  376. #ifdef BOOST_CHARCONV_HAS_INT128
  377. template <typename Integer>
  378. BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars128(char* first, char* last, Integer value, int base = 10) noexcept
  379. {
  380. if (base == 10)
  381. {
  382. return to_chars_128integer_impl(first, last, value);
  383. }
  384. return to_chars_integer_impl<Integer, boost::uint128_type>(first, last, value, base);
  385. }
  386. #endif
  387. }}} // Namespaces
  388. #endif //BOOST_CHARCONV_DETAIL_TO_CHARS_INTEGER_IMPL_HPP