xref: /openbsd-src/gnu/llvm/libcxx/src/ryu/f2s.cpp (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
2*4bdff4beSrobert //
3*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
5*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4bdff4beSrobert //
7*4bdff4beSrobert //===----------------------------------------------------------------------===//
8*4bdff4beSrobert 
9*4bdff4beSrobert // Copyright (c) Microsoft Corporation.
10*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11*4bdff4beSrobert 
12*4bdff4beSrobert // Copyright 2018 Ulf Adams
13*4bdff4beSrobert // Copyright (c) Microsoft Corporation. All rights reserved.
14*4bdff4beSrobert 
15*4bdff4beSrobert // Boost Software License - Version 1.0 - August 17th, 2003
16*4bdff4beSrobert 
17*4bdff4beSrobert // Permission is hereby granted, free of charge, to any person or organization
18*4bdff4beSrobert // obtaining a copy of the software and accompanying documentation covered by
19*4bdff4beSrobert // this license (the "Software") to use, reproduce, display, distribute,
20*4bdff4beSrobert // execute, and transmit the Software, and to prepare derivative works of the
21*4bdff4beSrobert // Software, and to permit third-parties to whom the Software is furnished to
22*4bdff4beSrobert // do so, all subject to the following:
23*4bdff4beSrobert 
24*4bdff4beSrobert // The copyright notices in the Software and this entire statement, including
25*4bdff4beSrobert // the above license grant, this restriction and the following disclaimer,
26*4bdff4beSrobert // must be included in all copies of the Software, in whole or in part, and
27*4bdff4beSrobert // all derivative works of the Software, unless such copies or derivative
28*4bdff4beSrobert // works are solely in the form of machine-executable object code generated by
29*4bdff4beSrobert // a source language processor.
30*4bdff4beSrobert 
31*4bdff4beSrobert // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32*4bdff4beSrobert // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33*4bdff4beSrobert // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
34*4bdff4beSrobert // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
35*4bdff4beSrobert // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
36*4bdff4beSrobert // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37*4bdff4beSrobert // DEALINGS IN THE SOFTWARE.
38*4bdff4beSrobert 
39*4bdff4beSrobert // Avoid formatting to keep the changes with the original code minimal.
40*4bdff4beSrobert // clang-format off
41*4bdff4beSrobert 
42*4bdff4beSrobert #include <__assert>
43*4bdff4beSrobert #include <__config>
44*4bdff4beSrobert #include <charconv>
45*4bdff4beSrobert 
46*4bdff4beSrobert #include "include/ryu/common.h"
47*4bdff4beSrobert #include "include/ryu/d2fixed.h"
48*4bdff4beSrobert #include "include/ryu/d2s_intrinsics.h"
49*4bdff4beSrobert #include "include/ryu/digit_table.h"
50*4bdff4beSrobert #include "include/ryu/f2s.h"
51*4bdff4beSrobert #include "include/ryu/ryu.h"
52*4bdff4beSrobert 
53*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
54*4bdff4beSrobert 
55*4bdff4beSrobert inline constexpr int __FLOAT_MANTISSA_BITS = 23;
56*4bdff4beSrobert inline constexpr int __FLOAT_EXPONENT_BITS = 8;
57*4bdff4beSrobert inline constexpr int __FLOAT_BIAS = 127;
58*4bdff4beSrobert 
59*4bdff4beSrobert inline constexpr int __FLOAT_POW5_INV_BITCOUNT = 59;
60*4bdff4beSrobert inline constexpr uint64_t __FLOAT_POW5_INV_SPLIT[31] = {
61*4bdff4beSrobert   576460752303423489u, 461168601842738791u, 368934881474191033u, 295147905179352826u,
62*4bdff4beSrobert   472236648286964522u, 377789318629571618u, 302231454903657294u, 483570327845851670u,
63*4bdff4beSrobert   386856262276681336u, 309485009821345069u, 495176015714152110u, 396140812571321688u,
64*4bdff4beSrobert   316912650057057351u, 507060240091291761u, 405648192073033409u, 324518553658426727u,
65*4bdff4beSrobert   519229685853482763u, 415383748682786211u, 332306998946228969u, 531691198313966350u,
66*4bdff4beSrobert   425352958651173080u, 340282366920938464u, 544451787073501542u, 435561429658801234u,
67*4bdff4beSrobert   348449143727040987u, 557518629963265579u, 446014903970612463u, 356811923176489971u,
68*4bdff4beSrobert   570899077082383953u, 456719261665907162u, 365375409332725730u
69*4bdff4beSrobert };
70*4bdff4beSrobert inline constexpr int __FLOAT_POW5_BITCOUNT = 61;
71*4bdff4beSrobert inline constexpr uint64_t __FLOAT_POW5_SPLIT[47] = {
72*4bdff4beSrobert   1152921504606846976u, 1441151880758558720u, 1801439850948198400u, 2251799813685248000u,
73*4bdff4beSrobert   1407374883553280000u, 1759218604441600000u, 2199023255552000000u, 1374389534720000000u,
74*4bdff4beSrobert   1717986918400000000u, 2147483648000000000u, 1342177280000000000u, 1677721600000000000u,
75*4bdff4beSrobert   2097152000000000000u, 1310720000000000000u, 1638400000000000000u, 2048000000000000000u,
76*4bdff4beSrobert   1280000000000000000u, 1600000000000000000u, 2000000000000000000u, 1250000000000000000u,
77*4bdff4beSrobert   1562500000000000000u, 1953125000000000000u, 1220703125000000000u, 1525878906250000000u,
78*4bdff4beSrobert   1907348632812500000u, 1192092895507812500u, 1490116119384765625u, 1862645149230957031u,
79*4bdff4beSrobert   1164153218269348144u, 1455191522836685180u, 1818989403545856475u, 2273736754432320594u,
80*4bdff4beSrobert   1421085471520200371u, 1776356839400250464u, 2220446049250313080u, 1387778780781445675u,
81*4bdff4beSrobert   1734723475976807094u, 2168404344971008868u, 1355252715606880542u, 1694065894508600678u,
82*4bdff4beSrobert   2117582368135750847u, 1323488980084844279u, 1654361225106055349u, 2067951531382569187u,
83*4bdff4beSrobert   1292469707114105741u, 1615587133892632177u, 2019483917365790221u
84*4bdff4beSrobert };
85*4bdff4beSrobert 
__pow5Factor(uint32_t __value)86*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __pow5Factor(uint32_t __value) {
87*4bdff4beSrobert   uint32_t __count = 0;
88*4bdff4beSrobert   for (;;) {
89*4bdff4beSrobert     _LIBCPP_ASSERT(__value != 0, "");
90*4bdff4beSrobert     const uint32_t __q = __value / 5;
91*4bdff4beSrobert     const uint32_t __r = __value % 5;
92*4bdff4beSrobert     if (__r != 0) {
93*4bdff4beSrobert       break;
94*4bdff4beSrobert     }
95*4bdff4beSrobert     __value = __q;
96*4bdff4beSrobert     ++__count;
97*4bdff4beSrobert   }
98*4bdff4beSrobert   return __count;
99*4bdff4beSrobert }
100*4bdff4beSrobert 
101*4bdff4beSrobert // Returns true if __value is divisible by 5^__p.
__multipleOfPowerOf5(const uint32_t __value,const uint32_t __p)102*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf5(const uint32_t __value, const uint32_t __p) {
103*4bdff4beSrobert   return __pow5Factor(__value) >= __p;
104*4bdff4beSrobert }
105*4bdff4beSrobert 
106*4bdff4beSrobert // Returns true if __value is divisible by 2^__p.
__multipleOfPowerOf2(const uint32_t __value,const uint32_t __p)107*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf2(const uint32_t __value, const uint32_t __p) {
108*4bdff4beSrobert   _LIBCPP_ASSERT(__value != 0, "");
109*4bdff4beSrobert   _LIBCPP_ASSERT(__p < 32, "");
110*4bdff4beSrobert   // __builtin_ctz doesn't appear to be faster here.
111*4bdff4beSrobert   return (__value & ((1u << __p) - 1)) == 0;
112*4bdff4beSrobert }
113*4bdff4beSrobert 
__mulShift(const uint32_t __m,const uint64_t __factor,const int32_t __shift)114*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulShift(const uint32_t __m, const uint64_t __factor, const int32_t __shift) {
115*4bdff4beSrobert   _LIBCPP_ASSERT(__shift > 32, "");
116*4bdff4beSrobert 
117*4bdff4beSrobert   // The casts here help MSVC to avoid calls to the __allmul library
118*4bdff4beSrobert   // function.
119*4bdff4beSrobert   const uint32_t __factorLo = static_cast<uint32_t>(__factor);
120*4bdff4beSrobert   const uint32_t __factorHi = static_cast<uint32_t>(__factor >> 32);
121*4bdff4beSrobert   const uint64_t __bits0 = static_cast<uint64_t>(__m) * __factorLo;
122*4bdff4beSrobert   const uint64_t __bits1 = static_cast<uint64_t>(__m) * __factorHi;
123*4bdff4beSrobert 
124*4bdff4beSrobert #ifndef _LIBCPP_64_BIT
125*4bdff4beSrobert   // On 32-bit platforms we can avoid a 64-bit shift-right since we only
126*4bdff4beSrobert   // need the upper 32 bits of the result and the shift value is > 32.
127*4bdff4beSrobert   const uint32_t __bits0Hi = static_cast<uint32_t>(__bits0 >> 32);
128*4bdff4beSrobert   uint32_t __bits1Lo = static_cast<uint32_t>(__bits1);
129*4bdff4beSrobert   uint32_t __bits1Hi = static_cast<uint32_t>(__bits1 >> 32);
130*4bdff4beSrobert   __bits1Lo += __bits0Hi;
131*4bdff4beSrobert   __bits1Hi += (__bits1Lo < __bits0Hi);
132*4bdff4beSrobert   const int32_t __s = __shift - 32;
133*4bdff4beSrobert   return (__bits1Hi << (32 - __s)) | (__bits1Lo >> __s);
134*4bdff4beSrobert #else // ^^^ 32-bit ^^^ / vvv 64-bit vvv
135*4bdff4beSrobert   const uint64_t __sum = (__bits0 >> 32) + __bits1;
136*4bdff4beSrobert   const uint64_t __shiftedSum = __sum >> (__shift - 32);
137*4bdff4beSrobert   _LIBCPP_ASSERT(__shiftedSum <= UINT32_MAX, "");
138*4bdff4beSrobert   return static_cast<uint32_t>(__shiftedSum);
139*4bdff4beSrobert #endif // ^^^ 64-bit ^^^
140*4bdff4beSrobert }
141*4bdff4beSrobert 
__mulPow5InvDivPow2(const uint32_t __m,const uint32_t __q,const int32_t __j)142*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulPow5InvDivPow2(const uint32_t __m, const uint32_t __q, const int32_t __j) {
143*4bdff4beSrobert   return __mulShift(__m, __FLOAT_POW5_INV_SPLIT[__q], __j);
144*4bdff4beSrobert }
145*4bdff4beSrobert 
__mulPow5divPow2(const uint32_t __m,const uint32_t __i,const int32_t __j)146*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulPow5divPow2(const uint32_t __m, const uint32_t __i, const int32_t __j) {
147*4bdff4beSrobert   return __mulShift(__m, __FLOAT_POW5_SPLIT[__i], __j);
148*4bdff4beSrobert }
149*4bdff4beSrobert 
150*4bdff4beSrobert // A floating decimal representing m * 10^e.
151*4bdff4beSrobert struct __floating_decimal_32 {
152*4bdff4beSrobert   uint32_t __mantissa;
153*4bdff4beSrobert   int32_t __exponent;
154*4bdff4beSrobert };
155*4bdff4beSrobert 
__f2d(const uint32_t __ieeeMantissa,const uint32_t __ieeeExponent)156*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline __floating_decimal_32 __f2d(const uint32_t __ieeeMantissa, const uint32_t __ieeeExponent) {
157*4bdff4beSrobert   int32_t __e2;
158*4bdff4beSrobert   uint32_t __m2;
159*4bdff4beSrobert   if (__ieeeExponent == 0) {
160*4bdff4beSrobert     // We subtract 2 so that the bounds computation has 2 additional bits.
161*4bdff4beSrobert     __e2 = 1 - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS - 2;
162*4bdff4beSrobert     __m2 = __ieeeMantissa;
163*4bdff4beSrobert   } else {
164*4bdff4beSrobert     __e2 = static_cast<int32_t>(__ieeeExponent) - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS - 2;
165*4bdff4beSrobert     __m2 = (1u << __FLOAT_MANTISSA_BITS) | __ieeeMantissa;
166*4bdff4beSrobert   }
167*4bdff4beSrobert   const bool __even = (__m2 & 1) == 0;
168*4bdff4beSrobert   const bool __acceptBounds = __even;
169*4bdff4beSrobert 
170*4bdff4beSrobert   // Step 2: Determine the interval of valid decimal representations.
171*4bdff4beSrobert   const uint32_t __mv = 4 * __m2;
172*4bdff4beSrobert   const uint32_t __mp = 4 * __m2 + 2;
173*4bdff4beSrobert   // Implicit bool -> int conversion. True is 1, false is 0.
174*4bdff4beSrobert   const uint32_t __mmShift = __ieeeMantissa != 0 || __ieeeExponent <= 1;
175*4bdff4beSrobert   const uint32_t __mm = 4 * __m2 - 1 - __mmShift;
176*4bdff4beSrobert 
177*4bdff4beSrobert   // Step 3: Convert to a decimal power base using 64-bit arithmetic.
178*4bdff4beSrobert   uint32_t __vr, __vp, __vm;
179*4bdff4beSrobert   int32_t __e10;
180*4bdff4beSrobert   bool __vmIsTrailingZeros = false;
181*4bdff4beSrobert   bool __vrIsTrailingZeros = false;
182*4bdff4beSrobert   uint8_t __lastRemovedDigit = 0;
183*4bdff4beSrobert   if (__e2 >= 0) {
184*4bdff4beSrobert     const uint32_t __q = __log10Pow2(__e2);
185*4bdff4beSrobert     __e10 = static_cast<int32_t>(__q);
186*4bdff4beSrobert     const int32_t __k = __FLOAT_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q)) - 1;
187*4bdff4beSrobert     const int32_t __i = -__e2 + static_cast<int32_t>(__q) + __k;
188*4bdff4beSrobert     __vr = __mulPow5InvDivPow2(__mv, __q, __i);
189*4bdff4beSrobert     __vp = __mulPow5InvDivPow2(__mp, __q, __i);
190*4bdff4beSrobert     __vm = __mulPow5InvDivPow2(__mm, __q, __i);
191*4bdff4beSrobert     if (__q != 0 && (__vp - 1) / 10 <= __vm / 10) {
192*4bdff4beSrobert       // We need to know one removed digit even if we are not going to loop below. We could use
193*4bdff4beSrobert       // __q = X - 1 above, except that would require 33 bits for the result, and we've found that
194*4bdff4beSrobert       // 32-bit arithmetic is faster even on 64-bit machines.
195*4bdff4beSrobert       const int32_t __l = __FLOAT_POW5_INV_BITCOUNT + __pow5bits(static_cast<int32_t>(__q - 1)) - 1;
196*4bdff4beSrobert       __lastRemovedDigit = static_cast<uint8_t>(__mulPow5InvDivPow2(__mv, __q - 1,
197*4bdff4beSrobert         -__e2 + static_cast<int32_t>(__q) - 1 + __l) % 10);
198*4bdff4beSrobert     }
199*4bdff4beSrobert     if (__q <= 9) {
200*4bdff4beSrobert       // The largest power of 5 that fits in 24 bits is 5^10, but __q <= 9 seems to be safe as well.
201*4bdff4beSrobert       // Only one of __mp, __mv, and __mm can be a multiple of 5, if any.
202*4bdff4beSrobert       if (__mv % 5 == 0) {
203*4bdff4beSrobert         __vrIsTrailingZeros = __multipleOfPowerOf5(__mv, __q);
204*4bdff4beSrobert       } else if (__acceptBounds) {
205*4bdff4beSrobert         __vmIsTrailingZeros = __multipleOfPowerOf5(__mm, __q);
206*4bdff4beSrobert       } else {
207*4bdff4beSrobert         __vp -= __multipleOfPowerOf5(__mp, __q);
208*4bdff4beSrobert       }
209*4bdff4beSrobert     }
210*4bdff4beSrobert   } else {
211*4bdff4beSrobert     const uint32_t __q = __log10Pow5(-__e2);
212*4bdff4beSrobert     __e10 = static_cast<int32_t>(__q) + __e2;
213*4bdff4beSrobert     const int32_t __i = -__e2 - static_cast<int32_t>(__q);
214*4bdff4beSrobert     const int32_t __k = __pow5bits(__i) - __FLOAT_POW5_BITCOUNT;
215*4bdff4beSrobert     int32_t __j = static_cast<int32_t>(__q) - __k;
216*4bdff4beSrobert     __vr = __mulPow5divPow2(__mv, static_cast<uint32_t>(__i), __j);
217*4bdff4beSrobert     __vp = __mulPow5divPow2(__mp, static_cast<uint32_t>(__i), __j);
218*4bdff4beSrobert     __vm = __mulPow5divPow2(__mm, static_cast<uint32_t>(__i), __j);
219*4bdff4beSrobert     if (__q != 0 && (__vp - 1) / 10 <= __vm / 10) {
220*4bdff4beSrobert       __j = static_cast<int32_t>(__q) - 1 - (__pow5bits(__i + 1) - __FLOAT_POW5_BITCOUNT);
221*4bdff4beSrobert       __lastRemovedDigit = static_cast<uint8_t>(__mulPow5divPow2(__mv, static_cast<uint32_t>(__i + 1), __j) % 10);
222*4bdff4beSrobert     }
223*4bdff4beSrobert     if (__q <= 1) {
224*4bdff4beSrobert       // {__vr,__vp,__vm} is trailing zeros if {__mv,__mp,__mm} has at least __q trailing 0 bits.
225*4bdff4beSrobert       // __mv = 4 * __m2, so it always has at least two trailing 0 bits.
226*4bdff4beSrobert       __vrIsTrailingZeros = true;
227*4bdff4beSrobert       if (__acceptBounds) {
228*4bdff4beSrobert         // __mm = __mv - 1 - __mmShift, so it has 1 trailing 0 bit iff __mmShift == 1.
229*4bdff4beSrobert         __vmIsTrailingZeros = __mmShift == 1;
230*4bdff4beSrobert       } else {
231*4bdff4beSrobert         // __mp = __mv + 2, so it always has at least one trailing 0 bit.
232*4bdff4beSrobert         --__vp;
233*4bdff4beSrobert       }
234*4bdff4beSrobert     } else if (__q < 31) { // TRANSITION(ulfjack): Use a tighter bound here.
235*4bdff4beSrobert       __vrIsTrailingZeros = __multipleOfPowerOf2(__mv, __q - 1);
236*4bdff4beSrobert     }
237*4bdff4beSrobert   }
238*4bdff4beSrobert 
239*4bdff4beSrobert   // Step 4: Find the shortest decimal representation in the interval of valid representations.
240*4bdff4beSrobert   int32_t __removed = 0;
241*4bdff4beSrobert   uint32_t _Output;
242*4bdff4beSrobert   if (__vmIsTrailingZeros || __vrIsTrailingZeros) {
243*4bdff4beSrobert     // General case, which happens rarely (~4.0%).
244*4bdff4beSrobert     while (__vp / 10 > __vm / 10) {
245*4bdff4beSrobert #ifdef __clang__ // TRANSITION, LLVM-23106
246*4bdff4beSrobert       __vmIsTrailingZeros &= __vm - (__vm / 10) * 10 == 0;
247*4bdff4beSrobert #else
248*4bdff4beSrobert       __vmIsTrailingZeros &= __vm % 10 == 0;
249*4bdff4beSrobert #endif
250*4bdff4beSrobert       __vrIsTrailingZeros &= __lastRemovedDigit == 0;
251*4bdff4beSrobert       __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
252*4bdff4beSrobert       __vr /= 10;
253*4bdff4beSrobert       __vp /= 10;
254*4bdff4beSrobert       __vm /= 10;
255*4bdff4beSrobert       ++__removed;
256*4bdff4beSrobert     }
257*4bdff4beSrobert     if (__vmIsTrailingZeros) {
258*4bdff4beSrobert       while (__vm % 10 == 0) {
259*4bdff4beSrobert         __vrIsTrailingZeros &= __lastRemovedDigit == 0;
260*4bdff4beSrobert         __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
261*4bdff4beSrobert         __vr /= 10;
262*4bdff4beSrobert         __vp /= 10;
263*4bdff4beSrobert         __vm /= 10;
264*4bdff4beSrobert         ++__removed;
265*4bdff4beSrobert       }
266*4bdff4beSrobert     }
267*4bdff4beSrobert     if (__vrIsTrailingZeros && __lastRemovedDigit == 5 && __vr % 2 == 0) {
268*4bdff4beSrobert       // Round even if the exact number is .....50..0.
269*4bdff4beSrobert       __lastRemovedDigit = 4;
270*4bdff4beSrobert     }
271*4bdff4beSrobert     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
272*4bdff4beSrobert     _Output = __vr + ((__vr == __vm && (!__acceptBounds || !__vmIsTrailingZeros)) || __lastRemovedDigit >= 5);
273*4bdff4beSrobert   } else {
274*4bdff4beSrobert     // Specialized for the common case (~96.0%). Percentages below are relative to this.
275*4bdff4beSrobert     // Loop iterations below (approximately):
276*4bdff4beSrobert     // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01%
277*4bdff4beSrobert     while (__vp / 10 > __vm / 10) {
278*4bdff4beSrobert       __lastRemovedDigit = static_cast<uint8_t>(__vr % 10);
279*4bdff4beSrobert       __vr /= 10;
280*4bdff4beSrobert       __vp /= 10;
281*4bdff4beSrobert       __vm /= 10;
282*4bdff4beSrobert       ++__removed;
283*4bdff4beSrobert     }
284*4bdff4beSrobert     // We need to take __vr + 1 if __vr is outside bounds or we need to round up.
285*4bdff4beSrobert     _Output = __vr + (__vr == __vm || __lastRemovedDigit >= 5);
286*4bdff4beSrobert   }
287*4bdff4beSrobert   const int32_t __exp = __e10 + __removed;
288*4bdff4beSrobert 
289*4bdff4beSrobert   __floating_decimal_32 __fd;
290*4bdff4beSrobert   __fd.__exponent = __exp;
291*4bdff4beSrobert   __fd.__mantissa = _Output;
292*4bdff4beSrobert   return __fd;
293*4bdff4beSrobert }
294*4bdff4beSrobert 
_Large_integer_to_chars(char * const _First,char * const _Last,const uint32_t _Mantissa2,const int32_t _Exponent2)295*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result _Large_integer_to_chars(char* const _First, char* const _Last,
296*4bdff4beSrobert   const uint32_t _Mantissa2, const int32_t _Exponent2) {
297*4bdff4beSrobert 
298*4bdff4beSrobert   // Print the integer _Mantissa2 * 2^_Exponent2 exactly.
299*4bdff4beSrobert 
300*4bdff4beSrobert   // For nonzero integers, _Exponent2 >= -23. (The minimum value occurs when _Mantissa2 * 2^_Exponent2 is 1.
301*4bdff4beSrobert   // In that case, _Mantissa2 is the implicit 1 bit followed by 23 zeros, so _Exponent2 is -23 to shift away
302*4bdff4beSrobert   // the zeros.) The dense range of exactly representable integers has negative or zero exponents
303*4bdff4beSrobert   // (as positive exponents make the range non-dense). For that dense range, Ryu will always be used:
304*4bdff4beSrobert   // every digit is necessary to uniquely identify the value, so Ryu must print them all.
305*4bdff4beSrobert 
306*4bdff4beSrobert   // Positive exponents are the non-dense range of exactly representable integers.
307*4bdff4beSrobert   // This contains all of the values for which Ryu can't be used (and a few Ryu-friendly values).
308*4bdff4beSrobert 
309*4bdff4beSrobert   // Performance note: Long division appears to be faster than losslessly widening float to double and calling
310*4bdff4beSrobert   // __d2fixed_buffered_n(). If __f2fixed_buffered_n() is implemented, it might be faster than long division.
311*4bdff4beSrobert 
312*4bdff4beSrobert   _LIBCPP_ASSERT(_Exponent2 > 0, "");
313*4bdff4beSrobert   _LIBCPP_ASSERT(_Exponent2 <= 104, ""); // because __ieeeExponent <= 254
314*4bdff4beSrobert 
315*4bdff4beSrobert   // Manually represent _Mantissa2 * 2^_Exponent2 as a large integer. _Mantissa2 is always 24 bits
316*4bdff4beSrobert   // (due to the implicit bit), while _Exponent2 indicates a shift of at most 104 bits.
317*4bdff4beSrobert   // 24 + 104 equals 128 equals 4 * 32, so we need exactly 4 32-bit elements.
318*4bdff4beSrobert   // We use a little-endian representation, visualized like this:
319*4bdff4beSrobert 
320*4bdff4beSrobert   // << left shift <<
321*4bdff4beSrobert   // most significant
322*4bdff4beSrobert   // _Data[3] _Data[2] _Data[1] _Data[0]
323*4bdff4beSrobert   //                   least significant
324*4bdff4beSrobert   //                   >> right shift >>
325*4bdff4beSrobert 
326*4bdff4beSrobert   constexpr uint32_t _Data_size = 4;
327*4bdff4beSrobert   uint32_t _Data[_Data_size]{};
328*4bdff4beSrobert 
329*4bdff4beSrobert   // _Maxidx is the index of the most significant nonzero element.
330*4bdff4beSrobert   uint32_t _Maxidx = ((24 + static_cast<uint32_t>(_Exponent2) + 31) / 32) - 1;
331*4bdff4beSrobert   _LIBCPP_ASSERT(_Maxidx < _Data_size, "");
332*4bdff4beSrobert 
333*4bdff4beSrobert   const uint32_t _Bit_shift = static_cast<uint32_t>(_Exponent2) % 32;
334*4bdff4beSrobert   if (_Bit_shift <= 8) { // _Mantissa2's 24 bits don't cross an element boundary
335*4bdff4beSrobert     _Data[_Maxidx] = _Mantissa2 << _Bit_shift;
336*4bdff4beSrobert   } else { // _Mantissa2's 24 bits cross an element boundary
337*4bdff4beSrobert     _Data[_Maxidx - 1] = _Mantissa2 << _Bit_shift;
338*4bdff4beSrobert     _Data[_Maxidx] = _Mantissa2 >> (32 - _Bit_shift);
339*4bdff4beSrobert   }
340*4bdff4beSrobert 
341*4bdff4beSrobert   // If Ryu hasn't determined the total output length, we need to buffer the digits generated from right to left
342*4bdff4beSrobert   // by long division. The largest possible float is: 340'282346638'528859811'704183484'516925440
343*4bdff4beSrobert   uint32_t _Blocks[4];
344*4bdff4beSrobert   int32_t _Filled_blocks = 0;
345*4bdff4beSrobert   // From left to right, we're going to print:
346*4bdff4beSrobert   // _Data[0] will be [1, 10] digits.
347*4bdff4beSrobert   // Then if _Filled_blocks > 0:
348*4bdff4beSrobert   // _Blocks[_Filled_blocks - 1], ..., _Blocks[0] will be 0-filled 9-digit blocks.
349*4bdff4beSrobert 
350*4bdff4beSrobert   if (_Maxidx != 0) { // If the integer is actually large, perform long division.
351*4bdff4beSrobert                       // Otherwise, skip to printing _Data[0].
352*4bdff4beSrobert     for (;;) {
353*4bdff4beSrobert       // Loop invariant: _Maxidx != 0 (i.e. the integer is actually large)
354*4bdff4beSrobert 
355*4bdff4beSrobert       const uint32_t _Most_significant_elem = _Data[_Maxidx];
356*4bdff4beSrobert       const uint32_t _Initial_remainder = _Most_significant_elem % 1000000000;
357*4bdff4beSrobert       const uint32_t _Initial_quotient = _Most_significant_elem / 1000000000;
358*4bdff4beSrobert       _Data[_Maxidx] = _Initial_quotient;
359*4bdff4beSrobert       uint64_t _Remainder = _Initial_remainder;
360*4bdff4beSrobert 
361*4bdff4beSrobert       // Process less significant elements.
362*4bdff4beSrobert       uint32_t _Idx = _Maxidx;
363*4bdff4beSrobert       do {
364*4bdff4beSrobert         --_Idx; // Initially, _Remainder is at most 10^9 - 1.
365*4bdff4beSrobert 
366*4bdff4beSrobert         // Now, _Remainder is at most (10^9 - 1) * 2^32 + 2^32 - 1, simplified to 10^9 * 2^32 - 1.
367*4bdff4beSrobert         _Remainder = (_Remainder << 32) | _Data[_Idx];
368*4bdff4beSrobert 
369*4bdff4beSrobert         // floor((10^9 * 2^32 - 1) / 10^9) == 2^32 - 1, so uint32_t _Quotient is lossless.
370*4bdff4beSrobert         const uint32_t _Quotient = static_cast<uint32_t>(__div1e9(_Remainder));
371*4bdff4beSrobert 
372*4bdff4beSrobert         // _Remainder is at most 10^9 - 1 again.
373*4bdff4beSrobert         // For uint32_t truncation, see the __mod1e9() comment in d2s_intrinsics.h.
374*4bdff4beSrobert         _Remainder = static_cast<uint32_t>(_Remainder) - 1000000000u * _Quotient;
375*4bdff4beSrobert 
376*4bdff4beSrobert         _Data[_Idx] = _Quotient;
377*4bdff4beSrobert       } while (_Idx != 0);
378*4bdff4beSrobert 
379*4bdff4beSrobert       // Store a 0-filled 9-digit block.
380*4bdff4beSrobert       _Blocks[_Filled_blocks++] = static_cast<uint32_t>(_Remainder);
381*4bdff4beSrobert 
382*4bdff4beSrobert       if (_Initial_quotient == 0) { // Is the large integer shrinking?
383*4bdff4beSrobert         --_Maxidx; // log2(10^9) is 29.9, so we can't shrink by more than one element.
384*4bdff4beSrobert         if (_Maxidx == 0) {
385*4bdff4beSrobert           break; // We've finished long division. Now we need to print _Data[0].
386*4bdff4beSrobert         }
387*4bdff4beSrobert       }
388*4bdff4beSrobert     }
389*4bdff4beSrobert   }
390*4bdff4beSrobert 
391*4bdff4beSrobert   _LIBCPP_ASSERT(_Data[0] != 0, "");
392*4bdff4beSrobert   for (uint32_t _Idx = 1; _Idx < _Data_size; ++_Idx) {
393*4bdff4beSrobert     _LIBCPP_ASSERT(_Data[_Idx] == 0, "");
394*4bdff4beSrobert   }
395*4bdff4beSrobert 
396*4bdff4beSrobert   const uint32_t _Data_olength = _Data[0] >= 1000000000 ? 10 : __decimalLength9(_Data[0]);
397*4bdff4beSrobert   const uint32_t _Total_fixed_length = _Data_olength + 9 * _Filled_blocks;
398*4bdff4beSrobert 
399*4bdff4beSrobert   if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
400*4bdff4beSrobert     return { _Last, errc::value_too_large };
401*4bdff4beSrobert   }
402*4bdff4beSrobert 
403*4bdff4beSrobert   char* _Result = _First;
404*4bdff4beSrobert 
405*4bdff4beSrobert   // Print _Data[0]. While it's up to 10 digits,
406*4bdff4beSrobert   // which is more than Ryu generates, the code below can handle this.
407*4bdff4beSrobert   __append_n_digits(_Data_olength, _Data[0], _Result);
408*4bdff4beSrobert   _Result += _Data_olength;
409*4bdff4beSrobert 
410*4bdff4beSrobert   // Print 0-filled 9-digit blocks.
411*4bdff4beSrobert   for (int32_t _Idx = _Filled_blocks - 1; _Idx >= 0; --_Idx) {
412*4bdff4beSrobert     __append_nine_digits(_Blocks[_Idx], _Result);
413*4bdff4beSrobert     _Result += 9;
414*4bdff4beSrobert   }
415*4bdff4beSrobert 
416*4bdff4beSrobert   return { _Result, errc{} };
417*4bdff4beSrobert }
418*4bdff4beSrobert 
__to_chars(char * const _First,char * const _Last,const __floating_decimal_32 __v,chars_format _Fmt,const uint32_t __ieeeMantissa,const uint32_t __ieeeExponent)419*4bdff4beSrobert [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline to_chars_result __to_chars(char* const _First, char* const _Last, const __floating_decimal_32 __v,
420*4bdff4beSrobert   chars_format _Fmt, const uint32_t __ieeeMantissa, const uint32_t __ieeeExponent) {
421*4bdff4beSrobert   // Step 5: Print the decimal representation.
422*4bdff4beSrobert   uint32_t _Output = __v.__mantissa;
423*4bdff4beSrobert   int32_t _Ryu_exponent = __v.__exponent;
424*4bdff4beSrobert   const uint32_t __olength = __decimalLength9(_Output);
425*4bdff4beSrobert   int32_t _Scientific_exponent = _Ryu_exponent + static_cast<int32_t>(__olength) - 1;
426*4bdff4beSrobert 
427*4bdff4beSrobert   if (_Fmt == chars_format{}) {
428*4bdff4beSrobert     int32_t _Lower;
429*4bdff4beSrobert     int32_t _Upper;
430*4bdff4beSrobert 
431*4bdff4beSrobert     if (__olength == 1) {
432*4bdff4beSrobert       // Value | Fixed   | Scientific
433*4bdff4beSrobert       // 1e-3  | "0.001" | "1e-03"
434*4bdff4beSrobert       // 1e4   | "10000" | "1e+04"
435*4bdff4beSrobert       _Lower = -3;
436*4bdff4beSrobert       _Upper = 4;
437*4bdff4beSrobert     } else {
438*4bdff4beSrobert       // Value   | Fixed       | Scientific
439*4bdff4beSrobert       // 1234e-7 | "0.0001234" | "1.234e-04"
440*4bdff4beSrobert       // 1234e5  | "123400000" | "1.234e+08"
441*4bdff4beSrobert       _Lower = -static_cast<int32_t>(__olength + 3);
442*4bdff4beSrobert       _Upper = 5;
443*4bdff4beSrobert     }
444*4bdff4beSrobert 
445*4bdff4beSrobert     if (_Lower <= _Ryu_exponent && _Ryu_exponent <= _Upper) {
446*4bdff4beSrobert       _Fmt = chars_format::fixed;
447*4bdff4beSrobert     } else {
448*4bdff4beSrobert       _Fmt = chars_format::scientific;
449*4bdff4beSrobert     }
450*4bdff4beSrobert   } else if (_Fmt == chars_format::general) {
451*4bdff4beSrobert     // C11 7.21.6.1 "The fprintf function"/8:
452*4bdff4beSrobert     // "Let P equal [...] 6 if the precision is omitted [...].
453*4bdff4beSrobert     // Then, if a conversion with style E would have an exponent of X:
454*4bdff4beSrobert     // - if P > X >= -4, the conversion is with style f [...].
455*4bdff4beSrobert     // - otherwise, the conversion is with style e [...]."
456*4bdff4beSrobert     if (-4 <= _Scientific_exponent && _Scientific_exponent < 6) {
457*4bdff4beSrobert       _Fmt = chars_format::fixed;
458*4bdff4beSrobert     } else {
459*4bdff4beSrobert       _Fmt = chars_format::scientific;
460*4bdff4beSrobert     }
461*4bdff4beSrobert   }
462*4bdff4beSrobert 
463*4bdff4beSrobert   if (_Fmt == chars_format::fixed) {
464*4bdff4beSrobert     // Example: _Output == 1729, __olength == 4
465*4bdff4beSrobert 
466*4bdff4beSrobert     // _Ryu_exponent | Printed  | _Whole_digits | _Total_fixed_length  | Notes
467*4bdff4beSrobert     // --------------|----------|---------------|----------------------|---------------------------------------
468*4bdff4beSrobert     //             2 | 172900   |  6            | _Whole_digits        | Ryu can't be used for printing
469*4bdff4beSrobert     //             1 | 17290    |  5            | (sometimes adjusted) | when the trimmed digits are nonzero.
470*4bdff4beSrobert     // --------------|----------|---------------|----------------------|---------------------------------------
471*4bdff4beSrobert     //             0 | 1729     |  4            | _Whole_digits        | Unified length cases.
472*4bdff4beSrobert     // --------------|----------|---------------|----------------------|---------------------------------------
473*4bdff4beSrobert     //            -1 | 172.9    |  3            | __olength + 1        | This case can't happen for
474*4bdff4beSrobert     //            -2 | 17.29    |  2            |                      | __olength == 1, but no additional
475*4bdff4beSrobert     //            -3 | 1.729    |  1            |                      | code is needed to avoid it.
476*4bdff4beSrobert     // --------------|----------|---------------|----------------------|---------------------------------------
477*4bdff4beSrobert     //            -4 | 0.1729   |  0            | 2 - _Ryu_exponent    | C11 7.21.6.1 "The fprintf function"/8:
478*4bdff4beSrobert     //            -5 | 0.01729  | -1            |                      | "If a decimal-point character appears,
479*4bdff4beSrobert     //            -6 | 0.001729 | -2            |                      | at least one digit appears before it."
480*4bdff4beSrobert 
481*4bdff4beSrobert     const int32_t _Whole_digits = static_cast<int32_t>(__olength) + _Ryu_exponent;
482*4bdff4beSrobert 
483*4bdff4beSrobert     uint32_t _Total_fixed_length;
484*4bdff4beSrobert     if (_Ryu_exponent >= 0) { // cases "172900" and "1729"
485*4bdff4beSrobert       _Total_fixed_length = static_cast<uint32_t>(_Whole_digits);
486*4bdff4beSrobert       if (_Output == 1) {
487*4bdff4beSrobert         // Rounding can affect the number of digits.
488*4bdff4beSrobert         // For example, 1e11f is exactly "99999997952" which is 11 digits instead of 12.
489*4bdff4beSrobert         // We can use a lookup table to detect this and adjust the total length.
490*4bdff4beSrobert         static constexpr uint8_t _Adjustment[39] = {
491*4bdff4beSrobert           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 };
492*4bdff4beSrobert         _Total_fixed_length -= _Adjustment[_Ryu_exponent];
493*4bdff4beSrobert         // _Whole_digits doesn't need to be adjusted because these cases won't refer to it later.
494*4bdff4beSrobert       }
495*4bdff4beSrobert     } else if (_Whole_digits > 0) { // case "17.29"
496*4bdff4beSrobert       _Total_fixed_length = __olength + 1;
497*4bdff4beSrobert     } else { // case "0.001729"
498*4bdff4beSrobert       _Total_fixed_length = static_cast<uint32_t>(2 - _Ryu_exponent);
499*4bdff4beSrobert     }
500*4bdff4beSrobert 
501*4bdff4beSrobert     if (_Last - _First < static_cast<ptrdiff_t>(_Total_fixed_length)) {
502*4bdff4beSrobert       return { _Last, errc::value_too_large };
503*4bdff4beSrobert     }
504*4bdff4beSrobert 
505*4bdff4beSrobert     char* _Mid;
506*4bdff4beSrobert     if (_Ryu_exponent > 0) { // case "172900"
507*4bdff4beSrobert       bool _Can_use_ryu;
508*4bdff4beSrobert 
509*4bdff4beSrobert       if (_Ryu_exponent > 10) { // 10^10 is the largest power of 10 that's exactly representable as a float.
510*4bdff4beSrobert         _Can_use_ryu = false;
511*4bdff4beSrobert       } else {
512*4bdff4beSrobert         // Ryu generated X: __v.__mantissa * 10^_Ryu_exponent
513*4bdff4beSrobert         // __v.__mantissa == 2^_Trailing_zero_bits * (__v.__mantissa >> _Trailing_zero_bits)
514*4bdff4beSrobert         // 10^_Ryu_exponent == 2^_Ryu_exponent * 5^_Ryu_exponent
515*4bdff4beSrobert 
516*4bdff4beSrobert         // _Trailing_zero_bits is [0, 29] (aside: because 2^29 is the largest power of 2
517*4bdff4beSrobert         // with 9 decimal digits, which is float's round-trip limit.)
518*4bdff4beSrobert         // _Ryu_exponent is [1, 10].
519*4bdff4beSrobert         // Normalization adds [2, 23] (aside: at least 2 because the pre-normalized mantissa is at least 5).
520*4bdff4beSrobert         // This adds up to [3, 62], which is well below float's maximum binary exponent 127.
521*4bdff4beSrobert 
522*4bdff4beSrobert         // Therefore, we just need to consider (__v.__mantissa >> _Trailing_zero_bits) * 5^_Ryu_exponent.
523*4bdff4beSrobert 
524*4bdff4beSrobert         // If that product would exceed 24 bits, then X can't be exactly represented as a float.
525*4bdff4beSrobert         // (That's not a problem for round-tripping, because X is close enough to the original float,
526*4bdff4beSrobert         // but X isn't mathematically equal to the original float.) This requires a high-precision fallback.
527*4bdff4beSrobert 
528*4bdff4beSrobert         // If the product is 24 bits or smaller, then X can be exactly represented as a float (and we don't
529*4bdff4beSrobert         // need to re-synthesize it; the original float must have been X, because Ryu wouldn't produce the
530*4bdff4beSrobert         // same output for two different floats X and Y). This allows Ryu's output to be used (zero-filled).
531*4bdff4beSrobert 
532*4bdff4beSrobert         // (2^24 - 1) / 5^0 (for indexing), (2^24 - 1) / 5^1, ..., (2^24 - 1) / 5^10
533*4bdff4beSrobert         static constexpr uint32_t _Max_shifted_mantissa[11] = {
534*4bdff4beSrobert           16777215, 3355443, 671088, 134217, 26843, 5368, 1073, 214, 42, 8, 1 };
535*4bdff4beSrobert 
536*4bdff4beSrobert         unsigned long _Trailing_zero_bits;
537*4bdff4beSrobert         (void) _BitScanForward(&_Trailing_zero_bits, __v.__mantissa); // __v.__mantissa is guaranteed nonzero
538*4bdff4beSrobert         const uint32_t _Shifted_mantissa = __v.__mantissa >> _Trailing_zero_bits;
539*4bdff4beSrobert         _Can_use_ryu = _Shifted_mantissa <= _Max_shifted_mantissa[_Ryu_exponent];
540*4bdff4beSrobert       }
541*4bdff4beSrobert 
542*4bdff4beSrobert       if (!_Can_use_ryu) {
543*4bdff4beSrobert         const uint32_t _Mantissa2 = __ieeeMantissa | (1u << __FLOAT_MANTISSA_BITS); // restore implicit bit
544*4bdff4beSrobert         const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
545*4bdff4beSrobert           - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS; // bias and normalization
546*4bdff4beSrobert 
547*4bdff4beSrobert         // Performance note: We've already called Ryu, so this will redundantly perform buffering and bounds checking.
548*4bdff4beSrobert         return _Large_integer_to_chars(_First, _Last, _Mantissa2, _Exponent2);
549*4bdff4beSrobert       }
550*4bdff4beSrobert 
551*4bdff4beSrobert       // _Can_use_ryu
552*4bdff4beSrobert       // Print the decimal digits, left-aligned within [_First, _First + _Total_fixed_length).
553*4bdff4beSrobert       _Mid = _First + __olength;
554*4bdff4beSrobert     } else { // cases "1729", "17.29", and "0.001729"
555*4bdff4beSrobert       // Print the decimal digits, right-aligned within [_First, _First + _Total_fixed_length).
556*4bdff4beSrobert       _Mid = _First + _Total_fixed_length;
557*4bdff4beSrobert     }
558*4bdff4beSrobert 
559*4bdff4beSrobert     while (_Output >= 10000) {
560*4bdff4beSrobert #ifdef __clang__ // TRANSITION, LLVM-38217
561*4bdff4beSrobert       const uint32_t __c = _Output - 10000 * (_Output / 10000);
562*4bdff4beSrobert #else
563*4bdff4beSrobert       const uint32_t __c = _Output % 10000;
564*4bdff4beSrobert #endif
565*4bdff4beSrobert       _Output /= 10000;
566*4bdff4beSrobert       const uint32_t __c0 = (__c % 100) << 1;
567*4bdff4beSrobert       const uint32_t __c1 = (__c / 100) << 1;
568*4bdff4beSrobert       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c0, 2);
569*4bdff4beSrobert       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c1, 2);
570*4bdff4beSrobert     }
571*4bdff4beSrobert     if (_Output >= 100) {
572*4bdff4beSrobert       const uint32_t __c = (_Output % 100) << 1;
573*4bdff4beSrobert       _Output /= 100;
574*4bdff4beSrobert       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
575*4bdff4beSrobert     }
576*4bdff4beSrobert     if (_Output >= 10) {
577*4bdff4beSrobert       const uint32_t __c = _Output << 1;
578*4bdff4beSrobert       _VSTD::memcpy(_Mid -= 2, __DIGIT_TABLE + __c, 2);
579*4bdff4beSrobert     } else {
580*4bdff4beSrobert       *--_Mid = static_cast<char>('0' + _Output);
581*4bdff4beSrobert     }
582*4bdff4beSrobert 
583*4bdff4beSrobert     if (_Ryu_exponent > 0) { // case "172900" with _Can_use_ryu
584*4bdff4beSrobert       // Performance note: it might be more efficient to do this immediately after setting _Mid.
585*4bdff4beSrobert       _VSTD::memset(_First + __olength, '0', static_cast<size_t>(_Ryu_exponent));
586*4bdff4beSrobert     } else if (_Ryu_exponent == 0) { // case "1729"
587*4bdff4beSrobert       // Done!
588*4bdff4beSrobert     } else if (_Whole_digits > 0) { // case "17.29"
589*4bdff4beSrobert       // Performance note: moving digits might not be optimal.
590*4bdff4beSrobert       _VSTD::memmove(_First, _First + 1, static_cast<size_t>(_Whole_digits));
591*4bdff4beSrobert       _First[_Whole_digits] = '.';
592*4bdff4beSrobert     } else { // case "0.001729"
593*4bdff4beSrobert       // Performance note: a larger memset() followed by overwriting '.' might be more efficient.
594*4bdff4beSrobert       _First[0] = '0';
595*4bdff4beSrobert       _First[1] = '.';
596*4bdff4beSrobert       _VSTD::memset(_First + 2, '0', static_cast<size_t>(-_Whole_digits));
597*4bdff4beSrobert     }
598*4bdff4beSrobert 
599*4bdff4beSrobert     return { _First + _Total_fixed_length, errc{} };
600*4bdff4beSrobert   }
601*4bdff4beSrobert 
602*4bdff4beSrobert   const uint32_t _Total_scientific_length =
603*4bdff4beSrobert     __olength + (__olength > 1) + 4; // digits + possible decimal point + scientific exponent
604*4bdff4beSrobert   if (_Last - _First < static_cast<ptrdiff_t>(_Total_scientific_length)) {
605*4bdff4beSrobert     return { _Last, errc::value_too_large };
606*4bdff4beSrobert   }
607*4bdff4beSrobert   char* const __result = _First;
608*4bdff4beSrobert 
609*4bdff4beSrobert   // Print the decimal digits.
610*4bdff4beSrobert   uint32_t __i = 0;
611*4bdff4beSrobert   while (_Output >= 10000) {
612*4bdff4beSrobert #ifdef __clang__ // TRANSITION, LLVM-38217
613*4bdff4beSrobert     const uint32_t __c = _Output - 10000 * (_Output / 10000);
614*4bdff4beSrobert #else
615*4bdff4beSrobert     const uint32_t __c = _Output % 10000;
616*4bdff4beSrobert #endif
617*4bdff4beSrobert     _Output /= 10000;
618*4bdff4beSrobert     const uint32_t __c0 = (__c % 100) << 1;
619*4bdff4beSrobert     const uint32_t __c1 = (__c / 100) << 1;
620*4bdff4beSrobert     _VSTD::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c0, 2);
621*4bdff4beSrobert     _VSTD::memcpy(__result + __olength - __i - 3, __DIGIT_TABLE + __c1, 2);
622*4bdff4beSrobert     __i += 4;
623*4bdff4beSrobert   }
624*4bdff4beSrobert   if (_Output >= 100) {
625*4bdff4beSrobert     const uint32_t __c = (_Output % 100) << 1;
626*4bdff4beSrobert     _Output /= 100;
627*4bdff4beSrobert     _VSTD::memcpy(__result + __olength - __i - 1, __DIGIT_TABLE + __c, 2);
628*4bdff4beSrobert     __i += 2;
629*4bdff4beSrobert   }
630*4bdff4beSrobert   if (_Output >= 10) {
631*4bdff4beSrobert     const uint32_t __c = _Output << 1;
632*4bdff4beSrobert     // We can't use memcpy here: the decimal dot goes between these two digits.
633*4bdff4beSrobert     __result[2] = __DIGIT_TABLE[__c + 1];
634*4bdff4beSrobert     __result[0] = __DIGIT_TABLE[__c];
635*4bdff4beSrobert   } else {
636*4bdff4beSrobert     __result[0] = static_cast<char>('0' + _Output);
637*4bdff4beSrobert   }
638*4bdff4beSrobert 
639*4bdff4beSrobert   // Print decimal point if needed.
640*4bdff4beSrobert   uint32_t __index;
641*4bdff4beSrobert   if (__olength > 1) {
642*4bdff4beSrobert     __result[1] = '.';
643*4bdff4beSrobert     __index = __olength + 1;
644*4bdff4beSrobert   } else {
645*4bdff4beSrobert     __index = 1;
646*4bdff4beSrobert   }
647*4bdff4beSrobert 
648*4bdff4beSrobert   // Print the exponent.
649*4bdff4beSrobert   __result[__index++] = 'e';
650*4bdff4beSrobert   if (_Scientific_exponent < 0) {
651*4bdff4beSrobert     __result[__index++] = '-';
652*4bdff4beSrobert     _Scientific_exponent = -_Scientific_exponent;
653*4bdff4beSrobert   } else {
654*4bdff4beSrobert     __result[__index++] = '+';
655*4bdff4beSrobert   }
656*4bdff4beSrobert 
657*4bdff4beSrobert   _VSTD::memcpy(__result + __index, __DIGIT_TABLE + 2 * _Scientific_exponent, 2);
658*4bdff4beSrobert   __index += 2;
659*4bdff4beSrobert 
660*4bdff4beSrobert   return { _First + _Total_scientific_length, errc{} };
661*4bdff4beSrobert }
662*4bdff4beSrobert 
__f2s_buffered_n(char * const _First,char * const _Last,const float __f,const chars_format _Fmt)663*4bdff4beSrobert [[nodiscard]] to_chars_result __f2s_buffered_n(char* const _First, char* const _Last, const float __f,
664*4bdff4beSrobert   const chars_format _Fmt) {
665*4bdff4beSrobert 
666*4bdff4beSrobert   // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
667*4bdff4beSrobert   const uint32_t __bits = __float_to_bits(__f);
668*4bdff4beSrobert 
669*4bdff4beSrobert   // Case distinction; exit early for the easy cases.
670*4bdff4beSrobert   if (__bits == 0) {
671*4bdff4beSrobert     if (_Fmt == chars_format::scientific) {
672*4bdff4beSrobert       if (_Last - _First < 5) {
673*4bdff4beSrobert         return { _Last, errc::value_too_large };
674*4bdff4beSrobert       }
675*4bdff4beSrobert 
676*4bdff4beSrobert       _VSTD::memcpy(_First, "0e+00", 5);
677*4bdff4beSrobert 
678*4bdff4beSrobert       return { _First + 5, errc{} };
679*4bdff4beSrobert     }
680*4bdff4beSrobert 
681*4bdff4beSrobert     // Print "0" for chars_format::fixed, chars_format::general, and chars_format{}.
682*4bdff4beSrobert     if (_First == _Last) {
683*4bdff4beSrobert       return { _Last, errc::value_too_large };
684*4bdff4beSrobert     }
685*4bdff4beSrobert 
686*4bdff4beSrobert     *_First = '0';
687*4bdff4beSrobert 
688*4bdff4beSrobert     return { _First + 1, errc{} };
689*4bdff4beSrobert   }
690*4bdff4beSrobert 
691*4bdff4beSrobert   // Decode __bits into mantissa and exponent.
692*4bdff4beSrobert   const uint32_t __ieeeMantissa = __bits & ((1u << __FLOAT_MANTISSA_BITS) - 1);
693*4bdff4beSrobert   const uint32_t __ieeeExponent = __bits >> __FLOAT_MANTISSA_BITS;
694*4bdff4beSrobert 
695*4bdff4beSrobert   // When _Fmt == chars_format::fixed and the floating-point number is a large integer,
696*4bdff4beSrobert   // it's faster to skip Ryu and immediately print the integer exactly.
697*4bdff4beSrobert   if (_Fmt == chars_format::fixed) {
698*4bdff4beSrobert     const uint32_t _Mantissa2 = __ieeeMantissa | (1u << __FLOAT_MANTISSA_BITS); // restore implicit bit
699*4bdff4beSrobert     const int32_t _Exponent2 = static_cast<int32_t>(__ieeeExponent)
700*4bdff4beSrobert       - __FLOAT_BIAS - __FLOAT_MANTISSA_BITS; // bias and normalization
701*4bdff4beSrobert 
702*4bdff4beSrobert     // Normal values are equal to _Mantissa2 * 2^_Exponent2.
703*4bdff4beSrobert     // (Subnormals are different, but they'll be rejected by the _Exponent2 test here, so they can be ignored.)
704*4bdff4beSrobert 
705*4bdff4beSrobert     if (_Exponent2 > 0) {
706*4bdff4beSrobert       return _Large_integer_to_chars(_First, _Last, _Mantissa2, _Exponent2);
707*4bdff4beSrobert     }
708*4bdff4beSrobert   }
709*4bdff4beSrobert 
710*4bdff4beSrobert   const __floating_decimal_32 __v = __f2d(__ieeeMantissa, __ieeeExponent);
711*4bdff4beSrobert   return __to_chars(_First, _Last, __v, _Fmt, __ieeeMantissa, __ieeeExponent);
712*4bdff4beSrobert }
713*4bdff4beSrobert 
714*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
715*4bdff4beSrobert 
716*4bdff4beSrobert // clang-format on
717