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
10 #ifndef _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
11 #define _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
12
13 #include <__algorithm/copy_n.h>
14 #include <__charconv/tables.h>
15 #include <__config>
16 #include <cstdint>
17 #include <limits>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
22
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25
26 _LIBCPP_BEGIN_NAMESPACE_STD
27
28 #if _LIBCPP_STD_VER > 14
29
30 namespace __itoa {
31
__append1(char * __first,uint32_t __value)32 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append1(char* __first, uint32_t __value) noexcept {
33 *__first = '0' + static_cast<char>(__value);
34 return __first + 1;
35 }
36
__append2(char * __first,uint32_t __value)37 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append2(char* __first, uint32_t __value) noexcept {
38 return std::copy_n(&__digits_base_10[__value * 2], 2, __first);
39 }
40
__append3(char * __first,uint32_t __value)41 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append3(char* __first, uint32_t __value) noexcept {
42 return __itoa::__append2(__itoa::__append1(__first, __value / 100), __value % 100);
43 }
44
__append4(char * __first,uint32_t __value)45 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append4(char* __first, uint32_t __value) noexcept {
46 return __itoa::__append2(__itoa::__append2(__first, __value / 100), __value % 100);
47 }
48
__append5(char * __first,uint32_t __value)49 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append5(char* __first, uint32_t __value) noexcept {
50 return __itoa::__append4(__itoa::__append1(__first, __value / 10000), __value % 10000);
51 }
52
__append6(char * __first,uint32_t __value)53 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append6(char* __first, uint32_t __value) noexcept {
54 return __itoa::__append4(__itoa::__append2(__first, __value / 10000), __value % 10000);
55 }
56
__append7(char * __first,uint32_t __value)57 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append7(char* __first, uint32_t __value) noexcept {
58 return __itoa::__append6(__itoa::__append1(__first, __value / 1000000), __value % 1000000);
59 }
60
__append8(char * __first,uint32_t __value)61 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append8(char* __first, uint32_t __value) noexcept {
62 return __itoa::__append6(__itoa::__append2(__first, __value / 1000000), __value % 1000000);
63 }
64
__append9(char * __first,uint32_t __value)65 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append9(char* __first, uint32_t __value) noexcept {
66 return __itoa::__append8(__itoa::__append1(__first, __value / 100000000), __value % 100000000);
67 }
68
69 template <class _Tp>
__append10(char * __first,_Tp __value)70 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __append10(char* __first, _Tp __value) noexcept {
71 return __itoa::__append8(__itoa::__append2(__first, static_cast<uint32_t>(__value / 100000000)),
72 static_cast<uint32_t>(__value % 100000000));
73 }
74
__base_10_u32(char * __first,uint32_t __value)75 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u32(char* __first, uint32_t __value) noexcept {
76 if (__value < 1000000) {
77 if (__value < 10000) {
78 if (__value < 100) {
79 // 0 <= __value < 100
80 if (__value < 10)
81 return __itoa::__append1(__first, __value);
82 return __itoa::__append2(__first, __value);
83 }
84 // 100 <= __value < 10'000
85 if (__value < 1000)
86 return __itoa::__append3(__first, __value);
87 return __itoa::__append4(__first, __value);
88 }
89
90 // 10'000 <= __value < 1'000'000
91 if (__value < 100000)
92 return __itoa::__append5(__first, __value);
93 return __itoa::__append6(__first, __value);
94 }
95
96 // __value => 1'000'000
97 if (__value < 100000000) {
98 // 1'000'000 <= __value < 100'000'000
99 if (__value < 10000000)
100 return __itoa::__append7(__first, __value);
101 return __itoa::__append8(__first, __value);
102 }
103
104 // 100'000'000 <= __value < max
105 if (__value < 1000000000)
106 return __itoa::__append9(__first, __value);
107 return __itoa::__append10(__first, __value);
108 }
109
__base_10_u64(char * __buffer,uint64_t __value)110 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u64(char* __buffer, uint64_t __value) noexcept {
111 if (__value <= UINT32_MAX)
112 return __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value));
113
114 // Numbers in the range UINT32_MAX <= val < 10'000'000'000 always contain 10
115 // digits and are outputted after this if statement.
116 if (__value >= 10000000000) {
117 // This function properly deterimines the first non-zero leading digit.
118 __buffer = __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value / 10000000000));
119 __value %= 10000000000;
120 }
121 return __itoa::__append10(__buffer, __value);
122 }
123
124 # ifndef _LIBCPP_HAS_NO_INT128
125 /// \returns 10^\a exp
126 ///
127 /// \pre \a exp [19, 39]
128 ///
129 /// \note The lookup table contains a partial set of exponents limiting the
130 /// range that can be used. However the range is sufficient for
131 /// \ref __base_10_u128.
__pow_10(int __exp)132 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline __uint128_t __pow_10(int __exp) noexcept {
133 _LIBCPP_ASSERT(__exp >= __pow10_128_offset, "Index out of bounds");
134 return __pow10_128[__exp - __pow10_128_offset];
135 }
136
__base_10_u128(char * __buffer,__uint128_t __value)137 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u128(char* __buffer, __uint128_t __value) noexcept {
138 _LIBCPP_ASSERT(
139 __value > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true.");
140
141 // Unlike the 64 to 32 bit case the 128 bit case the "upper half" can't be
142 // stored in the "lower half". Instead we first need to handle the top most
143 // digits separately.
144 //
145 // Maximum unsigned values
146 // 64 bit 18'446'744'073'709'551'615 (20 digits)
147 // 128 bit 340'282'366'920'938'463'463'374'607'431'768'211'455 (39 digits)
148 // step 1 ^ ([0-1] digits)
149 // step 2 ^^^^^^^^^^^^^^^^^^^^^^^^^ ([0-19] digits)
150 // step 3 ^^^^^^^^^^^^^^^^^^^^^^^^^ (19 digits)
151 if (__value >= __itoa::__pow_10(38)) {
152 // step 1
153 __buffer = __itoa::__append1(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(38)));
154 __value %= __itoa::__pow_10(38);
155
156 // step 2 always 19 digits.
157 // They are handled here since leading zeros need to be appended to the buffer,
158 __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(29)));
159 __value %= __itoa::__pow_10(29);
160 __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
161 __value %= __itoa::__pow_10(19);
162 }
163 else {
164 // step 2
165 // This version needs to determine the position of the leading non-zero digit.
166 __buffer = __base_10_u64(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
167 __value %= __itoa::__pow_10(19);
168 }
169
170 // Step 3
171 __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / 10000000000));
172 __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value % 10000000000));
173
174 return __buffer;
175 }
176 # endif
177 } // namespace __itoa
178
179 #endif // _LIBCPP_STD_VER > 14
180
181 _LIBCPP_END_NAMESPACE_STD
182
183 _LIBCPP_POP_MACROS
184
185 #endif // _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
186