2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
11 #define _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
13 #include <__algorithm/copy_n.h>
14 #include <__charconv/tables.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
24 #include <__undef_macros>
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 #if _LIBCPP_STD_VER >= 17
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
);
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
);
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);
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);
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);
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);
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);
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);
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);
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));
75 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
inline char*
76 __base_10_u32(char* __first
, uint32_t __value
) noexcept
{
77 if (__value
< 1000000) {
78 if (__value
< 10000) {
82 return __itoa::__append1(__first
, __value
);
83 return __itoa::__append2(__first
, __value
);
85 // 100 <= __value < 10'000
87 return __itoa::__append3(__first
, __value
);
88 return __itoa::__append4(__first
, __value
);
91 // 10'000 <= __value < 1'000'000
93 return __itoa::__append5(__first
, __value
);
94 return __itoa::__append6(__first
, __value
);
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
);
105 // 100'000'000 <= __value < max
106 if (__value
< 1000000000)
107 return __itoa::__append9(__first
, __value
);
108 return __itoa::__append10(__first
, __value
);
111 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
inline char*
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
));
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;
123 return __itoa::__append10(__buffer
, __value
);
126 # ifndef _LIBCPP_HAS_NO_INT128
127 /// \returns 10^\a exp
129 /// \pre \a exp [19, 39]
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.
134 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
inline __uint128_t
__pow_10(int __exp
) noexcept
{
135 _LIBCPP_ASSERT_UNCATEGORIZED(__exp
>= __pow10_128_offset
, "Index out of bounds");
136 return __pow10_128
[__exp
- __pow10_128_offset
];
139 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
inline char*
140 __base_10_u128(char* __buffer
, __uint128_t __value
) noexcept
{
141 _LIBCPP_ASSERT_UNCATEGORIZED(
142 __value
> numeric_limits
<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true.");
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.
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)) {
156 __buffer
= __itoa::__append1(__buffer
, static_cast<uint32_t>(__value
/ __itoa::__pow_10(38)));
157 __value
%= __itoa::__pow_10(38);
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);
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);
173 __buffer
= __itoa::__append9(__buffer
, static_cast<uint32_t>(__value
/ 10000000000));
174 __buffer
= __itoa::__append10(__buffer
, static_cast<uint64_t>(__value
% 10000000000));
179 } // namespace __itoa
181 #endif // _LIBCPP_STD_VER >= 17
183 _LIBCPP_END_NAMESPACE_STD
187 #endif // _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H