12e5b6d6dSopenharmony_ci// Copyright 2010 the V8 project authors. All rights reserved. 22e5b6d6dSopenharmony_ci// Redistribution and use in source and binary forms, with or without 32e5b6d6dSopenharmony_ci// modification, are permitted provided that the following conditions are 42e5b6d6dSopenharmony_ci// met: 52e5b6d6dSopenharmony_ci// 62e5b6d6dSopenharmony_ci// * Redistributions of source code must retain the above copyright 72e5b6d6dSopenharmony_ci// notice, this list of conditions and the following disclaimer. 82e5b6d6dSopenharmony_ci// * Redistributions in binary form must reproduce the above 92e5b6d6dSopenharmony_ci// copyright notice, this list of conditions and the following 102e5b6d6dSopenharmony_ci// disclaimer in the documentation and/or other materials provided 112e5b6d6dSopenharmony_ci// with the distribution. 122e5b6d6dSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 132e5b6d6dSopenharmony_ci// contributors may be used to endorse or promote products derived 142e5b6d6dSopenharmony_ci// from this software without specific prior written permission. 152e5b6d6dSopenharmony_ci// 162e5b6d6dSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172e5b6d6dSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182e5b6d6dSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192e5b6d6dSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202e5b6d6dSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212e5b6d6dSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222e5b6d6dSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232e5b6d6dSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242e5b6d6dSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252e5b6d6dSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262e5b6d6dSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272e5b6d6dSopenharmony_ci 282e5b6d6dSopenharmony_ci#include <climits> 292e5b6d6dSopenharmony_ci#include <cstdarg> 302e5b6d6dSopenharmony_ci 312e5b6d6dSopenharmony_ci#include "bignum.h" 322e5b6d6dSopenharmony_ci#include "cached-powers.h" 332e5b6d6dSopenharmony_ci#include "ieee.h" 342e5b6d6dSopenharmony_ci#include "strtod.h" 352e5b6d6dSopenharmony_ci 362e5b6d6dSopenharmony_cinamespace double_conversion { 372e5b6d6dSopenharmony_ci 382e5b6d6dSopenharmony_ci#if defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 392e5b6d6dSopenharmony_ci// 2^53 = 9007199254740992. 402e5b6d6dSopenharmony_ci// Any integer with at most 15 decimal digits will hence fit into a double 412e5b6d6dSopenharmony_ci// (which has a 53bit significand) without loss of precision. 422e5b6d6dSopenharmony_cistatic const int kMaxExactDoubleIntegerDecimalDigits = 15; 432e5b6d6dSopenharmony_ci#endif // #if defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 442e5b6d6dSopenharmony_ci// 2^64 = 18446744073709551616 > 10^19 452e5b6d6dSopenharmony_cistatic const int kMaxUint64DecimalDigits = 19; 462e5b6d6dSopenharmony_ci 472e5b6d6dSopenharmony_ci// Max double: 1.7976931348623157 x 10^308 482e5b6d6dSopenharmony_ci// Min non-zero double: 4.9406564584124654 x 10^-324 492e5b6d6dSopenharmony_ci// Any x >= 10^309 is interpreted as +infinity. 502e5b6d6dSopenharmony_ci// Any x <= 10^-324 is interpreted as 0. 512e5b6d6dSopenharmony_ci// Note that 2.5e-324 (despite being smaller than the min double) will be read 522e5b6d6dSopenharmony_ci// as non-zero (equal to the min non-zero double). 532e5b6d6dSopenharmony_cistatic const int kMaxDecimalPower = 309; 542e5b6d6dSopenharmony_cistatic const int kMinDecimalPower = -324; 552e5b6d6dSopenharmony_ci 562e5b6d6dSopenharmony_ci// 2^64 = 18446744073709551616 572e5b6d6dSopenharmony_cistatic const uint64_t kMaxUint64 = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); 582e5b6d6dSopenharmony_ci 592e5b6d6dSopenharmony_ci 602e5b6d6dSopenharmony_ci#if defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 612e5b6d6dSopenharmony_cistatic const double exact_powers_of_ten[] = { 622e5b6d6dSopenharmony_ci 1.0, // 10^0 632e5b6d6dSopenharmony_ci 10.0, 642e5b6d6dSopenharmony_ci 100.0, 652e5b6d6dSopenharmony_ci 1000.0, 662e5b6d6dSopenharmony_ci 10000.0, 672e5b6d6dSopenharmony_ci 100000.0, 682e5b6d6dSopenharmony_ci 1000000.0, 692e5b6d6dSopenharmony_ci 10000000.0, 702e5b6d6dSopenharmony_ci 100000000.0, 712e5b6d6dSopenharmony_ci 1000000000.0, 722e5b6d6dSopenharmony_ci 10000000000.0, // 10^10 732e5b6d6dSopenharmony_ci 100000000000.0, 742e5b6d6dSopenharmony_ci 1000000000000.0, 752e5b6d6dSopenharmony_ci 10000000000000.0, 762e5b6d6dSopenharmony_ci 100000000000000.0, 772e5b6d6dSopenharmony_ci 1000000000000000.0, 782e5b6d6dSopenharmony_ci 10000000000000000.0, 792e5b6d6dSopenharmony_ci 100000000000000000.0, 802e5b6d6dSopenharmony_ci 1000000000000000000.0, 812e5b6d6dSopenharmony_ci 10000000000000000000.0, 822e5b6d6dSopenharmony_ci 100000000000000000000.0, // 10^20 832e5b6d6dSopenharmony_ci 1000000000000000000000.0, 842e5b6d6dSopenharmony_ci // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 852e5b6d6dSopenharmony_ci 10000000000000000000000.0 862e5b6d6dSopenharmony_ci}; 872e5b6d6dSopenharmony_cistatic const int kExactPowersOfTenSize = DOUBLE_CONVERSION_ARRAY_SIZE(exact_powers_of_ten); 882e5b6d6dSopenharmony_ci#endif // #if defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 892e5b6d6dSopenharmony_ci 902e5b6d6dSopenharmony_ci// Maximum number of significant digits in the decimal representation. 912e5b6d6dSopenharmony_ci// In fact the value is 772 (see conversions.cc), but to give us some margin 922e5b6d6dSopenharmony_ci// we round up to 780. 932e5b6d6dSopenharmony_cistatic const int kMaxSignificantDecimalDigits = 780; 942e5b6d6dSopenharmony_ci 952e5b6d6dSopenharmony_cistatic Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { 962e5b6d6dSopenharmony_ci for (int i = 0; i < buffer.length(); i++) { 972e5b6d6dSopenharmony_ci if (buffer[i] != '0') { 982e5b6d6dSopenharmony_ci return buffer.SubVector(i, buffer.length()); 992e5b6d6dSopenharmony_ci } 1002e5b6d6dSopenharmony_ci } 1012e5b6d6dSopenharmony_ci return Vector<const char>(buffer.start(), 0); 1022e5b6d6dSopenharmony_ci} 1032e5b6d6dSopenharmony_ci 1042e5b6d6dSopenharmony_cistatic void CutToMaxSignificantDigits(Vector<const char> buffer, 1052e5b6d6dSopenharmony_ci int exponent, 1062e5b6d6dSopenharmony_ci char* significant_buffer, 1072e5b6d6dSopenharmony_ci int* significant_exponent) { 1082e5b6d6dSopenharmony_ci for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { 1092e5b6d6dSopenharmony_ci significant_buffer[i] = buffer[i]; 1102e5b6d6dSopenharmony_ci } 1112e5b6d6dSopenharmony_ci // The input buffer has been trimmed. Therefore the last digit must be 1122e5b6d6dSopenharmony_ci // different from '0'. 1132e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(buffer[buffer.length() - 1] != '0'); 1142e5b6d6dSopenharmony_ci // Set the last digit to be non-zero. This is sufficient to guarantee 1152e5b6d6dSopenharmony_ci // correct rounding. 1162e5b6d6dSopenharmony_ci significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; 1172e5b6d6dSopenharmony_ci *significant_exponent = 1182e5b6d6dSopenharmony_ci exponent + (buffer.length() - kMaxSignificantDecimalDigits); 1192e5b6d6dSopenharmony_ci} 1202e5b6d6dSopenharmony_ci 1212e5b6d6dSopenharmony_ci 1222e5b6d6dSopenharmony_ci// Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits. 1232e5b6d6dSopenharmony_ci// If possible the input-buffer is reused, but if the buffer needs to be 1242e5b6d6dSopenharmony_ci// modified (due to cutting), then the input needs to be copied into the 1252e5b6d6dSopenharmony_ci// buffer_copy_space. 1262e5b6d6dSopenharmony_cistatic void TrimAndCut(Vector<const char> buffer, int exponent, 1272e5b6d6dSopenharmony_ci char* buffer_copy_space, int space_size, 1282e5b6d6dSopenharmony_ci Vector<const char>* trimmed, int* updated_exponent) { 1292e5b6d6dSopenharmony_ci Vector<const char> left_trimmed = TrimLeadingZeros(buffer); 1302e5b6d6dSopenharmony_ci Vector<const char> right_trimmed = TrimTrailingZeros(left_trimmed); 1312e5b6d6dSopenharmony_ci exponent += left_trimmed.length() - right_trimmed.length(); 1322e5b6d6dSopenharmony_ci if (right_trimmed.length() > kMaxSignificantDecimalDigits) { 1332e5b6d6dSopenharmony_ci (void) space_size; // Mark variable as used. 1342e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(space_size >= kMaxSignificantDecimalDigits); 1352e5b6d6dSopenharmony_ci CutToMaxSignificantDigits(right_trimmed, exponent, 1362e5b6d6dSopenharmony_ci buffer_copy_space, updated_exponent); 1372e5b6d6dSopenharmony_ci *trimmed = Vector<const char>(buffer_copy_space, 1382e5b6d6dSopenharmony_ci kMaxSignificantDecimalDigits); 1392e5b6d6dSopenharmony_ci } else { 1402e5b6d6dSopenharmony_ci *trimmed = right_trimmed; 1412e5b6d6dSopenharmony_ci *updated_exponent = exponent; 1422e5b6d6dSopenharmony_ci } 1432e5b6d6dSopenharmony_ci} 1442e5b6d6dSopenharmony_ci 1452e5b6d6dSopenharmony_ci 1462e5b6d6dSopenharmony_ci// Reads digits from the buffer and converts them to a uint64. 1472e5b6d6dSopenharmony_ci// Reads in as many digits as fit into a uint64. 1482e5b6d6dSopenharmony_ci// When the string starts with "1844674407370955161" no further digit is read. 1492e5b6d6dSopenharmony_ci// Since 2^64 = 18446744073709551616 it would still be possible read another 1502e5b6d6dSopenharmony_ci// digit if it was less or equal than 6, but this would complicate the code. 1512e5b6d6dSopenharmony_cistatic uint64_t ReadUint64(Vector<const char> buffer, 1522e5b6d6dSopenharmony_ci int* number_of_read_digits) { 1532e5b6d6dSopenharmony_ci uint64_t result = 0; 1542e5b6d6dSopenharmony_ci int i = 0; 1552e5b6d6dSopenharmony_ci while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { 1562e5b6d6dSopenharmony_ci int digit = buffer[i++] - '0'; 1572e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9); 1582e5b6d6dSopenharmony_ci result = 10 * result + digit; 1592e5b6d6dSopenharmony_ci } 1602e5b6d6dSopenharmony_ci *number_of_read_digits = i; 1612e5b6d6dSopenharmony_ci return result; 1622e5b6d6dSopenharmony_ci} 1632e5b6d6dSopenharmony_ci 1642e5b6d6dSopenharmony_ci 1652e5b6d6dSopenharmony_ci// Reads a DiyFp from the buffer. 1662e5b6d6dSopenharmony_ci// The returned DiyFp is not necessarily normalized. 1672e5b6d6dSopenharmony_ci// If remaining_decimals is zero then the returned DiyFp is accurate. 1682e5b6d6dSopenharmony_ci// Otherwise it has been rounded and has error of at most 1/2 ulp. 1692e5b6d6dSopenharmony_cistatic void ReadDiyFp(Vector<const char> buffer, 1702e5b6d6dSopenharmony_ci DiyFp* result, 1712e5b6d6dSopenharmony_ci int* remaining_decimals) { 1722e5b6d6dSopenharmony_ci int read_digits; 1732e5b6d6dSopenharmony_ci uint64_t significand = ReadUint64(buffer, &read_digits); 1742e5b6d6dSopenharmony_ci if (buffer.length() == read_digits) { 1752e5b6d6dSopenharmony_ci *result = DiyFp(significand, 0); 1762e5b6d6dSopenharmony_ci *remaining_decimals = 0; 1772e5b6d6dSopenharmony_ci } else { 1782e5b6d6dSopenharmony_ci // Round the significand. 1792e5b6d6dSopenharmony_ci if (buffer[read_digits] >= '5') { 1802e5b6d6dSopenharmony_ci significand++; 1812e5b6d6dSopenharmony_ci } 1822e5b6d6dSopenharmony_ci // Compute the binary exponent. 1832e5b6d6dSopenharmony_ci int exponent = 0; 1842e5b6d6dSopenharmony_ci *result = DiyFp(significand, exponent); 1852e5b6d6dSopenharmony_ci *remaining_decimals = buffer.length() - read_digits; 1862e5b6d6dSopenharmony_ci } 1872e5b6d6dSopenharmony_ci} 1882e5b6d6dSopenharmony_ci 1892e5b6d6dSopenharmony_ci 1902e5b6d6dSopenharmony_cistatic bool DoubleStrtod(Vector<const char> trimmed, 1912e5b6d6dSopenharmony_ci int exponent, 1922e5b6d6dSopenharmony_ci double* result) { 1932e5b6d6dSopenharmony_ci#if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 1942e5b6d6dSopenharmony_ci // Avoid "unused parameter" warnings 1952e5b6d6dSopenharmony_ci (void) trimmed; 1962e5b6d6dSopenharmony_ci (void) exponent; 1972e5b6d6dSopenharmony_ci (void) result; 1982e5b6d6dSopenharmony_ci // On x86 the floating-point stack can be 64 or 80 bits wide. If it is 1992e5b6d6dSopenharmony_ci // 80 bits wide (as is the case on Linux) then double-rounding occurs and the 2002e5b6d6dSopenharmony_ci // result is not accurate. 2012e5b6d6dSopenharmony_ci // We know that Windows32 uses 64 bits and is therefore accurate. 2022e5b6d6dSopenharmony_ci return false; 2032e5b6d6dSopenharmony_ci#else 2042e5b6d6dSopenharmony_ci if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { 2052e5b6d6dSopenharmony_ci int read_digits; 2062e5b6d6dSopenharmony_ci // The trimmed input fits into a double. 2072e5b6d6dSopenharmony_ci // If the 10^exponent (resp. 10^-exponent) fits into a double too then we 2082e5b6d6dSopenharmony_ci // can compute the result-double simply by multiplying (resp. dividing) the 2092e5b6d6dSopenharmony_ci // two numbers. 2102e5b6d6dSopenharmony_ci // This is possible because IEEE guarantees that floating-point operations 2112e5b6d6dSopenharmony_ci // return the best possible approximation. 2122e5b6d6dSopenharmony_ci if (exponent < 0 && -exponent < kExactPowersOfTenSize) { 2132e5b6d6dSopenharmony_ci // 10^-exponent fits into a double. 2142e5b6d6dSopenharmony_ci *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 2152e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); 2162e5b6d6dSopenharmony_ci *result /= exact_powers_of_ten[-exponent]; 2172e5b6d6dSopenharmony_ci return true; 2182e5b6d6dSopenharmony_ci } 2192e5b6d6dSopenharmony_ci if (0 <= exponent && exponent < kExactPowersOfTenSize) { 2202e5b6d6dSopenharmony_ci // 10^exponent fits into a double. 2212e5b6d6dSopenharmony_ci *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 2222e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); 2232e5b6d6dSopenharmony_ci *result *= exact_powers_of_ten[exponent]; 2242e5b6d6dSopenharmony_ci return true; 2252e5b6d6dSopenharmony_ci } 2262e5b6d6dSopenharmony_ci int remaining_digits = 2272e5b6d6dSopenharmony_ci kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); 2282e5b6d6dSopenharmony_ci if ((0 <= exponent) && 2292e5b6d6dSopenharmony_ci (exponent - remaining_digits < kExactPowersOfTenSize)) { 2302e5b6d6dSopenharmony_ci // The trimmed string was short and we can multiply it with 2312e5b6d6dSopenharmony_ci // 10^remaining_digits. As a result the remaining exponent now fits 2322e5b6d6dSopenharmony_ci // into a double too. 2332e5b6d6dSopenharmony_ci *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 2342e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); 2352e5b6d6dSopenharmony_ci *result *= exact_powers_of_ten[remaining_digits]; 2362e5b6d6dSopenharmony_ci *result *= exact_powers_of_ten[exponent - remaining_digits]; 2372e5b6d6dSopenharmony_ci return true; 2382e5b6d6dSopenharmony_ci } 2392e5b6d6dSopenharmony_ci } 2402e5b6d6dSopenharmony_ci return false; 2412e5b6d6dSopenharmony_ci#endif 2422e5b6d6dSopenharmony_ci} 2432e5b6d6dSopenharmony_ci 2442e5b6d6dSopenharmony_ci 2452e5b6d6dSopenharmony_ci// Returns 10^exponent as an exact DiyFp. 2462e5b6d6dSopenharmony_ci// The given exponent must be in the range [1; kDecimalExponentDistance[. 2472e5b6d6dSopenharmony_cistatic DiyFp AdjustmentPowerOfTen(int exponent) { 2482e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(0 < exponent); 2492e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); 2502e5b6d6dSopenharmony_ci // Simply hardcode the remaining powers for the given decimal exponent 2512e5b6d6dSopenharmony_ci // distance. 2522e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); 2532e5b6d6dSopenharmony_ci switch (exponent) { 2542e5b6d6dSopenharmony_ci case 1: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xa0000000, 00000000), -60); 2552e5b6d6dSopenharmony_ci case 2: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc8000000, 00000000), -57); 2562e5b6d6dSopenharmony_ci case 3: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xfa000000, 00000000), -54); 2572e5b6d6dSopenharmony_ci case 4: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50); 2582e5b6d6dSopenharmony_ci case 5: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc3500000, 00000000), -47); 2592e5b6d6dSopenharmony_ci case 6: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xf4240000, 00000000), -44); 2602e5b6d6dSopenharmony_ci case 7: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x98968000, 00000000), -40); 2612e5b6d6dSopenharmony_ci default: 2622e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_UNREACHABLE(); 2632e5b6d6dSopenharmony_ci } 2642e5b6d6dSopenharmony_ci} 2652e5b6d6dSopenharmony_ci 2662e5b6d6dSopenharmony_ci 2672e5b6d6dSopenharmony_ci// If the function returns true then the result is the correct double. 2682e5b6d6dSopenharmony_ci// Otherwise it is either the correct double or the double that is just below 2692e5b6d6dSopenharmony_ci// the correct double. 2702e5b6d6dSopenharmony_cistatic bool DiyFpStrtod(Vector<const char> buffer, 2712e5b6d6dSopenharmony_ci int exponent, 2722e5b6d6dSopenharmony_ci double* result) { 2732e5b6d6dSopenharmony_ci DiyFp input; 2742e5b6d6dSopenharmony_ci int remaining_decimals; 2752e5b6d6dSopenharmony_ci ReadDiyFp(buffer, &input, &remaining_decimals); 2762e5b6d6dSopenharmony_ci // Since we may have dropped some digits the input is not accurate. 2772e5b6d6dSopenharmony_ci // If remaining_decimals is different than 0 than the error is at most 2782e5b6d6dSopenharmony_ci // .5 ulp (unit in the last place). 2792e5b6d6dSopenharmony_ci // We don't want to deal with fractions and therefore keep a common 2802e5b6d6dSopenharmony_ci // denominator. 2812e5b6d6dSopenharmony_ci const int kDenominatorLog = 3; 2822e5b6d6dSopenharmony_ci const int kDenominator = 1 << kDenominatorLog; 2832e5b6d6dSopenharmony_ci // Move the remaining decimals into the exponent. 2842e5b6d6dSopenharmony_ci exponent += remaining_decimals; 2852e5b6d6dSopenharmony_ci uint64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2); 2862e5b6d6dSopenharmony_ci 2872e5b6d6dSopenharmony_ci int old_e = input.e(); 2882e5b6d6dSopenharmony_ci input.Normalize(); 2892e5b6d6dSopenharmony_ci error <<= old_e - input.e(); 2902e5b6d6dSopenharmony_ci 2912e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); 2922e5b6d6dSopenharmony_ci if (exponent < PowersOfTenCache::kMinDecimalExponent) { 2932e5b6d6dSopenharmony_ci *result = 0.0; 2942e5b6d6dSopenharmony_ci return true; 2952e5b6d6dSopenharmony_ci } 2962e5b6d6dSopenharmony_ci DiyFp cached_power; 2972e5b6d6dSopenharmony_ci int cached_decimal_exponent; 2982e5b6d6dSopenharmony_ci PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, 2992e5b6d6dSopenharmony_ci &cached_power, 3002e5b6d6dSopenharmony_ci &cached_decimal_exponent); 3012e5b6d6dSopenharmony_ci 3022e5b6d6dSopenharmony_ci if (cached_decimal_exponent != exponent) { 3032e5b6d6dSopenharmony_ci int adjustment_exponent = exponent - cached_decimal_exponent; 3042e5b6d6dSopenharmony_ci DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); 3052e5b6d6dSopenharmony_ci input.Multiply(adjustment_power); 3062e5b6d6dSopenharmony_ci if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { 3072e5b6d6dSopenharmony_ci // The product of input with the adjustment power fits into a 64 bit 3082e5b6d6dSopenharmony_ci // integer. 3092e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64); 3102e5b6d6dSopenharmony_ci } else { 3112e5b6d6dSopenharmony_ci // The adjustment power is exact. There is hence only an error of 0.5. 3122e5b6d6dSopenharmony_ci error += kDenominator / 2; 3132e5b6d6dSopenharmony_ci } 3142e5b6d6dSopenharmony_ci } 3152e5b6d6dSopenharmony_ci 3162e5b6d6dSopenharmony_ci input.Multiply(cached_power); 3172e5b6d6dSopenharmony_ci // The error introduced by a multiplication of a*b equals 3182e5b6d6dSopenharmony_ci // error_a + error_b + error_a*error_b/2^64 + 0.5 3192e5b6d6dSopenharmony_ci // Substituting a with 'input' and b with 'cached_power' we have 3202e5b6d6dSopenharmony_ci // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp), 3212e5b6d6dSopenharmony_ci // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 3222e5b6d6dSopenharmony_ci int error_b = kDenominator / 2; 3232e5b6d6dSopenharmony_ci int error_ab = (error == 0 ? 0 : 1); // We round up to 1. 3242e5b6d6dSopenharmony_ci int fixed_error = kDenominator / 2; 3252e5b6d6dSopenharmony_ci error += error_b + error_ab + fixed_error; 3262e5b6d6dSopenharmony_ci 3272e5b6d6dSopenharmony_ci old_e = input.e(); 3282e5b6d6dSopenharmony_ci input.Normalize(); 3292e5b6d6dSopenharmony_ci error <<= old_e - input.e(); 3302e5b6d6dSopenharmony_ci 3312e5b6d6dSopenharmony_ci // See if the double's significand changes if we add/subtract the error. 3322e5b6d6dSopenharmony_ci int order_of_magnitude = DiyFp::kSignificandSize + input.e(); 3332e5b6d6dSopenharmony_ci int effective_significand_size = 3342e5b6d6dSopenharmony_ci Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); 3352e5b6d6dSopenharmony_ci int precision_digits_count = 3362e5b6d6dSopenharmony_ci DiyFp::kSignificandSize - effective_significand_size; 3372e5b6d6dSopenharmony_ci if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { 3382e5b6d6dSopenharmony_ci // This can only happen for very small denormals. In this case the 3392e5b6d6dSopenharmony_ci // half-way multiplied by the denominator exceeds the range of an uint64. 3402e5b6d6dSopenharmony_ci // Simply shift everything to the right. 3412e5b6d6dSopenharmony_ci int shift_amount = (precision_digits_count + kDenominatorLog) - 3422e5b6d6dSopenharmony_ci DiyFp::kSignificandSize + 1; 3432e5b6d6dSopenharmony_ci input.set_f(input.f() >> shift_amount); 3442e5b6d6dSopenharmony_ci input.set_e(input.e() + shift_amount); 3452e5b6d6dSopenharmony_ci // We add 1 for the lost precision of error, and kDenominator for 3462e5b6d6dSopenharmony_ci // the lost precision of input.f(). 3472e5b6d6dSopenharmony_ci error = (error >> shift_amount) + 1 + kDenominator; 3482e5b6d6dSopenharmony_ci precision_digits_count -= shift_amount; 3492e5b6d6dSopenharmony_ci } 3502e5b6d6dSopenharmony_ci // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. 3512e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64); 3522e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(precision_digits_count < 64); 3532e5b6d6dSopenharmony_ci uint64_t one64 = 1; 3542e5b6d6dSopenharmony_ci uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; 3552e5b6d6dSopenharmony_ci uint64_t precision_bits = input.f() & precision_bits_mask; 3562e5b6d6dSopenharmony_ci uint64_t half_way = one64 << (precision_digits_count - 1); 3572e5b6d6dSopenharmony_ci precision_bits *= kDenominator; 3582e5b6d6dSopenharmony_ci half_way *= kDenominator; 3592e5b6d6dSopenharmony_ci DiyFp rounded_input(input.f() >> precision_digits_count, 3602e5b6d6dSopenharmony_ci input.e() + precision_digits_count); 3612e5b6d6dSopenharmony_ci if (precision_bits >= half_way + error) { 3622e5b6d6dSopenharmony_ci rounded_input.set_f(rounded_input.f() + 1); 3632e5b6d6dSopenharmony_ci } 3642e5b6d6dSopenharmony_ci // If the last_bits are too close to the half-way case than we are too 3652e5b6d6dSopenharmony_ci // inaccurate and round down. In this case we return false so that we can 3662e5b6d6dSopenharmony_ci // fall back to a more precise algorithm. 3672e5b6d6dSopenharmony_ci 3682e5b6d6dSopenharmony_ci *result = Double(rounded_input).value(); 3692e5b6d6dSopenharmony_ci if (half_way - error < precision_bits && precision_bits < half_way + error) { 3702e5b6d6dSopenharmony_ci // Too imprecise. The caller will have to fall back to a slower version. 3712e5b6d6dSopenharmony_ci // However the returned number is guaranteed to be either the correct 3722e5b6d6dSopenharmony_ci // double, or the next-lower double. 3732e5b6d6dSopenharmony_ci return false; 3742e5b6d6dSopenharmony_ci } else { 3752e5b6d6dSopenharmony_ci return true; 3762e5b6d6dSopenharmony_ci } 3772e5b6d6dSopenharmony_ci} 3782e5b6d6dSopenharmony_ci 3792e5b6d6dSopenharmony_ci 3802e5b6d6dSopenharmony_ci// Returns 3812e5b6d6dSopenharmony_ci// - -1 if buffer*10^exponent < diy_fp. 3822e5b6d6dSopenharmony_ci// - 0 if buffer*10^exponent == diy_fp. 3832e5b6d6dSopenharmony_ci// - +1 if buffer*10^exponent > diy_fp. 3842e5b6d6dSopenharmony_ci// Preconditions: 3852e5b6d6dSopenharmony_ci// buffer.length() + exponent <= kMaxDecimalPower + 1 3862e5b6d6dSopenharmony_ci// buffer.length() + exponent > kMinDecimalPower 3872e5b6d6dSopenharmony_ci// buffer.length() <= kMaxDecimalSignificantDigits 3882e5b6d6dSopenharmony_cistatic int CompareBufferWithDiyFp(Vector<const char> buffer, 3892e5b6d6dSopenharmony_ci int exponent, 3902e5b6d6dSopenharmony_ci DiyFp diy_fp) { 3912e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); 3922e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent > kMinDecimalPower); 3932e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); 3942e5b6d6dSopenharmony_ci // Make sure that the Bignum will be able to hold all our numbers. 3952e5b6d6dSopenharmony_ci // Our Bignum implementation has a separate field for exponents. Shifts will 3962e5b6d6dSopenharmony_ci // consume at most one bigit (< 64 bits). 3972e5b6d6dSopenharmony_ci // ln(10) == 3.3219... 3982e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); 3992e5b6d6dSopenharmony_ci Bignum buffer_bignum; 4002e5b6d6dSopenharmony_ci Bignum diy_fp_bignum; 4012e5b6d6dSopenharmony_ci buffer_bignum.AssignDecimalString(buffer); 4022e5b6d6dSopenharmony_ci diy_fp_bignum.AssignUInt64(diy_fp.f()); 4032e5b6d6dSopenharmony_ci if (exponent >= 0) { 4042e5b6d6dSopenharmony_ci buffer_bignum.MultiplyByPowerOfTen(exponent); 4052e5b6d6dSopenharmony_ci } else { 4062e5b6d6dSopenharmony_ci diy_fp_bignum.MultiplyByPowerOfTen(-exponent); 4072e5b6d6dSopenharmony_ci } 4082e5b6d6dSopenharmony_ci if (diy_fp.e() > 0) { 4092e5b6d6dSopenharmony_ci diy_fp_bignum.ShiftLeft(diy_fp.e()); 4102e5b6d6dSopenharmony_ci } else { 4112e5b6d6dSopenharmony_ci buffer_bignum.ShiftLeft(-diy_fp.e()); 4122e5b6d6dSopenharmony_ci } 4132e5b6d6dSopenharmony_ci return Bignum::Compare(buffer_bignum, diy_fp_bignum); 4142e5b6d6dSopenharmony_ci} 4152e5b6d6dSopenharmony_ci 4162e5b6d6dSopenharmony_ci 4172e5b6d6dSopenharmony_ci// Returns true if the guess is the correct double. 4182e5b6d6dSopenharmony_ci// Returns false, when guess is either correct or the next-lower double. 4192e5b6d6dSopenharmony_cistatic bool ComputeGuess(Vector<const char> trimmed, int exponent, 4202e5b6d6dSopenharmony_ci double* guess) { 4212e5b6d6dSopenharmony_ci if (trimmed.length() == 0) { 4222e5b6d6dSopenharmony_ci *guess = 0.0; 4232e5b6d6dSopenharmony_ci return true; 4242e5b6d6dSopenharmony_ci } 4252e5b6d6dSopenharmony_ci if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { 4262e5b6d6dSopenharmony_ci *guess = Double::Infinity(); 4272e5b6d6dSopenharmony_ci return true; 4282e5b6d6dSopenharmony_ci } 4292e5b6d6dSopenharmony_ci if (exponent + trimmed.length() <= kMinDecimalPower) { 4302e5b6d6dSopenharmony_ci *guess = 0.0; 4312e5b6d6dSopenharmony_ci return true; 4322e5b6d6dSopenharmony_ci } 4332e5b6d6dSopenharmony_ci 4342e5b6d6dSopenharmony_ci if (DoubleStrtod(trimmed, exponent, guess) || 4352e5b6d6dSopenharmony_ci DiyFpStrtod(trimmed, exponent, guess)) { 4362e5b6d6dSopenharmony_ci return true; 4372e5b6d6dSopenharmony_ci } 4382e5b6d6dSopenharmony_ci if (*guess == Double::Infinity()) { 4392e5b6d6dSopenharmony_ci return true; 4402e5b6d6dSopenharmony_ci } 4412e5b6d6dSopenharmony_ci return false; 4422e5b6d6dSopenharmony_ci} 4432e5b6d6dSopenharmony_ci 4442e5b6d6dSopenharmony_cistatic bool IsDigit(const char d) { 4452e5b6d6dSopenharmony_ci return ('0' <= d) && (d <= '9'); 4462e5b6d6dSopenharmony_ci} 4472e5b6d6dSopenharmony_ci 4482e5b6d6dSopenharmony_cistatic bool IsNonZeroDigit(const char d) { 4492e5b6d6dSopenharmony_ci return ('1' <= d) && (d <= '9'); 4502e5b6d6dSopenharmony_ci} 4512e5b6d6dSopenharmony_ci 4522e5b6d6dSopenharmony_ci#ifdef __has_cpp_attribute 4532e5b6d6dSopenharmony_ci#if __has_cpp_attribute(maybe_unused) 4542e5b6d6dSopenharmony_ci[[maybe_unused]] 4552e5b6d6dSopenharmony_ci#endif 4562e5b6d6dSopenharmony_ci#endif 4572e5b6d6dSopenharmony_cistatic bool AssertTrimmedDigits(const Vector<const char>& buffer) { 4582e5b6d6dSopenharmony_ci for(int i = 0; i < buffer.length(); ++i) { 4592e5b6d6dSopenharmony_ci if(!IsDigit(buffer[i])) { 4602e5b6d6dSopenharmony_ci return false; 4612e5b6d6dSopenharmony_ci } 4622e5b6d6dSopenharmony_ci } 4632e5b6d6dSopenharmony_ci return (buffer.length() == 0) || (IsNonZeroDigit(buffer[0]) && IsNonZeroDigit(buffer[buffer.length()-1])); 4642e5b6d6dSopenharmony_ci} 4652e5b6d6dSopenharmony_ci 4662e5b6d6dSopenharmony_cidouble StrtodTrimmed(Vector<const char> trimmed, int exponent) { 4672e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(trimmed.length() <= kMaxSignificantDecimalDigits); 4682e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(AssertTrimmedDigits(trimmed)); 4692e5b6d6dSopenharmony_ci double guess; 4702e5b6d6dSopenharmony_ci const bool is_correct = ComputeGuess(trimmed, exponent, &guess); 4712e5b6d6dSopenharmony_ci if (is_correct) { 4722e5b6d6dSopenharmony_ci return guess; 4732e5b6d6dSopenharmony_ci } 4742e5b6d6dSopenharmony_ci DiyFp upper_boundary = Double(guess).UpperBoundary(); 4752e5b6d6dSopenharmony_ci int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); 4762e5b6d6dSopenharmony_ci if (comparison < 0) { 4772e5b6d6dSopenharmony_ci return guess; 4782e5b6d6dSopenharmony_ci } else if (comparison > 0) { 4792e5b6d6dSopenharmony_ci return Double(guess).NextDouble(); 4802e5b6d6dSopenharmony_ci } else if ((Double(guess).Significand() & 1) == 0) { 4812e5b6d6dSopenharmony_ci // Round towards even. 4822e5b6d6dSopenharmony_ci return guess; 4832e5b6d6dSopenharmony_ci } else { 4842e5b6d6dSopenharmony_ci return Double(guess).NextDouble(); 4852e5b6d6dSopenharmony_ci } 4862e5b6d6dSopenharmony_ci} 4872e5b6d6dSopenharmony_ci 4882e5b6d6dSopenharmony_cidouble Strtod(Vector<const char> buffer, int exponent) { 4892e5b6d6dSopenharmony_ci char copy_buffer[kMaxSignificantDecimalDigits]; 4902e5b6d6dSopenharmony_ci Vector<const char> trimmed; 4912e5b6d6dSopenharmony_ci int updated_exponent; 4922e5b6d6dSopenharmony_ci TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, 4932e5b6d6dSopenharmony_ci &trimmed, &updated_exponent); 4942e5b6d6dSopenharmony_ci return StrtodTrimmed(trimmed, updated_exponent); 4952e5b6d6dSopenharmony_ci} 4962e5b6d6dSopenharmony_ci 4972e5b6d6dSopenharmony_cistatic float SanitizedDoubletof(double d) { 4982e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(d >= 0.0); 4992e5b6d6dSopenharmony_ci // ASAN has a sanitize check that disallows casting doubles to floats if 5002e5b6d6dSopenharmony_ci // they are too big. 5012e5b6d6dSopenharmony_ci // https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks 5022e5b6d6dSopenharmony_ci // The behavior should be covered by IEEE 754, but some projects use this 5032e5b6d6dSopenharmony_ci // flag, so work around it. 5042e5b6d6dSopenharmony_ci float max_finite = 3.4028234663852885981170418348451692544e+38; 5052e5b6d6dSopenharmony_ci // The half-way point between the max-finite and infinity value. 5062e5b6d6dSopenharmony_ci // Since infinity has an even significand everything equal or greater than 5072e5b6d6dSopenharmony_ci // this value should become infinity. 5082e5b6d6dSopenharmony_ci double half_max_finite_infinity = 5092e5b6d6dSopenharmony_ci 3.40282356779733661637539395458142568448e+38; 5102e5b6d6dSopenharmony_ci if (d >= max_finite) { 5112e5b6d6dSopenharmony_ci if (d >= half_max_finite_infinity) { 5122e5b6d6dSopenharmony_ci return Single::Infinity(); 5132e5b6d6dSopenharmony_ci } else { 5142e5b6d6dSopenharmony_ci return max_finite; 5152e5b6d6dSopenharmony_ci } 5162e5b6d6dSopenharmony_ci } else { 5172e5b6d6dSopenharmony_ci return static_cast<float>(d); 5182e5b6d6dSopenharmony_ci } 5192e5b6d6dSopenharmony_ci} 5202e5b6d6dSopenharmony_ci 5212e5b6d6dSopenharmony_cifloat Strtof(Vector<const char> buffer, int exponent) { 5222e5b6d6dSopenharmony_ci char copy_buffer[kMaxSignificantDecimalDigits]; 5232e5b6d6dSopenharmony_ci Vector<const char> trimmed; 5242e5b6d6dSopenharmony_ci int updated_exponent; 5252e5b6d6dSopenharmony_ci TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, 5262e5b6d6dSopenharmony_ci &trimmed, &updated_exponent); 5272e5b6d6dSopenharmony_ci exponent = updated_exponent; 5282e5b6d6dSopenharmony_ci return StrtofTrimmed(trimmed, exponent); 5292e5b6d6dSopenharmony_ci} 5302e5b6d6dSopenharmony_ci 5312e5b6d6dSopenharmony_cifloat StrtofTrimmed(Vector<const char> trimmed, int exponent) { 5322e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(trimmed.length() <= kMaxSignificantDecimalDigits); 5332e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(AssertTrimmedDigits(trimmed)); 5342e5b6d6dSopenharmony_ci 5352e5b6d6dSopenharmony_ci double double_guess; 5362e5b6d6dSopenharmony_ci bool is_correct = ComputeGuess(trimmed, exponent, &double_guess); 5372e5b6d6dSopenharmony_ci 5382e5b6d6dSopenharmony_ci float float_guess = SanitizedDoubletof(double_guess); 5392e5b6d6dSopenharmony_ci if (float_guess == double_guess) { 5402e5b6d6dSopenharmony_ci // This shortcut triggers for integer values. 5412e5b6d6dSopenharmony_ci return float_guess; 5422e5b6d6dSopenharmony_ci } 5432e5b6d6dSopenharmony_ci 5442e5b6d6dSopenharmony_ci // We must catch double-rounding. Say the double has been rounded up, and is 5452e5b6d6dSopenharmony_ci // now a boundary of a float, and rounds up again. This is why we have to 5462e5b6d6dSopenharmony_ci // look at previous too. 5472e5b6d6dSopenharmony_ci // Example (in decimal numbers): 5482e5b6d6dSopenharmony_ci // input: 12349 5492e5b6d6dSopenharmony_ci // high-precision (4 digits): 1235 5502e5b6d6dSopenharmony_ci // low-precision (3 digits): 5512e5b6d6dSopenharmony_ci // when read from input: 123 5522e5b6d6dSopenharmony_ci // when rounded from high precision: 124. 5532e5b6d6dSopenharmony_ci // To do this we simply look at the neighbors of the correct result and see 5542e5b6d6dSopenharmony_ci // if they would round to the same float. If the guess is not correct we have 5552e5b6d6dSopenharmony_ci // to look at four values (since two different doubles could be the correct 5562e5b6d6dSopenharmony_ci // double). 5572e5b6d6dSopenharmony_ci 5582e5b6d6dSopenharmony_ci double double_next = Double(double_guess).NextDouble(); 5592e5b6d6dSopenharmony_ci double double_previous = Double(double_guess).PreviousDouble(); 5602e5b6d6dSopenharmony_ci 5612e5b6d6dSopenharmony_ci float f1 = SanitizedDoubletof(double_previous); 5622e5b6d6dSopenharmony_ci float f2 = float_guess; 5632e5b6d6dSopenharmony_ci float f3 = SanitizedDoubletof(double_next); 5642e5b6d6dSopenharmony_ci float f4; 5652e5b6d6dSopenharmony_ci if (is_correct) { 5662e5b6d6dSopenharmony_ci f4 = f3; 5672e5b6d6dSopenharmony_ci } else { 5682e5b6d6dSopenharmony_ci double double_next2 = Double(double_next).NextDouble(); 5692e5b6d6dSopenharmony_ci f4 = SanitizedDoubletof(double_next2); 5702e5b6d6dSopenharmony_ci } 5712e5b6d6dSopenharmony_ci (void) f2; // Mark variable as used. 5722e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4); 5732e5b6d6dSopenharmony_ci 5742e5b6d6dSopenharmony_ci // If the guess doesn't lie near a single-precision boundary we can simply 5752e5b6d6dSopenharmony_ci // return its float-value. 5762e5b6d6dSopenharmony_ci if (f1 == f4) { 5772e5b6d6dSopenharmony_ci return float_guess; 5782e5b6d6dSopenharmony_ci } 5792e5b6d6dSopenharmony_ci 5802e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT((f1 != f2 && f2 == f3 && f3 == f4) || 5812e5b6d6dSopenharmony_ci (f1 == f2 && f2 != f3 && f3 == f4) || 5822e5b6d6dSopenharmony_ci (f1 == f2 && f2 == f3 && f3 != f4)); 5832e5b6d6dSopenharmony_ci 5842e5b6d6dSopenharmony_ci // guess and next are the two possible candidates (in the same way that 5852e5b6d6dSopenharmony_ci // double_guess was the lower candidate for a double-precision guess). 5862e5b6d6dSopenharmony_ci float guess = f1; 5872e5b6d6dSopenharmony_ci float next = f4; 5882e5b6d6dSopenharmony_ci DiyFp upper_boundary; 5892e5b6d6dSopenharmony_ci if (guess == 0.0f) { 5902e5b6d6dSopenharmony_ci float min_float = 1e-45f; 5912e5b6d6dSopenharmony_ci upper_boundary = Double(static_cast<double>(min_float) / 2).AsDiyFp(); 5922e5b6d6dSopenharmony_ci } else { 5932e5b6d6dSopenharmony_ci upper_boundary = Single(guess).UpperBoundary(); 5942e5b6d6dSopenharmony_ci } 5952e5b6d6dSopenharmony_ci int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); 5962e5b6d6dSopenharmony_ci if (comparison < 0) { 5972e5b6d6dSopenharmony_ci return guess; 5982e5b6d6dSopenharmony_ci } else if (comparison > 0) { 5992e5b6d6dSopenharmony_ci return next; 6002e5b6d6dSopenharmony_ci } else if ((Single(guess).Significand() & 1) == 0) { 6012e5b6d6dSopenharmony_ci // Round towards even. 6022e5b6d6dSopenharmony_ci return guess; 6032e5b6d6dSopenharmony_ci } else { 6042e5b6d6dSopenharmony_ci return next; 6052e5b6d6dSopenharmony_ci } 6062e5b6d6dSopenharmony_ci} 6072e5b6d6dSopenharmony_ci 6082e5b6d6dSopenharmony_ci} // namespace double_conversion 609