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