Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:23

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___CHARCONV_TO_CHARS_BASE_10_H
0011 #define _LIBCPP___CXX03___CHARCONV_TO_CHARS_BASE_10_H
0012 
0013 #include <__cxx03/__algorithm/copy_n.h>
0014 #include <__cxx03/__assert>
0015 #include <__cxx03/__charconv/tables.h>
0016 #include <__cxx03/__config>
0017 #include <__cxx03/cstdint>
0018 #include <__cxx03/limits>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_PUSH_MACROS
0025 #include <__cxx03/__undef_macros>
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 #if _LIBCPP_STD_VER >= 17
0030 
0031 namespace __itoa {
0032 
0033 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append1(char* __first, uint32_t __value) noexcept {
0034   *__first = '0' + static_cast<char>(__value);
0035   return __first + 1;
0036 }
0037 
0038 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append2(char* __first, uint32_t __value) noexcept {
0039   return std::copy_n(&__digits_base_10[__value * 2], 2, __first);
0040 }
0041 
0042 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append3(char* __first, uint32_t __value) noexcept {
0043   return __itoa::__append2(__itoa::__append1(__first, __value / 100), __value % 100);
0044 }
0045 
0046 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append4(char* __first, uint32_t __value) noexcept {
0047   return __itoa::__append2(__itoa::__append2(__first, __value / 100), __value % 100);
0048 }
0049 
0050 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append5(char* __first, uint32_t __value) noexcept {
0051   return __itoa::__append4(__itoa::__append1(__first, __value / 10000), __value % 10000);
0052 }
0053 
0054 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append6(char* __first, uint32_t __value) noexcept {
0055   return __itoa::__append4(__itoa::__append2(__first, __value / 10000), __value % 10000);
0056 }
0057 
0058 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append7(char* __first, uint32_t __value) noexcept {
0059   return __itoa::__append6(__itoa::__append1(__first, __value / 1000000), __value % 1000000);
0060 }
0061 
0062 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append8(char* __first, uint32_t __value) noexcept {
0063   return __itoa::__append6(__itoa::__append2(__first, __value / 1000000), __value % 1000000);
0064 }
0065 
0066 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __append9(char* __first, uint32_t __value) noexcept {
0067   return __itoa::__append8(__itoa::__append1(__first, __value / 100000000), __value % 100000000);
0068 }
0069 
0070 template <class _Tp>
0071 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __append10(char* __first, _Tp __value) noexcept {
0072   return __itoa::__append8(__itoa::__append2(__first, static_cast<uint32_t>(__value / 100000000)),
0073                            static_cast<uint32_t>(__value % 100000000));
0074 }
0075 
0076 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
0077 __base_10_u32(char* __first, uint32_t __value) noexcept {
0078   if (__value < 1000000) {
0079     if (__value < 10000) {
0080       if (__value < 100) {
0081         // 0 <= __value < 100
0082         if (__value < 10)
0083           return __itoa::__append1(__first, __value);
0084         return __itoa::__append2(__first, __value);
0085       }
0086       // 100 <= __value < 10'000
0087       if (__value < 1000)
0088         return __itoa::__append3(__first, __value);
0089       return __itoa::__append4(__first, __value);
0090     }
0091 
0092     // 10'000 <= __value < 1'000'000
0093     if (__value < 100000)
0094       return __itoa::__append5(__first, __value);
0095     return __itoa::__append6(__first, __value);
0096   }
0097 
0098   // __value => 1'000'000
0099   if (__value < 100000000) {
0100     // 1'000'000 <= __value < 100'000'000
0101     if (__value < 10000000)
0102       return __itoa::__append7(__first, __value);
0103     return __itoa::__append8(__first, __value);
0104   }
0105 
0106   // 100'000'000 <= __value < max
0107   if (__value < 1000000000)
0108     return __itoa::__append9(__first, __value);
0109   return __itoa::__append10(__first, __value);
0110 }
0111 
0112 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
0113 __base_10_u64(char* __buffer, uint64_t __value) noexcept {
0114   if (__value <= UINT32_MAX)
0115     return __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value));
0116 
0117   // Numbers in the range UINT32_MAX <= val < 10'000'000'000 always contain 10
0118   // digits and are outputted after this if statement.
0119   if (__value >= 10000000000) {
0120     // This function properly deterimines the first non-zero leading digit.
0121     __buffer = __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value / 10000000000));
0122     __value %= 10000000000;
0123   }
0124   return __itoa::__append10(__buffer, __value);
0125 }
0126 
0127 #  ifndef _LIBCPP_HAS_NO_INT128
0128 /// \returns 10^\a exp
0129 ///
0130 /// \pre \a exp [19, 39]
0131 ///
0132 /// \note The lookup table contains a partial set of exponents limiting the
0133 /// range that can be used. However the range is sufficient for
0134 /// \ref __base_10_u128.
0135 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline __uint128_t __pow_10(int __exp) noexcept {
0136   _LIBCPP_ASSERT_INTERNAL(__exp >= __pow10_128_offset, "Index out of bounds");
0137   return __pow10_128[__exp - __pow10_128_offset];
0138 }
0139 
0140 _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char*
0141 __base_10_u128(char* __buffer, __uint128_t __value) noexcept {
0142   _LIBCPP_ASSERT_INTERNAL(
0143       __value > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fails when this isn't true.");
0144 
0145   // Unlike the 64 to 32 bit case the 128 bit case the "upper half" can't be
0146   // stored in the "lower half". Instead we first need to handle the top most
0147   // digits separately.
0148   //
0149   // Maximum unsigned values
0150   // 64  bit                             18'446'744'073'709'551'615 (20 digits)
0151   // 128 bit    340'282'366'920'938'463'463'374'607'431'768'211'455 (39 digits)
0152   // step 1     ^                                                   ([0-1] digits)
0153   // step 2      ^^^^^^^^^^^^^^^^^^^^^^^^^                          ([0-19] digits)
0154   // step 3                               ^^^^^^^^^^^^^^^^^^^^^^^^^ (19 digits)
0155   if (__value >= __itoa::__pow_10(38)) {
0156     // step 1
0157     __buffer = __itoa::__append1(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(38)));
0158     __value %= __itoa::__pow_10(38);
0159 
0160     // step 2 always 19 digits.
0161     // They are handled here since leading zeros need to be appended to the buffer,
0162     __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / __itoa::__pow_10(29)));
0163     __value %= __itoa::__pow_10(29);
0164     __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
0165     __value %= __itoa::__pow_10(19);
0166   } else {
0167     // step 2
0168     // This version needs to determine the position of the leading non-zero digit.
0169     __buffer = __base_10_u64(__buffer, static_cast<uint64_t>(__value / __itoa::__pow_10(19)));
0170     __value %= __itoa::__pow_10(19);
0171   }
0172 
0173   // Step 3
0174   __buffer = __itoa::__append9(__buffer, static_cast<uint32_t>(__value / 10000000000));
0175   __buffer = __itoa::__append10(__buffer, static_cast<uint64_t>(__value % 10000000000));
0176 
0177   return __buffer;
0178 }
0179 #  endif
0180 } // namespace __itoa
0181 
0182 #endif // _LIBCPP_STD_VER >= 17
0183 
0184 _LIBCPP_END_NAMESPACE_STD
0185 
0186 _LIBCPP_POP_MACROS
0187 
0188 #endif // _LIBCPP___CXX03___CHARCONV_TO_CHARS_BASE_10_H