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