xref: /freebsd/contrib/llvm-project/libcxx/src/ryu/d2s.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
2*0eae32dcSDimitry Andric //
3*0eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0eae32dcSDimitry Andric //
7*0eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
8*0eae32dcSDimitry Andric 
9*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation.
10*0eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11*0eae32dcSDimitry Andric 
12*0eae32dcSDimitry Andric // Copyright 2018 Ulf Adams
13*0eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. All rights reserved.
14*0eae32dcSDimitry Andric 
15*0eae32dcSDimitry Andric // Boost Software License - Version 1.0 - August 17th, 2003
16*0eae32dcSDimitry Andric 
17*0eae32dcSDimitry Andric // Permission is hereby granted, free of charge, to any person or organization
18*0eae32dcSDimitry Andric // obtaining a copy of the software and accompanying documentation covered by
19*0eae32dcSDimitry Andric // this license (the "Software") to use, reproduce, display, distribute,
20*0eae32dcSDimitry Andric // execute, and transmit the Software, and to prepare derivative works of the
21*0eae32dcSDimitry Andric // Software, and to permit third-parties to whom the Software is furnished to
22*0eae32dcSDimitry Andric // do so, all subject to the following:
23*0eae32dcSDimitry Andric 
24*0eae32dcSDimitry Andric // The copyright notices in the Software and this entire statement, including
25*0eae32dcSDimitry Andric // the above license grant, this restriction and the following disclaimer,
26*0eae32dcSDimitry Andric // must be included in all copies of the Software, in whole or in part, and
27*0eae32dcSDimitry Andric // all derivative works of the Software, unless such copies or derivative
28*0eae32dcSDimitry Andric // works are solely in the form of machine-executable object code generated by
29*0eae32dcSDimitry Andric // a source language processor.
30*0eae32dcSDimitry Andric 
31*0eae32dcSDimitry Andric // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32*0eae32dcSDimitry Andric // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33*0eae32dcSDimitry Andric // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
34*0eae32dcSDimitry Andric // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
35*0eae32dcSDimitry Andric // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
36*0eae32dcSDimitry Andric // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37*0eae32dcSDimitry Andric // DEALINGS IN THE SOFTWARE.
38*0eae32dcSDimitry Andric 
39*0eae32dcSDimitry Andric // Avoid formatting to keep the changes with the original code minimal.
40*0eae32dcSDimitry Andric // clang-format off
41*0eae32dcSDimitry Andric 
42*0eae32dcSDimitry Andric #include "__config"
43*0eae32dcSDimitry Andric #include "charconv"
44*0eae32dcSDimitry Andric 
45*0eae32dcSDimitry Andric #include "include/ryu/common.h"
46*0eae32dcSDimitry Andric #include "include/ryu/d2fixed.h"
47*0eae32dcSDimitry Andric #include "include/ryu/d2s.h"
48*0eae32dcSDimitry Andric #include "include/ryu/d2s_full_table.h"
49*0eae32dcSDimitry Andric #include "include/ryu/d2s_intrinsics.h"
50*0eae32dcSDimitry Andric #include "include/ryu/digit_table.h"
51*0eae32dcSDimitry Andric #include "include/ryu/ryu.h"
52*0eae32dcSDimitry Andric 
53*0eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
54*0eae32dcSDimitry Andric 
55*0eae32dcSDimitry Andric // We need a 64x128-bit multiplication and a subsequent 128-bit shift.
56*0eae32dcSDimitry Andric // Multiplication:
57*0eae32dcSDimitry Andric //   The 64-bit factor is variable and passed in, the 128-bit factor comes
58*0eae32dcSDimitry Andric //   from a lookup table. We know that the 64-bit factor only has 55
59*0eae32dcSDimitry Andric //   significant bits (i.e., the 9 topmost bits are zeros). The 128-bit
60*0eae32dcSDimitry Andric //   factor only has 124 significant bits (i.e., the 4 topmost bits are
61*0eae32dcSDimitry Andric //   zeros).
62*0eae32dcSDimitry Andric // Shift:
63*0eae32dcSDimitry Andric //   In principle, the multiplication result requires 55 + 124 = 179 bits to
64*0eae32dcSDimitry Andric //   represent. However, we then shift this value to the right by __j, which is
65*0eae32dcSDimitry Andric //   at least __j >= 115, so the result is guaranteed to fit into 179 - 115 = 64
66*0eae32dcSDimitry Andric //   bits. This means that we only need the topmost 64 significant bits of
67*0eae32dcSDimitry Andric //   the 64x128-bit multiplication.
68*0eae32dcSDimitry Andric //
69*0eae32dcSDimitry Andric // There are several ways to do this:
70*0eae32dcSDimitry Andric // 1. Best case: the compiler exposes a 128-bit type.
71*0eae32dcSDimitry Andric //    We perform two 64x64-bit multiplications, add the higher 64 bits of the
72*0eae32dcSDimitry Andric //    lower result to the higher result, and shift by __j - 64 bits.
73*0eae32dcSDimitry Andric //
74*0eae32dcSDimitry Andric //    We explicitly cast from 64-bit to 128-bit, so the compiler can tell
75*0eae32dcSDimitry Andric //    that these are only 64-bit inputs, and can map these to the best
76*0eae32dcSDimitry Andric //    possible sequence of assembly instructions.
77*0eae32dcSDimitry Andric //    x64 machines happen to have matching assembly instructions for
78*0eae32dcSDimitry Andric //    64x64-bit multiplications and 128-bit shifts.
79*0eae32dcSDimitry Andric //
80*0eae32dcSDimitry Andric // 2. Second best case: the compiler exposes intrinsics for the x64 assembly
81*0eae32dcSDimitry Andric //    instructions mentioned in 1.
82*0eae32dcSDimitry Andric //
83*0eae32dcSDimitry Andric // 3. We only have 64x64 bit instructions that return the lower 64 bits of
84*0eae32dcSDimitry Andric //    the result, i.e., we have to use plain C.
85*0eae32dcSDimitry Andric //    Our inputs are less than the full width, so we have three options:
86*0eae32dcSDimitry Andric //    a. Ignore this fact and just implement the intrinsics manually.
87*0eae32dcSDimitry Andric //    b. Split both into 31-bit pieces, which guarantees no internal overflow,
88*0eae32dcSDimitry Andric //       but requires extra work upfront (unless we change the lookup table).
89*0eae32dcSDimitry Andric //    c. Split only the first factor into 31-bit pieces, which also guarantees
90*0eae32dcSDimitry Andric //       no internal overflow, but requires extra work since the intermediate
91*0eae32dcSDimitry Andric //       results are not perfectly aligned.
92*0eae32dcSDimitry Andric #ifdef _LIBCPP_INTRINSIC128
93*0eae32dcSDimitry Andric 
94*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __mulShift(const uint64_t __m, const uint64_t* const __mul, const int32_t __j) {
95*0eae32dcSDimitry Andric   // __m is maximum 55 bits
96*0eae32dcSDimitry Andric   uint64_t __high1;                                               // 128
97*0eae32dcSDimitry Andric   const uint64_t __low1 = __ryu_umul128(__m, __mul[1], &__high1); // 64
98*0eae32dcSDimitry Andric   uint64_t __high0;                                               // 64
99*0eae32dcSDimitry Andric   (void) __ryu_umul128(__m, __mul[0], &__high0);                  // 0
100*0eae32dcSDimitry Andric   const uint64_t __sum = __high0 + __low1;
101*0eae32dcSDimitry Andric   if (__sum < __high0) {
102*0eae32dcSDimitry Andric     ++__high1; // overflow into __high1
103*0eae32dcSDimitry Andric   }
104*0eae32dcSDimitry Andric   return __ryu_shiftright128(__sum, __high1, static_cast<uint32_t>(__j - 64));
105*0eae32dcSDimitry Andric }
106*0eae32dcSDimitry Andric 
107*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __mulShiftAll(const uint64_t __m, const uint64_t* const __mul, const int32_t __j,
108*0eae32dcSDimitry Andric   uint64_t* const __vp, uint64_t* const __vm, const uint32_t __mmShift) {
109*0eae32dcSDimitry Andric   *__vp = __mulShift(4 * __m + 2, __mul, __j);
110*0eae32dcSDimitry Andric   *__vm = __mulShift(4 * __m - 1 - __mmShift, __mul, __j);
111*0eae32dcSDimitry Andric   return __mulShift(4 * __m, __mul, __j);
112*0eae32dcSDimitry Andric }
113*0eae32dcSDimitry Andric 
114*0eae32dcSDimitry Andric #else // ^^^ intrinsics available ^^^ / vvv intrinsics unavailable vvv
115*0eae32dcSDimitry Andric 
116*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_ALWAYS_INLINE uint64_t __mulShiftAll(uint64_t __m, const uint64_t* const __mul, const int32_t __j,
117*0eae32dcSDimitry Andric   uint64_t* const __vp, uint64_t* const __vm, const uint32_t __mmShift) { // TRANSITION, VSO-634761
118*0eae32dcSDimitry Andric   __m <<= 1;
119*0eae32dcSDimitry Andric   // __m is maximum 55 bits
120*0eae32dcSDimitry Andric   uint64_t __tmp;
121*0eae32dcSDimitry Andric   const uint64_t __lo = __ryu_umul128(__m, __mul[0], &__tmp);
122*0eae32dcSDimitry Andric   uint64_t __hi;
123*0eae32dcSDimitry Andric   const uint64_t __mid = __tmp + __ryu_umul128(__m, __mul[1], &__hi);
124*0eae32dcSDimitry Andric   __hi += __mid < __tmp; // overflow into __hi
125*0eae32dcSDimitry Andric 
126*0eae32dcSDimitry Andric   const uint64_t __lo2 = __lo + __mul[0];
127*0eae32dcSDimitry Andric   const uint64_t __mid2 = __mid + __mul[1] + (__lo2 < __lo);
128*0eae32dcSDimitry Andric   const uint64_t __hi2 = __hi + (__mid2 < __mid);
129*0eae32dcSDimitry Andric   *__vp = __ryu_shiftright128(__mid2, __hi2, static_cast<uint32_t>(__j - 64 - 1));
130*0eae32dcSDimitry Andric 
131*0eae32dcSDimitry Andric   if (__mmShift == 1) {
132*0eae32dcSDimitry Andric     const uint64_t __lo3 = __lo - __mul[0];
133*0eae32dcSDimitry Andric     const uint64_t __mid3 = __mid - __mul[1] - (__lo3 > __lo);
134*0eae32dcSDimitry Andric     const uint64_t __hi3 = __hi - (__mid3 > __mid);
135*0eae32dcSDimitry Andric     *__vm = __ryu_shiftright128(__mid3, __hi3, static_cast<uint32_t>(__j - 64 - 1));
136*0eae32dcSDimitry Andric   } else {
137*0eae32dcSDimitry Andric     const uint64_t __lo3 = __lo + __lo;
138*0eae32dcSDimitry Andric     const uint64_t __mid3 = __mid + __mid + (__lo3 < __lo);
139*0eae32dcSDimitry Andric     const uint64_t __hi3 = __hi + __hi + (__mid3 < __mid);
140*0eae32dcSDimitry Andric     const uint64_t __lo4 = __lo3 - __mul[0];
141*0eae32dcSDimitry Andric     const uint64_t __mid4 = __mid3 - __mul[1] - (__lo4 > __lo3);
142*0eae32dcSDimitry Andric     const uint64_t __hi4 = __hi3 - (__mid4 > __mid3);
143*0eae32dcSDimitry Andric     *__vm = __ryu_shiftright128(__mid4, __hi4, static_cast<uint32_t>(__j - 64));
144*0eae32dcSDimitry Andric   }
145*0eae32dcSDimitry Andric 
146*0eae32dcSDimitry Andric   return __ryu_shiftright128(__mid, __hi, static_cast<uint32_t>(__j - 64 - 1));
147*0eae32dcSDimitry Andric }
148*0eae32dcSDimitry Andric 
149*0eae32dcSDimitry Andric #endif // ^^^ intrinsics unavailable ^^^
150*0eae32dcSDimitry Andric 
151*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __decimalLength17(const uint64_t __v) {
152*0eae32dcSDimitry Andric   // This is slightly faster than a loop.
153*0eae32dcSDimitry Andric   // The average output length is 16.38 digits, so we check high-to-low.
154*0eae32dcSDimitry Andric   // Function precondition: __v is not an 18, 19, or 20-digit number.
155*0eae32dcSDimitry Andric   // (17 digits are sufficient for round-tripping.)
156*0eae32dcSDimitry Andric   _LIBCPP_ASSERT(__v < 100000000000000000u, "");
157*0eae32dcSDimitry Andric   if (__v >= 10000000000000000u) { return 17; }
158*0eae32dcSDimitry Andric   if (__v >= 1000000000000000u) { return 16; }
159*0eae32dcSDimitry Andric   if (__v >= 100000000000000u) { return 15; }
160*0eae32dcSDimitry Andric   if (__v >= 10000000000000u) { return 14; }
161*0eae32dcSDimitry Andric   if (__v >= 1000000000000u) { return 13; }
162*0eae32dcSDimitry Andric   if (__v >= 100000000000u) { return 12; }
163*0eae32dcSDimitry Andric   if (__v >= 10000000000u) { return 11; }
164*0eae32dcSDimitry Andric   if (__v >= 1000000000u) { return 10; }
165*0eae32dcSDimitry Andric   if (__v >= 100000000u) { return 9; }
166*0eae32dcSDimitry Andric   if (__v >= 10000000u) { return 8; }
167*0eae32dcSDimitry Andric   if (__v >= 1000000u) { return 7; }
168*0eae32dcSDimitry Andric   if (__v >= 100000u) { return 6; }
169*0eae32dcSDimitry Andric   if (__v >= 10000u) { return 5; }
170*0eae32dcSDimitry Andric   if (__v >= 1000u) { return 4; }
171*0eae32dcSDimitry Andric   if (__v >= 100u) { return 3; }
172*0eae32dcSDimitry Andric   if (__v >= 10u) { return 2; }
173*0eae32dcSDimitry Andric   return 1;
174*0eae32dcSDimitry Andric }
175*0eae32dcSDimitry Andric 
176*0eae32dcSDimitry Andric // A floating decimal representing m * 10^e.
177*0eae32dcSDimitry Andric struct __floating_decimal_64 {
178*0eae32dcSDimitry Andric   uint64_t __mantissa;
179*0eae32dcSDimitry Andric   int32_t __exponent;
180*0eae32dcSDimitry Andric };
181*0eae32dcSDimitry Andric 
182*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline __floating_decimal_64 __d2d(const uint64_t __ieeeMantissa, const uint32_t __ieeeExponent) {
183*0eae32dcSDimitry Andric   int32_t __e2;
184*0eae32dcSDimitry Andric   uint64_t __m2;
185*0eae32dcSDimitry Andric   if (__ieeeExponent == 0) {
186*0eae32dcSDimitry Andric     // We subtract 2 so that the bounds computation has 2 additional bits.
187*0eae32dcSDimitry Andric     __e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS - 2;
188*0eae32dcSDimitry Andric     __m2 = __ieeeMantissa;
189*0eae32dcSDimitry Andric   } else {
190*0eae32dcSDimitry Andric     __e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS - 2;
191*0eae32dcSDimitry Andric     __m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
192*0eae32dcSDimitry Andric   }
193*0eae32dcSDimitry Andric   const bool __even = (__m2 & 1) == 0;
194*0eae32dcSDimitry Andric   const bool __acceptBounds = __even;
195*0eae32dcSDimitry Andric 
196*0eae32dcSDimitry Andric   // Step 2: Determine the interval of valid decimal representations.
197*0eae32dcSDimitry Andric   const uint64_t __mv = 4 * __m2;
198*0eae32dcSDimitry Andric   // Implicit bool -> int conversion. True is 1, false is 0.
199*0eae32dcSDimitry Andric   const uint32_t __mmShift = __ieeeMantissa != 0 || __ieeeExponent <= 1;
200*0eae32dcSDimitry Andric   // We would compute __mp and __mm like this:
201*0eae32dcSDimitry Andric   // uint64_t __mp = 4 * __m2 + 2;
202*0eae32dcSDimitry Andric   // uint64_t __mm = __mv - 1 - __mmShift;
203*0eae32dcSDimitry Andric 
204*0eae32dcSDimitry Andric   // Step 3: Convert to a decimal power base using 128-bit arithmetic.
205*0eae32dcSDimitry Andric   uint64_t __vr, __vp, __vm;
206*0eae32dcSDimitry Andric   int32_t __e10;
207*0eae32dcSDimitry Andric   bool __vmIsTrailingZeros = false;
208*0eae32dcSDimitry Andric   bool __vrIsTrailingZeros = false;
209*0eae32dcSDimitry Andric   if (__e2 >= 0) {
210*0eae32dcSDimitry Andric     // I tried special-casing __q == 0, but there was no effect on performance.
211*0eae32dcSDimitry Andric     // This expression is slightly faster than max(0, __log10Pow2(__e2) - 1).
212*0eae32dcSDimitry Andric     const uint32_t __q = __log10Pow2(__e2) - (__e2 > 3);
213*0eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q);
214*0eae32dcSDimitry Andric     const int32_t __k = __DOUBLE_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q)) - 1;
215*0eae32dcSDimitry Andric     const int32_t __i = -__e2 + static_cast<int32_t>(__q) + __k;
216*0eae32dcSDimitry Andric     __vr = __mulShiftAll(__m2, __DOUBLE_POW5_INV_SPLIT[__q], __i, &__vp, &__vm, __mmShift);
217*0eae32dcSDimitry Andric     if (__q <= 21) {
218*0eae32dcSDimitry Andric       // This should use __q <= 22, but I think 21 is also safe. Smaller values
219*0eae32dcSDimitry Andric       // may still be safe, but it's more difficult to reason about them.
220*0eae32dcSDimitry Andric       // Only one of __mp, __mv, and __mm can be a multiple of 5, if any.
221*0eae32dcSDimitry Andric       const uint32_t __mvMod5 = static_cast<uint32_t>(__mv) - 5 * static_cast<uint32_t>(__div5(__mv));
222*0eae32dcSDimitry Andric       if (__mvMod5 == 0) {
223*0eae32dcSDimitry Andric         __vrIsTrailingZeros = __multipleOfPowerOf5(__mv, __q);
224*0eae32dcSDimitry Andric       } else if (__acceptBounds) {
225*0eae32dcSDimitry Andric         // Same as min(__e2 + (~__mm & 1), __pow5Factor(__mm)) >= __q
226*0eae32dcSDimitry Andric         // <=> __e2 + (~__mm & 1) >= __q && __pow5Factor(__mm) >= __q
227*0eae32dcSDimitry Andric         // <=> true && __pow5Factor(__mm) >= __q, since __e2 >= __q.
228*0eae32dcSDimitry Andric         __vmIsTrailingZeros = __multipleOfPowerOf5(__mv - 1 - __mmShift, __q);
229*0eae32dcSDimitry Andric       } else {
230*0eae32dcSDimitry Andric         // Same as min(__e2 + 1, __pow5Factor(__mp)) >= __q.
231*0eae32dcSDimitry Andric         __vp -= __multipleOfPowerOf5(__mv + 2, __q);
232*0eae32dcSDimitry Andric       }
233*0eae32dcSDimitry Andric     }
234*0eae32dcSDimitry Andric   } else {
235*0eae32dcSDimitry Andric     // This expression is slightly faster than max(0, __log10Pow5(-__e2) - 1).
236*0eae32dcSDimitry Andric     const uint32_t __q = __log10Pow5(-__e2) - (-__e2 > 1);
237*0eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q) + __e2;
238*0eae32dcSDimitry Andric     const int32_t __i = -__e2 - static_cast<int32_t>(__q);
239*0eae32dcSDimitry Andric     const int32_t __k = __pow5bits(__i) - __DOUBLE_POW5_BITCOUNT;
240*0eae32dcSDimitry Andric     const int32_t __j = static_cast<int32_t>(__q) - __k;
241*0eae32dcSDimitry Andric     __vr = __mulShiftAll(__m2, __DOUBLE_POW5_SPLIT[__i], __j, &__vp, &__vm, __mmShift);
242*0eae32dcSDimitry Andric     if (__q <= 1) {
243*0eae32dcSDimitry Andric       // {__vr,__vp,__vm} is trailing zeros if {__mv,__mp,__mm} has at least __q trailing 0 bits.
244*0eae32dcSDimitry Andric       // __mv = 4 * __m2, so it always has at least two trailing 0 bits.
245*0eae32dcSDimitry Andric       __vrIsTrailingZeros = true;
246*0eae32dcSDimitry Andric       if (__acceptBounds) {
247*0eae32dcSDimitry Andric         // __mm = __mv - 1 - __mmShift, so it has 1 trailing 0 bit iff __mmShift == 1.
248*0eae32dcSDimitry Andric         __vmIsTrailingZeros = __mmShift == 1;
249*0eae32dcSDimitry Andric       } else {
250*0eae32dcSDimitry Andric         // __mp = __mv + 2, so it always has at least one trailing 0 bit.
251*0eae32dcSDimitry Andric         --__vp;
252*0eae32dcSDimitry Andric       }
253*0eae32dcSDimitry Andric     } else if (__q < 63) { // TRANSITION(ulfjack): Use a tighter bound here.
254*0eae32dcSDimitry Andric       // We need to compute min(ntz(__mv), __pow5Factor(__mv) - __e2) >= __q - 1
255*0eae32dcSDimitry Andric       // <=> ntz(__mv) >= __q - 1 && __pow5Factor(__mv) - __e2 >= __q - 1
256*0eae32dcSDimitry Andric       // <=> ntz(__mv) >= __q - 1 (__e2 is negative and -__e2 >= __q)
257*0eae32dcSDimitry Andric       // <=> (__mv & ((1 << (__q - 1)) - 1)) == 0
258*0eae32dcSDimitry Andric       // We also need to make sure that the left shift does not overflow.
259*0eae32dcSDimitry Andric       __vrIsTrailingZeros = __multipleOfPowerOf2(__mv, __q - 1);
260*0eae32dcSDimitry Andric     }
261*0eae32dcSDimitry Andric   }
262*0eae32dcSDimitry Andric 
263*0eae32dcSDimitry Andric   // Step 4: Find the shortest decimal representation in the interval of valid representations.
264*0eae32dcSDimitry Andric   int32_t __removed = 0;
265*0eae32dcSDimitry Andric   uint8_t __lastRemovedDigit = 0;
266*0eae32dcSDimitry Andric   uint64_t _Output;
267*0eae32dcSDimitry Andric   // On average, we remove ~2 digits.
268*0eae32dcSDimitry Andric   if (__vmIsTrailingZeros || __vrIsTrailingZeros) {
269*0eae32dcSDimitry Andric     // General case, which happens rarely (~0.7%).
270*0eae32dcSDimitry Andric     for (;;) {
271*0eae32dcSDimitry Andric       const uint64_t __vpDiv10 = __div10(__vp);
272*0eae32dcSDimitry Andric       const uint64_t __vmDiv10 = __div10(__vm);
273*0eae32dcSDimitry Andric       if (__vpDiv10 <= __vmDiv10) {
274*0eae32dcSDimitry Andric         break;
275*0eae32dcSDimitry Andric       }
276*0eae32dcSDimitry Andric       const uint32_t __vmMod10 = static_cast<uint32_t>(__vm) - 10 * static_cast<uint32_t>(__vmDiv10);
277*0eae32dcSDimitry Andric       const uint64_t __vrDiv10 = __div10(__vr);
278*0eae32dcSDimitry Andric       const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
279*0eae32dcSDimitry Andric       __vmIsTrailingZeros &= __vmMod10 == 0;
280*0eae32dcSDimitry Andric       __vrIsTrailingZeros &= __lastRemovedDigit == 0;
281*0eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__vrMod10);
282*0eae32dcSDimitry Andric       __vr = __vrDiv10;
283*0eae32dcSDimitry Andric       __vp = __vpDiv10;
284*0eae32dcSDimitry Andric       __vm = __vmDiv10;
285*0eae32dcSDimitry Andric       ++__removed;
286*0eae32dcSDimitry Andric     }
287*0eae32dcSDimitry Andric     if (__vmIsTrailingZeros) {
288*0eae32dcSDimitry Andric       for (;;) {
289*0eae32dcSDimitry Andric         const uint64_t __vmDiv10 = __div10(__vm);
290*0eae32dcSDimitry Andric         const uint32_t __vmMod10 = static_cast<uint32_t>(__vm) - 10 * static_cast<uint32_t>(__vmDiv10);
291*0eae32dcSDimitry Andric         if (__vmMod10 != 0) {
292*0eae32dcSDimitry Andric           break;
293*0eae32dcSDimitry Andric         }
294*0eae32dcSDimitry Andric         const uint64_t __vpDiv10 = __div10(__vp);
295*0eae32dcSDimitry Andric         const uint64_t __vrDiv10 = __div10(__vr);
296*0eae32dcSDimitry Andric         const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
297*0eae32dcSDimitry Andric         __vrIsTrailingZeros &= __lastRemovedDigit == 0;
298*0eae32dcSDimitry Andric         __lastRemovedDigit = static_cast<uint8_t>(__vrMod10);
299*0eae32dcSDimitry Andric         __vr = __vrDiv10;
300*0eae32dcSDimitry Andric         __vp = __vpDiv10;
301*0eae32dcSDimitry Andric         __vm = __vmDiv10;
302*0eae32dcSDimitry Andric         ++__removed;
303*0eae32dcSDimitry Andric       }
304*0eae32dcSDimitry Andric     }
305*0eae32dcSDimitry Andric     if (__vrIsTrailingZeros && __lastRemovedDigit == 5 && __vr % 2 == 0) {
306*0eae32dcSDimitry Andric       // Round even if the exact number is .....50..0.
307*0eae32dcSDimitry Andric       __lastRemovedDigit = 4;
308*0eae32dcSDimitry Andric     }
309*0eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
310*0eae32dcSDimitry Andric     _Output = __vr + ((__vr == __vm && (!__acceptBounds || !__vmIsTrailingZeros)) || __lastRemovedDigit >= 5);
311*0eae32dcSDimitry Andric   } else {
312*0eae32dcSDimitry Andric     // Specialized for the common case (~99.3%). Percentages below are relative to this.
313*0eae32dcSDimitry Andric     bool __roundUp = false;
314*0eae32dcSDimitry Andric     const uint64_t __vpDiv100 = __div100(__vp);
315*0eae32dcSDimitry Andric     const uint64_t __vmDiv100 = __div100(__vm);
316*0eae32dcSDimitry Andric     if (__vpDiv100 > __vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
317*0eae32dcSDimitry Andric       const uint64_t __vrDiv100 = __div100(__vr);
318*0eae32dcSDimitry Andric       const uint32_t __vrMod100 = static_cast<uint32_t>(__vr) - 100 * static_cast<uint32_t>(__vrDiv100);
319*0eae32dcSDimitry Andric       __roundUp = __vrMod100 >= 50;
320*0eae32dcSDimitry Andric       __vr = __vrDiv100;
321*0eae32dcSDimitry Andric       __vp = __vpDiv100;
322*0eae32dcSDimitry Andric       __vm = __vmDiv100;
323*0eae32dcSDimitry Andric       __removed += 2;
324*0eae32dcSDimitry Andric     }
325*0eae32dcSDimitry Andric     // Loop iterations below (approximately), without optimization above:
326*0eae32dcSDimitry Andric     // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
327*0eae32dcSDimitry Andric     // Loop iterations below (approximately), with optimization above:
328*0eae32dcSDimitry Andric     // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
329*0eae32dcSDimitry Andric     for (;;) {
330*0eae32dcSDimitry Andric       const uint64_t __vpDiv10 = __div10(__vp);
331*0eae32dcSDimitry Andric       const uint64_t __vmDiv10 = __div10(__vm);
332*0eae32dcSDimitry Andric       if (__vpDiv10 <= __vmDiv10) {
333*0eae32dcSDimitry Andric         break;
334*0eae32dcSDimitry Andric       }
335*0eae32dcSDimitry Andric       const uint64_t __vrDiv10 = __div10(__vr);
336*0eae32dcSDimitry Andric       const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
337*0eae32dcSDimitry Andric       __roundUp = __vrMod10 >= 5;
338*0eae32dcSDimitry Andric       __vr = __vrDiv10;
339*0eae32dcSDimitry Andric       __vp = __vpDiv10;
340*0eae32dcSDimitry Andric       __vm = __vmDiv10;
341*0eae32dcSDimitry Andric       ++__removed;
342*0eae32dcSDimitry Andric     }
343*0eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
344*0eae32dcSDimitry Andric     _Output = __vr + (__vr == __vm || __roundUp);
345*0eae32dcSDimitry Andric   }
346*0eae32dcSDimitry Andric   const int32_t __exp = __e10 + __removed;
347*0eae32dcSDimitry Andric 
348*0eae32dcSDimitry Andric   __floating_decimal_64 __fd;
349*0eae32dcSDimitry Andric   __fd.__exponent = __exp;
350*0eae32dcSDimitry Andric   __fd.__mantissa = _Output;
351*0eae32dcSDimitry Andric   return __fd;
352*0eae32dcSDimitry Andric }
353*0eae32dcSDimitry Andric 
354*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result __to_chars(char* const _First, char* const _Last, const __floating_decimal_64 __v,
355*0eae32dcSDimitry Andric   chars_format _Fmt, const double __f) {
356*0eae32dcSDimitry Andric   // Step 5: Print the decimal representation.
357*0eae32dcSDimitry Andric   uint64_t _Output = __v.__mantissa;
358*0eae32dcSDimitry Andric   int32_t _Ryu_exponent = __v.__exponent;
359*0eae32dcSDimitry Andric   const uint32_t __olength = __decimalLength17(_Output);
360*0eae32dcSDimitry Andric   int32_t _Scientific_exponent = _Ryu_exponent + static_cast<int32_t>(__olength) - 1;
361*0eae32dcSDimitry Andric 
362*0eae32dcSDimitry Andric   if (_Fmt == chars_format{}) {
363*0eae32dcSDimitry Andric     int32_t _Lower;
364*0eae32dcSDimitry Andric     int32_t _Upper;
365*0eae32dcSDimitry Andric 
366*0eae32dcSDimitry Andric     if (__olength == 1) {
367*0eae32dcSDimitry Andric       // Value | Fixed   | Scientific
368*0eae32dcSDimitry Andric       // 1e-3  | "0.001" | "1e-03"
369*0eae32dcSDimitry Andric       // 1e4   | "10000" | "1e+04"
370*0eae32dcSDimitry Andric       _Lower = -3;
371*0eae32dcSDimitry Andric       _Upper = 4;
372*0eae32dcSDimitry Andric     } else {
373*0eae32dcSDimitry Andric       // Value   | Fixed       | Scientific
374*0eae32dcSDimitry Andric       // 1234e-7 | "0.0001234" | "1.234e-04"
375*0eae32dcSDimitry Andric       // 1234e5  | "123400000" | "1.234e+08"
376*0eae32dcSDimitry Andric       _Lower = -static_cast<int32_t>(__olength + 3);
377*0eae32dcSDimitry Andric       _Upper = 5;
378*0eae32dcSDimitry Andric     }
379*0eae32dcSDimitry Andric 
380*0eae32dcSDimitry Andric     if (_Lower <= _Ryu_exponent && _Ryu_exponent <= _Upper) {
381*0eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
382*0eae32dcSDimitry Andric     } else {
383*0eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
384*0eae32dcSDimitry Andric     }
385*0eae32dcSDimitry Andric   } else if (_Fmt == chars_format::general) {
386*0eae32dcSDimitry Andric     // C11 7.21.6.1 "The fprintf function"/8:
387*0eae32dcSDimitry Andric     // "Let P equal [...] 6 if the precision is omitted [...].
388*0eae32dcSDimitry Andric     // Then, if a conversion with style E would have an exponent of X:
389*0eae32dcSDimitry Andric     // - if P > X >= -4, the conversion is with style f [...].
390*0eae32dcSDimitry Andric     // - otherwise, the conversion is with style e [...]."
391*0eae32dcSDimitry Andric     if (-4 <= _Scientific_exponent && _Scientific_exponent < 6) {
392*0eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
393*0eae32dcSDimitry Andric     } else {
394*0eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
395*0eae32dcSDimitry Andric     }
396*0eae32dcSDimitry Andric   }
397*0eae32dcSDimitry Andric 
398*0eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
399*0eae32dcSDimitry Andric     // Example: _Output == 1729, __olength == 4
400*0eae32dcSDimitry Andric 
401*0eae32dcSDimitry Andric     // _Ryu_exponent | Printed  | _Whole_digits | _Total_fixed_length  | Notes
402*0eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
403*0eae32dcSDimitry Andric     //             2 | 172900   |  6            | _Whole_digits        | Ryu can't be used for printing
404*0eae32dcSDimitry Andric     //             1 | 17290    |  5            | (sometimes adjusted) | when the trimmed digits are nonzero.
405*0eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
406*0eae32dcSDimitry Andric     //             0 | 1729     |  4            | _Whole_digits        | Unified length cases.
407*0eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
408*0eae32dcSDimitry Andric     //            -1 | 172.9    |  3            | __olength + 1        | This case can't happen for
409*0eae32dcSDimitry Andric     //            -2 | 17.29    |  2            |                      | __olength == 1, but no additional
410*0eae32dcSDimitry Andric     //            -3 | 1.729    |  1            |                      | code is needed to avoid it.
411*0eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
412*0eae32dcSDimitry Andric     //            -4 | 0.1729   |  0            | 2 - _Ryu_exponent    | C11 7.21.6.1 "The fprintf function"/8:
413*0eae32dcSDimitry Andric     //            -5 | 0.01729  | -1            |                      | "If a decimal-point character appears,
414*0eae32dcSDimitry Andric     //            -6 | 0.001729 | -2            |                      | at least one digit appears before it."
415*0eae32dcSDimitry Andric 
416*0eae32dcSDimitry Andric     const int32_t _Whole_digits = static_cast<int32_t>(__olength) + _Ryu_exponent;
417*0eae32dcSDimitry Andric 
418*0eae32dcSDimitry Andric     uint32_t _Total_fixed_length;
419*0eae32dcSDimitry Andric     if (_Ryu_exponent >= 0) { // cases "172900" and "1729"
420*0eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(_Whole_digits);
421*0eae32dcSDimitry Andric       if (_Output == 1) {
422*0eae32dcSDimitry Andric         // Rounding can affect the number of digits.
423*0eae32dcSDimitry Andric         // For example, 1e23 is exactly "99999999999999991611392" which is 23 digits instead of 24.
424*0eae32dcSDimitry Andric         // We can use a lookup table to detect this and adjust the total length.
425*0eae32dcSDimitry Andric         static constexpr uint8_t _Adjustment[309] = {
426*0eae32dcSDimitry Andric           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,1,0,1,1,1,0,1,1,1,0,0,0,0,0,
427*0eae32dcSDimitry Andric           1,1,0,0,1,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,1,0,1,0,1,1,0,0,0,0,1,1,1,
428*0eae32dcSDimitry Andric           1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,1,0,1,1,0,1,
429*0eae32dcSDimitry Andric           1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,1,0,0,1,1,1,1,0,0,1,1,0,1,1,0,1,1,0,1,0,0,0,1,0,0,0,1,
430*0eae32dcSDimitry Andric           0,1,0,1,0,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1,0,1,1,1,0,0,0,1,0,1,1,1,1,1,1,0,1,0,1,1,0,0,0,1,
431*0eae32dcSDimitry Andric           1,1,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,1,0,0,1,1,1,0,0,0,1,0,1,0,0,0,0,0,1,1,0,
432*0eae32dcSDimitry Andric           0,1,0,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,1,0,1,0,0,0,0,0,1,1,0,1,0 };
433*0eae32dcSDimitry Andric         _Total_fixed_length -= _Adjustment[_Ryu_exponent];
434*0eae32dcSDimitry Andric         // _Whole_digits doesn't need to be adjusted because these cases won't refer to it later.
435*0eae32dcSDimitry Andric       }
436*0eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
437*0eae32dcSDimitry Andric       _Total_fixed_length = __olength + 1;
438*0eae32dcSDimitry Andric     } else { // case "0.001729"
439*0eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(2 - _Ryu_exponent);
440*0eae32dcSDimitry Andric     }
441*0eae32dcSDimitry Andric 
442*0eae32dcSDimitry Andric     if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
443*0eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
444*0eae32dcSDimitry Andric     }
445*0eae32dcSDimitry Andric 
446*0eae32dcSDimitry Andric     char* _Mid;
447*0eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900"
448*0eae32dcSDimitry Andric       bool _Can_use_ryu;
449*0eae32dcSDimitry Andric 
450*0eae32dcSDimitry Andric       if (_Ryu_exponent > 22) { // 10^22 is the largest power of 10 that's exactly representable as a double.
451*0eae32dcSDimitry Andric         _Can_use_ryu = false;
452*0eae32dcSDimitry Andric       } else {
453*0eae32dcSDimitry Andric         // Ryu generated X: __v.__mantissa * 10^_Ryu_exponent
454*0eae32dcSDimitry Andric         // __v.__mantissa == 2^_Trailing_zero_bits * (__v.__mantissa >> _Trailing_zero_bits)
455*0eae32dcSDimitry Andric         // 10^_Ryu_exponent == 2^_Ryu_exponent * 5^_Ryu_exponent
456*0eae32dcSDimitry Andric 
457*0eae32dcSDimitry Andric         // _Trailing_zero_bits is [0, 56] (aside: because 2^56 is the largest power of 2
458*0eae32dcSDimitry Andric         // with 17 decimal digits, which is double's round-trip limit.)
459*0eae32dcSDimitry Andric         // _Ryu_exponent is [1, 22].
460*0eae32dcSDimitry Andric         // Normalization adds [2, 52] (aside: at least 2 because the pre-normalized mantissa is at least 5).
461*0eae32dcSDimitry Andric         // This adds up to [3, 130], which is well below double's maximum binary exponent 1023.
462*0eae32dcSDimitry Andric 
463*0eae32dcSDimitry Andric         // Therefore, we just need to consider (__v.__mantissa >> _Trailing_zero_bits) * 5^_Ryu_exponent.
464*0eae32dcSDimitry Andric 
465*0eae32dcSDimitry Andric         // If that product would exceed 53 bits, then X can't be exactly represented as a double.
466*0eae32dcSDimitry Andric         // (That's not a problem for round-tripping, because X is close enough to the original double,
467*0eae32dcSDimitry Andric         // but X isn't mathematically equal to the original double.) This requires a high-precision fallback.
468*0eae32dcSDimitry Andric 
469*0eae32dcSDimitry Andric         // If the product is 53 bits or smaller, then X can be exactly represented as a double (and we don't
470*0eae32dcSDimitry Andric         // need to re-synthesize it; the original double must have been X, because Ryu wouldn't produce the
471*0eae32dcSDimitry Andric         // same output for two different doubles X and Y). This allows Ryu's output to be used (zero-filled).
472*0eae32dcSDimitry Andric 
473*0eae32dcSDimitry Andric         // (2^53 - 1) / 5^0 (for indexing), (2^53 - 1) / 5^1, ..., (2^53 - 1) / 5^22
474*0eae32dcSDimitry Andric         static constexpr uint64_t _Max_shifted_mantissa[23] = {
475*0eae32dcSDimitry Andric           9007199254740991u, 1801439850948198u, 360287970189639u, 72057594037927u, 14411518807585u,
476*0eae32dcSDimitry Andric           2882303761517u, 576460752303u, 115292150460u, 23058430092u, 4611686018u, 922337203u, 184467440u,
477*0eae32dcSDimitry Andric           36893488u, 7378697u, 1475739u, 295147u, 59029u, 11805u, 2361u, 472u, 94u, 18u, 3u };
478*0eae32dcSDimitry Andric 
479*0eae32dcSDimitry Andric         unsigned long _Trailing_zero_bits;
480*0eae32dcSDimitry Andric #ifdef _LIBCPP_HAS_BITSCAN64
481*0eae32dcSDimitry Andric         (void) _BitScanForward64(&_Trailing_zero_bits, __v.__mantissa); // __v.__mantissa is guaranteed nonzero
482*0eae32dcSDimitry Andric #else // ^^^ 64-bit ^^^ / vvv 32-bit vvv
483*0eae32dcSDimitry Andric         const uint32_t _Low_mantissa = static_cast<uint32_t>(__v.__mantissa);
484*0eae32dcSDimitry Andric         if (_Low_mantissa != 0) {
485*0eae32dcSDimitry Andric           (void) _BitScanForward(&_Trailing_zero_bits, _Low_mantissa);
486*0eae32dcSDimitry Andric         } else {
487*0eae32dcSDimitry Andric           const uint32_t _High_mantissa = static_cast<uint32_t>(__v.__mantissa >> 32); // nonzero here
488*0eae32dcSDimitry Andric           (void) _BitScanForward(&_Trailing_zero_bits, _High_mantissa);
489*0eae32dcSDimitry Andric           _Trailing_zero_bits += 32;
490*0eae32dcSDimitry Andric         }
491*0eae32dcSDimitry Andric #endif // ^^^ 32-bit ^^^
492*0eae32dcSDimitry Andric         const uint64_t _Shifted_mantissa = __v.__mantissa >> _Trailing_zero_bits;
493*0eae32dcSDimitry Andric         _Can_use_ryu = _Shifted_mantissa <= _Max_shifted_mantissa[_Ryu_exponent];
494*0eae32dcSDimitry Andric       }
495*0eae32dcSDimitry Andric 
496*0eae32dcSDimitry Andric       if (!_Can_use_ryu) {
497*0eae32dcSDimitry Andric         // Print the integer exactly.
498*0eae32dcSDimitry Andric         // Performance note: This will redundantly perform bounds checking.
499*0eae32dcSDimitry Andric         // Performance note: This will redundantly decompose the IEEE representation.
500*0eae32dcSDimitry Andric         return __d2fixed_buffered_n(_First, _Last, __f, 0);
501*0eae32dcSDimitry Andric       }
502*0eae32dcSDimitry Andric 
503*0eae32dcSDimitry Andric       // _Can_use_ryu
504*0eae32dcSDimitry Andric       // Print the decimal digits, left-aligned within [_First, _First + _Total_fixed_length).
505*0eae32dcSDimitry Andric       _Mid = _First + __olength;
506*0eae32dcSDimitry Andric     } else { // cases "1729", "17.29", and "0.001729"
507*0eae32dcSDimitry Andric       // Print the decimal digits, right-aligned within [_First, _First + _Total_fixed_length).
508*0eae32dcSDimitry Andric       _Mid = _First + _Total_fixed_length;
509*0eae32dcSDimitry Andric     }
510*0eae32dcSDimitry Andric 
511*0eae32dcSDimitry Andric     // We prefer 32-bit operations, even on 64-bit platforms.
512*0eae32dcSDimitry Andric     // We have at most 17 digits, and uint32_t can store 9 digits.
513*0eae32dcSDimitry Andric     // If _Output doesn't fit into uint32_t, we cut off 8 digits,
514*0eae32dcSDimitry Andric     // so the rest will fit into uint32_t.
515*0eae32dcSDimitry Andric     if ((_Output >> 32) != 0) {
516*0eae32dcSDimitry Andric       // Expensive 64-bit division.
517*0eae32dcSDimitry Andric       const uint64_t __q = __div1e8(_Output);
518*0eae32dcSDimitry Andric       uint32_t __output2 = static_cast<uint32_t>(_Output - 100000000 * __q);
519*0eae32dcSDimitry Andric       _Output = __q;
520*0eae32dcSDimitry Andric 
521*0eae32dcSDimitry Andric       const uint32_t __c = __output2 % 10000;
522*0eae32dcSDimitry Andric       __output2 /= 10000;
523*0eae32dcSDimitry Andric       const uint32_t __d = __output2 % 10000;
524*0eae32dcSDimitry Andric       const uint32_t __c0 = (__c % 100) << 1;
525*0eae32dcSDimitry Andric       const uint32_t __c1 = (__c / 100) << 1;
526*0eae32dcSDimitry Andric       const uint32_t __d0 = (__d % 100) << 1;
527*0eae32dcSDimitry Andric       const uint32_t __d1 = (__d / 100) << 1;
528*0eae32dcSDimitry Andric 
529*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
530*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
531*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __d0, 2);
532*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __d1, 2);
533*0eae32dcSDimitry Andric     }
534*0eae32dcSDimitry Andric     uint32_t __output2 = static_cast<uint32_t>(_Output);
535*0eae32dcSDimitry Andric     while (__output2 >= 10000) {
536*0eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
537*0eae32dcSDimitry Andric       const uint32_t __c = __output2 - 10000 * (__output2 / 10000);
538*0eae32dcSDimitry Andric #else
539*0eae32dcSDimitry Andric       const uint32_t __c = __output2 % 10000;
540*0eae32dcSDimitry Andric #endif
541*0eae32dcSDimitry Andric       __output2 /= 10000;
542*0eae32dcSDimitry Andric       const uint32_t __c0 = (__c % 100) << 1;
543*0eae32dcSDimitry Andric       const uint32_t __c1 = (__c / 100) << 1;
544*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
545*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
546*0eae32dcSDimitry Andric     }
547*0eae32dcSDimitry Andric     if (__output2 >= 100) {
548*0eae32dcSDimitry Andric       const uint32_t __c = (__output2 % 100) << 1;
549*0eae32dcSDimitry Andric       __output2 /= 100;
550*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
551*0eae32dcSDimitry Andric     }
552*0eae32dcSDimitry Andric     if (__output2 >= 10) {
553*0eae32dcSDimitry Andric       const uint32_t __c = __output2 << 1;
554*0eae32dcSDimitry Andric       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
555*0eae32dcSDimitry Andric     } else {
556*0eae32dcSDimitry Andric       *--_Mid = static_cast<char>('0' + __output2);
557*0eae32dcSDimitry Andric     }
558*0eae32dcSDimitry Andric 
559*0eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900" with _Can_use_ryu
560*0eae32dcSDimitry Andric       // Performance note: it might be more efficient to do this immediately after setting _Mid.
561*0eae32dcSDimitry Andric       _VSTD::memset(_First + __olength, '0', static_cast<size_t>(_Ryu_exponent));
562*0eae32dcSDimitry Andric     } else if (_Ryu_exponent == 0) { // case "1729"
563*0eae32dcSDimitry Andric       // Done!
564*0eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
565*0eae32dcSDimitry Andric       // Performance note: moving digits might not be optimal.
566*0eae32dcSDimitry Andric       _VSTD::memmove(_First, _First + 1, static_cast<size_t>(_Whole_digits));
567*0eae32dcSDimitry Andric       _First[_Whole_digits] = '.';
568*0eae32dcSDimitry Andric     } else { // case "0.001729"
569*0eae32dcSDimitry Andric       // Performance note: a larger memset() followed by overwriting '.' might be more efficient.
570*0eae32dcSDimitry Andric       _First[0] = '0';
571*0eae32dcSDimitry Andric       _First[1] = '.';
572*0eae32dcSDimitry Andric       _VSTD::memset(_First + 2, '0', static_cast<size_t>(-_Whole_digits));
573*0eae32dcSDimitry Andric     }
574*0eae32dcSDimitry Andric 
575*0eae32dcSDimitry Andric     return { _First + _Total_fixed_length, errc{} };
576*0eae32dcSDimitry Andric   }
577*0eae32dcSDimitry Andric 
578*0eae32dcSDimitry Andric   const uint32_t _Total_scientific_length = __olength + (__olength > 1) // digits + possible decimal point
579*0eae32dcSDimitry Andric     + (-100 < _Scientific_exponent && _Scientific_exponent < 100 ? 4 : 5); // + scientific exponent
580*0eae32dcSDimitry Andric   if (_Last - _First < static_cast<ptrdiff_t>(_Total_scientific_length)) {
581*0eae32dcSDimitry Andric     return { _Last, errc::value_too_large };
582*0eae32dcSDimitry Andric   }
583*0eae32dcSDimitry Andric   char* const __result = _First;
584*0eae32dcSDimitry Andric 
585*0eae32dcSDimitry Andric   // Print the decimal digits.
586*0eae32dcSDimitry Andric   uint32_t __i = 0;
587*0eae32dcSDimitry Andric   // We prefer 32-bit operations, even on 64-bit platforms.
588*0eae32dcSDimitry Andric   // We have at most 17 digits, and uint32_t can store 9 digits.
589*0eae32dcSDimitry Andric   // If _Output doesn't fit into uint32_t, we cut off 8 digits,
590*0eae32dcSDimitry Andric   // so the rest will fit into uint32_t.
591*0eae32dcSDimitry Andric   if ((_Output >> 32) != 0) {
592*0eae32dcSDimitry Andric     // Expensive 64-bit division.
593*0eae32dcSDimitry Andric     const uint64_t __q = __div1e8(_Output);
594*0eae32dcSDimitry Andric     uint32_t __output2 = static_cast<uint32_t>(_Output) - 100000000 * static_cast<uint32_t>(__q);
595*0eae32dcSDimitry Andric     _Output = __q;
596*0eae32dcSDimitry Andric 
597*0eae32dcSDimitry Andric     const uint32_t __c = __output2 % 10000;
598*0eae32dcSDimitry Andric     __output2 /= 10000;
599*0eae32dcSDimitry Andric     const uint32_t __d = __output2 % 10000;
600*0eae32dcSDimitry Andric     const uint32_t __c0 = (__c % 100) << 1;
601*0eae32dcSDimitry Andric     const uint32_t __c1 = (__c / 100) << 1;
602*0eae32dcSDimitry Andric     const uint32_t __d0 = (__d % 100) << 1;
603*0eae32dcSDimitry Andric     const uint32_t __d1 = (__d / 100) << 1;
604*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
605*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
606*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 5, __DIGIT_TABLE + __d0, 2);
607*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 7, __DIGIT_TABLE + __d1, 2);
608*0eae32dcSDimitry Andric     __i += 8;
609*0eae32dcSDimitry Andric   }
610*0eae32dcSDimitry Andric   uint32_t __output2 = static_cast<uint32_t>(_Output);
611*0eae32dcSDimitry Andric   while (__output2 >= 10000) {
612*0eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
613*0eae32dcSDimitry Andric     const uint32_t __c = __output2 - 10000 * (__output2 / 10000);
614*0eae32dcSDimitry Andric #else
615*0eae32dcSDimitry Andric     const uint32_t __c = __output2 % 10000;
616*0eae32dcSDimitry Andric #endif
617*0eae32dcSDimitry Andric     __output2 /= 10000;
618*0eae32dcSDimitry Andric     const uint32_t __c0 = (__c % 100) << 1;
619*0eae32dcSDimitry Andric     const uint32_t __c1 = (__c / 100) << 1;
620*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
621*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
622*0eae32dcSDimitry Andric     __i += 4;
623*0eae32dcSDimitry Andric   }
624*0eae32dcSDimitry Andric   if (__output2 >= 100) {
625*0eae32dcSDimitry Andric     const uint32_t __c = (__output2 % 100) << 1;
626*0eae32dcSDimitry Andric     __output2 /= 100;
627*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c, 2);
628*0eae32dcSDimitry Andric     __i += 2;
629*0eae32dcSDimitry Andric   }
630*0eae32dcSDimitry Andric   if (__output2 >= 10) {
631*0eae32dcSDimitry Andric     const uint32_t __c = __output2 << 1;
632*0eae32dcSDimitry Andric     // We can't use memcpy here: the decimal dot goes between these two digits.
633*0eae32dcSDimitry Andric     __result[2] = __DIGIT_TABLE[__c + 1];
634*0eae32dcSDimitry Andric     __result[0] = __DIGIT_TABLE[__c];
635*0eae32dcSDimitry Andric   } else {
636*0eae32dcSDimitry Andric     __result[0] = static_cast<char>('0' + __output2);
637*0eae32dcSDimitry Andric   }
638*0eae32dcSDimitry Andric 
639*0eae32dcSDimitry Andric   // Print decimal point if needed.
640*0eae32dcSDimitry Andric   uint32_t __index;
641*0eae32dcSDimitry Andric   if (__olength > 1) {
642*0eae32dcSDimitry Andric     __result[1] = '.';
643*0eae32dcSDimitry Andric     __index = __olength + 1;
644*0eae32dcSDimitry Andric   } else {
645*0eae32dcSDimitry Andric     __index = 1;
646*0eae32dcSDimitry Andric   }
647*0eae32dcSDimitry Andric 
648*0eae32dcSDimitry Andric   // Print the exponent.
649*0eae32dcSDimitry Andric   __result[__index++] = 'e';
650*0eae32dcSDimitry Andric   if (_Scientific_exponent < 0) {
651*0eae32dcSDimitry Andric     __result[__index++] = '-';
652*0eae32dcSDimitry Andric     _Scientific_exponent = -_Scientific_exponent;
653*0eae32dcSDimitry Andric   } else {
654*0eae32dcSDimitry Andric     __result[__index++] = '+';
655*0eae32dcSDimitry Andric   }
656*0eae32dcSDimitry Andric 
657*0eae32dcSDimitry Andric   if (_Scientific_exponent >= 100) {
658*0eae32dcSDimitry Andric     const int32_t __c = _Scientific_exponent % 10;
659*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __index, __DIGIT_TABLE + 2 * (_Scientific_exponent / 10), 2);
660*0eae32dcSDimitry Andric     __result[__index + 2] = static_cast<char>('0' + __c);
661*0eae32dcSDimitry Andric     __index += 3;
662*0eae32dcSDimitry Andric   } else {
663*0eae32dcSDimitry Andric     _VSTD::memcpy(__result + __index, __DIGIT_TABLE + 2 * _Scientific_exponent, 2);
664*0eae32dcSDimitry Andric     __index += 2;
665*0eae32dcSDimitry Andric   }
666*0eae32dcSDimitry Andric 
667*0eae32dcSDimitry Andric   return { _First + _Total_scientific_length, errc{} };
668*0eae32dcSDimitry Andric }
669*0eae32dcSDimitry Andric 
670*0eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __d2d_small_int(const uint64_t __ieeeMantissa, const uint32_t __ieeeExponent,
671*0eae32dcSDimitry Andric   __floating_decimal_64* const __v) {
672*0eae32dcSDimitry Andric   const uint64_t __m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
673*0eae32dcSDimitry Andric   const int32_t __e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
674*0eae32dcSDimitry Andric 
675*0eae32dcSDimitry Andric   if (__e2 > 0) {
676*0eae32dcSDimitry Andric     // f = __m2 * 2^__e2 >= 2^53 is an integer.
677*0eae32dcSDimitry Andric     // Ignore this case for now.
678*0eae32dcSDimitry Andric     return false;
679*0eae32dcSDimitry Andric   }
680*0eae32dcSDimitry Andric 
681*0eae32dcSDimitry Andric   if (__e2 < -52) {
682*0eae32dcSDimitry Andric     // f < 1.
683*0eae32dcSDimitry Andric     return false;
684*0eae32dcSDimitry Andric   }
685*0eae32dcSDimitry Andric 
686*0eae32dcSDimitry Andric   // Since 2^52 <= __m2 < 2^53 and 0 <= -__e2 <= 52: 1 <= f = __m2 / 2^-__e2 < 2^53.
687*0eae32dcSDimitry Andric   // Test if the lower -__e2 bits of the significand are 0, i.e. whether the fraction is 0.
688*0eae32dcSDimitry Andric   const uint64_t __mask = (1ull << -__e2) - 1;
689*0eae32dcSDimitry Andric   const uint64_t __fraction = __m2 & __mask;
690*0eae32dcSDimitry Andric   if (__fraction != 0) {
691*0eae32dcSDimitry Andric     return false;
692*0eae32dcSDimitry Andric   }
693*0eae32dcSDimitry Andric 
694*0eae32dcSDimitry Andric   // f is an integer in the range [1, 2^53).
695*0eae32dcSDimitry Andric   // Note: __mantissa might contain trailing (decimal) 0's.
696*0eae32dcSDimitry Andric   // Note: since 2^53 < 10^16, there is no need to adjust __decimalLength17().
697*0eae32dcSDimitry Andric   __v->__mantissa = __m2 >> -__e2;
698*0eae32dcSDimitry Andric   __v->__exponent = 0;
699*0eae32dcSDimitry Andric   return true;
700*0eae32dcSDimitry Andric }
701*0eae32dcSDimitry Andric 
702*0eae32dcSDimitry Andric [[nodiscard]] to_chars_result __d2s_buffered_n(char* const _First, char* const _Last, const double __f,
703*0eae32dcSDimitry Andric   const chars_format _Fmt) {
704*0eae32dcSDimitry Andric 
705*0eae32dcSDimitry Andric   // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
706*0eae32dcSDimitry Andric   const uint64_t __bits = __double_to_bits(__f);
707*0eae32dcSDimitry Andric 
708*0eae32dcSDimitry Andric   // Case distinction; exit early for the easy cases.
709*0eae32dcSDimitry Andric   if (__bits == 0) {
710*0eae32dcSDimitry Andric     if (_Fmt == chars_format::scientific) {
711*0eae32dcSDimitry Andric       if (_Last - _First < 5) {
712*0eae32dcSDimitry Andric         return { _Last, errc::value_too_large };
713*0eae32dcSDimitry Andric       }
714*0eae32dcSDimitry Andric 
715*0eae32dcSDimitry Andric       _VSTD::memcpy(_First, "0e+00", 5);
716*0eae32dcSDimitry Andric 
717*0eae32dcSDimitry Andric       return { _First + 5, errc{} };
718*0eae32dcSDimitry Andric     }
719*0eae32dcSDimitry Andric 
720*0eae32dcSDimitry Andric     // Print "0" for chars_format::fixed, chars_format::general, and chars_format{}.
721*0eae32dcSDimitry Andric     if (_First == _Last) {
722*0eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
723*0eae32dcSDimitry Andric     }
724*0eae32dcSDimitry Andric 
725*0eae32dcSDimitry Andric     *_First = '0';
726*0eae32dcSDimitry Andric 
727*0eae32dcSDimitry Andric     return { _First + 1, errc{} };
728*0eae32dcSDimitry Andric   }
729*0eae32dcSDimitry Andric 
730*0eae32dcSDimitry Andric   // Decode __bits into mantissa and exponent.
731*0eae32dcSDimitry Andric   const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);
732*0eae32dcSDimitry Andric   const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);
733*0eae32dcSDimitry Andric 
734*0eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
735*0eae32dcSDimitry Andric     // const uint64_t _Mantissa2 = __ieeeMantissa | (1ull << __DOUBLE_MANTISSA_BITS); // restore implicit bit
736*0eae32dcSDimitry Andric     const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
737*0eae32dcSDimitry Andric       - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS; // bias and normalization
738*0eae32dcSDimitry Andric 
739*0eae32dcSDimitry Andric     // Normal values are equal to _Mantissa2 * 2^_Exponent2.
740*0eae32dcSDimitry Andric     // (Subnormals are different, but they'll be rejected by the _Exponent2 test here, so they can be ignored.)
741*0eae32dcSDimitry Andric 
742*0eae32dcSDimitry Andric     // For nonzero integers, _Exponent2 >= -52. (The minimum value occurs when _Mantissa2 * 2^_Exponent2 is 1.
743*0eae32dcSDimitry Andric     // In that case, _Mantissa2 is the implicit 1 bit followed by 52 zeros, so _Exponent2 is -52 to shift away
744*0eae32dcSDimitry Andric     // the zeros.) The dense range of exactly representable integers has negative or zero exponents
745*0eae32dcSDimitry Andric     // (as positive exponents make the range non-dense). For that dense range, Ryu will always be used:
746*0eae32dcSDimitry Andric     // every digit is necessary to uniquely identify the value, so Ryu must print them all.
747*0eae32dcSDimitry Andric 
748*0eae32dcSDimitry Andric     // Positive exponents are the non-dense range of exactly representable integers. This contains all of the values
749*0eae32dcSDimitry Andric     // for which Ryu can't be used (and a few Ryu-friendly values). We can save time by detecting positive
750*0eae32dcSDimitry Andric     // exponents here and skipping Ryu. Calling __d2fixed_buffered_n() with precision 0 is valid for all integers
751*0eae32dcSDimitry Andric     // (so it's okay if we call it with a Ryu-friendly value).
752*0eae32dcSDimitry Andric     if (_Exponent2 > 0) {
753*0eae32dcSDimitry Andric       return __d2fixed_buffered_n(_First, _Last, __f, 0);
754*0eae32dcSDimitry Andric     }
755*0eae32dcSDimitry Andric   }
756*0eae32dcSDimitry Andric 
757*0eae32dcSDimitry Andric   __floating_decimal_64 __v;
758*0eae32dcSDimitry Andric   const bool __isSmallInt = __d2d_small_int(__ieeeMantissa, __ieeeExponent, &__v);
759*0eae32dcSDimitry Andric   if (__isSmallInt) {
760*0eae32dcSDimitry Andric     // For small integers in the range [1, 2^53), __v.__mantissa might contain trailing (decimal) zeros.
761*0eae32dcSDimitry Andric     // For scientific notation we need to move these zeros into the exponent.
762*0eae32dcSDimitry Andric     // (This is not needed for fixed-point notation, so it might be beneficial to trim
763*0eae32dcSDimitry Andric     // trailing zeros in __to_chars only if needed - once fixed-point notation output is implemented.)
764*0eae32dcSDimitry Andric     for (;;) {
765*0eae32dcSDimitry Andric       const uint64_t __q = __div10(__v.__mantissa);
766*0eae32dcSDimitry Andric       const uint32_t __r = static_cast<uint32_t>(__v.__mantissa) - 10 * static_cast<uint32_t>(__q);
767*0eae32dcSDimitry Andric       if (__r != 0) {
768*0eae32dcSDimitry Andric         break;
769*0eae32dcSDimitry Andric       }
770*0eae32dcSDimitry Andric       __v.__mantissa = __q;
771*0eae32dcSDimitry Andric       ++__v.__exponent;
772*0eae32dcSDimitry Andric     }
773*0eae32dcSDimitry Andric   } else {
774*0eae32dcSDimitry Andric     __v = __d2d(__ieeeMantissa, __ieeeExponent);
775*0eae32dcSDimitry Andric   }
776*0eae32dcSDimitry Andric 
777*0eae32dcSDimitry Andric   return __to_chars(_First, _Last, __v, _Fmt, __f);
778*0eae32dcSDimitry Andric }
779*0eae32dcSDimitry Andric 
780*0eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD
781*0eae32dcSDimitry Andric 
782*0eae32dcSDimitry Andric // clang-format on
783