to_chars_integral.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHARCONV_TO_CHARS_INTEGRAL_H
  10. #define _LIBCPP___CHARCONV_TO_CHARS_INTEGRAL_H
  11. #include <__algorithm/copy_n.h>
  12. #include <__assert>
  13. #include <__bit/countl.h>
  14. #include <__charconv/tables.h>
  15. #include <__charconv/to_chars_base_10.h>
  16. #include <__charconv/to_chars_result.h>
  17. #include <__charconv/traits.h>
  18. #include <__config>
  19. #include <__system_error/errc.h>
  20. #include <__type_traits/enable_if.h>
  21. #include <__type_traits/integral_constant.h>
  22. #include <__type_traits/is_same.h>
  23. #include <__type_traits/make_32_64_or_128_bit.h>
  24. #include <__type_traits/make_unsigned.h>
  25. #include <__utility/unreachable.h>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <limits>
  29. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  30. # pragma GCC system_header
  31. #endif
  32. _LIBCPP_PUSH_MACROS
  33. #include <__undef_macros>
  34. _LIBCPP_BEGIN_NAMESPACE_STD
  35. #if _LIBCPP_STD_VER >= 17
  36. to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
  37. template <typename _Tp>
  38. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  39. __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type);
  40. template <typename _Tp>
  41. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  42. __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) {
  43. auto __x = std::__to_unsigned_like(__value);
  44. if (__value < 0 && __first != __last) {
  45. *__first++ = '-';
  46. __x = std::__complement(__x);
  47. }
  48. return std::__to_chars_itoa(__first, __last, __x, false_type());
  49. }
  50. template <typename _Tp>
  51. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  52. __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) {
  53. using __tx = __itoa::__traits<_Tp>;
  54. auto __diff = __last - __first;
  55. if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
  56. return {__tx::__convert(__first, __value), errc(0)};
  57. else
  58. return {__last, errc::value_too_large};
  59. }
  60. # ifndef _LIBCPP_HAS_NO_INT128
  61. template <>
  62. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  63. __to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type) {
  64. // When the value fits in 64-bits use the 64-bit code path. This reduces
  65. // the number of expensive calculations on 128-bit values.
  66. //
  67. // NOTE the 128-bit code path requires this optimization.
  68. if (__value <= numeric_limits<uint64_t>::max())
  69. return __to_chars_itoa(__first, __last, static_cast<uint64_t>(__value), false_type());
  70. using __tx = __itoa::__traits<__uint128_t>;
  71. auto __diff = __last - __first;
  72. if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
  73. return {__tx::__convert(__first, __value), errc(0)};
  74. else
  75. return {__last, errc::value_too_large};
  76. }
  77. # endif
  78. template <class _Tp>
  79. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  80. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, false_type);
  81. template <typename _Tp>
  82. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  83. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, true_type) {
  84. auto __x = std::__to_unsigned_like(__value);
  85. if (__value < 0 && __first != __last) {
  86. *__first++ = '-';
  87. __x = std::__complement(__x);
  88. }
  89. return std::__to_chars_integral(__first, __last, __x, __base, false_type());
  90. }
  91. namespace __itoa {
  92. template <unsigned _Base>
  93. struct _LIBCPP_HIDDEN __integral;
  94. template <>
  95. struct _LIBCPP_HIDDEN __integral<2> {
  96. template <typename _Tp>
  97. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  98. // If value == 0 still need one digit. If the value != this has no
  99. // effect since the code scans for the most significant bit set. (Note
  100. // that __libcpp_clz doesn't work for 0.)
  101. return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1);
  102. }
  103. template <typename _Tp>
  104. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result
  105. __to_chars(char* __first, char* __last, _Tp __value) {
  106. ptrdiff_t __cap = __last - __first;
  107. int __n = __width(__value);
  108. if (__n > __cap)
  109. return {__last, errc::value_too_large};
  110. __last = __first + __n;
  111. char* __p = __last;
  112. const unsigned __divisor = 16;
  113. while (__value > __divisor) {
  114. unsigned __c = __value % __divisor;
  115. __value /= __divisor;
  116. __p -= 4;
  117. std::copy_n(&__base_2_lut[4 * __c], 4, __p);
  118. }
  119. do {
  120. unsigned __c = __value % 2;
  121. __value /= 2;
  122. *--__p = "01"[__c];
  123. } while (__value != 0);
  124. return {__last, errc(0)};
  125. }
  126. };
  127. template <>
  128. struct _LIBCPP_HIDDEN __integral<8> {
  129. template <typename _Tp>
  130. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  131. // If value == 0 still need one digit. If the value != this has no
  132. // effect since the code scans for the most significat bit set. (Note
  133. // that __libcpp_clz doesn't work for 0.)
  134. return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3;
  135. }
  136. template <typename _Tp>
  137. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result
  138. __to_chars(char* __first, char* __last, _Tp __value) {
  139. ptrdiff_t __cap = __last - __first;
  140. int __n = __width(__value);
  141. if (__n > __cap)
  142. return {__last, errc::value_too_large};
  143. __last = __first + __n;
  144. char* __p = __last;
  145. unsigned __divisor = 64;
  146. while (__value > __divisor) {
  147. unsigned __c = __value % __divisor;
  148. __value /= __divisor;
  149. __p -= 2;
  150. std::copy_n(&__base_8_lut[2 * __c], 2, __p);
  151. }
  152. do {
  153. unsigned __c = __value % 8;
  154. __value /= 8;
  155. *--__p = "01234567"[__c];
  156. } while (__value != 0);
  157. return {__last, errc(0)};
  158. }
  159. };
  160. template <>
  161. struct _LIBCPP_HIDDEN __integral<16> {
  162. template <typename _Tp>
  163. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  164. // If value == 0 still need one digit. If the value != this has no
  165. // effect since the code scans for the most significat bit set. (Note
  166. // that __libcpp_clz doesn't work for 0.)
  167. return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4;
  168. }
  169. template <typename _Tp>
  170. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result
  171. __to_chars(char* __first, char* __last, _Tp __value) {
  172. ptrdiff_t __cap = __last - __first;
  173. int __n = __width(__value);
  174. if (__n > __cap)
  175. return {__last, errc::value_too_large};
  176. __last = __first + __n;
  177. char* __p = __last;
  178. unsigned __divisor = 256;
  179. while (__value > __divisor) {
  180. unsigned __c = __value % __divisor;
  181. __value /= __divisor;
  182. __p -= 2;
  183. std::copy_n(&__base_16_lut[2 * __c], 2, __p);
  184. }
  185. if (__first != __last)
  186. do {
  187. unsigned __c = __value % 16;
  188. __value /= 16;
  189. *--__p = "0123456789abcdef"[__c];
  190. } while (__value != 0);
  191. return {__last, errc(0)};
  192. }
  193. };
  194. } // namespace __itoa
  195. template <unsigned _Base, typename _Tp, __enable_if_t<(sizeof(_Tp) >= sizeof(unsigned)), int> = 0>
  196. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value) {
  197. return __itoa::__integral<_Base>::__width(__value);
  198. }
  199. template <unsigned _Base, typename _Tp, __enable_if_t<(sizeof(_Tp) < sizeof(unsigned)), int> = 0>
  200. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value) {
  201. return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value));
  202. }
  203. template <unsigned _Base, typename _Tp, __enable_if_t<(sizeof(_Tp) >= sizeof(unsigned)), int> = 0>
  204. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  205. __to_chars_integral(char* __first, char* __last, _Tp __value) {
  206. return __itoa::__integral<_Base>::__to_chars(__first, __last, __value);
  207. }
  208. template <unsigned _Base, typename _Tp, __enable_if_t<(sizeof(_Tp) < sizeof(unsigned)), int> = 0>
  209. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  210. __to_chars_integral(char* __first, char* __last, _Tp __value) {
  211. return std::__to_chars_integral<_Base>(__first, __last, static_cast<unsigned>(__value));
  212. }
  213. template <typename _Tp>
  214. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value, unsigned __base) {
  215. _LIBCPP_ASSERT_INTERNAL(__value >= 0, "The function requires a non-negative value.");
  216. unsigned __base_2 = __base * __base;
  217. unsigned __base_3 = __base_2 * __base;
  218. unsigned __base_4 = __base_2 * __base_2;
  219. int __r = 0;
  220. while (true) {
  221. if (__value < __base)
  222. return __r + 1;
  223. if (__value < __base_2)
  224. return __r + 2;
  225. if (__value < __base_3)
  226. return __r + 3;
  227. if (__value < __base_4)
  228. return __r + 4;
  229. __value /= __base_4;
  230. __r += 4;
  231. }
  232. __libcpp_unreachable();
  233. }
  234. template <typename _Tp>
  235. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  236. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, false_type) {
  237. if (__base == 10) [[likely]]
  238. return std::__to_chars_itoa(__first, __last, __value, false_type());
  239. switch (__base) {
  240. case 2:
  241. return std::__to_chars_integral<2>(__first, __last, __value);
  242. case 8:
  243. return std::__to_chars_integral<8>(__first, __last, __value);
  244. case 16:
  245. return std::__to_chars_integral<16>(__first, __last, __value);
  246. }
  247. ptrdiff_t __cap = __last - __first;
  248. int __n = std::__to_chars_integral_width(__value, __base);
  249. if (__n > __cap)
  250. return {__last, errc::value_too_large};
  251. __last = __first + __n;
  252. char* __p = __last;
  253. do {
  254. unsigned __c = __value % __base;
  255. __value /= __base;
  256. *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
  257. } while (__value != 0);
  258. return {__last, errc(0)};
  259. }
  260. template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
  261. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  262. to_chars(char* __first, char* __last, _Tp __value) {
  263. using _Type = __make_32_64_or_128_bit_t<_Tp>;
  264. static_assert(!is_same<_Type, void>::value, "unsupported integral type used in to_chars");
  265. return std::__to_chars_itoa(__first, __last, static_cast<_Type>(__value), is_signed<_Tp>());
  266. }
  267. template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
  268. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  269. to_chars(char* __first, char* __last, _Tp __value, int __base) {
  270. _LIBCPP_ASSERT_UNCATEGORIZED(2 <= __base && __base <= 36, "base not in [2, 36]");
  271. using _Type = __make_32_64_or_128_bit_t<_Tp>;
  272. return std::__to_chars_integral(__first, __last, static_cast<_Type>(__value), __base, is_signed<_Tp>());
  273. }
  274. #endif // _LIBCPP_STD_VER >= 17
  275. _LIBCPP_END_NAMESPACE_STD
  276. _LIBCPP_POP_MACROS
  277. #endif // _LIBCPP___CHARCONV_TO_CHARS_INTEGRAL_H