xref: /freebsd/contrib/llvm-project/libcxx/src/ryu/d2s.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
20eae32dcSDimitry Andric //
30eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60eae32dcSDimitry Andric //
70eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
80eae32dcSDimitry Andric 
90eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation.
100eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
110eae32dcSDimitry Andric 
120eae32dcSDimitry Andric // Copyright 2018 Ulf Adams
130eae32dcSDimitry Andric // Copyright (c) Microsoft Corporation. All rights reserved.
140eae32dcSDimitry Andric 
150eae32dcSDimitry Andric // Boost Software License - Version 1.0 - August 17th, 2003
160eae32dcSDimitry Andric 
170eae32dcSDimitry Andric // Permission is hereby granted, free of charge, to any person or organization
180eae32dcSDimitry Andric // obtaining a copy of the software and accompanying documentation covered by
190eae32dcSDimitry Andric // this license (the "Software") to use, reproduce, display, distribute,
200eae32dcSDimitry Andric // execute, and transmit the Software, and to prepare derivative works of the
210eae32dcSDimitry Andric // Software, and to permit third-parties to whom the Software is furnished to
220eae32dcSDimitry Andric // do so, all subject to the following:
230eae32dcSDimitry Andric 
240eae32dcSDimitry Andric // The copyright notices in the Software and this entire statement, including
250eae32dcSDimitry Andric // the above license grant, this restriction and the following disclaimer,
260eae32dcSDimitry Andric // must be included in all copies of the Software, in whole or in part, and
270eae32dcSDimitry Andric // all derivative works of the Software, unless such copies or derivative
280eae32dcSDimitry Andric // works are solely in the form of machine-executable object code generated by
290eae32dcSDimitry Andric // a source language processor.
300eae32dcSDimitry Andric 
310eae32dcSDimitry Andric // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
320eae32dcSDimitry Andric // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
330eae32dcSDimitry Andric // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
340eae32dcSDimitry Andric // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
350eae32dcSDimitry Andric // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
360eae32dcSDimitry Andric // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
370eae32dcSDimitry Andric // DEALINGS IN THE SOFTWARE.
380eae32dcSDimitry Andric 
390eae32dcSDimitry Andric // Avoid formatting to keep the changes with the original code minimal.
400eae32dcSDimitry Andric // clang-format off
410eae32dcSDimitry Andric 
4281ad6265SDimitry Andric #include <__assert>
4381ad6265SDimitry Andric #include <__config>
4481ad6265SDimitry Andric #include <charconv>
450eae32dcSDimitry Andric 
460eae32dcSDimitry Andric #include "include/ryu/common.h"
470eae32dcSDimitry Andric #include "include/ryu/d2fixed.h"
480eae32dcSDimitry Andric #include "include/ryu/d2s.h"
490eae32dcSDimitry Andric #include "include/ryu/d2s_full_table.h"
500eae32dcSDimitry Andric #include "include/ryu/d2s_intrinsics.h"
510eae32dcSDimitry Andric #include "include/ryu/digit_table.h"
520eae32dcSDimitry Andric #include "include/ryu/ryu.h"
530eae32dcSDimitry Andric 
540eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
550eae32dcSDimitry Andric 
560eae32dcSDimitry Andric // We need a 64x128-bit multiplication and a subsequent 128-bit shift.
570eae32dcSDimitry Andric // Multiplication:
580eae32dcSDimitry Andric //   The 64-bit factor is variable and passed in, the 128-bit factor comes
590eae32dcSDimitry Andric //   from a lookup table. We know that the 64-bit factor only has 55
600eae32dcSDimitry Andric //   significant bits (i.e., the 9 topmost bits are zeros). The 128-bit
610eae32dcSDimitry Andric //   factor only has 124 significant bits (i.e., the 4 topmost bits are
620eae32dcSDimitry Andric //   zeros).
630eae32dcSDimitry Andric // Shift:
640eae32dcSDimitry Andric //   In principle, the multiplication result requires 55 + 124 = 179 bits to
650eae32dcSDimitry Andric //   represent. However, we then shift this value to the right by __j, which is
660eae32dcSDimitry Andric //   at least __j >= 115, so the result is guaranteed to fit into 179 - 115 = 64
670eae32dcSDimitry Andric //   bits. This means that we only need the topmost 64 significant bits of
680eae32dcSDimitry Andric //   the 64x128-bit multiplication.
690eae32dcSDimitry Andric //
700eae32dcSDimitry Andric // There are several ways to do this:
710eae32dcSDimitry Andric // 1. Best case: the compiler exposes a 128-bit type.
720eae32dcSDimitry Andric //    We perform two 64x64-bit multiplications, add the higher 64 bits of the
730eae32dcSDimitry Andric //    lower result to the higher result, and shift by __j - 64 bits.
740eae32dcSDimitry Andric //
750eae32dcSDimitry Andric //    We explicitly cast from 64-bit to 128-bit, so the compiler can tell
760eae32dcSDimitry Andric //    that these are only 64-bit inputs, and can map these to the best
770eae32dcSDimitry Andric //    possible sequence of assembly instructions.
780eae32dcSDimitry Andric //    x64 machines happen to have matching assembly instructions for
790eae32dcSDimitry Andric //    64x64-bit multiplications and 128-bit shifts.
800eae32dcSDimitry Andric //
810eae32dcSDimitry Andric // 2. Second best case: the compiler exposes intrinsics for the x64 assembly
820eae32dcSDimitry Andric //    instructions mentioned in 1.
830eae32dcSDimitry Andric //
840eae32dcSDimitry Andric // 3. We only have 64x64 bit instructions that return the lower 64 bits of
850eae32dcSDimitry Andric //    the result, i.e., we have to use plain C.
860eae32dcSDimitry Andric //    Our inputs are less than the full width, so we have three options:
870eae32dcSDimitry Andric //    a. Ignore this fact and just implement the intrinsics manually.
880eae32dcSDimitry Andric //    b. Split both into 31-bit pieces, which guarantees no internal overflow,
890eae32dcSDimitry Andric //       but requires extra work upfront (unless we change the lookup table).
900eae32dcSDimitry Andric //    c. Split only the first factor into 31-bit pieces, which also guarantees
910eae32dcSDimitry Andric //       no internal overflow, but requires extra work since the intermediate
920eae32dcSDimitry Andric //       results are not perfectly aligned.
930eae32dcSDimitry Andric #ifdef _LIBCPP_INTRINSIC128
940eae32dcSDimitry Andric 
950eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __mulShift(const uint64_t __m, const uint64_t* const __mul, const int32_t __j) {
960eae32dcSDimitry Andric   // __m is maximum 55 bits
970eae32dcSDimitry Andric   uint64_t __high1;                                               // 128
980eae32dcSDimitry Andric   const uint64_t __low1 = __ryu_umul128(__m, __mul[1], &__high1); // 64
990eae32dcSDimitry Andric   uint64_t __high0;                                               // 64
1000eae32dcSDimitry Andric   (void) __ryu_umul128(__m, __mul[0], &__high0);                  // 0
1010eae32dcSDimitry Andric   const uint64_t __sum = __high0 + __low1;
1020eae32dcSDimitry Andric   if (__sum < __high0) {
1030eae32dcSDimitry Andric     ++__high1; // overflow into __high1
1040eae32dcSDimitry Andric   }
1050eae32dcSDimitry Andric   return __ryu_shiftright128(__sum, __high1, static_cast<uint32_t>(__j - 64));
1060eae32dcSDimitry Andric }
1070eae32dcSDimitry Andric 
1080eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __mulShiftAll(const uint64_t __m, const uint64_t* const __mul, const int32_t __j,
1090eae32dcSDimitry Andric   uint64_t* const __vp, uint64_t* const __vm, const uint32_t __mmShift) {
1100eae32dcSDimitry Andric   *__vp = __mulShift(4 * __m + 2, __mul, __j);
1110eae32dcSDimitry Andric   *__vm = __mulShift(4 * __m - 1 - __mmShift, __mul, __j);
1120eae32dcSDimitry Andric   return __mulShift(4 * __m, __mul, __j);
1130eae32dcSDimitry Andric }
1140eae32dcSDimitry Andric 
1150eae32dcSDimitry Andric #else // ^^^ intrinsics available ^^^ / vvv intrinsics unavailable vvv
1160eae32dcSDimitry Andric 
1170eae32dcSDimitry 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,
1180eae32dcSDimitry Andric   uint64_t* const __vp, uint64_t* const __vm, const uint32_t __mmShift) { // TRANSITION, VSO-634761
1190eae32dcSDimitry Andric   __m <<= 1;
1200eae32dcSDimitry Andric   // __m is maximum 55 bits
1210eae32dcSDimitry Andric   uint64_t __tmp;
1220eae32dcSDimitry Andric   const uint64_t __lo = __ryu_umul128(__m, __mul[0], &__tmp);
1230eae32dcSDimitry Andric   uint64_t __hi;
1240eae32dcSDimitry Andric   const uint64_t __mid = __tmp + __ryu_umul128(__m, __mul[1], &__hi);
1250eae32dcSDimitry Andric   __hi += __mid < __tmp; // overflow into __hi
1260eae32dcSDimitry Andric 
1270eae32dcSDimitry Andric   const uint64_t __lo2 = __lo + __mul[0];
1280eae32dcSDimitry Andric   const uint64_t __mid2 = __mid + __mul[1] + (__lo2 < __lo);
1290eae32dcSDimitry Andric   const uint64_t __hi2 = __hi + (__mid2 < __mid);
1300eae32dcSDimitry Andric   *__vp = __ryu_shiftright128(__mid2, __hi2, static_cast<uint32_t>(__j - 64 - 1));
1310eae32dcSDimitry Andric 
1320eae32dcSDimitry Andric   if (__mmShift == 1) {
1330eae32dcSDimitry Andric     const uint64_t __lo3 = __lo - __mul[0];
1340eae32dcSDimitry Andric     const uint64_t __mid3 = __mid - __mul[1] - (__lo3 > __lo);
1350eae32dcSDimitry Andric     const uint64_t __hi3 = __hi - (__mid3 > __mid);
1360eae32dcSDimitry Andric     *__vm = __ryu_shiftright128(__mid3, __hi3, static_cast<uint32_t>(__j - 64 - 1));
1370eae32dcSDimitry Andric   } else {
1380eae32dcSDimitry Andric     const uint64_t __lo3 = __lo + __lo;
1390eae32dcSDimitry Andric     const uint64_t __mid3 = __mid + __mid + (__lo3 < __lo);
1400eae32dcSDimitry Andric     const uint64_t __hi3 = __hi + __hi + (__mid3 < __mid);
1410eae32dcSDimitry Andric     const uint64_t __lo4 = __lo3 - __mul[0];
1420eae32dcSDimitry Andric     const uint64_t __mid4 = __mid3 - __mul[1] - (__lo4 > __lo3);
1430eae32dcSDimitry Andric     const uint64_t __hi4 = __hi3 - (__mid4 > __mid3);
1440eae32dcSDimitry Andric     *__vm = __ryu_shiftright128(__mid4, __hi4, static_cast<uint32_t>(__j - 64));
1450eae32dcSDimitry Andric   }
1460eae32dcSDimitry Andric 
1470eae32dcSDimitry Andric   return __ryu_shiftright128(__mid, __hi, static_cast<uint32_t>(__j - 64 - 1));
1480eae32dcSDimitry Andric }
1490eae32dcSDimitry Andric 
1500eae32dcSDimitry Andric #endif // ^^^ intrinsics unavailable ^^^
1510eae32dcSDimitry Andric 
1520eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __decimalLength17(const uint64_t __v) {
1530eae32dcSDimitry Andric   // This is slightly faster than a loop.
1540eae32dcSDimitry Andric   // The average output length is 16.38 digits, so we check high-to-low.
1550eae32dcSDimitry Andric   // Function precondition: __v is not an 18, 19, or 20-digit number.
1560eae32dcSDimitry Andric   // (17 digits are sufficient for round-tripping.)
157*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(__v < 100000000000000000u, "");
1580eae32dcSDimitry Andric   if (__v >= 10000000000000000u) { return 17; }
1590eae32dcSDimitry Andric   if (__v >= 1000000000000000u) { return 16; }
1600eae32dcSDimitry Andric   if (__v >= 100000000000000u) { return 15; }
1610eae32dcSDimitry Andric   if (__v >= 10000000000000u) { return 14; }
1620eae32dcSDimitry Andric   if (__v >= 1000000000000u) { return 13; }
1630eae32dcSDimitry Andric   if (__v >= 100000000000u) { return 12; }
1640eae32dcSDimitry Andric   if (__v >= 10000000000u) { return 11; }
1650eae32dcSDimitry Andric   if (__v >= 1000000000u) { return 10; }
1660eae32dcSDimitry Andric   if (__v >= 100000000u) { return 9; }
1670eae32dcSDimitry Andric   if (__v >= 10000000u) { return 8; }
1680eae32dcSDimitry Andric   if (__v >= 1000000u) { return 7; }
1690eae32dcSDimitry Andric   if (__v >= 100000u) { return 6; }
1700eae32dcSDimitry Andric   if (__v >= 10000u) { return 5; }
1710eae32dcSDimitry Andric   if (__v >= 1000u) { return 4; }
1720eae32dcSDimitry Andric   if (__v >= 100u) { return 3; }
1730eae32dcSDimitry Andric   if (__v >= 10u) { return 2; }
1740eae32dcSDimitry Andric   return 1;
1750eae32dcSDimitry Andric }
1760eae32dcSDimitry Andric 
1770eae32dcSDimitry Andric // A floating decimal representing m * 10^e.
1780eae32dcSDimitry Andric struct __floating_decimal_64 {
1790eae32dcSDimitry Andric   uint64_t __mantissa;
1800eae32dcSDimitry Andric   int32_t __exponent;
1810eae32dcSDimitry Andric };
1820eae32dcSDimitry Andric 
1830eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline __floating_decimal_64 __d2d(const uint64_t __ieeeMantissa, const uint32_t __ieeeExponent) {
1840eae32dcSDimitry Andric   int32_t __e2;
1850eae32dcSDimitry Andric   uint64_t __m2;
1860eae32dcSDimitry Andric   if (__ieeeExponent == 0) {
1870eae32dcSDimitry Andric     // We subtract 2 so that the bounds computation has 2 additional bits.
1880eae32dcSDimitry Andric     __e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS - 2;
1890eae32dcSDimitry Andric     __m2 = __ieeeMantissa;
1900eae32dcSDimitry Andric   } else {
1910eae32dcSDimitry Andric     __e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS - 2;
1920eae32dcSDimitry Andric     __m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
1930eae32dcSDimitry Andric   }
1940eae32dcSDimitry Andric   const bool __even = (__m2 & 1) == 0;
1950eae32dcSDimitry Andric   const bool __acceptBounds = __even;
1960eae32dcSDimitry Andric 
1970eae32dcSDimitry Andric   // Step 2: Determine the interval of valid decimal representations.
1980eae32dcSDimitry Andric   const uint64_t __mv = 4 * __m2;
1990eae32dcSDimitry Andric   // Implicit bool -> int conversion. True is 1, false is 0.
2000eae32dcSDimitry Andric   const uint32_t __mmShift = __ieeeMantissa != 0 || __ieeeExponent <= 1;
2010eae32dcSDimitry Andric   // We would compute __mp and __mm like this:
2020eae32dcSDimitry Andric   // uint64_t __mp = 4 * __m2 + 2;
2030eae32dcSDimitry Andric   // uint64_t __mm = __mv - 1 - __mmShift;
2040eae32dcSDimitry Andric 
2050eae32dcSDimitry Andric   // Step 3: Convert to a decimal power base using 128-bit arithmetic.
2060eae32dcSDimitry Andric   uint64_t __vr, __vp, __vm;
2070eae32dcSDimitry Andric   int32_t __e10;
2080eae32dcSDimitry Andric   bool __vmIsTrailingZeros = false;
2090eae32dcSDimitry Andric   bool __vrIsTrailingZeros = false;
2100eae32dcSDimitry Andric   if (__e2 >= 0) {
2110eae32dcSDimitry Andric     // I tried special-casing __q == 0, but there was no effect on performance.
2120eae32dcSDimitry Andric     // This expression is slightly faster than max(0, __log10Pow2(__e2) - 1).
2130eae32dcSDimitry Andric     const uint32_t __q = __log10Pow2(__e2) - (__e2 > 3);
2140eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q);
2150eae32dcSDimitry Andric     const int32_t __k = __DOUBLE_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q)) - 1;
2160eae32dcSDimitry Andric     const int32_t __i = -__e2 + static_cast<int32_t>(__q) + __k;
2170eae32dcSDimitry Andric     __vr = __mulShiftAll(__m2, __DOUBLE_POW5_INV_SPLIT[__q], __i, &__vp, &__vm, __mmShift);
2180eae32dcSDimitry Andric     if (__q <= 21) {
2190eae32dcSDimitry Andric       // This should use __q <= 22, but I think 21 is also safe. Smaller values
2200eae32dcSDimitry Andric       // may still be safe, but it's more difficult to reason about them.
2210eae32dcSDimitry Andric       // Only one of __mp, __mv, and __mm can be a multiple of 5, if any.
2220eae32dcSDimitry Andric       const uint32_t __mvMod5 = static_cast<uint32_t>(__mv) - 5 * static_cast<uint32_t>(__div5(__mv));
2230eae32dcSDimitry Andric       if (__mvMod5 == 0) {
2240eae32dcSDimitry Andric         __vrIsTrailingZeros = __multipleOfPowerOf5(__mv, __q);
2250eae32dcSDimitry Andric       } else if (__acceptBounds) {
2260eae32dcSDimitry Andric         // Same as min(__e2 + (~__mm & 1), __pow5Factor(__mm)) >= __q
2270eae32dcSDimitry Andric         // <=> __e2 + (~__mm & 1) >= __q && __pow5Factor(__mm) >= __q
2280eae32dcSDimitry Andric         // <=> true && __pow5Factor(__mm) >= __q, since __e2 >= __q.
2290eae32dcSDimitry Andric         __vmIsTrailingZeros = __multipleOfPowerOf5(__mv - 1 - __mmShift, __q);
2300eae32dcSDimitry Andric       } else {
2310eae32dcSDimitry Andric         // Same as min(__e2 + 1, __pow5Factor(__mp)) >= __q.
2320eae32dcSDimitry Andric         __vp -= __multipleOfPowerOf5(__mv + 2, __q);
2330eae32dcSDimitry Andric       }
2340eae32dcSDimitry Andric     }
2350eae32dcSDimitry Andric   } else {
2360eae32dcSDimitry Andric     // This expression is slightly faster than max(0, __log10Pow5(-__e2) - 1).
2370eae32dcSDimitry Andric     const uint32_t __q = __log10Pow5(-__e2) - (-__e2 > 1);
2380eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q) + __e2;
2390eae32dcSDimitry Andric     const int32_t __i = -__e2 - static_cast<int32_t>(__q);
2400eae32dcSDimitry Andric     const int32_t __k = __pow5bits(__i) - __DOUBLE_POW5_BITCOUNT;
2410eae32dcSDimitry Andric     const int32_t __j = static_cast<int32_t>(__q) - __k;
2420eae32dcSDimitry Andric     __vr = __mulShiftAll(__m2, __DOUBLE_POW5_SPLIT[__i], __j, &__vp, &__vm, __mmShift);
2430eae32dcSDimitry Andric     if (__q <= 1) {
2440eae32dcSDimitry Andric       // {__vr,__vp,__vm} is trailing zeros if {__mv,__mp,__mm} has at least __q trailing 0 bits.
2450eae32dcSDimitry Andric       // __mv = 4 * __m2, so it always has at least two trailing 0 bits.
2460eae32dcSDimitry Andric       __vrIsTrailingZeros = true;
2470eae32dcSDimitry Andric       if (__acceptBounds) {
2480eae32dcSDimitry Andric         // __mm = __mv - 1 - __mmShift, so it has 1 trailing 0 bit iff __mmShift == 1.
2490eae32dcSDimitry Andric         __vmIsTrailingZeros = __mmShift == 1;
2500eae32dcSDimitry Andric       } else {
2510eae32dcSDimitry Andric         // __mp = __mv + 2, so it always has at least one trailing 0 bit.
2520eae32dcSDimitry Andric         --__vp;
2530eae32dcSDimitry Andric       }
2540eae32dcSDimitry Andric     } else if (__q < 63) { // TRANSITION(ulfjack): Use a tighter bound here.
2550eae32dcSDimitry Andric       // We need to compute min(ntz(__mv), __pow5Factor(__mv) - __e2) >= __q - 1
2560eae32dcSDimitry Andric       // <=> ntz(__mv) >= __q - 1 && __pow5Factor(__mv) - __e2 >= __q - 1
2570eae32dcSDimitry Andric       // <=> ntz(__mv) >= __q - 1 (__e2 is negative and -__e2 >= __q)
2580eae32dcSDimitry Andric       // <=> (__mv & ((1 << (__q - 1)) - 1)) == 0
2590eae32dcSDimitry Andric       // We also need to make sure that the left shift does not overflow.
2600eae32dcSDimitry Andric       __vrIsTrailingZeros = __multipleOfPowerOf2(__mv, __q - 1);
2610eae32dcSDimitry Andric     }
2620eae32dcSDimitry Andric   }
2630eae32dcSDimitry Andric 
2640eae32dcSDimitry Andric   // Step 4: Find the shortest decimal representation in the interval of valid representations.
2650eae32dcSDimitry Andric   int32_t __removed = 0;
2660eae32dcSDimitry Andric   uint8_t __lastRemovedDigit = 0;
2670eae32dcSDimitry Andric   uint64_t _Output;
2680eae32dcSDimitry Andric   // On average, we remove ~2 digits.
2690eae32dcSDimitry Andric   if (__vmIsTrailingZeros || __vrIsTrailingZeros) {
2700eae32dcSDimitry Andric     // General case, which happens rarely (~0.7%).
2710eae32dcSDimitry Andric     for (;;) {
2720eae32dcSDimitry Andric       const uint64_t __vpDiv10 = __div10(__vp);
2730eae32dcSDimitry Andric       const uint64_t __vmDiv10 = __div10(__vm);
2740eae32dcSDimitry Andric       if (__vpDiv10 <= __vmDiv10) {
2750eae32dcSDimitry Andric         break;
2760eae32dcSDimitry Andric       }
2770eae32dcSDimitry Andric       const uint32_t __vmMod10 = static_cast<uint32_t>(__vm) - 10 * static_cast<uint32_t>(__vmDiv10);
2780eae32dcSDimitry Andric       const uint64_t __vrDiv10 = __div10(__vr);
2790eae32dcSDimitry Andric       const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
2800eae32dcSDimitry Andric       __vmIsTrailingZeros &= __vmMod10 == 0;
2810eae32dcSDimitry Andric       __vrIsTrailingZeros &= __lastRemovedDigit == 0;
2820eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__vrMod10);
2830eae32dcSDimitry Andric       __vr = __vrDiv10;
2840eae32dcSDimitry Andric       __vp = __vpDiv10;
2850eae32dcSDimitry Andric       __vm = __vmDiv10;
2860eae32dcSDimitry Andric       ++__removed;
2870eae32dcSDimitry Andric     }
2880eae32dcSDimitry Andric     if (__vmIsTrailingZeros) {
2890eae32dcSDimitry Andric       for (;;) {
2900eae32dcSDimitry Andric         const uint64_t __vmDiv10 = __div10(__vm);
2910eae32dcSDimitry Andric         const uint32_t __vmMod10 = static_cast<uint32_t>(__vm) - 10 * static_cast<uint32_t>(__vmDiv10);
2920eae32dcSDimitry Andric         if (__vmMod10 != 0) {
2930eae32dcSDimitry Andric           break;
2940eae32dcSDimitry Andric         }
2950eae32dcSDimitry Andric         const uint64_t __vpDiv10 = __div10(__vp);
2960eae32dcSDimitry Andric         const uint64_t __vrDiv10 = __div10(__vr);
2970eae32dcSDimitry Andric         const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
2980eae32dcSDimitry Andric         __vrIsTrailingZeros &= __lastRemovedDigit == 0;
2990eae32dcSDimitry Andric         __lastRemovedDigit = static_cast<uint8_t>(__vrMod10);
3000eae32dcSDimitry Andric         __vr = __vrDiv10;
3010eae32dcSDimitry Andric         __vp = __vpDiv10;
3020eae32dcSDimitry Andric         __vm = __vmDiv10;
3030eae32dcSDimitry Andric         ++__removed;
3040eae32dcSDimitry Andric       }
3050eae32dcSDimitry Andric     }
3060eae32dcSDimitry Andric     if (__vrIsTrailingZeros && __lastRemovedDigit == 5 && __vr % 2 == 0) {
3070eae32dcSDimitry Andric       // Round even if the exact number is .....50..0.
3080eae32dcSDimitry Andric       __lastRemovedDigit = 4;
3090eae32dcSDimitry Andric     }
3100eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
3110eae32dcSDimitry Andric     _Output = __vr + ((__vr == __vm && (!__acceptBounds || !__vmIsTrailingZeros)) || __lastRemovedDigit >= 5);
3120eae32dcSDimitry Andric   } else {
3130eae32dcSDimitry Andric     // Specialized for the common case (~99.3%). Percentages below are relative to this.
3140eae32dcSDimitry Andric     bool __roundUp = false;
3150eae32dcSDimitry Andric     const uint64_t __vpDiv100 = __div100(__vp);
3160eae32dcSDimitry Andric     const uint64_t __vmDiv100 = __div100(__vm);
3170eae32dcSDimitry Andric     if (__vpDiv100 > __vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
3180eae32dcSDimitry Andric       const uint64_t __vrDiv100 = __div100(__vr);
3190eae32dcSDimitry Andric       const uint32_t __vrMod100 = static_cast<uint32_t>(__vr) - 100 * static_cast<uint32_t>(__vrDiv100);
3200eae32dcSDimitry Andric       __roundUp = __vrMod100 >= 50;
3210eae32dcSDimitry Andric       __vr = __vrDiv100;
3220eae32dcSDimitry Andric       __vp = __vpDiv100;
3230eae32dcSDimitry Andric       __vm = __vmDiv100;
3240eae32dcSDimitry Andric       __removed += 2;
3250eae32dcSDimitry Andric     }
3260eae32dcSDimitry Andric     // Loop iterations below (approximately), without optimization above:
3270eae32dcSDimitry Andric     // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
3280eae32dcSDimitry Andric     // Loop iterations below (approximately), with optimization above:
3290eae32dcSDimitry Andric     // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
3300eae32dcSDimitry Andric     for (;;) {
3310eae32dcSDimitry Andric       const uint64_t __vpDiv10 = __div10(__vp);
3320eae32dcSDimitry Andric       const uint64_t __vmDiv10 = __div10(__vm);
3330eae32dcSDimitry Andric       if (__vpDiv10 <= __vmDiv10) {
3340eae32dcSDimitry Andric         break;
3350eae32dcSDimitry Andric       }
3360eae32dcSDimitry Andric       const uint64_t __vrDiv10 = __div10(__vr);
3370eae32dcSDimitry Andric       const uint32_t __vrMod10 = static_cast<uint32_t>(__vr) - 10 * static_cast<uint32_t>(__vrDiv10);
3380eae32dcSDimitry Andric       __roundUp = __vrMod10 >= 5;
3390eae32dcSDimitry Andric       __vr = __vrDiv10;
3400eae32dcSDimitry Andric       __vp = __vpDiv10;
3410eae32dcSDimitry Andric       __vm = __vmDiv10;
3420eae32dcSDimitry Andric       ++__removed;
3430eae32dcSDimitry Andric     }
3440eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
3450eae32dcSDimitry Andric     _Output = __vr + (__vr == __vm || __roundUp);
3460eae32dcSDimitry Andric   }
3470eae32dcSDimitry Andric   const int32_t __exp = __e10 + __removed;
3480eae32dcSDimitry Andric 
3490eae32dcSDimitry Andric   __floating_decimal_64 __fd;
3500eae32dcSDimitry Andric   __fd.__exponent = __exp;
3510eae32dcSDimitry Andric   __fd.__mantissa = _Output;
3520eae32dcSDimitry Andric   return __fd;
3530eae32dcSDimitry Andric }
3540eae32dcSDimitry Andric 
3550eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result __to_chars(char* const _First, char* const _Last, const __floating_decimal_64 __v,
3560eae32dcSDimitry Andric   chars_format _Fmt, const double __f) {
3570eae32dcSDimitry Andric   // Step 5: Print the decimal representation.
3580eae32dcSDimitry Andric   uint64_t _Output = __v.__mantissa;
3590eae32dcSDimitry Andric   int32_t _Ryu_exponent = __v.__exponent;
3600eae32dcSDimitry Andric   const uint32_t __olength = __decimalLength17(_Output);
3610eae32dcSDimitry Andric   int32_t _Scientific_exponent = _Ryu_exponent + static_cast<int32_t>(__olength) - 1;
3620eae32dcSDimitry Andric 
3630eae32dcSDimitry Andric   if (_Fmt == chars_format{}) {
3640eae32dcSDimitry Andric     int32_t _Lower;
3650eae32dcSDimitry Andric     int32_t _Upper;
3660eae32dcSDimitry Andric 
3670eae32dcSDimitry Andric     if (__olength == 1) {
3680eae32dcSDimitry Andric       // Value | Fixed   | Scientific
3690eae32dcSDimitry Andric       // 1e-3  | "0.001" | "1e-03"
3700eae32dcSDimitry Andric       // 1e4   | "10000" | "1e+04"
3710eae32dcSDimitry Andric       _Lower = -3;
3720eae32dcSDimitry Andric       _Upper = 4;
3730eae32dcSDimitry Andric     } else {
3740eae32dcSDimitry Andric       // Value   | Fixed       | Scientific
3750eae32dcSDimitry Andric       // 1234e-7 | "0.0001234" | "1.234e-04"
3760eae32dcSDimitry Andric       // 1234e5  | "123400000" | "1.234e+08"
3770eae32dcSDimitry Andric       _Lower = -static_cast<int32_t>(__olength + 3);
3780eae32dcSDimitry Andric       _Upper = 5;
3790eae32dcSDimitry Andric     }
3800eae32dcSDimitry Andric 
3810eae32dcSDimitry Andric     if (_Lower <= _Ryu_exponent && _Ryu_exponent <= _Upper) {
3820eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
3830eae32dcSDimitry Andric     } else {
3840eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
3850eae32dcSDimitry Andric     }
3860eae32dcSDimitry Andric   } else if (_Fmt == chars_format::general) {
3870eae32dcSDimitry Andric     // C11 7.21.6.1 "The fprintf function"/8:
3880eae32dcSDimitry Andric     // "Let P equal [...] 6 if the precision is omitted [...].
3890eae32dcSDimitry Andric     // Then, if a conversion with style E would have an exponent of X:
3900eae32dcSDimitry Andric     // - if P > X >= -4, the conversion is with style f [...].
3910eae32dcSDimitry Andric     // - otherwise, the conversion is with style e [...]."
3920eae32dcSDimitry Andric     if (-4 <= _Scientific_exponent && _Scientific_exponent < 6) {
3930eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
3940eae32dcSDimitry Andric     } else {
3950eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
3960eae32dcSDimitry Andric     }
3970eae32dcSDimitry Andric   }
3980eae32dcSDimitry Andric 
3990eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
4000eae32dcSDimitry Andric     // Example: _Output == 1729, __olength == 4
4010eae32dcSDimitry Andric 
4020eae32dcSDimitry Andric     // _Ryu_exponent | Printed  | _Whole_digits | _Total_fixed_length  | Notes
4030eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4040eae32dcSDimitry Andric     //             2 | 172900   |  6            | _Whole_digits        | Ryu can't be used for printing
4050eae32dcSDimitry Andric     //             1 | 17290    |  5            | (sometimes adjusted) | when the trimmed digits are nonzero.
4060eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4070eae32dcSDimitry Andric     //             0 | 1729     |  4            | _Whole_digits        | Unified length cases.
4080eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4090eae32dcSDimitry Andric     //            -1 | 172.9    |  3            | __olength + 1        | This case can't happen for
4100eae32dcSDimitry Andric     //            -2 | 17.29    |  2            |                      | __olength == 1, but no additional
4110eae32dcSDimitry Andric     //            -3 | 1.729    |  1            |                      | code is needed to avoid it.
4120eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4130eae32dcSDimitry Andric     //            -4 | 0.1729   |  0            | 2 - _Ryu_exponent    | C11 7.21.6.1 "The fprintf function"/8:
4140eae32dcSDimitry Andric     //            -5 | 0.01729  | -1            |                      | "If a decimal-point character appears,
4150eae32dcSDimitry Andric     //            -6 | 0.001729 | -2            |                      | at least one digit appears before it."
4160eae32dcSDimitry Andric 
4170eae32dcSDimitry Andric     const int32_t _Whole_digits = static_cast<int32_t>(__olength) + _Ryu_exponent;
4180eae32dcSDimitry Andric 
4190eae32dcSDimitry Andric     uint32_t _Total_fixed_length;
4200eae32dcSDimitry Andric     if (_Ryu_exponent >= 0) { // cases "172900" and "1729"
4210eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(_Whole_digits);
4220eae32dcSDimitry Andric       if (_Output == 1) {
4230eae32dcSDimitry Andric         // Rounding can affect the number of digits.
4240eae32dcSDimitry Andric         // For example, 1e23 is exactly "99999999999999991611392" which is 23 digits instead of 24.
4250eae32dcSDimitry Andric         // We can use a lookup table to detect this and adjust the total length.
4260eae32dcSDimitry Andric         static constexpr uint8_t _Adjustment[309] = {
4270eae32dcSDimitry 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,
4280eae32dcSDimitry 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,
4290eae32dcSDimitry 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,
4300eae32dcSDimitry 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,
4310eae32dcSDimitry 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,
4320eae32dcSDimitry 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,
4330eae32dcSDimitry 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 };
4340eae32dcSDimitry Andric         _Total_fixed_length -= _Adjustment[_Ryu_exponent];
4350eae32dcSDimitry Andric         // _Whole_digits doesn't need to be adjusted because these cases won't refer to it later.
4360eae32dcSDimitry Andric       }
4370eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
4380eae32dcSDimitry Andric       _Total_fixed_length = __olength + 1;
4390eae32dcSDimitry Andric     } else { // case "0.001729"
4400eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(2 - _Ryu_exponent);
4410eae32dcSDimitry Andric     }
4420eae32dcSDimitry Andric 
4430eae32dcSDimitry Andric     if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
4440eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
4450eae32dcSDimitry Andric     }
4460eae32dcSDimitry Andric 
4470eae32dcSDimitry Andric     char* _Mid;
4480eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900"
4490eae32dcSDimitry Andric       bool _Can_use_ryu;
4500eae32dcSDimitry Andric 
4510eae32dcSDimitry Andric       if (_Ryu_exponent > 22) { // 10^22 is the largest power of 10 that's exactly representable as a double.
4520eae32dcSDimitry Andric         _Can_use_ryu = false;
4530eae32dcSDimitry Andric       } else {
4540eae32dcSDimitry Andric         // Ryu generated X: __v.__mantissa * 10^_Ryu_exponent
4550eae32dcSDimitry Andric         // __v.__mantissa == 2^_Trailing_zero_bits * (__v.__mantissa >> _Trailing_zero_bits)
4560eae32dcSDimitry Andric         // 10^_Ryu_exponent == 2^_Ryu_exponent * 5^_Ryu_exponent
4570eae32dcSDimitry Andric 
4580eae32dcSDimitry Andric         // _Trailing_zero_bits is [0, 56] (aside: because 2^56 is the largest power of 2
4590eae32dcSDimitry Andric         // with 17 decimal digits, which is double's round-trip limit.)
4600eae32dcSDimitry Andric         // _Ryu_exponent is [1, 22].
4610eae32dcSDimitry Andric         // Normalization adds [2, 52] (aside: at least 2 because the pre-normalized mantissa is at least 5).
4620eae32dcSDimitry Andric         // This adds up to [3, 130], which is well below double's maximum binary exponent 1023.
4630eae32dcSDimitry Andric 
4640eae32dcSDimitry Andric         // Therefore, we just need to consider (__v.__mantissa >> _Trailing_zero_bits) * 5^_Ryu_exponent.
4650eae32dcSDimitry Andric 
4660eae32dcSDimitry Andric         // If that product would exceed 53 bits, then X can't be exactly represented as a double.
4670eae32dcSDimitry Andric         // (That's not a problem for round-tripping, because X is close enough to the original double,
4680eae32dcSDimitry Andric         // but X isn't mathematically equal to the original double.) This requires a high-precision fallback.
4690eae32dcSDimitry Andric 
4700eae32dcSDimitry Andric         // If the product is 53 bits or smaller, then X can be exactly represented as a double (and we don't
4710eae32dcSDimitry Andric         // need to re-synthesize it; the original double must have been X, because Ryu wouldn't produce the
4720eae32dcSDimitry Andric         // same output for two different doubles X and Y). This allows Ryu's output to be used (zero-filled).
4730eae32dcSDimitry Andric 
4740eae32dcSDimitry Andric         // (2^53 - 1) / 5^0 (for indexing), (2^53 - 1) / 5^1, ..., (2^53 - 1) / 5^22
4750eae32dcSDimitry Andric         static constexpr uint64_t _Max_shifted_mantissa[23] = {
4760eae32dcSDimitry Andric           9007199254740991u, 1801439850948198u, 360287970189639u, 72057594037927u, 14411518807585u,
4770eae32dcSDimitry Andric           2882303761517u, 576460752303u, 115292150460u, 23058430092u, 4611686018u, 922337203u, 184467440u,
4780eae32dcSDimitry Andric           36893488u, 7378697u, 1475739u, 295147u, 59029u, 11805u, 2361u, 472u, 94u, 18u, 3u };
4790eae32dcSDimitry Andric 
4800eae32dcSDimitry Andric         unsigned long _Trailing_zero_bits;
4810eae32dcSDimitry Andric #ifdef _LIBCPP_HAS_BITSCAN64
4820eae32dcSDimitry Andric         (void) _BitScanForward64(&_Trailing_zero_bits, __v.__mantissa); // __v.__mantissa is guaranteed nonzero
4830eae32dcSDimitry Andric #else // ^^^ 64-bit ^^^ / vvv 32-bit vvv
4840eae32dcSDimitry Andric         const uint32_t _Low_mantissa = static_cast<uint32_t>(__v.__mantissa);
4850eae32dcSDimitry Andric         if (_Low_mantissa != 0) {
4860eae32dcSDimitry Andric           (void) _BitScanForward(&_Trailing_zero_bits, _Low_mantissa);
4870eae32dcSDimitry Andric         } else {
4880eae32dcSDimitry Andric           const uint32_t _High_mantissa = static_cast<uint32_t>(__v.__mantissa >> 32); // nonzero here
4890eae32dcSDimitry Andric           (void) _BitScanForward(&_Trailing_zero_bits, _High_mantissa);
4900eae32dcSDimitry Andric           _Trailing_zero_bits += 32;
4910eae32dcSDimitry Andric         }
4920eae32dcSDimitry Andric #endif // ^^^ 32-bit ^^^
4930eae32dcSDimitry Andric         const uint64_t _Shifted_mantissa = __v.__mantissa >> _Trailing_zero_bits;
4940eae32dcSDimitry Andric         _Can_use_ryu = _Shifted_mantissa <= _Max_shifted_mantissa[_Ryu_exponent];
4950eae32dcSDimitry Andric       }
4960eae32dcSDimitry Andric 
4970eae32dcSDimitry Andric       if (!_Can_use_ryu) {
4980eae32dcSDimitry Andric         // Print the integer exactly.
4990eae32dcSDimitry Andric         // Performance note: This will redundantly perform bounds checking.
5000eae32dcSDimitry Andric         // Performance note: This will redundantly decompose the IEEE representation.
5010eae32dcSDimitry Andric         return __d2fixed_buffered_n(_First, _Last, __f, 0);
5020eae32dcSDimitry Andric       }
5030eae32dcSDimitry Andric 
5040eae32dcSDimitry Andric       // _Can_use_ryu
5050eae32dcSDimitry Andric       // Print the decimal digits, left-aligned within [_First, _First + _Total_fixed_length).
5060eae32dcSDimitry Andric       _Mid = _First + __olength;
5070eae32dcSDimitry Andric     } else { // cases "1729", "17.29", and "0.001729"
5080eae32dcSDimitry Andric       // Print the decimal digits, right-aligned within [_First, _First + _Total_fixed_length).
5090eae32dcSDimitry Andric       _Mid = _First + _Total_fixed_length;
5100eae32dcSDimitry Andric     }
5110eae32dcSDimitry Andric 
5120eae32dcSDimitry Andric     // We prefer 32-bit operations, even on 64-bit platforms.
5130eae32dcSDimitry Andric     // We have at most 17 digits, and uint32_t can store 9 digits.
5140eae32dcSDimitry Andric     // If _Output doesn't fit into uint32_t, we cut off 8 digits,
5150eae32dcSDimitry Andric     // so the rest will fit into uint32_t.
5160eae32dcSDimitry Andric     if ((_Output >> 32) != 0) {
5170eae32dcSDimitry Andric       // Expensive 64-bit division.
5180eae32dcSDimitry Andric       const uint64_t __q = __div1e8(_Output);
5190eae32dcSDimitry Andric       uint32_t __output2 = static_cast<uint32_t>(_Output - 100000000 * __q);
5200eae32dcSDimitry Andric       _Output = __q;
5210eae32dcSDimitry Andric 
5220eae32dcSDimitry Andric       const uint32_t __c = __output2 % 10000;
5230eae32dcSDimitry Andric       __output2 /= 10000;
5240eae32dcSDimitry Andric       const uint32_t __d = __output2 % 10000;
5250eae32dcSDimitry Andric       const uint32_t __c0 = (__c % 100) << 1;
5260eae32dcSDimitry Andric       const uint32_t __c1 = (__c / 100) << 1;
5270eae32dcSDimitry Andric       const uint32_t __d0 = (__d % 100) << 1;
5280eae32dcSDimitry Andric       const uint32_t __d1 = (__d / 100) << 1;
5290eae32dcSDimitry Andric 
53006c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
53106c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
53206c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __d0, 2);
53306c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __d1, 2);
5340eae32dcSDimitry Andric     }
5350eae32dcSDimitry Andric     uint32_t __output2 = static_cast<uint32_t>(_Output);
5360eae32dcSDimitry Andric     while (__output2 >= 10000) {
5370eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
5380eae32dcSDimitry Andric       const uint32_t __c = __output2 - 10000 * (__output2 / 10000);
5390eae32dcSDimitry Andric #else
5400eae32dcSDimitry Andric       const uint32_t __c = __output2 % 10000;
5410eae32dcSDimitry Andric #endif
5420eae32dcSDimitry Andric       __output2 /= 10000;
5430eae32dcSDimitry Andric       const uint32_t __c0 = (__c % 100) << 1;
5440eae32dcSDimitry Andric       const uint32_t __c1 = (__c / 100) << 1;
54506c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
54606c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
5470eae32dcSDimitry Andric     }
5480eae32dcSDimitry Andric     if (__output2 >= 100) {
5490eae32dcSDimitry Andric       const uint32_t __c = (__output2 % 100) << 1;
5500eae32dcSDimitry Andric       __output2 /= 100;
55106c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
5520eae32dcSDimitry Andric     }
5530eae32dcSDimitry Andric     if (__output2 >= 10) {
5540eae32dcSDimitry Andric       const uint32_t __c = __output2 << 1;
55506c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
5560eae32dcSDimitry Andric     } else {
5570eae32dcSDimitry Andric       *--_Mid = static_cast<char>('0' + __output2);
5580eae32dcSDimitry Andric     }
5590eae32dcSDimitry Andric 
5600eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900" with _Can_use_ryu
5610eae32dcSDimitry Andric       // Performance note: it might be more efficient to do this immediately after setting _Mid.
56206c3fb27SDimitry Andric       std::memset(_First + __olength, '0', static_cast<size_t>(_Ryu_exponent));
5630eae32dcSDimitry Andric     } else if (_Ryu_exponent == 0) { // case "1729"
5640eae32dcSDimitry Andric       // Done!
5650eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
5660eae32dcSDimitry Andric       // Performance note: moving digits might not be optimal.
56706c3fb27SDimitry Andric       std::memmove(_First, _First + 1, static_cast<size_t>(_Whole_digits));
5680eae32dcSDimitry Andric       _First[_Whole_digits] = '.';
5690eae32dcSDimitry Andric     } else { // case "0.001729"
5700eae32dcSDimitry Andric       // Performance note: a larger memset() followed by overwriting '.' might be more efficient.
5710eae32dcSDimitry Andric       _First[0] = '0';
5720eae32dcSDimitry Andric       _First[1] = '.';
57306c3fb27SDimitry Andric       std::memset(_First + 2, '0', static_cast<size_t>(-_Whole_digits));
5740eae32dcSDimitry Andric     }
5750eae32dcSDimitry Andric 
5760eae32dcSDimitry Andric     return { _First + _Total_fixed_length, errc{} };
5770eae32dcSDimitry Andric   }
5780eae32dcSDimitry Andric 
5790eae32dcSDimitry Andric   const uint32_t _Total_scientific_length = __olength + (__olength > 1) // digits + possible decimal point
5800eae32dcSDimitry Andric     + (-100 < _Scientific_exponent && _Scientific_exponent < 100 ? 4 : 5); // + scientific exponent
5810eae32dcSDimitry Andric   if (_Last - _First < static_cast<ptrdiff_t>(_Total_scientific_length)) {
5820eae32dcSDimitry Andric     return { _Last, errc::value_too_large };
5830eae32dcSDimitry Andric   }
5840eae32dcSDimitry Andric   char* const __result = _First;
5850eae32dcSDimitry Andric 
5860eae32dcSDimitry Andric   // Print the decimal digits.
5870eae32dcSDimitry Andric   uint32_t __i = 0;
5880eae32dcSDimitry Andric   // We prefer 32-bit operations, even on 64-bit platforms.
5890eae32dcSDimitry Andric   // We have at most 17 digits, and uint32_t can store 9 digits.
5900eae32dcSDimitry Andric   // If _Output doesn't fit into uint32_t, we cut off 8 digits,
5910eae32dcSDimitry Andric   // so the rest will fit into uint32_t.
5920eae32dcSDimitry Andric   if ((_Output >> 32) != 0) {
5930eae32dcSDimitry Andric     // Expensive 64-bit division.
5940eae32dcSDimitry Andric     const uint64_t __q = __div1e8(_Output);
5950eae32dcSDimitry Andric     uint32_t __output2 = static_cast<uint32_t>(_Output) - 100000000 * static_cast<uint32_t>(__q);
5960eae32dcSDimitry Andric     _Output = __q;
5970eae32dcSDimitry Andric 
5980eae32dcSDimitry Andric     const uint32_t __c = __output2 % 10000;
5990eae32dcSDimitry Andric     __output2 /= 10000;
6000eae32dcSDimitry Andric     const uint32_t __d = __output2 % 10000;
6010eae32dcSDimitry Andric     const uint32_t __c0 = (__c % 100) << 1;
6020eae32dcSDimitry Andric     const uint32_t __c1 = (__c / 100) << 1;
6030eae32dcSDimitry Andric     const uint32_t __d0 = (__d % 100) << 1;
6040eae32dcSDimitry Andric     const uint32_t __d1 = (__d / 100) << 1;
60506c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
60606c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
60706c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 5, __DIGIT_TABLE + __d0, 2);
60806c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 7, __DIGIT_TABLE + __d1, 2);
6090eae32dcSDimitry Andric     __i += 8;
6100eae32dcSDimitry Andric   }
6110eae32dcSDimitry Andric   uint32_t __output2 = static_cast<uint32_t>(_Output);
6120eae32dcSDimitry Andric   while (__output2 >= 10000) {
6130eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
6140eae32dcSDimitry Andric     const uint32_t __c = __output2 - 10000 * (__output2 / 10000);
6150eae32dcSDimitry Andric #else
6160eae32dcSDimitry Andric     const uint32_t __c = __output2 % 10000;
6170eae32dcSDimitry Andric #endif
6180eae32dcSDimitry Andric     __output2 /= 10000;
6190eae32dcSDimitry Andric     const uint32_t __c0 = (__c % 100) << 1;
6200eae32dcSDimitry Andric     const uint32_t __c1 = (__c / 100) << 1;
62106c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
62206c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
6230eae32dcSDimitry Andric     __i += 4;
6240eae32dcSDimitry Andric   }
6250eae32dcSDimitry Andric   if (__output2 >= 100) {
6260eae32dcSDimitry Andric     const uint32_t __c = (__output2 % 100) << 1;
6270eae32dcSDimitry Andric     __output2 /= 100;
62806c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c, 2);
6290eae32dcSDimitry Andric     __i += 2;
6300eae32dcSDimitry Andric   }
6310eae32dcSDimitry Andric   if (__output2 >= 10) {
6320eae32dcSDimitry Andric     const uint32_t __c = __output2 << 1;
6330eae32dcSDimitry Andric     // We can't use memcpy here: the decimal dot goes between these two digits.
6340eae32dcSDimitry Andric     __result[2] = __DIGIT_TABLE[__c + 1];
6350eae32dcSDimitry Andric     __result[0] = __DIGIT_TABLE[__c];
6360eae32dcSDimitry Andric   } else {
6370eae32dcSDimitry Andric     __result[0] = static_cast<char>('0' + __output2);
6380eae32dcSDimitry Andric   }
6390eae32dcSDimitry Andric 
6400eae32dcSDimitry Andric   // Print decimal point if needed.
6410eae32dcSDimitry Andric   uint32_t __index;
6420eae32dcSDimitry Andric   if (__olength > 1) {
6430eae32dcSDimitry Andric     __result[1] = '.';
6440eae32dcSDimitry Andric     __index = __olength + 1;
6450eae32dcSDimitry Andric   } else {
6460eae32dcSDimitry Andric     __index = 1;
6470eae32dcSDimitry Andric   }
6480eae32dcSDimitry Andric 
6490eae32dcSDimitry Andric   // Print the exponent.
6500eae32dcSDimitry Andric   __result[__index++] = 'e';
6510eae32dcSDimitry Andric   if (_Scientific_exponent < 0) {
6520eae32dcSDimitry Andric     __result[__index++] = '-';
6530eae32dcSDimitry Andric     _Scientific_exponent = -_Scientific_exponent;
6540eae32dcSDimitry Andric   } else {
6550eae32dcSDimitry Andric     __result[__index++] = '+';
6560eae32dcSDimitry Andric   }
6570eae32dcSDimitry Andric 
6580eae32dcSDimitry Andric   if (_Scientific_exponent >= 100) {
6590eae32dcSDimitry Andric     const int32_t __c = _Scientific_exponent % 10;
66006c3fb27SDimitry Andric     std::memcpy(__result + __index, __DIGIT_TABLE + 2 * (_Scientific_exponent / 10), 2);
6610eae32dcSDimitry Andric     __result[__index + 2] = static_cast<char>('0' + __c);
6620eae32dcSDimitry Andric     __index += 3;
6630eae32dcSDimitry Andric   } else {
66406c3fb27SDimitry Andric     std::memcpy(__result + __index, __DIGIT_TABLE + 2 * _Scientific_exponent, 2);
6650eae32dcSDimitry Andric     __index += 2;
6660eae32dcSDimitry Andric   }
6670eae32dcSDimitry Andric 
6680eae32dcSDimitry Andric   return { _First + _Total_scientific_length, errc{} };
6690eae32dcSDimitry Andric }
6700eae32dcSDimitry Andric 
6710eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __d2d_small_int(const uint64_t __ieeeMantissa, const uint32_t __ieeeExponent,
6720eae32dcSDimitry Andric   __floating_decimal_64* const __v) {
6730eae32dcSDimitry Andric   const uint64_t __m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
6740eae32dcSDimitry Andric   const int32_t __e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
6750eae32dcSDimitry Andric 
6760eae32dcSDimitry Andric   if (__e2 > 0) {
6770eae32dcSDimitry Andric     // f = __m2 * 2^__e2 >= 2^53 is an integer.
6780eae32dcSDimitry Andric     // Ignore this case for now.
6790eae32dcSDimitry Andric     return false;
6800eae32dcSDimitry Andric   }
6810eae32dcSDimitry Andric 
6820eae32dcSDimitry Andric   if (__e2 < -52) {
6830eae32dcSDimitry Andric     // f < 1.
6840eae32dcSDimitry Andric     return false;
6850eae32dcSDimitry Andric   }
6860eae32dcSDimitry Andric 
6870eae32dcSDimitry Andric   // Since 2^52 <= __m2 < 2^53 and 0 <= -__e2 <= 52: 1 <= f = __m2 / 2^-__e2 < 2^53.
6880eae32dcSDimitry Andric   // Test if the lower -__e2 bits of the significand are 0, i.e. whether the fraction is 0.
6890eae32dcSDimitry Andric   const uint64_t __mask = (1ull << -__e2) - 1;
6900eae32dcSDimitry Andric   const uint64_t __fraction = __m2 & __mask;
6910eae32dcSDimitry Andric   if (__fraction != 0) {
6920eae32dcSDimitry Andric     return false;
6930eae32dcSDimitry Andric   }
6940eae32dcSDimitry Andric 
6950eae32dcSDimitry Andric   // f is an integer in the range [1, 2^53).
6960eae32dcSDimitry Andric   // Note: __mantissa might contain trailing (decimal) 0's.
6970eae32dcSDimitry Andric   // Note: since 2^53 < 10^16, there is no need to adjust __decimalLength17().
6980eae32dcSDimitry Andric   __v->__mantissa = __m2 >> -__e2;
6990eae32dcSDimitry Andric   __v->__exponent = 0;
7000eae32dcSDimitry Andric   return true;
7010eae32dcSDimitry Andric }
7020eae32dcSDimitry Andric 
7030eae32dcSDimitry Andric [[nodiscard]] to_chars_result __d2s_buffered_n(char* const _First, char* const _Last, const double __f,
7040eae32dcSDimitry Andric   const chars_format _Fmt) {
7050eae32dcSDimitry Andric 
7060eae32dcSDimitry Andric   // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
7070eae32dcSDimitry Andric   const uint64_t __bits = __double_to_bits(__f);
7080eae32dcSDimitry Andric 
7090eae32dcSDimitry Andric   // Case distinction; exit early for the easy cases.
7100eae32dcSDimitry Andric   if (__bits == 0) {
7110eae32dcSDimitry Andric     if (_Fmt == chars_format::scientific) {
7120eae32dcSDimitry Andric       if (_Last - _First < 5) {
7130eae32dcSDimitry Andric         return { _Last, errc::value_too_large };
7140eae32dcSDimitry Andric       }
7150eae32dcSDimitry Andric 
71606c3fb27SDimitry Andric       std::memcpy(_First, "0e+00", 5);
7170eae32dcSDimitry Andric 
7180eae32dcSDimitry Andric       return { _First + 5, errc{} };
7190eae32dcSDimitry Andric     }
7200eae32dcSDimitry Andric 
7210eae32dcSDimitry Andric     // Print "0" for chars_format::fixed, chars_format::general, and chars_format{}.
7220eae32dcSDimitry Andric     if (_First == _Last) {
7230eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
7240eae32dcSDimitry Andric     }
7250eae32dcSDimitry Andric 
7260eae32dcSDimitry Andric     *_First = '0';
7270eae32dcSDimitry Andric 
7280eae32dcSDimitry Andric     return { _First + 1, errc{} };
7290eae32dcSDimitry Andric   }
7300eae32dcSDimitry Andric 
7310eae32dcSDimitry Andric   // Decode __bits into mantissa and exponent.
7320eae32dcSDimitry Andric   const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);
7330eae32dcSDimitry Andric   const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);
7340eae32dcSDimitry Andric 
7350eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
7360eae32dcSDimitry Andric     // const uint64_t _Mantissa2 = __ieeeMantissa | (1ull << __DOUBLE_MANTISSA_BITS); // restore implicit bit
7370eae32dcSDimitry Andric     const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
7380eae32dcSDimitry Andric       - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS; // bias and normalization
7390eae32dcSDimitry Andric 
7400eae32dcSDimitry Andric     // Normal values are equal to _Mantissa2 * 2^_Exponent2.
7410eae32dcSDimitry Andric     // (Subnormals are different, but they'll be rejected by the _Exponent2 test here, so they can be ignored.)
7420eae32dcSDimitry Andric 
7430eae32dcSDimitry Andric     // For nonzero integers, _Exponent2 >= -52. (The minimum value occurs when _Mantissa2 * 2^_Exponent2 is 1.
7440eae32dcSDimitry Andric     // In that case, _Mantissa2 is the implicit 1 bit followed by 52 zeros, so _Exponent2 is -52 to shift away
7450eae32dcSDimitry Andric     // the zeros.) The dense range of exactly representable integers has negative or zero exponents
7460eae32dcSDimitry Andric     // (as positive exponents make the range non-dense). For that dense range, Ryu will always be used:
7470eae32dcSDimitry Andric     // every digit is necessary to uniquely identify the value, so Ryu must print them all.
7480eae32dcSDimitry Andric 
7490eae32dcSDimitry Andric     // Positive exponents are the non-dense range of exactly representable integers. This contains all of the values
7500eae32dcSDimitry Andric     // for which Ryu can't be used (and a few Ryu-friendly values). We can save time by detecting positive
7510eae32dcSDimitry Andric     // exponents here and skipping Ryu. Calling __d2fixed_buffered_n() with precision 0 is valid for all integers
7520eae32dcSDimitry Andric     // (so it's okay if we call it with a Ryu-friendly value).
7530eae32dcSDimitry Andric     if (_Exponent2 > 0) {
7540eae32dcSDimitry Andric       return __d2fixed_buffered_n(_First, _Last, __f, 0);
7550eae32dcSDimitry Andric     }
7560eae32dcSDimitry Andric   }
7570eae32dcSDimitry Andric 
7580eae32dcSDimitry Andric   __floating_decimal_64 __v;
7590eae32dcSDimitry Andric   const bool __isSmallInt = __d2d_small_int(__ieeeMantissa, __ieeeExponent, &__v);
7600eae32dcSDimitry Andric   if (__isSmallInt) {
7610eae32dcSDimitry Andric     // For small integers in the range [1, 2^53), __v.__mantissa might contain trailing (decimal) zeros.
7620eae32dcSDimitry Andric     // For scientific notation we need to move these zeros into the exponent.
7630eae32dcSDimitry Andric     // (This is not needed for fixed-point notation, so it might be beneficial to trim
7640eae32dcSDimitry Andric     // trailing zeros in __to_chars only if needed - once fixed-point notation output is implemented.)
7650eae32dcSDimitry Andric     for (;;) {
7660eae32dcSDimitry Andric       const uint64_t __q = __div10(__v.__mantissa);
7670eae32dcSDimitry Andric       const uint32_t __r = static_cast<uint32_t>(__v.__mantissa) - 10 * static_cast<uint32_t>(__q);
7680eae32dcSDimitry Andric       if (__r != 0) {
7690eae32dcSDimitry Andric         break;
7700eae32dcSDimitry Andric       }
7710eae32dcSDimitry Andric       __v.__mantissa = __q;
7720eae32dcSDimitry Andric       ++__v.__exponent;
7730eae32dcSDimitry Andric     }
7740eae32dcSDimitry Andric   } else {
7750eae32dcSDimitry Andric     __v = __d2d(__ieeeMantissa, __ieeeExponent);
7760eae32dcSDimitry Andric   }
7770eae32dcSDimitry Andric 
7780eae32dcSDimitry Andric   return __to_chars(_First, _Last, __v, _Fmt, __f);
7790eae32dcSDimitry Andric }
7800eae32dcSDimitry Andric 
7810eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD
7820eae32dcSDimitry Andric 
7830eae32dcSDimitry Andric // clang-format on
784