xref: /freebsd/contrib/llvm-project/libcxx/src/ryu/f2s.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_intrinsics.h"
490eae32dcSDimitry Andric #include "include/ryu/digit_table.h"
500eae32dcSDimitry Andric #include "include/ryu/f2s.h"
510eae32dcSDimitry Andric #include "include/ryu/ryu.h"
520eae32dcSDimitry Andric 
530eae32dcSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
540eae32dcSDimitry Andric 
550eae32dcSDimitry Andric inline constexpr int __FLOAT_MANTISSA_BITS = 23;
560eae32dcSDimitry Andric inline constexpr int __FLOAT_EXPONENT_BITS = 8;
570eae32dcSDimitry Andric inline constexpr int __FLOAT_BIAS = 127;
580eae32dcSDimitry Andric 
590eae32dcSDimitry Andric inline constexpr int __FLOAT_POW5_INV_BITCOUNT = 59;
600eae32dcSDimitry Andric inline constexpr uint64_t __FLOAT_POW5_INV_SPLIT[31] = {
610eae32dcSDimitry Andric   576460752303423489u, 461168601842738791u, 368934881474191033u, 295147905179352826u,
620eae32dcSDimitry Andric   472236648286964522u, 377789318629571618u, 302231454903657294u, 483570327845851670u,
630eae32dcSDimitry Andric   386856262276681336u, 309485009821345069u, 495176015714152110u, 396140812571321688u,
640eae32dcSDimitry Andric   316912650057057351u, 507060240091291761u, 405648192073033409u, 324518553658426727u,
650eae32dcSDimitry Andric   519229685853482763u, 415383748682786211u, 332306998946228969u, 531691198313966350u,
660eae32dcSDimitry Andric   425352958651173080u, 340282366920938464u, 544451787073501542u, 435561429658801234u,
670eae32dcSDimitry Andric   348449143727040987u, 557518629963265579u, 446014903970612463u, 356811923176489971u,
680eae32dcSDimitry Andric   570899077082383953u, 456719261665907162u, 365375409332725730u
690eae32dcSDimitry Andric };
700eae32dcSDimitry Andric inline constexpr int __FLOAT_POW5_BITCOUNT = 61;
710eae32dcSDimitry Andric inline constexpr uint64_t __FLOAT_POW5_SPLIT[47] = {
720eae32dcSDimitry Andric   1152921504606846976u, 1441151880758558720u, 1801439850948198400u, 2251799813685248000u,
730eae32dcSDimitry Andric   1407374883553280000u, 1759218604441600000u, 2199023255552000000u, 1374389534720000000u,
740eae32dcSDimitry Andric   1717986918400000000u, 2147483648000000000u, 1342177280000000000u, 1677721600000000000u,
750eae32dcSDimitry Andric   2097152000000000000u, 1310720000000000000u, 1638400000000000000u, 2048000000000000000u,
760eae32dcSDimitry Andric   1280000000000000000u, 1600000000000000000u, 2000000000000000000u, 1250000000000000000u,
770eae32dcSDimitry Andric   1562500000000000000u, 1953125000000000000u, 1220703125000000000u, 1525878906250000000u,
780eae32dcSDimitry Andric   1907348632812500000u, 1192092895507812500u, 1490116119384765625u, 1862645149230957031u,
790eae32dcSDimitry Andric   1164153218269348144u, 1455191522836685180u, 1818989403545856475u, 2273736754432320594u,
800eae32dcSDimitry Andric   1421085471520200371u, 1776356839400250464u, 2220446049250313080u, 1387778780781445675u,
810eae32dcSDimitry Andric   1734723475976807094u, 2168404344971008868u, 1355252715606880542u, 1694065894508600678u,
820eae32dcSDimitry Andric   2117582368135750847u, 1323488980084844279u, 1654361225106055349u, 2067951531382569187u,
830eae32dcSDimitry Andric   1292469707114105741u, 1615587133892632177u, 2019483917365790221u
840eae32dcSDimitry Andric };
850eae32dcSDimitry Andric 
860eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __pow5Factor(uint32_t __value) {
870eae32dcSDimitry Andric   uint32_t __count = 0;
880eae32dcSDimitry Andric   for (;;) {
89*5f757f3fSDimitry Andric     _LIBCPP_ASSERT_INTERNAL(__value != 0, "");
900eae32dcSDimitry Andric     const uint32_t __q = __value / 5;
910eae32dcSDimitry Andric     const uint32_t __r = __value % 5;
920eae32dcSDimitry Andric     if (__r != 0) {
930eae32dcSDimitry Andric       break;
940eae32dcSDimitry Andric     }
950eae32dcSDimitry Andric     __value = __q;
960eae32dcSDimitry Andric     ++__count;
970eae32dcSDimitry Andric   }
980eae32dcSDimitry Andric   return __count;
990eae32dcSDimitry Andric }
1000eae32dcSDimitry Andric 
1010eae32dcSDimitry Andric // Returns true if __value is divisible by 5^__p.
1020eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf5(const uint32_t __value, const uint32_t __p) {
1030eae32dcSDimitry Andric   return __pow5Factor(__value) >= __p;
1040eae32dcSDimitry Andric }
1050eae32dcSDimitry Andric 
1060eae32dcSDimitry Andric // Returns true if __value is divisible by 2^__p.
1070eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf2(const uint32_t __value, const uint32_t __p) {
108*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(__value != 0, "");
109*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(__p < 32, "");
1100eae32dcSDimitry Andric   // __builtin_ctz doesn't appear to be faster here.
1110eae32dcSDimitry Andric   return (__value & ((1u << __p) - 1)) == 0;
1120eae32dcSDimitry Andric }
1130eae32dcSDimitry Andric 
1140eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulShift(const uint32_t __m, const uint64_t __factor, const int32_t __shift) {
115*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(__shift > 32, "");
1160eae32dcSDimitry Andric 
1170eae32dcSDimitry Andric   // The casts here help MSVC to avoid calls to the __allmul library
1180eae32dcSDimitry Andric   // function.
1190eae32dcSDimitry Andric   const uint32_t __factorLo = static_cast<uint32_t>(__factor);
1200eae32dcSDimitry Andric   const uint32_t __factorHi = static_cast<uint32_t>(__factor >> 32);
1210eae32dcSDimitry Andric   const uint64_t __bits0 = static_cast<uint64_t>(__m) * __factorLo;
1220eae32dcSDimitry Andric   const uint64_t __bits1 = static_cast<uint64_t>(__m) * __factorHi;
1230eae32dcSDimitry Andric 
1240eae32dcSDimitry Andric #ifndef _LIBCPP_64_BIT
1250eae32dcSDimitry Andric   // On 32-bit platforms we can avoid a 64-bit shift-right since we only
1260eae32dcSDimitry Andric   // need the upper 32 bits of the result and the shift value is > 32.
1270eae32dcSDimitry Andric   const uint32_t __bits0Hi = static_cast<uint32_t>(__bits0 >> 32);
1280eae32dcSDimitry Andric   uint32_t __bits1Lo = static_cast<uint32_t>(__bits1);
1290eae32dcSDimitry Andric   uint32_t __bits1Hi = static_cast<uint32_t>(__bits1 >> 32);
1300eae32dcSDimitry Andric   __bits1Lo += __bits0Hi;
1310eae32dcSDimitry Andric   __bits1Hi += (__bits1Lo < __bits0Hi);
1320eae32dcSDimitry Andric   const int32_t __s = __shift - 32;
1330eae32dcSDimitry Andric   return (__bits1Hi << (32 - __s)) | (__bits1Lo >> __s);
1340eae32dcSDimitry Andric #else // ^^^ 32-bit ^^^ / vvv 64-bit vvv
1350eae32dcSDimitry Andric   const uint64_t __sum = (__bits0 >> 32) + __bits1;
1360eae32dcSDimitry Andric   const uint64_t __shiftedSum = __sum >> (__shift - 32);
137*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(__shiftedSum <= UINT32_MAX, "");
1380eae32dcSDimitry Andric   return static_cast<uint32_t>(__shiftedSum);
1390eae32dcSDimitry Andric #endif // ^^^ 64-bit ^^^
1400eae32dcSDimitry Andric }
1410eae32dcSDimitry Andric 
1420eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulPow5InvDivPow2(const uint32_t __m, const uint32_t __q, const int32_t __j) {
1430eae32dcSDimitry Andric   return __mulShift(__m, __FLOAT_POW5_INV_SPLIT[__q], __j);
1440eae32dcSDimitry Andric }
1450eae32dcSDimitry Andric 
1460eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulPow5divPow2(const uint32_t __m, const uint32_t __i, const int32_t __j) {
1470eae32dcSDimitry Andric   return __mulShift(__m, __FLOAT_POW5_SPLIT[__i], __j);
1480eae32dcSDimitry Andric }
1490eae32dcSDimitry Andric 
1500eae32dcSDimitry Andric // A floating decimal representing m * 10^e.
1510eae32dcSDimitry Andric struct __floating_decimal_32 {
1520eae32dcSDimitry Andric   uint32_t __mantissa;
1530eae32dcSDimitry Andric   int32_t __exponent;
1540eae32dcSDimitry Andric };
1550eae32dcSDimitry Andric 
1560eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline __floating_decimal_32 __f2d(const uint32_t __ieeeMantissa, const uint32_t __ieeeExponent) {
1570eae32dcSDimitry Andric   int32_t __e2;
1580eae32dcSDimitry Andric   uint32_t __m2;
1590eae32dcSDimitry Andric   if (__ieeeExponent == 0) {
1600eae32dcSDimitry Andric     // We subtract 2 so that the bounds computation has 2 additional bits.
1610eae32dcSDimitry Andric     __e2 = 1 - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS - 2;
1620eae32dcSDimitry Andric     __m2 = __ieeeMantissa;
1630eae32dcSDimitry Andric   } else {
1640eae32dcSDimitry Andric     __e2 = static_cast<int32_t>(__ieeeExponent) - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS - 2;
1650eae32dcSDimitry Andric     __m2 = (1u << __FLOAT_MANTISSA_BITS) | __ieeeMantissa;
1660eae32dcSDimitry Andric   }
1670eae32dcSDimitry Andric   const bool __even = (__m2 & 1) == 0;
1680eae32dcSDimitry Andric   const bool __acceptBounds = __even;
1690eae32dcSDimitry Andric 
1700eae32dcSDimitry Andric   // Step 2: Determine the interval of valid decimal representations.
1710eae32dcSDimitry Andric   const uint32_t __mv = 4 * __m2;
1720eae32dcSDimitry Andric   const uint32_t __mp = 4 * __m2 + 2;
1730eae32dcSDimitry Andric   // Implicit bool -> int conversion. True is 1, false is 0.
1740eae32dcSDimitry Andric   const uint32_t __mmShift = __ieeeMantissa != 0 || __ieeeExponent <= 1;
1750eae32dcSDimitry Andric   const uint32_t __mm = 4 * __m2 - 1 - __mmShift;
1760eae32dcSDimitry Andric 
1770eae32dcSDimitry Andric   // Step 3: Convert to a decimal power base using 64-bit arithmetic.
1780eae32dcSDimitry Andric   uint32_t __vr, __vp, __vm;
1790eae32dcSDimitry Andric   int32_t __e10;
1800eae32dcSDimitry Andric   bool __vmIsTrailingZeros = false;
1810eae32dcSDimitry Andric   bool __vrIsTrailingZeros = false;
1820eae32dcSDimitry Andric   uint8_t __lastRemovedDigit = 0;
1830eae32dcSDimitry Andric   if (__e2 >= 0) {
1840eae32dcSDimitry Andric     const uint32_t __q = __log10Pow2(__e2);
1850eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q);
1860eae32dcSDimitry Andric     const int32_t __k = __FLOAT_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q)) - 1;
1870eae32dcSDimitry Andric     const int32_t __i = -__e2 + static_cast<int32_t>(__q) + __k;
1880eae32dcSDimitry Andric     __vr = __mulPow5InvDivPow2(__mv, __q, __i);
1890eae32dcSDimitry Andric     __vp = __mulPow5InvDivPow2(__mp, __q, __i);
1900eae32dcSDimitry Andric     __vm = __mulPow5InvDivPow2(__mm, __q, __i);
1910eae32dcSDimitry Andric     if (__q != 0 && (__vp - 1) / 10 <= __vm / 10) {
1920eae32dcSDimitry Andric       // We need to know one removed digit even if we are not going to loop below. We could use
1930eae32dcSDimitry Andric       // __q = X - 1 above, except that would require 33 bits for the result, and we've found that
1940eae32dcSDimitry Andric       // 32-bit arithmetic is faster even on 64-bit machines.
1950eae32dcSDimitry Andric       const int32_t __l = __FLOAT_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q - 1)) - 1;
1960eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__mulPow5InvDivPow2(__mv, __q - 1,
1970eae32dcSDimitry Andric         -__e2 + static_cast<int32_t>(__q) - 1 + __l) % 10);
1980eae32dcSDimitry Andric     }
1990eae32dcSDimitry Andric     if (__q <= 9) {
2000eae32dcSDimitry Andric       // The largest power of 5 that fits in 24 bits is 5^10, but __q <= 9 seems to be safe as well.
2010eae32dcSDimitry Andric       // Only one of __mp, __mv, and __mm can be a multiple of 5, if any.
2020eae32dcSDimitry Andric       if (__mv % 5 == 0) {
2030eae32dcSDimitry Andric         __vrIsTrailingZeros = __multipleOfPowerOf5(__mv, __q);
2040eae32dcSDimitry Andric       } else if (__acceptBounds) {
2050eae32dcSDimitry Andric         __vmIsTrailingZeros = __multipleOfPowerOf5(__mm, __q);
2060eae32dcSDimitry Andric       } else {
2070eae32dcSDimitry Andric         __vp -= __multipleOfPowerOf5(__mp, __q);
2080eae32dcSDimitry Andric       }
2090eae32dcSDimitry Andric     }
2100eae32dcSDimitry Andric   } else {
2110eae32dcSDimitry Andric     const uint32_t __q = __log10Pow5(-__e2);
2120eae32dcSDimitry Andric     __e10 = static_cast<int32_t>(__q) + __e2;
2130eae32dcSDimitry Andric     const int32_t __i = -__e2 - static_cast<int32_t>(__q);
2140eae32dcSDimitry Andric     const int32_t __k = __pow5bits(__i) - __FLOAT_POW5_BITCOUNT;
2150eae32dcSDimitry Andric     int32_t __j = static_cast<int32_t>(__q) - __k;
2160eae32dcSDimitry Andric     __vr = __mulPow5divPow2(__mv, static_cast<uint32_t>(__i), __j);
2170eae32dcSDimitry Andric     __vp = __mulPow5divPow2(__mp, static_cast<uint32_t>(__i), __j);
2180eae32dcSDimitry Andric     __vm = __mulPow5divPow2(__mm, static_cast<uint32_t>(__i), __j);
2190eae32dcSDimitry Andric     if (__q != 0 && (__vp - 1) / 10 <= __vm / 10) {
2200eae32dcSDimitry Andric       __j = static_cast<int32_t>(__q) - 1 - (__pow5bits(__i + 1) - __FLOAT_POW5_BITCOUNT);
2210eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__mulPow5divPow2(__mv, static_cast<uint32_t>(__i + 1), __j) % 10);
2220eae32dcSDimitry Andric     }
2230eae32dcSDimitry Andric     if (__q <= 1) {
2240eae32dcSDimitry Andric       // {__vr,__vp,__vm} is trailing zeros if {__mv,__mp,__mm} has at least __q trailing 0 bits.
2250eae32dcSDimitry Andric       // __mv = 4 * __m2, so it always has at least two trailing 0 bits.
2260eae32dcSDimitry Andric       __vrIsTrailingZeros = true;
2270eae32dcSDimitry Andric       if (__acceptBounds) {
2280eae32dcSDimitry Andric         // __mm = __mv - 1 - __mmShift, so it has 1 trailing 0 bit iff __mmShift == 1.
2290eae32dcSDimitry Andric         __vmIsTrailingZeros = __mmShift == 1;
2300eae32dcSDimitry Andric       } else {
2310eae32dcSDimitry Andric         // __mp = __mv + 2, so it always has at least one trailing 0 bit.
2320eae32dcSDimitry Andric         --__vp;
2330eae32dcSDimitry Andric       }
2340eae32dcSDimitry Andric     } else if (__q < 31) { // TRANSITION(ulfjack): Use a tighter bound here.
2350eae32dcSDimitry Andric       __vrIsTrailingZeros = __multipleOfPowerOf2(__mv, __q - 1);
2360eae32dcSDimitry Andric     }
2370eae32dcSDimitry Andric   }
2380eae32dcSDimitry Andric 
2390eae32dcSDimitry Andric   // Step 4: Find the shortest decimal representation in the interval of valid representations.
2400eae32dcSDimitry Andric   int32_t __removed = 0;
2410eae32dcSDimitry Andric   uint32_t _Output;
2420eae32dcSDimitry Andric   if (__vmIsTrailingZeros || __vrIsTrailingZeros) {
2430eae32dcSDimitry Andric     // General case, which happens rarely (~4.0%).
2440eae32dcSDimitry Andric     while (__vp / 10 > __vm / 10) {
2450eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-23106
2460eae32dcSDimitry Andric       __vmIsTrailingZeros &= __vm - (__vm / 10) * 10 == 0;
2470eae32dcSDimitry Andric #else
2480eae32dcSDimitry Andric       __vmIsTrailingZeros &= __vm % 10 == 0;
2490eae32dcSDimitry Andric #endif
2500eae32dcSDimitry Andric       __vrIsTrailingZeros &= __lastRemovedDigit == 0;
2510eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
2520eae32dcSDimitry Andric       __vr /= 10;
2530eae32dcSDimitry Andric       __vp /= 10;
2540eae32dcSDimitry Andric       __vm /= 10;
2550eae32dcSDimitry Andric       ++__removed;
2560eae32dcSDimitry Andric     }
2570eae32dcSDimitry Andric     if (__vmIsTrailingZeros) {
2580eae32dcSDimitry Andric       while (__vm % 10 == 0) {
2590eae32dcSDimitry Andric         __vrIsTrailingZeros &= __lastRemovedDigit == 0;
2600eae32dcSDimitry Andric         __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
2610eae32dcSDimitry Andric         __vr /= 10;
2620eae32dcSDimitry Andric         __vp /= 10;
2630eae32dcSDimitry Andric         __vm /= 10;
2640eae32dcSDimitry Andric         ++__removed;
2650eae32dcSDimitry Andric       }
2660eae32dcSDimitry Andric     }
2670eae32dcSDimitry Andric     if (__vrIsTrailingZeros && __lastRemovedDigit == 5 && __vr % 2 == 0) {
2680eae32dcSDimitry Andric       // Round even if the exact number is .....50..0.
2690eae32dcSDimitry Andric       __lastRemovedDigit = 4;
2700eae32dcSDimitry Andric     }
2710eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
2720eae32dcSDimitry Andric     _Output = __vr + ((__vr == __vm && (!__acceptBounds || !__vmIsTrailingZeros)) || __lastRemovedDigit >= 5);
2730eae32dcSDimitry Andric   } else {
2740eae32dcSDimitry Andric     // Specialized for the common case (~96.0%). Percentages below are relative to this.
2750eae32dcSDimitry Andric     // Loop iterations below (approximately):
2760eae32dcSDimitry Andric     // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01%
2770eae32dcSDimitry Andric     while (__vp / 10 > __vm / 10) {
2780eae32dcSDimitry Andric       __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
2790eae32dcSDimitry Andric       __vr /= 10;
2800eae32dcSDimitry Andric       __vp /= 10;
2810eae32dcSDimitry Andric       __vm /= 10;
2820eae32dcSDimitry Andric       ++__removed;
2830eae32dcSDimitry Andric     }
2840eae32dcSDimitry Andric     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
2850eae32dcSDimitry Andric     _Output = __vr + (__vr == __vm || __lastRemovedDigit >= 5);
2860eae32dcSDimitry Andric   }
2870eae32dcSDimitry Andric   const int32_t __exp = __e10 + __removed;
2880eae32dcSDimitry Andric 
2890eae32dcSDimitry Andric   __floating_decimal_32 __fd;
2900eae32dcSDimitry Andric   __fd.__exponent = __exp;
2910eae32dcSDimitry Andric   __fd.__mantissa = _Output;
2920eae32dcSDimitry Andric   return __fd;
2930eae32dcSDimitry Andric }
2940eae32dcSDimitry Andric 
2950eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result _Large_integer_to_chars(char* const _First, char* const _Last,
2960eae32dcSDimitry Andric   const uint32_t _Mantissa2, const int32_t _Exponent2) {
2970eae32dcSDimitry Andric 
2980eae32dcSDimitry Andric   // Print the integer _Mantissa2 * 2^_Exponent2 exactly.
2990eae32dcSDimitry Andric 
3000eae32dcSDimitry Andric   // For nonzero integers, _Exponent2 >= -23. (The minimum value occurs when _Mantissa2 * 2^_Exponent2 is 1.
3010eae32dcSDimitry Andric   // In that case, _Mantissa2 is the implicit 1 bit followed by 23 zeros, so _Exponent2 is -23 to shift away
3020eae32dcSDimitry Andric   // the zeros.) The dense range of exactly representable integers has negative or zero exponents
3030eae32dcSDimitry Andric   // (as positive exponents make the range non-dense). For that dense range, Ryu will always be used:
3040eae32dcSDimitry Andric   // every digit is necessary to uniquely identify the value, so Ryu must print them all.
3050eae32dcSDimitry Andric 
3060eae32dcSDimitry Andric   // Positive exponents are the non-dense range of exactly representable integers.
3070eae32dcSDimitry Andric   // This contains all of the values for which Ryu can't be used (and a few Ryu-friendly values).
3080eae32dcSDimitry Andric 
3090eae32dcSDimitry Andric   // Performance note: Long division appears to be faster than losslessly widening float to double and calling
3100eae32dcSDimitry Andric   // __d2fixed_buffered_n(). If __f2fixed_buffered_n() is implemented, it might be faster than long division.
3110eae32dcSDimitry Andric 
312*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(_Exponent2 > 0, "");
313*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(_Exponent2 <= 104, ""); // because __ieeeExponent <= 254
3140eae32dcSDimitry Andric 
3150eae32dcSDimitry Andric   // Manually represent _Mantissa2 * 2^_Exponent2 as a large integer. _Mantissa2 is always 24 bits
3160eae32dcSDimitry Andric   // (due to the implicit bit), while _Exponent2 indicates a shift of at most 104 bits.
3170eae32dcSDimitry Andric   // 24 + 104 equals 128 equals 4 * 32, so we need exactly 4 32-bit elements.
3180eae32dcSDimitry Andric   // We use a little-endian representation, visualized like this:
3190eae32dcSDimitry Andric 
3200eae32dcSDimitry Andric   // << left shift <<
3210eae32dcSDimitry Andric   // most significant
3220eae32dcSDimitry Andric   // _Data[3] _Data[2] _Data[1] _Data[0]
3230eae32dcSDimitry Andric   //                   least significant
3240eae32dcSDimitry Andric   //                   >> right shift >>
3250eae32dcSDimitry Andric 
3260eae32dcSDimitry Andric   constexpr uint32_t _Data_size = 4;
3270eae32dcSDimitry Andric   uint32_t _Data[_Data_size]{};
3280eae32dcSDimitry Andric 
3290eae32dcSDimitry Andric   // _Maxidx is the index of the most significant nonzero element.
3300eae32dcSDimitry Andric   uint32_t _Maxidx = ((24 + static_cast<uint32_t>(_Exponent2) + 31) / 32) - 1;
331*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(_Maxidx < _Data_size, "");
3320eae32dcSDimitry Andric 
3330eae32dcSDimitry Andric   const uint32_t _Bit_shift = static_cast<uint32_t>(_Exponent2) % 32;
3340eae32dcSDimitry Andric   if (_Bit_shift <= 8) { // _Mantissa2's 24 bits don't cross an element boundary
3350eae32dcSDimitry Andric     _Data[_Maxidx] = _Mantissa2 << _Bit_shift;
3360eae32dcSDimitry Andric   } else { // _Mantissa2's 24 bits cross an element boundary
3370eae32dcSDimitry Andric     _Data[_Maxidx - 1] = _Mantissa2 << _Bit_shift;
3380eae32dcSDimitry Andric     _Data[_Maxidx] = _Mantissa2 >> (32 - _Bit_shift);
3390eae32dcSDimitry Andric   }
3400eae32dcSDimitry Andric 
3410eae32dcSDimitry Andric   // If Ryu hasn't determined the total output length, we need to buffer the digits generated from right to left
3420eae32dcSDimitry Andric   // by long division. The largest possible float is: 340'282346638'528859811'704183484'516925440
3430eae32dcSDimitry Andric   uint32_t _Blocks[4];
3440eae32dcSDimitry Andric   int32_t _Filled_blocks = 0;
3450eae32dcSDimitry Andric   // From left to right, we're going to print:
3460eae32dcSDimitry Andric   // _Data[0] will be [1, 10] digits.
3470eae32dcSDimitry Andric   // Then if _Filled_blocks > 0:
3480eae32dcSDimitry Andric   // _Blocks[_Filled_blocks - 1], ..., _Blocks[0] will be 0-filled 9-digit blocks.
3490eae32dcSDimitry Andric 
3500eae32dcSDimitry Andric   if (_Maxidx != 0) { // If the integer is actually large, perform long division.
3510eae32dcSDimitry Andric                       // Otherwise, skip to printing _Data[0].
3520eae32dcSDimitry Andric     for (;;) {
3530eae32dcSDimitry Andric       // Loop invariant: _Maxidx != 0 (i.e. the integer is actually large)
3540eae32dcSDimitry Andric 
3550eae32dcSDimitry Andric       const uint32_t _Most_significant_elem = _Data[_Maxidx];
3560eae32dcSDimitry Andric       const uint32_t _Initial_remainder = _Most_significant_elem % 1000000000;
3570eae32dcSDimitry Andric       const uint32_t _Initial_quotient = _Most_significant_elem / 1000000000;
3580eae32dcSDimitry Andric       _Data[_Maxidx] = _Initial_quotient;
3590eae32dcSDimitry Andric       uint64_t _Remainder = _Initial_remainder;
3600eae32dcSDimitry Andric 
3610eae32dcSDimitry Andric       // Process less significant elements.
3620eae32dcSDimitry Andric       uint32_t _Idx = _Maxidx;
3630eae32dcSDimitry Andric       do {
3640eae32dcSDimitry Andric         --_Idx; // Initially, _Remainder is at most 10^9 - 1.
3650eae32dcSDimitry Andric 
3660eae32dcSDimitry Andric         // Now, _Remainder is at most (10^9 - 1) * 2^32 + 2^32 - 1, simplified to 10^9 * 2^32 - 1.
3670eae32dcSDimitry Andric         _Remainder = (_Remainder << 32) | _Data[_Idx];
3680eae32dcSDimitry Andric 
3690eae32dcSDimitry Andric         // floor((10^9 * 2^32 - 1) / 10^9) == 2^32 - 1, so uint32_t _Quotient is lossless.
3700eae32dcSDimitry Andric         const uint32_t _Quotient = static_cast<uint32_t>(__div1e9(_Remainder));
3710eae32dcSDimitry Andric 
3720eae32dcSDimitry Andric         // _Remainder is at most 10^9 - 1 again.
3730eae32dcSDimitry Andric         // For uint32_t truncation, see the __mod1e9() comment in d2s_intrinsics.h.
3740eae32dcSDimitry Andric         _Remainder = static_cast<uint32_t>(_Remainder) - 1000000000u * _Quotient;
3750eae32dcSDimitry Andric 
3760eae32dcSDimitry Andric         _Data[_Idx] = _Quotient;
3770eae32dcSDimitry Andric       } while (_Idx != 0);
3780eae32dcSDimitry Andric 
3790eae32dcSDimitry Andric       // Store a 0-filled 9-digit block.
3800eae32dcSDimitry Andric       _Blocks[_Filled_blocks++] = static_cast<uint32_t>(_Remainder);
3810eae32dcSDimitry Andric 
3820eae32dcSDimitry Andric       if (_Initial_quotient == 0) { // Is the large integer shrinking?
3830eae32dcSDimitry Andric         --_Maxidx; // log2(10^9) is 29.9, so we can't shrink by more than one element.
3840eae32dcSDimitry Andric         if (_Maxidx == 0) {
3850eae32dcSDimitry Andric           break; // We've finished long division. Now we need to print _Data[0].
3860eae32dcSDimitry Andric         }
3870eae32dcSDimitry Andric       }
3880eae32dcSDimitry Andric     }
3890eae32dcSDimitry Andric   }
3900eae32dcSDimitry Andric 
391*5f757f3fSDimitry Andric   _LIBCPP_ASSERT_INTERNAL(_Data[0] != 0, "");
3920eae32dcSDimitry Andric   for (uint32_t _Idx = 1; _Idx < _Data_size; ++_Idx) {
393*5f757f3fSDimitry Andric     _LIBCPP_ASSERT_INTERNAL(_Data[_Idx] == 0, "");
3940eae32dcSDimitry Andric   }
3950eae32dcSDimitry Andric 
3960eae32dcSDimitry Andric   const uint32_t _Data_olength = _Data[0] >= 1000000000 ? 10 : __decimalLength9(_Data[0]);
3970eae32dcSDimitry Andric   const uint32_t _Total_fixed_length = _Data_olength + 9 * _Filled_blocks;
3980eae32dcSDimitry Andric 
3990eae32dcSDimitry Andric   if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
4000eae32dcSDimitry Andric     return { _Last, errc::value_too_large };
4010eae32dcSDimitry Andric   }
4020eae32dcSDimitry Andric 
4030eae32dcSDimitry Andric   char* _Result = _First;
4040eae32dcSDimitry Andric 
4050eae32dcSDimitry Andric   // Print _Data[0]. While it's up to 10 digits,
4060eae32dcSDimitry Andric   // which is more than Ryu generates, the code below can handle this.
4070eae32dcSDimitry Andric   __append_n_digits(_Data_olength, _Data[0], _Result);
4080eae32dcSDimitry Andric   _Result += _Data_olength;
4090eae32dcSDimitry Andric 
4100eae32dcSDimitry Andric   // Print 0-filled 9-digit blocks.
4110eae32dcSDimitry Andric   for (int32_t _Idx = _Filled_blocks - 1; _Idx >= 0; --_Idx) {
4120eae32dcSDimitry Andric     __append_nine_digits(_Blocks[_Idx], _Result);
4130eae32dcSDimitry Andric     _Result += 9;
4140eae32dcSDimitry Andric   }
4150eae32dcSDimitry Andric 
4160eae32dcSDimitry Andric   return { _Result, errc{} };
4170eae32dcSDimitry Andric }
4180eae32dcSDimitry Andric 
4190eae32dcSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result __to_chars(char* const _First, char* const _Last, const __floating_decimal_32 __v,
4200eae32dcSDimitry Andric   chars_format _Fmt, const uint32_t __ieeeMantissa, const uint32_t __ieeeExponent) {
4210eae32dcSDimitry Andric   // Step 5: Print the decimal representation.
4220eae32dcSDimitry Andric   uint32_t _Output = __v.__mantissa;
4230eae32dcSDimitry Andric   int32_t _Ryu_exponent = __v.__exponent;
4240eae32dcSDimitry Andric   const uint32_t __olength = __decimalLength9(_Output);
4250eae32dcSDimitry Andric   int32_t _Scientific_exponent = _Ryu_exponent + static_cast<int32_t>(__olength) - 1;
4260eae32dcSDimitry Andric 
4270eae32dcSDimitry Andric   if (_Fmt == chars_format{}) {
4280eae32dcSDimitry Andric     int32_t _Lower;
4290eae32dcSDimitry Andric     int32_t _Upper;
4300eae32dcSDimitry Andric 
4310eae32dcSDimitry Andric     if (__olength == 1) {
4320eae32dcSDimitry Andric       // Value | Fixed   | Scientific
4330eae32dcSDimitry Andric       // 1e-3  | "0.001" | "1e-03"
4340eae32dcSDimitry Andric       // 1e4   | "10000" | "1e+04"
4350eae32dcSDimitry Andric       _Lower = -3;
4360eae32dcSDimitry Andric       _Upper = 4;
4370eae32dcSDimitry Andric     } else {
4380eae32dcSDimitry Andric       // Value   | Fixed       | Scientific
4390eae32dcSDimitry Andric       // 1234e-7 | "0.0001234" | "1.234e-04"
4400eae32dcSDimitry Andric       // 1234e5  | "123400000" | "1.234e+08"
4410eae32dcSDimitry Andric       _Lower = -static_cast<int32_t>(__olength + 3);
4420eae32dcSDimitry Andric       _Upper = 5;
4430eae32dcSDimitry Andric     }
4440eae32dcSDimitry Andric 
4450eae32dcSDimitry Andric     if (_Lower <= _Ryu_exponent && _Ryu_exponent <= _Upper) {
4460eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
4470eae32dcSDimitry Andric     } else {
4480eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
4490eae32dcSDimitry Andric     }
4500eae32dcSDimitry Andric   } else if (_Fmt == chars_format::general) {
4510eae32dcSDimitry Andric     // C11 7.21.6.1 "The fprintf function"/8:
4520eae32dcSDimitry Andric     // "Let P equal [...] 6 if the precision is omitted [...].
4530eae32dcSDimitry Andric     // Then, if a conversion with style E would have an exponent of X:
4540eae32dcSDimitry Andric     // - if P > X >= -4, the conversion is with style f [...].
4550eae32dcSDimitry Andric     // - otherwise, the conversion is with style e [...]."
4560eae32dcSDimitry Andric     if (-4 <= _Scientific_exponent && _Scientific_exponent < 6) {
4570eae32dcSDimitry Andric       _Fmt = chars_format::fixed;
4580eae32dcSDimitry Andric     } else {
4590eae32dcSDimitry Andric       _Fmt = chars_format::scientific;
4600eae32dcSDimitry Andric     }
4610eae32dcSDimitry Andric   }
4620eae32dcSDimitry Andric 
4630eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
4640eae32dcSDimitry Andric     // Example: _Output == 1729, __olength == 4
4650eae32dcSDimitry Andric 
4660eae32dcSDimitry Andric     // _Ryu_exponent | Printed  | _Whole_digits | _Total_fixed_length  | Notes
4670eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4680eae32dcSDimitry Andric     //             2 | 172900   |  6            | _Whole_digits        | Ryu can't be used for printing
4690eae32dcSDimitry Andric     //             1 | 17290    |  5            | (sometimes adjusted) | when the trimmed digits are nonzero.
4700eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4710eae32dcSDimitry Andric     //             0 | 1729     |  4            | _Whole_digits        | Unified length cases.
4720eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4730eae32dcSDimitry Andric     //            -1 | 172.9    |  3            | __olength + 1        | This case can't happen for
4740eae32dcSDimitry Andric     //            -2 | 17.29    |  2            |                      | __olength == 1, but no additional
4750eae32dcSDimitry Andric     //            -3 | 1.729    |  1            |                      | code is needed to avoid it.
4760eae32dcSDimitry Andric     // --------------|----------|---------------|----------------------|---------------------------------------
4770eae32dcSDimitry Andric     //            -4 | 0.1729   |  0            | 2 - _Ryu_exponent    | C11 7.21.6.1 "The fprintf function"/8:
4780eae32dcSDimitry Andric     //            -5 | 0.01729  | -1            |                      | "If a decimal-point character appears,
4790eae32dcSDimitry Andric     //            -6 | 0.001729 | -2            |                      | at least one digit appears before it."
4800eae32dcSDimitry Andric 
4810eae32dcSDimitry Andric     const int32_t _Whole_digits = static_cast<int32_t>(__olength) + _Ryu_exponent;
4820eae32dcSDimitry Andric 
4830eae32dcSDimitry Andric     uint32_t _Total_fixed_length;
4840eae32dcSDimitry Andric     if (_Ryu_exponent >= 0) { // cases "172900" and "1729"
4850eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(_Whole_digits);
4860eae32dcSDimitry Andric       if (_Output == 1) {
4870eae32dcSDimitry Andric         // Rounding can affect the number of digits.
4880eae32dcSDimitry Andric         // For example, 1e11f is exactly "99999997952" which is 11 digits instead of 12.
4890eae32dcSDimitry Andric         // We can use a lookup table to detect this and adjust the total length.
4900eae32dcSDimitry Andric         static constexpr uint8_t _Adjustment[39] = {
4910eae32dcSDimitry Andric           0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,1,1,1,0,0,1,1,0,1,0,1,1,0,0,1,0,1,1,0,1,1,1 };
4920eae32dcSDimitry Andric         _Total_fixed_length -= _Adjustment[_Ryu_exponent];
4930eae32dcSDimitry Andric         // _Whole_digits doesn't need to be adjusted because these cases won't refer to it later.
4940eae32dcSDimitry Andric       }
4950eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
4960eae32dcSDimitry Andric       _Total_fixed_length = __olength + 1;
4970eae32dcSDimitry Andric     } else { // case "0.001729"
4980eae32dcSDimitry Andric       _Total_fixed_length = static_cast<uint32_t>(2 - _Ryu_exponent);
4990eae32dcSDimitry Andric     }
5000eae32dcSDimitry Andric 
5010eae32dcSDimitry Andric     if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
5020eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
5030eae32dcSDimitry Andric     }
5040eae32dcSDimitry Andric 
5050eae32dcSDimitry Andric     char* _Mid;
5060eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900"
5070eae32dcSDimitry Andric       bool _Can_use_ryu;
5080eae32dcSDimitry Andric 
5090eae32dcSDimitry Andric       if (_Ryu_exponent > 10) { // 10^10 is the largest power of 10 that's exactly representable as a float.
5100eae32dcSDimitry Andric         _Can_use_ryu = false;
5110eae32dcSDimitry Andric       } else {
5120eae32dcSDimitry Andric         // Ryu generated X: __v.__mantissa * 10^_Ryu_exponent
5130eae32dcSDimitry Andric         // __v.__mantissa == 2^_Trailing_zero_bits * (__v.__mantissa >> _Trailing_zero_bits)
5140eae32dcSDimitry Andric         // 10^_Ryu_exponent == 2^_Ryu_exponent * 5^_Ryu_exponent
5150eae32dcSDimitry Andric 
5160eae32dcSDimitry Andric         // _Trailing_zero_bits is [0, 29] (aside: because 2^29 is the largest power of 2
5170eae32dcSDimitry Andric         // with 9 decimal digits, which is float's round-trip limit.)
5180eae32dcSDimitry Andric         // _Ryu_exponent is [1, 10].
5190eae32dcSDimitry Andric         // Normalization adds [2, 23] (aside: at least 2 because the pre-normalized mantissa is at least 5).
5200eae32dcSDimitry Andric         // This adds up to [3, 62], which is well below float's maximum binary exponent 127.
5210eae32dcSDimitry Andric 
5220eae32dcSDimitry Andric         // Therefore, we just need to consider (__v.__mantissa >> _Trailing_zero_bits) * 5^_Ryu_exponent.
5230eae32dcSDimitry Andric 
5240eae32dcSDimitry Andric         // If that product would exceed 24 bits, then X can't be exactly represented as a float.
5250eae32dcSDimitry Andric         // (That's not a problem for round-tripping, because X is close enough to the original float,
5260eae32dcSDimitry Andric         // but X isn't mathematically equal to the original float.) This requires a high-precision fallback.
5270eae32dcSDimitry Andric 
5280eae32dcSDimitry Andric         // If the product is 24 bits or smaller, then X can be exactly represented as a float (and we don't
5290eae32dcSDimitry Andric         // need to re-synthesize it; the original float must have been X, because Ryu wouldn't produce the
5300eae32dcSDimitry Andric         // same output for two different floats X and Y). This allows Ryu's output to be used (zero-filled).
5310eae32dcSDimitry Andric 
5320eae32dcSDimitry Andric         // (2^24 - 1) / 5^0 (for indexing), (2^24 - 1) / 5^1, ..., (2^24 - 1) / 5^10
5330eae32dcSDimitry Andric         static constexpr uint32_t _Max_shifted_mantissa[11] = {
5340eae32dcSDimitry Andric           16777215, 3355443, 671088, 134217, 26843, 5368, 1073, 214, 42, 8, 1 };
5350eae32dcSDimitry Andric 
5360eae32dcSDimitry Andric         unsigned long _Trailing_zero_bits;
5370eae32dcSDimitry Andric         (void) _BitScanForward(&_Trailing_zero_bits, __v.__mantissa); // __v.__mantissa is guaranteed nonzero
5380eae32dcSDimitry Andric         const uint32_t _Shifted_mantissa = __v.__mantissa >> _Trailing_zero_bits;
5390eae32dcSDimitry Andric         _Can_use_ryu = _Shifted_mantissa <= _Max_shifted_mantissa[_Ryu_exponent];
5400eae32dcSDimitry Andric       }
5410eae32dcSDimitry Andric 
5420eae32dcSDimitry Andric       if (!_Can_use_ryu) {
5430eae32dcSDimitry Andric         const uint32_t _Mantissa2 = __ieeeMantissa | (1u << __FLOAT_MANTISSA_BITS); // restore implicit bit
5440eae32dcSDimitry Andric         const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
5450eae32dcSDimitry Andric           - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS; // bias and normalization
5460eae32dcSDimitry Andric 
5470eae32dcSDimitry Andric         // Performance note: We've already called Ryu, so this will redundantly perform buffering and bounds checking.
5480eae32dcSDimitry Andric         return _Large_integer_to_chars(_First, _Last, _Mantissa2, _Exponent2);
5490eae32dcSDimitry Andric       }
5500eae32dcSDimitry Andric 
5510eae32dcSDimitry Andric       // _Can_use_ryu
5520eae32dcSDimitry Andric       // Print the decimal digits, left-aligned within [_First, _First + _Total_fixed_length).
5530eae32dcSDimitry Andric       _Mid = _First + __olength;
5540eae32dcSDimitry Andric     } else { // cases "1729", "17.29", and "0.001729"
5550eae32dcSDimitry Andric       // Print the decimal digits, right-aligned within [_First, _First + _Total_fixed_length).
5560eae32dcSDimitry Andric       _Mid = _First + _Total_fixed_length;
5570eae32dcSDimitry Andric     }
5580eae32dcSDimitry Andric 
5590eae32dcSDimitry Andric     while (_Output >= 10000) {
5600eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
5610eae32dcSDimitry Andric       const uint32_t __c = _Output - 10000 * (_Output / 10000);
5620eae32dcSDimitry Andric #else
5630eae32dcSDimitry Andric       const uint32_t __c = _Output % 10000;
5640eae32dcSDimitry Andric #endif
5650eae32dcSDimitry Andric       _Output /= 10000;
5660eae32dcSDimitry Andric       const uint32_t __c0 = (__c % 100) << 1;
5670eae32dcSDimitry Andric       const uint32_t __c1 = (__c / 100) << 1;
56806c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
56906c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
5700eae32dcSDimitry Andric     }
5710eae32dcSDimitry Andric     if (_Output >= 100) {
5720eae32dcSDimitry Andric       const uint32_t __c = (_Output % 100) << 1;
5730eae32dcSDimitry Andric       _Output /= 100;
57406c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
5750eae32dcSDimitry Andric     }
5760eae32dcSDimitry Andric     if (_Output >= 10) {
5770eae32dcSDimitry Andric       const uint32_t __c = _Output << 1;
57806c3fb27SDimitry Andric       std::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
5790eae32dcSDimitry Andric     } else {
5800eae32dcSDimitry Andric       *--_Mid = static_cast<char>('0' + _Output);
5810eae32dcSDimitry Andric     }
5820eae32dcSDimitry Andric 
5830eae32dcSDimitry Andric     if (_Ryu_exponent > 0) { // case "172900" with _Can_use_ryu
5840eae32dcSDimitry Andric       // Performance note: it might be more efficient to do this immediately after setting _Mid.
58506c3fb27SDimitry Andric       std::memset(_First + __olength, '0', static_cast<size_t>(_Ryu_exponent));
5860eae32dcSDimitry Andric     } else if (_Ryu_exponent == 0) { // case "1729"
5870eae32dcSDimitry Andric       // Done!
5880eae32dcSDimitry Andric     } else if (_Whole_digits > 0) { // case "17.29"
5890eae32dcSDimitry Andric       // Performance note: moving digits might not be optimal.
59006c3fb27SDimitry Andric       std::memmove(_First, _First + 1, static_cast<size_t>(_Whole_digits));
5910eae32dcSDimitry Andric       _First[_Whole_digits] = '.';
5920eae32dcSDimitry Andric     } else { // case "0.001729"
5930eae32dcSDimitry Andric       // Performance note: a larger memset() followed by overwriting '.' might be more efficient.
5940eae32dcSDimitry Andric       _First[0] = '0';
5950eae32dcSDimitry Andric       _First[1] = '.';
59606c3fb27SDimitry Andric       std::memset(_First + 2, '0', static_cast<size_t>(-_Whole_digits));
5970eae32dcSDimitry Andric     }
5980eae32dcSDimitry Andric 
5990eae32dcSDimitry Andric     return { _First + _Total_fixed_length, errc{} };
6000eae32dcSDimitry Andric   }
6010eae32dcSDimitry Andric 
6020eae32dcSDimitry Andric   const uint32_t _Total_scientific_length =
6030eae32dcSDimitry Andric     __olength + (__olength > 1) + 4; // digits + possible decimal point + scientific exponent
6040eae32dcSDimitry Andric   if (_Last - _First < static_cast<ptrdiff_t>(_Total_scientific_length)) {
6050eae32dcSDimitry Andric     return { _Last, errc::value_too_large };
6060eae32dcSDimitry Andric   }
6070eae32dcSDimitry Andric   char* const __result = _First;
6080eae32dcSDimitry Andric 
6090eae32dcSDimitry Andric   // Print the decimal digits.
6100eae32dcSDimitry Andric   uint32_t __i = 0;
6110eae32dcSDimitry Andric   while (_Output >= 10000) {
6120eae32dcSDimitry Andric #ifdef __clang__ // TRANSITION, LLVM-38217
6130eae32dcSDimitry Andric     const uint32_t __c = _Output - 10000 * (_Output / 10000);
6140eae32dcSDimitry Andric #else
6150eae32dcSDimitry Andric     const uint32_t __c = _Output % 10000;
6160eae32dcSDimitry Andric #endif
6170eae32dcSDimitry Andric     _Output /= 10000;
6180eae32dcSDimitry Andric     const uint32_t __c0 = (__c % 100) << 1;
6190eae32dcSDimitry Andric     const uint32_t __c1 = (__c / 100) << 1;
62006c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
62106c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
6220eae32dcSDimitry Andric     __i += 4;
6230eae32dcSDimitry Andric   }
6240eae32dcSDimitry Andric   if (_Output >= 100) {
6250eae32dcSDimitry Andric     const uint32_t __c = (_Output % 100) << 1;
6260eae32dcSDimitry Andric     _Output /= 100;
62706c3fb27SDimitry Andric     std::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c, 2);
6280eae32dcSDimitry Andric     __i += 2;
6290eae32dcSDimitry Andric   }
6300eae32dcSDimitry Andric   if (_Output >= 10) {
6310eae32dcSDimitry Andric     const uint32_t __c = _Output << 1;
6320eae32dcSDimitry Andric     // We can't use memcpy here: the decimal dot goes between these two digits.
6330eae32dcSDimitry Andric     __result[2] = __DIGIT_TABLE[__c + 1];
6340eae32dcSDimitry Andric     __result[0] = __DIGIT_TABLE[__c];
6350eae32dcSDimitry Andric   } else {
6360eae32dcSDimitry Andric     __result[0] = static_cast<char>('0' + _Output);
6370eae32dcSDimitry Andric   }
6380eae32dcSDimitry Andric 
6390eae32dcSDimitry Andric   // Print decimal point if needed.
6400eae32dcSDimitry Andric   uint32_t __index;
6410eae32dcSDimitry Andric   if (__olength > 1) {
6420eae32dcSDimitry Andric     __result[1] = '.';
6430eae32dcSDimitry Andric     __index = __olength + 1;
6440eae32dcSDimitry Andric   } else {
6450eae32dcSDimitry Andric     __index = 1;
6460eae32dcSDimitry Andric   }
6470eae32dcSDimitry Andric 
6480eae32dcSDimitry Andric   // Print the exponent.
6490eae32dcSDimitry Andric   __result[__index++] = 'e';
6500eae32dcSDimitry Andric   if (_Scientific_exponent < 0) {
6510eae32dcSDimitry Andric     __result[__index++] = '-';
6520eae32dcSDimitry Andric     _Scientific_exponent = -_Scientific_exponent;
6530eae32dcSDimitry Andric   } else {
6540eae32dcSDimitry Andric     __result[__index++] = '+';
6550eae32dcSDimitry Andric   }
6560eae32dcSDimitry Andric 
65706c3fb27SDimitry Andric   std::memcpy(__result + __index, __DIGIT_TABLE + 2 * _Scientific_exponent, 2);
6580eae32dcSDimitry Andric   __index += 2;
6590eae32dcSDimitry Andric 
6600eae32dcSDimitry Andric   return { _First + _Total_scientific_length, errc{} };
6610eae32dcSDimitry Andric }
6620eae32dcSDimitry Andric 
6630eae32dcSDimitry Andric [[nodiscard]] to_chars_result __f2s_buffered_n(char* const _First, char* const _Last, const float __f,
6640eae32dcSDimitry Andric   const chars_format _Fmt) {
6650eae32dcSDimitry Andric 
6660eae32dcSDimitry Andric   // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
6670eae32dcSDimitry Andric   const uint32_t __bits = __float_to_bits(__f);
6680eae32dcSDimitry Andric 
6690eae32dcSDimitry Andric   // Case distinction; exit early for the easy cases.
6700eae32dcSDimitry Andric   if (__bits == 0) {
6710eae32dcSDimitry Andric     if (_Fmt == chars_format::scientific) {
6720eae32dcSDimitry Andric       if (_Last - _First < 5) {
6730eae32dcSDimitry Andric         return { _Last, errc::value_too_large };
6740eae32dcSDimitry Andric       }
6750eae32dcSDimitry Andric 
67606c3fb27SDimitry Andric       std::memcpy(_First, "0e+00", 5);
6770eae32dcSDimitry Andric 
6780eae32dcSDimitry Andric       return { _First + 5, errc{} };
6790eae32dcSDimitry Andric     }
6800eae32dcSDimitry Andric 
6810eae32dcSDimitry Andric     // Print "0" for chars_format::fixed, chars_format::general, and chars_format{}.
6820eae32dcSDimitry Andric     if (_First == _Last) {
6830eae32dcSDimitry Andric       return { _Last, errc::value_too_large };
6840eae32dcSDimitry Andric     }
6850eae32dcSDimitry Andric 
6860eae32dcSDimitry Andric     *_First = '0';
6870eae32dcSDimitry Andric 
6880eae32dcSDimitry Andric     return { _First + 1, errc{} };
6890eae32dcSDimitry Andric   }
6900eae32dcSDimitry Andric 
6910eae32dcSDimitry Andric   // Decode __bits into mantissa and exponent.
6920eae32dcSDimitry Andric   const uint32_t __ieeeMantissa = __bits & ((1u << __FLOAT_MANTISSA_BITS) - 1);
6930eae32dcSDimitry Andric   const uint32_t __ieeeExponent = __bits >> __FLOAT_MANTISSA_BITS;
6940eae32dcSDimitry Andric 
6950eae32dcSDimitry Andric   // When _Fmt == chars_format::fixed and the floating-point number is a large integer,
6960eae32dcSDimitry Andric   // it's faster to skip Ryu and immediately print the integer exactly.
6970eae32dcSDimitry Andric   if (_Fmt == chars_format::fixed) {
6980eae32dcSDimitry Andric     const uint32_t _Mantissa2 = __ieeeMantissa | (1u << __FLOAT_MANTISSA_BITS); // restore implicit bit
6990eae32dcSDimitry Andric     const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
7000eae32dcSDimitry Andric       - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS; // bias and normalization
7010eae32dcSDimitry Andric 
7020eae32dcSDimitry Andric     // Normal values are equal to _Mantissa2 * 2^_Exponent2.
7030eae32dcSDimitry Andric     // (Subnormals are different, but they'll be rejected by the _Exponent2 test here, so they can be ignored.)
7040eae32dcSDimitry Andric 
7050eae32dcSDimitry Andric     if (_Exponent2 > 0) {
7060eae32dcSDimitry Andric       return _Large_integer_to_chars(_First, _Last, _Mantissa2, _Exponent2);
7070eae32dcSDimitry Andric     }
7080eae32dcSDimitry Andric   }
7090eae32dcSDimitry Andric 
7100eae32dcSDimitry Andric   const __floating_decimal_32 __v = __f2d(__ieeeMantissa, __ieeeExponent);
7110eae32dcSDimitry Andric   return __to_chars(_First, _Last, __v, _Fmt, __ieeeMantissa, __ieeeExponent);
7120eae32dcSDimitry Andric }
7130eae32dcSDimitry Andric 
7140eae32dcSDimitry Andric _LIBCPP_END_NAMESPACE_STD
7150eae32dcSDimitry Andric 
7160eae32dcSDimitry Andric // clang-format on
717