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