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 >= 17
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
75 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
__base_10_u32(char * __first,uint32_t __value)76 __base_10_u32(char* __first, uint32_t __value) noexcept {
77 if (__value < 1000000) {
78 if (__value < 10000) {
79 if (__value < 100) {
80 // 0 <= __value < 100
81 if (__value < 10)
82 return __itoa::__append1(__first, __value);
83 return __itoa::__append2(__first, __value);
84 }
85 // 100 <= __value < 10'000
86 if (__value < 1000)
87 return __itoa::__append3(__first, __value);
88 return __itoa::__append4(__first, __value);
89 }
90
91 // 10'000 <= __value < 1'000'000
92 if (__value < 100000)
93 return __itoa::__append5(__first, __value);
94 return __itoa::__append6(__first, __value);
95 }
96
97 // __value => 1'000'000
98 if (__value < 100000000) {
99 // 1'000'000 <= __value < 100'000'000
100 if (__value < 10000000)
101 return __itoa::__append7(__first, __value);
102 return __itoa::__append8(__first, __value);
103 }
104
105 // 100'000'000 <= __value < max
106 if (__value < 1000000000)
107 return __itoa::__append9(__first, __value);
108 return __itoa::__append10(__first, __value);
109 }
110
111 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
__base_10_u64(char * __buffer,uint64_t __value)112 __base_10_u64(char* __buffer, uint64_t __value) noexcept {
113 if (__value <= UINT32_MAX)
114 return __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value));
115
116 // Numbers in the range UINT32_MAX <= val < 10'000'000'000 always contain 10
117 // digits and are outputted after this if statement.
118 if (__value >= 10000000000) {
119 // This function properly deterimines the first non-zero leading digit.
120 __buffer = __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value / 10000000000));
121 __value %= 10000000000;
122 }
123 return __itoa::__append10(__buffer, __value);
124 }
125
126 # ifndef _LIBCPP_HAS_NO_INT128
127 /// \returns 10^\a exp
128 ///
129 /// \pre \a exp [19, 39]
130 ///
131 /// \note The lookup table contains a partial set of exponents limiting the
132 /// range that can be used. However the range is sufficient for
133 /// \ref __base_10_u128.
__pow_10(int __exp)134 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline __uint128_t __pow_10(int __exp) noexcept {
135 _LIBCPP_ASSERT_INTERNAL(__exp >= __pow10_128_offset, "Index out of bounds");
136 return __pow10_128[__exp - __pow10_128_offset];
137 }
138
139 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
__base_10_u128(char * __buffer,__uint128_t __value)140 __base_10_u128(char* __buffer, __uint128_t __value) noexcept {
141 _LIBCPP_ASSERT_INTERNAL(
142 __value > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fails when this isn't true.");
143
144 // Unlike the 64 to 32 bit case the 128 bit case the "upper half" can't be
145 // stored in the "lower half". Instead we first need to handle the top most
146 // digits separately.
147 //
148 // Maximum unsigned values
149 // 64 bit 18'446'744'073'709'551'615 (20 digits)
150 // 128 bit 340'282'366'920'938'463'463'374'607'431'768'211'455 (39 digits)
151 // step 1 ^ ([0-1] digits)
152 // step 2 ^^^^^^^^^^^^^^^^^^^^^^^^^ ([0-19] digits)
153 // step 3 ^^^^^^^^^^^^^^^^^^^^^^^^^ (19 digits)
154 if (__value >= __itoa::__pow_10(38)) {
155 // step 1
156 __buffer = __itoa::__append1(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(38)));
157 __value %= __itoa::__pow_10(38);
158
159 // step 2 always 19 digits.
160 // They are handled here since leading zeros need to be appended to the buffer,
161 __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(29)));
162 __value %= __itoa::__pow_10(29);
163 __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
164 __value %= __itoa::__pow_10(19);
165 } else {
166 // step 2
167 // This version needs to determine the position of the leading non-zero digit.
168 __buffer = __base_10_u64(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
169 __value %= __itoa::__pow_10(19);
170 }
171
172 // Step 3
173 __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / 10000000000));
174 __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value % 10000000000));
175
176 return __buffer;
177 }
178 # endif
179 } // namespace __itoa
180
181 #endif // _LIBCPP_STD_VER >= 17
182
183 _LIBCPP_END_NAMESPACE_STD
184
185 _LIBCPP_POP_MACROS
186
187 #endif // _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
188