Path: blob/main/contrib/llvm-project/libcxx/src/ryu/d2fixed.cpp
35231 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// Copyright (c) Microsoft Corporation.9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception1011// Copyright 2018 Ulf Adams12// Copyright (c) Microsoft Corporation. All rights reserved.1314// Boost Software License - Version 1.0 - August 17th, 20031516// Permission is hereby granted, free of charge, to any person or organization17// obtaining a copy of the software and accompanying documentation covered by18// this license (the "Software") to use, reproduce, display, distribute,19// execute, and transmit the Software, and to prepare derivative works of the20// Software, and to permit third-parties to whom the Software is furnished to21// do so, all subject to the following:2223// The copyright notices in the Software and this entire statement, including24// the above license grant, this restriction and the following disclaimer,25// must be included in all copies of the Software, in whole or in part, and26// all derivative works of the Software, unless such copies or derivative27// works are solely in the form of machine-executable object code generated by28// a source language processor.2930// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR31// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,32// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT33// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE34// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,35// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER36// DEALINGS IN THE SOFTWARE.3738// Avoid formatting to keep the changes with the original code minimal.39// clang-format off4041#include <__assert>42#include <__config>43#include <charconv>44#include <cstring>4546#include "include/ryu/common.h"47#include "include/ryu/d2fixed.h"48#include "include/ryu/d2fixed_full_table.h"49#include "include/ryu/d2s.h"50#include "include/ryu/d2s_intrinsics.h"51#include "include/ryu/digit_table.h"5253_LIBCPP_BEGIN_NAMESPACE_STD5455inline constexpr int __POW10_ADDITIONAL_BITS = 120;5657#ifdef _LIBCPP_INTRINSIC12858// Returns the low 64 bits of the high 128 bits of the 256-bit product of a and b.59[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __umul256_hi128_lo64(60const uint64_t __aHi, const uint64_t __aLo, const uint64_t __bHi, const uint64_t __bLo) {61uint64_t __b00Hi;62const uint64_t __b00Lo = __ryu_umul128(__aLo, __bLo, &__b00Hi);63uint64_t __b01Hi;64const uint64_t __b01Lo = __ryu_umul128(__aLo, __bHi, &__b01Hi);65uint64_t __b10Hi;66const uint64_t __b10Lo = __ryu_umul128(__aHi, __bLo, &__b10Hi);67uint64_t __b11Hi;68const uint64_t __b11Lo = __ryu_umul128(__aHi, __bHi, &__b11Hi);69(void) __b00Lo; // unused70(void) __b11Hi; // unused71const uint64_t __temp1Lo = __b10Lo + __b00Hi;72const uint64_t __temp1Hi = __b10Hi + (__temp1Lo < __b10Lo);73const uint64_t __temp2Lo = __b01Lo + __temp1Lo;74const uint64_t __temp2Hi = __b01Hi + (__temp2Lo < __b01Lo);75return __b11Lo + __temp1Hi + __temp2Hi;76}7778[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __uint128_mod1e9(const uint64_t __vHi, const uint64_t __vLo) {79// After multiplying, we're going to shift right by 29, then truncate to uint32_t.80// This means that we need only 29 + 32 = 61 bits, so we can truncate to uint64_t before shifting.81const uint64_t __multiplied = __umul256_hi128_lo64(__vHi, __vLo, 0x89705F4136B4A597u, 0x31680A88F8953031u);8283// For uint32_t truncation, see the __mod1e9() comment in d2s_intrinsics.h.84const uint32_t __shifted = static_cast<uint32_t>(__multiplied >> 29);8586return static_cast<uint32_t>(__vLo) - 1000000000 * __shifted;87}88#endif // ^^^ intrinsics available ^^^8990[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulShift_mod1e9(const uint64_t __m, const uint64_t* const __mul, const int32_t __j) {91uint64_t __high0; // 6492const uint64_t __low0 = __ryu_umul128(__m, __mul[0], &__high0); // 093uint64_t __high1; // 12894const uint64_t __low1 = __ryu_umul128(__m, __mul[1], &__high1); // 6495uint64_t __high2; // 19296const uint64_t __low2 = __ryu_umul128(__m, __mul[2], &__high2); // 12897const uint64_t __s0low = __low0; // 098(void) __s0low; // unused99const uint64_t __s0high = __low1 + __high0; // 64100const uint32_t __c1 = __s0high < __low1;101const uint64_t __s1low = __low2 + __high1 + __c1; // 128102const uint32_t __c2 = __s1low < __low2; // __high1 + __c1 can't overflow, so compare against __low2103const uint64_t __s1high = __high2 + __c2; // 192104_LIBCPP_ASSERT_INTERNAL(__j >= 128, "");105_LIBCPP_ASSERT_INTERNAL(__j <= 180, "");106#ifdef _LIBCPP_INTRINSIC128107const uint32_t __dist = static_cast<uint32_t>(__j - 128); // __dist: [0, 52]108const uint64_t __shiftedhigh = __s1high >> __dist;109const uint64_t __shiftedlow = __ryu_shiftright128(__s1low, __s1high, __dist);110return __uint128_mod1e9(__shiftedhigh, __shiftedlow);111#else // ^^^ intrinsics available ^^^ / vvv intrinsics unavailable vvv112if (__j < 160) { // __j: [128, 160)113const uint64_t __r0 = __mod1e9(__s1high);114const uint64_t __r1 = __mod1e9((__r0 << 32) | (__s1low >> 32));115const uint64_t __r2 = ((__r1 << 32) | (__s1low & 0xffffffff));116return __mod1e9(__r2 >> (__j - 128));117} else { // __j: [160, 192)118const uint64_t __r0 = __mod1e9(__s1high);119const uint64_t __r1 = ((__r0 << 32) | (__s1low >> 32));120return __mod1e9(__r1 >> (__j - 160));121}122#endif // ^^^ intrinsics unavailable ^^^123}124125void __append_n_digits(const uint32_t __olength, uint32_t __digits, char* const __result) {126uint32_t __i = 0;127while (__digits >= 10000) {128#ifdef __clang__ // TRANSITION, LLVM-38217129const uint32_t __c = __digits - 10000 * (__digits / 10000);130#else131const uint32_t __c = __digits % 10000;132#endif133__digits /= 10000;134const uint32_t __c0 = (__c % 100) << 1;135const uint32_t __c1 = (__c / 100) << 1;136std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c0, 2);137std::memcpy(__result + __olength - __i - 4, __DIGIT_TABLE + __c1, 2);138__i += 4;139}140if (__digits >= 100) {141const uint32_t __c = (__digits % 100) << 1;142__digits /= 100;143std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c, 2);144__i += 2;145}146if (__digits >= 10) {147const uint32_t __c = __digits << 1;148std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c, 2);149} else {150__result[0] = static_cast<char>('0' + __digits);151}152}153154_LIBCPP_HIDE_FROM_ABI inline void __append_d_digits(const uint32_t __olength, uint32_t __digits, char* const __result) {155uint32_t __i = 0;156while (__digits >= 10000) {157#ifdef __clang__ // TRANSITION, LLVM-38217158const uint32_t __c = __digits - 10000 * (__digits / 10000);159#else160const uint32_t __c = __digits % 10000;161#endif162__digits /= 10000;163const uint32_t __c0 = (__c % 100) << 1;164const uint32_t __c1 = (__c / 100) << 1;165std::memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE + __c0, 2);166std::memcpy(__result + __olength + 1 - __i - 4, __DIGIT_TABLE + __c1, 2);167__i += 4;168}169if (__digits >= 100) {170const uint32_t __c = (__digits % 100) << 1;171__digits /= 100;172std::memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE + __c, 2);173__i += 2;174}175if (__digits >= 10) {176const uint32_t __c = __digits << 1;177__result[2] = __DIGIT_TABLE[__c + 1];178__result[1] = '.';179__result[0] = __DIGIT_TABLE[__c];180} else {181__result[1] = '.';182__result[0] = static_cast<char>('0' + __digits);183}184}185186_LIBCPP_HIDE_FROM_ABI inline void __append_c_digits(const uint32_t __count, uint32_t __digits, char* const __result) {187uint32_t __i = 0;188for (; __i < __count - 1; __i += 2) {189const uint32_t __c = (__digits % 100) << 1;190__digits /= 100;191std::memcpy(__result + __count - __i - 2, __DIGIT_TABLE + __c, 2);192}193if (__i < __count) {194const char __c = static_cast<char>('0' + (__digits % 10));195__result[__count - __i - 1] = __c;196}197}198199void __append_nine_digits(uint32_t __digits, char* const __result) {200if (__digits == 0) {201std::memset(__result, '0', 9);202return;203}204205for (uint32_t __i = 0; __i < 5; __i += 4) {206#ifdef __clang__ // TRANSITION, LLVM-38217207const uint32_t __c = __digits - 10000 * (__digits / 10000);208#else209const uint32_t __c = __digits % 10000;210#endif211__digits /= 10000;212const uint32_t __c0 = (__c % 100) << 1;213const uint32_t __c1 = (__c / 100) << 1;214std::memcpy(__result + 7 - __i, __DIGIT_TABLE + __c0, 2);215std::memcpy(__result + 5 - __i, __DIGIT_TABLE + __c1, 2);216}217__result[0] = static_cast<char>('0' + __digits);218}219220[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __indexForExponent(const uint32_t __e) {221return (__e + 15) / 16;222}223224[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __pow10BitsForIndex(const uint32_t __idx) {225return 16 * __idx + __POW10_ADDITIONAL_BITS;226}227228[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __lengthForIndex(const uint32_t __idx) {229// +1 for ceil, +16 for mantissa, +8 to round up when dividing by 9230return (__log10Pow2(16 * static_cast<int32_t>(__idx)) + 1 + 16 + 8) / 9;231}232233[[nodiscard]] to_chars_result __d2fixed_buffered_n(char* _First, char* const _Last, const double __d,234const uint32_t __precision) {235char* const _Original_first = _First;236237const uint64_t __bits = __double_to_bits(__d);238239// Case distinction; exit early for the easy cases.240if (__bits == 0) {241const int32_t _Total_zero_length = 1 // leading zero242+ static_cast<int32_t>(__precision != 0) // possible decimal point243+ static_cast<int32_t>(__precision); // zeroes after decimal point244245if (_Last - _First < _Total_zero_length) {246return { _Last, errc::value_too_large };247}248249*_First++ = '0';250if (__precision > 0) {251*_First++ = '.';252std::memset(_First, '0', __precision);253_First += __precision;254}255return { _First, errc{} };256}257258// Decode __bits into mantissa and exponent.259const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);260const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);261262int32_t __e2;263uint64_t __m2;264if (__ieeeExponent == 0) {265__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;266__m2 = __ieeeMantissa;267} else {268__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;269__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;270}271272bool __nonzero = false;273if (__e2 >= -52) {274const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));275const uint32_t __p10bits = __pow10BitsForIndex(__idx);276const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));277for (int32_t __i = __len - 1; __i >= 0; --__i) {278const uint32_t __j = __p10bits - __e2;279// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is280// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.281const uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],282static_cast<int32_t>(__j + 8));283if (__nonzero) {284if (_Last - _First < 9) {285return { _Last, errc::value_too_large };286}287__append_nine_digits(__digits, _First);288_First += 9;289} else if (__digits != 0) {290const uint32_t __olength = __decimalLength9(__digits);291if (_Last - _First < static_cast<ptrdiff_t>(__olength)) {292return { _Last, errc::value_too_large };293}294__append_n_digits(__olength, __digits, _First);295_First += __olength;296__nonzero = true;297}298}299}300if (!__nonzero) {301if (_First == _Last) {302return { _Last, errc::value_too_large };303}304*_First++ = '0';305}306if (__precision > 0) {307if (_First == _Last) {308return { _Last, errc::value_too_large };309}310*_First++ = '.';311}312if (__e2 < 0) {313const int32_t __idx = -__e2 / 16;314const uint32_t __blocks = __precision / 9 + 1;315// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.316int __roundUp = 0;317uint32_t __i = 0;318if (__blocks <= __MIN_BLOCK_2[__idx]) {319__i = __blocks;320if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {321return { _Last, errc::value_too_large };322}323std::memset(_First, '0', __precision);324_First += __precision;325} else if (__i < __MIN_BLOCK_2[__idx]) {326__i = __MIN_BLOCK_2[__idx];327if (_Last - _First < static_cast<ptrdiff_t>(9 * __i)) {328return { _Last, errc::value_too_large };329}330std::memset(_First, '0', 9 * __i);331_First += 9 * __i;332}333for (; __i < __blocks; ++__i) {334const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);335const uint32_t __p = __POW10_OFFSET_2[__idx] + __i - __MIN_BLOCK_2[__idx];336if (__p >= __POW10_OFFSET_2[__idx + 1]) {337// If the remaining digits are all 0, then we might as well use memset.338// No rounding required in this case.339const uint32_t __fill = __precision - 9 * __i;340if (_Last - _First < static_cast<ptrdiff_t>(__fill)) {341return { _Last, errc::value_too_large };342}343std::memset(_First, '0', __fill);344_First += __fill;345break;346}347// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is348// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.349uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);350if (__i < __blocks - 1) {351if (_Last - _First < 9) {352return { _Last, errc::value_too_large };353}354__append_nine_digits(__digits, _First);355_First += 9;356} else {357const uint32_t __maximum = __precision - 9 * __i;358uint32_t __lastDigit = 0;359for (uint32_t __k = 0; __k < 9 - __maximum; ++__k) {360__lastDigit = __digits % 10;361__digits /= 10;362}363if (__lastDigit != 5) {364__roundUp = __lastDigit > 5;365} else {366// Is m * 10^(additionalDigits + 1) / 2^(-__e2) integer?367const int32_t __requiredTwos = -__e2 - static_cast<int32_t>(__precision) - 1;368const bool __trailingZeros = __requiredTwos <= 0369|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));370__roundUp = __trailingZeros ? 2 : 1;371}372if (__maximum > 0) {373if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {374return { _Last, errc::value_too_large };375}376__append_c_digits(__maximum, __digits, _First);377_First += __maximum;378}379break;380}381}382if (__roundUp != 0) {383char* _Round = _First;384char* _Dot = _Last;385while (true) {386if (_Round == _Original_first) {387_Round[0] = '1';388if (_Dot != _Last) {389_Dot[0] = '0';390_Dot[1] = '.';391}392if (_First == _Last) {393return { _Last, errc::value_too_large };394}395*_First++ = '0';396break;397}398--_Round;399const char __c = _Round[0];400if (__c == '.') {401_Dot = _Round;402} else if (__c == '9') {403_Round[0] = '0';404__roundUp = 1;405} else {406if (__roundUp == 1 || __c % 2 != 0) {407_Round[0] = __c + 1;408}409break;410}411}412}413} else {414if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {415return { _Last, errc::value_too_large };416}417std::memset(_First, '0', __precision);418_First += __precision;419}420return { _First, errc{} };421}422423[[nodiscard]] to_chars_result __d2exp_buffered_n(char* _First, char* const _Last, const double __d,424uint32_t __precision) {425char* const _Original_first = _First;426427const uint64_t __bits = __double_to_bits(__d);428429// Case distinction; exit early for the easy cases.430if (__bits == 0) {431const int32_t _Total_zero_length = 1 // leading zero432+ static_cast<int32_t>(__precision != 0) // possible decimal point433+ static_cast<int32_t>(__precision) // zeroes after decimal point434+ 4; // "e+00"435if (_Last - _First < _Total_zero_length) {436return { _Last, errc::value_too_large };437}438*_First++ = '0';439if (__precision > 0) {440*_First++ = '.';441std::memset(_First, '0', __precision);442_First += __precision;443}444std::memcpy(_First, "e+00", 4);445_First += 4;446return { _First, errc{} };447}448449// Decode __bits into mantissa and exponent.450const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);451const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);452453int32_t __e2;454uint64_t __m2;455if (__ieeeExponent == 0) {456__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;457__m2 = __ieeeMantissa;458} else {459__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;460__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;461}462463const bool __printDecimalPoint = __precision > 0;464++__precision;465uint32_t __digits = 0;466uint32_t __printedDigits = 0;467uint32_t __availableDigits = 0;468int32_t __exp = 0;469if (__e2 >= -52) {470const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));471const uint32_t __p10bits = __pow10BitsForIndex(__idx);472const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));473for (int32_t __i = __len - 1; __i >= 0; --__i) {474const uint32_t __j = __p10bits - __e2;475// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is476// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.477__digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],478static_cast<int32_t>(__j + 8));479if (__printedDigits != 0) {480if (__printedDigits + 9 > __precision) {481__availableDigits = 9;482break;483}484if (_Last - _First < 9) {485return { _Last, errc::value_too_large };486}487__append_nine_digits(__digits, _First);488_First += 9;489__printedDigits += 9;490} else if (__digits != 0) {491__availableDigits = __decimalLength9(__digits);492__exp = __i * 9 + static_cast<int32_t>(__availableDigits) - 1;493if (__availableDigits > __precision) {494break;495}496if (__printDecimalPoint) {497if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {498return { _Last, errc::value_too_large };499}500__append_d_digits(__availableDigits, __digits, _First);501_First += __availableDigits + 1; // +1 for decimal point502} else {503if (_First == _Last) {504return { _Last, errc::value_too_large };505}506*_First++ = static_cast<char>('0' + __digits);507}508__printedDigits = __availableDigits;509__availableDigits = 0;510}511}512}513514if (__e2 < 0 && __availableDigits == 0) {515const int32_t __idx = -__e2 / 16;516for (int32_t __i = __MIN_BLOCK_2[__idx]; __i < 200; ++__i) {517const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);518const uint32_t __p = __POW10_OFFSET_2[__idx] + static_cast<uint32_t>(__i) - __MIN_BLOCK_2[__idx];519// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is520// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.521__digits = (__p >= __POW10_OFFSET_2[__idx + 1]) ? 0 : __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);522if (__printedDigits != 0) {523if (__printedDigits + 9 > __precision) {524__availableDigits = 9;525break;526}527if (_Last - _First < 9) {528return { _Last, errc::value_too_large };529}530__append_nine_digits(__digits, _First);531_First += 9;532__printedDigits += 9;533} else if (__digits != 0) {534__availableDigits = __decimalLength9(__digits);535__exp = -(__i + 1) * 9 + static_cast<int32_t>(__availableDigits) - 1;536if (__availableDigits > __precision) {537break;538}539if (__printDecimalPoint) {540if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {541return { _Last, errc::value_too_large };542}543__append_d_digits(__availableDigits, __digits, _First);544_First += __availableDigits + 1; // +1 for decimal point545} else {546if (_First == _Last) {547return { _Last, errc::value_too_large };548}549*_First++ = static_cast<char>('0' + __digits);550}551__printedDigits = __availableDigits;552__availableDigits = 0;553}554}555}556557const uint32_t __maximum = __precision - __printedDigits;558if (__availableDigits == 0) {559__digits = 0;560}561uint32_t __lastDigit = 0;562if (__availableDigits > __maximum) {563for (uint32_t __k = 0; __k < __availableDigits - __maximum; ++__k) {564__lastDigit = __digits % 10;565__digits /= 10;566}567}568// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.569int __roundUp = 0;570if (__lastDigit != 5) {571__roundUp = __lastDigit > 5;572} else {573// Is m * 2^__e2 * 10^(__precision + 1 - __exp) integer?574// __precision was already increased by 1, so we don't need to write + 1 here.575const int32_t __rexp = static_cast<int32_t>(__precision) - __exp;576const int32_t __requiredTwos = -__e2 - __rexp;577bool __trailingZeros = __requiredTwos <= 0578|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));579if (__rexp < 0) {580const int32_t __requiredFives = -__rexp;581__trailingZeros = __trailingZeros && __multipleOfPowerOf5(__m2, static_cast<uint32_t>(__requiredFives));582}583__roundUp = __trailingZeros ? 2 : 1;584}585if (__printedDigits != 0) {586if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {587return { _Last, errc::value_too_large };588}589if (__digits == 0) {590std::memset(_First, '0', __maximum);591} else {592__append_c_digits(__maximum, __digits, _First);593}594_First += __maximum;595} else {596if (__printDecimalPoint) {597if (_Last - _First < static_cast<ptrdiff_t>(__maximum + 1)) {598return { _Last, errc::value_too_large };599}600__append_d_digits(__maximum, __digits, _First);601_First += __maximum + 1; // +1 for decimal point602} else {603if (_First == _Last) {604return { _Last, errc::value_too_large };605}606*_First++ = static_cast<char>('0' + __digits);607}608}609if (__roundUp != 0) {610char* _Round = _First;611while (true) {612if (_Round == _Original_first) {613_Round[0] = '1';614++__exp;615break;616}617--_Round;618const char __c = _Round[0];619if (__c == '.') {620// Keep going.621} else if (__c == '9') {622_Round[0] = '0';623__roundUp = 1;624} else {625if (__roundUp == 1 || __c % 2 != 0) {626_Round[0] = __c + 1;627}628break;629}630}631}632633char _Sign_character;634635if (__exp < 0) {636_Sign_character = '-';637__exp = -__exp;638} else {639_Sign_character = '+';640}641642const int _Exponent_part_length = __exp >= 100643? 5 // "e+NNN"644: 4; // "e+NN"645646if (_Last - _First < _Exponent_part_length) {647return { _Last, errc::value_too_large };648}649650*_First++ = 'e';651*_First++ = _Sign_character;652653if (__exp >= 100) {654const int32_t __c = __exp % 10;655std::memcpy(_First, __DIGIT_TABLE + 2 * (__exp / 10), 2);656_First[2] = static_cast<char>('0' + __c);657_First += 3;658} else {659std::memcpy(_First, __DIGIT_TABLE + 2 * __exp, 2);660_First += 2;661}662663return { _First, errc{} };664}665666_LIBCPP_END_NAMESPACE_STD667668// clang-format on669670671