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 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #ifndef _LIBCPP_CXX03_LANG
25 
26 namespace __itoa {
27 
28 _LIBCPP_HIDE_FROM_ABI inline char* __append1(char* __first, uint32_t __value) noexcept {
29   *__first = '0' + static_cast<char>(__value);
30   return __first + 1;
31 }
32 
33 _LIBCPP_HIDE_FROM_ABI inline char* __append2(char* __first, uint32_t __value) noexcept {
34   return std::copy_n(&__table<>::__digits_base_10[__value * 2], 2, __first);
35 }
36 
37 _LIBCPP_HIDE_FROM_ABI inline char* __append3(char* __first, uint32_t __value) noexcept {
38   return __itoa::__append2(__itoa::__append1(__first, __value / 100), __value % 100);
39 }
40 
41 _LIBCPP_HIDE_FROM_ABI inline char* __append4(char* __first, uint32_t __value) noexcept {
42   return __itoa::__append2(__itoa::__append2(__first, __value / 100), __value % 100);
43 }
44 
45 _LIBCPP_HIDE_FROM_ABI inline char* __append5(char* __first, uint32_t __value) noexcept {
46   return __itoa::__append4(__itoa::__append1(__first, __value / 10000), __value % 10000);
47 }
48 
49 _LIBCPP_HIDE_FROM_ABI inline char* __append6(char* __first, uint32_t __value) noexcept {
50   return __itoa::__append4(__itoa::__append2(__first, __value / 10000), __value % 10000);
51 }
52 
53 _LIBCPP_HIDE_FROM_ABI inline char* __append7(char* __first, uint32_t __value) noexcept {
54   return __itoa::__append6(__itoa::__append1(__first, __value / 1000000), __value % 1000000);
55 }
56 
57 _LIBCPP_HIDE_FROM_ABI inline char* __append8(char* __first, uint32_t __value) noexcept {
58   return __itoa::__append6(__itoa::__append2(__first, __value / 1000000), __value % 1000000);
59 }
60 
61 _LIBCPP_HIDE_FROM_ABI inline char* __append9(char* __first, uint32_t __value) noexcept {
62   return __itoa::__append8(__itoa::__append1(__first, __value / 100000000), __value % 100000000);
63 }
64 
65 // This function is used for uint32_t and uint64_t.
66 template <class _Tp>
67 _LIBCPP_HIDE_FROM_ABI char* __append10(char* __first, _Tp __value) noexcept {
68   return __itoa::__append8(__itoa::__append2(__first, static_cast<uint32_t>(__value / 100000000)),
69                            static_cast<uint32_t>(__value % 100000000));
70 }
71 
72 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u32(char* __first, uint32_t __value) noexcept {
73   if (__value < 1000000) {
74     if (__value < 10000) {
75       if (__value < 100) {
76         // 0 <= __value < 100
77         if (__value < 10)
78           return __itoa::__append1(__first, __value);
79         return __itoa::__append2(__first, __value);
80       }
81       // 100 <= __value < 10'000
82       if (__value < 1000)
83         return __itoa::__append3(__first, __value);
84       return __itoa::__append4(__first, __value);
85     }
86 
87     // 10'000 <= __value < 1'000'000
88     if (__value < 100000)
89       return __itoa::__append5(__first, __value);
90     return __itoa::__append6(__first, __value);
91   }
92 
93   // __value => 1'000'000
94   if (__value < 100000000) {
95     // 1'000'000 <= __value < 100'000'000
96     if (__value < 10000000)
97       return __itoa::__append7(__first, __value);
98     return __itoa::__append8(__first, __value);
99   }
100 
101   // 100'000'000 <= __value < max
102   if (__value < 1000000000)
103     return __itoa::__append9(__first, __value);
104   return __itoa::__append10(__first, __value);
105 }
106 
107 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u64(char* __buffer, uint64_t __value) noexcept {
108   if (__value <= UINT32_MAX)
109     return __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value));
110 
111   // Numbers in the range UINT32_MAX <= val < 10'000'000'000 always contain 10
112   // digits and are outputted after this if statement.
113   if (__value >= 10000000000) {
114     // This function properly deterimines the first non-zero leading digit.
115     __buffer = __itoa::__base_10_u32(__buffer, static_cast<uint32_t>(__value / 10000000000));
116     __value %= 10000000000;
117   }
118   return __itoa::__append10(__buffer, __value);
119 }
120 
121 } // namespace __itoa
122 
123 #endif // _LIBCPP_CXX03_LANG
124 
125 _LIBCPP_END_NAMESPACE_STD
126 
127 #endif // _LIBCPP___CHARCONV_TO_CHARS_BASE_10_H
128