Path: blob/main/system/lib/libcxx/src/ryu/d2fixed.cpp
6175 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 <cstddef>45#include <cstring>4647#include "include/ryu/common.h"48#include "include/ryu/d2fixed.h"49#include "include/ryu/d2fixed_full_table.h"50#include "include/ryu/d2s.h"51#include "include/ryu/d2s_intrinsics.h"52#include "include/ryu/digit_table.h"5354_LIBCPP_BEGIN_NAMESPACE_STD5556inline constexpr int __POW10_ADDITIONAL_BITS = 120;5758#ifdef _LIBCPP_INTRINSIC12859// Returns the low 64 bits of the high 128 bits of the 256-bit product of a and b.60[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint64_t __umul256_hi128_lo64(61const uint64_t __aHi, const uint64_t __aLo, const uint64_t __bHi, const uint64_t __bLo) {62uint64_t __b00Hi;63const uint64_t __b00Lo = __ryu_umul128(__aLo, __bLo, &__b00Hi);64uint64_t __b01Hi;65const uint64_t __b01Lo = __ryu_umul128(__aLo, __bHi, &__b01Hi);66uint64_t __b10Hi;67const uint64_t __b10Lo = __ryu_umul128(__aHi, __bLo, &__b10Hi);68uint64_t __b11Hi;69const uint64_t __b11Lo = __ryu_umul128(__aHi, __bHi, &__b11Hi);70(void) __b00Lo; // unused71(void) __b11Hi; // unused72const uint64_t __temp1Lo = __b10Lo + __b00Hi;73const uint64_t __temp1Hi = __b10Hi + (__temp1Lo < __b10Lo);74const uint64_t __temp2Lo = __b01Lo + __temp1Lo;75const uint64_t __temp2Hi = __b01Hi + (__temp2Lo < __b01Lo);76return __b11Lo + __temp1Hi + __temp2Hi;77}7879[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __uint128_mod1e9(const uint64_t __vHi, const uint64_t __vLo) {80// After multiplying, we're going to shift right by 29, then truncate to uint32_t.81// This means that we need only 29 + 32 = 61 bits, so we can truncate to uint64_t before shifting.82const uint64_t __multiplied = __umul256_hi128_lo64(__vHi, __vLo, 0x89705F4136B4A597u, 0x31680A88F8953031u);8384// For uint32_t truncation, see the __mod1e9() comment in d2s_intrinsics.h.85const uint32_t __shifted = static_cast<uint32_t>(__multiplied >> 29);8687return static_cast<uint32_t>(__vLo) - 1000000000 * __shifted;88}89#endif // ^^^ intrinsics available ^^^9091[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __mulShift_mod1e9(const uint64_t __m, const uint64_t* const __mul, const int32_t __j) {92uint64_t __high0; // 6493const uint64_t __low0 = __ryu_umul128(__m, __mul[0], &__high0); // 094uint64_t __high1; // 12895const uint64_t __low1 = __ryu_umul128(__m, __mul[1], &__high1); // 6496uint64_t __high2; // 19297const uint64_t __low2 = __ryu_umul128(__m, __mul[2], &__high2); // 12898const uint64_t __s0low = __low0; // 099(void) __s0low; // unused100const uint64_t __s0high = __low1 + __high0; // 64101const uint32_t __c1 = __s0high < __low1;102const uint64_t __s1low = __low2 + __high1 + __c1; // 128103const uint32_t __c2 = __s1low < __low2; // __high1 + __c1 can't overflow, so compare against __low2104const uint64_t __s1high = __high2 + __c2; // 192105_LIBCPP_ASSERT_INTERNAL(__j >= 128, "");106_LIBCPP_ASSERT_INTERNAL(__j <= 180, "");107#ifdef _LIBCPP_INTRINSIC128108const uint32_t __dist = static_cast<uint32_t>(__j - 128); // __dist: [0, 52]109const uint64_t __shiftedhigh = __s1high >> __dist;110const uint64_t __shiftedlow = __ryu_shiftright128(__s1low, __s1high, __dist);111return __uint128_mod1e9(__shiftedhigh, __shiftedlow);112#else // ^^^ intrinsics available ^^^ / vvv intrinsics unavailable vvv113if (__j < 160) { // __j: [128, 160)114const uint64_t __r0 = __mod1e9(__s1high);115const uint64_t __r1 = __mod1e9((__r0 << 32) | (__s1low >> 32));116const uint64_t __r2 = ((__r1 << 32) | (__s1low & 0xffffffff));117return __mod1e9(__r2 >> (__j - 128));118} else { // __j: [160, 192)119const uint64_t __r0 = __mod1e9(__s1high);120const uint64_t __r1 = ((__r0 << 32) | (__s1low >> 32));121return __mod1e9(__r1 >> (__j - 160));122}123#endif // ^^^ intrinsics unavailable ^^^124}125126void __append_n_digits(const uint32_t __olength, uint32_t __digits, char* const __result) {127uint32_t __i = 0;128while (__digits >= 10000) {129#ifdef __clang__ // TRANSITION, LLVM-38217130const uint32_t __c = __digits - 10000 * (__digits / 10000);131#else132const uint32_t __c = __digits % 10000;133#endif134__digits /= 10000;135const uint32_t __c0 = (__c % 100) << 1;136const uint32_t __c1 = (__c / 100) << 1;137std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c0, 2);138std::memcpy(__result + __olength - __i - 4, __DIGIT_TABLE + __c1, 2);139__i += 4;140}141if (__digits >= 100) {142const uint32_t __c = (__digits % 100) << 1;143__digits /= 100;144std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c, 2);145__i += 2;146}147if (__digits >= 10) {148const uint32_t __c = __digits << 1;149std::memcpy(__result + __olength - __i - 2, __DIGIT_TABLE + __c, 2);150} else {151__result[0] = static_cast<char>('0' + __digits);152}153}154155_LIBCPP_HIDE_FROM_ABI inline void __append_d_digits(const uint32_t __olength, uint32_t __digits, char* const __result) {156uint32_t __i = 0;157while (__digits >= 10000) {158#ifdef __clang__ // TRANSITION, LLVM-38217159const uint32_t __c = __digits - 10000 * (__digits / 10000);160#else161const uint32_t __c = __digits % 10000;162#endif163__digits /= 10000;164const uint32_t __c0 = (__c % 100) << 1;165const uint32_t __c1 = (__c / 100) << 1;166std::memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE + __c0, 2);167std::memcpy(__result + __olength + 1 - __i - 4, __DIGIT_TABLE + __c1, 2);168__i += 4;169}170if (__digits >= 100) {171const uint32_t __c = (__digits % 100) << 1;172__digits /= 100;173std::memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE + __c, 2);174__i += 2;175}176if (__digits >= 10) {177const uint32_t __c = __digits << 1;178__result[2] = __DIGIT_TABLE[__c + 1];179__result[1] = '.';180__result[0] = __DIGIT_TABLE[__c];181} else {182__result[1] = '.';183__result[0] = static_cast<char>('0' + __digits);184}185}186187_LIBCPP_HIDE_FROM_ABI inline void __append_c_digits(const uint32_t __count, uint32_t __digits, char* const __result) {188uint32_t __i = 0;189for (; __i < __count - 1; __i += 2) {190const uint32_t __c = (__digits % 100) << 1;191__digits /= 100;192std::memcpy(__result + __count - __i - 2, __DIGIT_TABLE + __c, 2);193}194if (__i < __count) {195const char __c = static_cast<char>('0' + (__digits % 10));196__result[__count - __i - 1] = __c;197}198}199200void __append_nine_digits(uint32_t __digits, char* const __result) {201if (__digits == 0) {202std::memset(__result, '0', 9);203return;204}205206for (uint32_t __i = 0; __i < 5; __i += 4) {207#ifdef __clang__ // TRANSITION, LLVM-38217208const uint32_t __c = __digits - 10000 * (__digits / 10000);209#else210const uint32_t __c = __digits % 10000;211#endif212__digits /= 10000;213const uint32_t __c0 = (__c % 100) << 1;214const uint32_t __c1 = (__c / 100) << 1;215std::memcpy(__result + 7 - __i, __DIGIT_TABLE + __c0, 2);216std::memcpy(__result + 5 - __i, __DIGIT_TABLE + __c1, 2);217}218__result[0] = static_cast<char>('0' + __digits);219}220221[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __indexForExponent(const uint32_t __e) {222return (__e + 15) / 16;223}224225[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __pow10BitsForIndex(const uint32_t __idx) {226return 16 * __idx + __POW10_ADDITIONAL_BITS;227}228229[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline uint32_t __lengthForIndex(const uint32_t __idx) {230// +1 for ceil, +16 for mantissa, +8 to round up when dividing by 9231return (__log10Pow2(16 * static_cast<int32_t>(__idx)) + 1 + 16 + 8) / 9;232}233234[[nodiscard]] to_chars_result __d2fixed_buffered_n(char* _First, char* const _Last, const double __d,235const uint32_t __precision) {236char* const _Original_first = _First;237238const uint64_t __bits = __double_to_bits(__d);239240// Case distinction; exit early for the easy cases.241if (__bits == 0) {242const int32_t _Total_zero_length = 1 // leading zero243+ static_cast<int32_t>(__precision != 0) // possible decimal point244+ static_cast<int32_t>(__precision); // zeroes after decimal point245246if (_Last - _First < _Total_zero_length) {247return { _Last, errc::value_too_large };248}249250*_First++ = '0';251if (__precision > 0) {252*_First++ = '.';253std::memset(_First, '0', __precision);254_First += __precision;255}256return { _First, errc{} };257}258259// Decode __bits into mantissa and exponent.260const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);261const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);262263int32_t __e2;264uint64_t __m2;265if (__ieeeExponent == 0) {266__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;267__m2 = __ieeeMantissa;268} else {269__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;270__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;271}272273bool __nonzero = false;274if (__e2 >= -52) {275const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));276const uint32_t __p10bits = __pow10BitsForIndex(__idx);277const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));278for (int32_t __i = __len - 1; __i >= 0; --__i) {279const uint32_t __j = __p10bits - __e2;280// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is281// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.282const uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],283static_cast<int32_t>(__j + 8));284if (__nonzero) {285if (_Last - _First < 9) {286return { _Last, errc::value_too_large };287}288__append_nine_digits(__digits, _First);289_First += 9;290} else if (__digits != 0) {291const uint32_t __olength = __decimalLength9(__digits);292if (_Last - _First < static_cast<ptrdiff_t>(__olength)) {293return { _Last, errc::value_too_large };294}295__append_n_digits(__olength, __digits, _First);296_First += __olength;297__nonzero = true;298}299}300}301if (!__nonzero) {302if (_First == _Last) {303return { _Last, errc::value_too_large };304}305*_First++ = '0';306}307if (__precision > 0) {308if (_First == _Last) {309return { _Last, errc::value_too_large };310}311*_First++ = '.';312}313if (__e2 < 0) {314const int32_t __idx = -__e2 / 16;315const uint32_t __blocks = __precision / 9 + 1;316// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.317int __roundUp = 0;318uint32_t __i = 0;319if (__blocks <= __MIN_BLOCK_2[__idx]) {320__i = __blocks;321if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {322return { _Last, errc::value_too_large };323}324std::memset(_First, '0', __precision);325_First += __precision;326} else if (__i < __MIN_BLOCK_2[__idx]) {327__i = __MIN_BLOCK_2[__idx];328if (_Last - _First < static_cast<ptrdiff_t>(9 * __i)) {329return { _Last, errc::value_too_large };330}331std::memset(_First, '0', 9 * __i);332_First += 9 * __i;333}334for (; __i < __blocks; ++__i) {335const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);336const uint32_t __p = __POW10_OFFSET_2[__idx] + __i - __MIN_BLOCK_2[__idx];337if (__p >= __POW10_OFFSET_2[__idx + 1]) {338// If the remaining digits are all 0, then we might as well use memset.339// No rounding required in this case.340const uint32_t __fill = __precision - 9 * __i;341if (_Last - _First < static_cast<ptrdiff_t>(__fill)) {342return { _Last, errc::value_too_large };343}344std::memset(_First, '0', __fill);345_First += __fill;346break;347}348// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is349// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.350uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);351if (__i < __blocks - 1) {352if (_Last - _First < 9) {353return { _Last, errc::value_too_large };354}355__append_nine_digits(__digits, _First);356_First += 9;357} else {358const uint32_t __maximum = __precision - 9 * __i;359uint32_t __lastDigit = 0;360for (uint32_t __k = 0; __k < 9 - __maximum; ++__k) {361__lastDigit = __digits % 10;362__digits /= 10;363}364if (__lastDigit != 5) {365__roundUp = __lastDigit > 5;366} else {367// Is m * 10^(additionalDigits + 1) / 2^(-__e2) integer?368const int32_t __requiredTwos = -__e2 - static_cast<int32_t>(__precision) - 1;369const bool __trailingZeros = __requiredTwos <= 0370|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));371__roundUp = __trailingZeros ? 2 : 1;372}373if (__maximum > 0) {374if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {375return { _Last, errc::value_too_large };376}377__append_c_digits(__maximum, __digits, _First);378_First += __maximum;379}380break;381}382}383if (__roundUp != 0) {384char* _Round = _First;385char* _Dot = _Last;386while (true) {387if (_Round == _Original_first) {388_Round[0] = '1';389if (_Dot != _Last) {390_Dot[0] = '0';391_Dot[1] = '.';392}393if (_First == _Last) {394return { _Last, errc::value_too_large };395}396*_First++ = '0';397break;398}399--_Round;400const char __c = _Round[0];401if (__c == '.') {402_Dot = _Round;403} else if (__c == '9') {404_Round[0] = '0';405__roundUp = 1;406} else {407if (__roundUp == 1 || __c % 2 != 0) {408_Round[0] = __c + 1;409}410break;411}412}413}414} else {415if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {416return { _Last, errc::value_too_large };417}418std::memset(_First, '0', __precision);419_First += __precision;420}421return { _First, errc{} };422}423424[[nodiscard]] to_chars_result __d2exp_buffered_n(char* _First, char* const _Last, const double __d,425uint32_t __precision) {426char* const _Original_first = _First;427428const uint64_t __bits = __double_to_bits(__d);429430// Case distinction; exit early for the easy cases.431if (__bits == 0) {432const int32_t _Total_zero_length = 1 // leading zero433+ static_cast<int32_t>(__precision != 0) // possible decimal point434+ static_cast<int32_t>(__precision) // zeroes after decimal point435+ 4; // "e+00"436if (_Last - _First < _Total_zero_length) {437return { _Last, errc::value_too_large };438}439*_First++ = '0';440if (__precision > 0) {441*_First++ = '.';442std::memset(_First, '0', __precision);443_First += __precision;444}445std::memcpy(_First, "e+00", 4);446_First += 4;447return { _First, errc{} };448}449450// Decode __bits into mantissa and exponent.451const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);452const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);453454int32_t __e2;455uint64_t __m2;456if (__ieeeExponent == 0) {457__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;458__m2 = __ieeeMantissa;459} else {460__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;461__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;462}463464const bool __printDecimalPoint = __precision > 0;465++__precision;466uint32_t __digits = 0;467uint32_t __printedDigits = 0;468uint32_t __availableDigits = 0;469int32_t __exp = 0;470if (__e2 >= -52) {471const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));472const uint32_t __p10bits = __pow10BitsForIndex(__idx);473const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));474for (int32_t __i = __len - 1; __i >= 0; --__i) {475const uint32_t __j = __p10bits - __e2;476// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is477// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.478__digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],479static_cast<int32_t>(__j + 8));480if (__printedDigits != 0) {481if (__printedDigits + 9 > __precision) {482__availableDigits = 9;483break;484}485if (_Last - _First < 9) {486return { _Last, errc::value_too_large };487}488__append_nine_digits(__digits, _First);489_First += 9;490__printedDigits += 9;491} else if (__digits != 0) {492__availableDigits = __decimalLength9(__digits);493__exp = __i * 9 + static_cast<int32_t>(__availableDigits) - 1;494if (__availableDigits > __precision) {495break;496}497if (__printDecimalPoint) {498if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {499return { _Last, errc::value_too_large };500}501__append_d_digits(__availableDigits, __digits, _First);502_First += __availableDigits + 1; // +1 for decimal point503} else {504if (_First == _Last) {505return { _Last, errc::value_too_large };506}507*_First++ = static_cast<char>('0' + __digits);508}509__printedDigits = __availableDigits;510__availableDigits = 0;511}512}513}514515if (__e2 < 0 && __availableDigits == 0) {516const int32_t __idx = -__e2 / 16;517for (int32_t __i = __MIN_BLOCK_2[__idx]; __i < 200; ++__i) {518const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);519const uint32_t __p = __POW10_OFFSET_2[__idx] + static_cast<uint32_t>(__i) - __MIN_BLOCK_2[__idx];520// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is521// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.522__digits = (__p >= __POW10_OFFSET_2[__idx + 1]) ? 0 : __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);523if (__printedDigits != 0) {524if (__printedDigits + 9 > __precision) {525__availableDigits = 9;526break;527}528if (_Last - _First < 9) {529return { _Last, errc::value_too_large };530}531__append_nine_digits(__digits, _First);532_First += 9;533__printedDigits += 9;534} else if (__digits != 0) {535__availableDigits = __decimalLength9(__digits);536__exp = -(__i + 1) * 9 + static_cast<int32_t>(__availableDigits) - 1;537if (__availableDigits > __precision) {538break;539}540if (__printDecimalPoint) {541if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {542return { _Last, errc::value_too_large };543}544__append_d_digits(__availableDigits, __digits, _First);545_First += __availableDigits + 1; // +1 for decimal point546} else {547if (_First == _Last) {548return { _Last, errc::value_too_large };549}550*_First++ = static_cast<char>('0' + __digits);551}552__printedDigits = __availableDigits;553__availableDigits = 0;554}555}556}557558const uint32_t __maximum = __precision - __printedDigits;559if (__availableDigits == 0) {560__digits = 0;561}562uint32_t __lastDigit = 0;563if (__availableDigits > __maximum) {564for (uint32_t __k = 0; __k < __availableDigits - __maximum; ++__k) {565__lastDigit = __digits % 10;566__digits /= 10;567}568}569// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.570int __roundUp = 0;571if (__lastDigit != 5) {572__roundUp = __lastDigit > 5;573} else {574// Is m * 2^__e2 * 10^(__precision + 1 - __exp) integer?575// __precision was already increased by 1, so we don't need to write + 1 here.576const int32_t __rexp = static_cast<int32_t>(__precision) - __exp;577const int32_t __requiredTwos = -__e2 - __rexp;578bool __trailingZeros = __requiredTwos <= 0579|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));580if (__rexp < 0) {581const int32_t __requiredFives = -__rexp;582__trailingZeros = __trailingZeros && __multipleOfPowerOf5(__m2, static_cast<uint32_t>(__requiredFives));583}584__roundUp = __trailingZeros ? 2 : 1;585}586if (__printedDigits != 0) {587if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {588return { _Last, errc::value_too_large };589}590if (__digits == 0) {591std::memset(_First, '0', __maximum);592} else {593__append_c_digits(__maximum, __digits, _First);594}595_First += __maximum;596} else {597if (__printDecimalPoint) {598if (_Last - _First < static_cast<ptrdiff_t>(__maximum + 1)) {599return { _Last, errc::value_too_large };600}601__append_d_digits(__maximum, __digits, _First);602_First += __maximum + 1; // +1 for decimal point603} else {604if (_First == _Last) {605return { _Last, errc::value_too_large };606}607*_First++ = static_cast<char>('0' + __digits);608}609}610if (__roundUp != 0) {611char* _Round = _First;612while (true) {613if (_Round == _Original_first) {614_Round[0] = '1';615++__exp;616break;617}618--_Round;619const char __c = _Round[0];620if (__c == '.') {621// Keep going.622} else if (__c == '9') {623_Round[0] = '0';624__roundUp = 1;625} else {626if (__roundUp == 1 || __c % 2 != 0) {627_Round[0] = __c + 1;628}629break;630}631}632}633634char _Sign_character;635636if (__exp < 0) {637_Sign_character = '-';638__exp = -__exp;639} else {640_Sign_character = '+';641}642643const int _Exponent_part_length = __exp >= 100644? 5 // "e+NNN"645: 4; // "e+NN"646647if (_Last - _First < _Exponent_part_length) {648return { _Last, errc::value_too_large };649}650651*_First++ = 'e';652*_First++ = _Sign_character;653654if (__exp >= 100) {655const int32_t __c = __exp % 10;656std::memcpy(_First, __DIGIT_TABLE + 2 * (__exp / 10), 2);657_First[2] = static_cast<char>('0' + __c);658_First += 3;659} else {660std::memcpy(_First, __DIGIT_TABLE + 2 * __exp, 2);661_First += 2;662}663664return { _First, errc{} };665}666667_LIBCPP_END_NAMESPACE_STD668669// clang-format on670671672