strtod.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright 2010 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #include <climits>
  28. #include <cstdarg>
  29. #include "bignum.h"
  30. #include "cached-powers.h"
  31. #include "ieee.h"
  32. #include "strtod.h"
  33. namespace double_conversion {
  34. // 2^53 = 9007199254740992.
  35. // Any integer with at most 15 decimal digits will hence fit into a double
  36. // (which has a 53bit significand) without loss of precision.
  37. static const int kMaxExactDoubleIntegerDecimalDigits = 15;
  38. // 2^64 = 18446744073709551616 > 10^19
  39. static const int kMaxUint64DecimalDigits = 19;
  40. // Max double: 1.7976931348623157 x 10^308
  41. // Min non-zero double: 4.9406564584124654 x 10^-324
  42. // Any x >= 10^309 is interpreted as +infinity.
  43. // Any x <= 10^-324 is interpreted as 0.
  44. // Note that 2.5e-324 (despite being smaller than the min double) will be read
  45. // as non-zero (equal to the min non-zero double).
  46. static const int kMaxDecimalPower = 309;
  47. static const int kMinDecimalPower = -324;
  48. // 2^64 = 18446744073709551616
  49. static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
  50. static const double exact_powers_of_ten[] = {
  51. 1.0, // 10^0
  52. 10.0,
  53. 100.0,
  54. 1000.0,
  55. 10000.0,
  56. 100000.0,
  57. 1000000.0,
  58. 10000000.0,
  59. 100000000.0,
  60. 1000000000.0,
  61. 10000000000.0, // 10^10
  62. 100000000000.0,
  63. 1000000000000.0,
  64. 10000000000000.0,
  65. 100000000000000.0,
  66. 1000000000000000.0,
  67. 10000000000000000.0,
  68. 100000000000000000.0,
  69. 1000000000000000000.0,
  70. 10000000000000000000.0,
  71. 100000000000000000000.0, // 10^20
  72. 1000000000000000000000.0,
  73. // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
  74. 10000000000000000000000.0
  75. };
  76. static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
  77. // Maximum number of significant digits in the decimal representation.
  78. // In fact the value is 772 (see conversions.cc), but to give us some margin
  79. // we round up to 780.
  80. static const int kMaxSignificantDecimalDigits = 780;
  81. static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
  82. for (int i = 0; i < buffer.length(); i++) {
  83. if (buffer[i] != '0') {
  84. return buffer.SubVector(i, buffer.length());
  85. }
  86. }
  87. return Vector<const char>(buffer.start(), 0);
  88. }
  89. static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
  90. for (int i = buffer.length() - 1; i >= 0; --i) {
  91. if (buffer[i] != '0') {
  92. return buffer.SubVector(0, i + 1);
  93. }
  94. }
  95. return Vector<const char>(buffer.start(), 0);
  96. }
  97. static void CutToMaxSignificantDigits(Vector<const char> buffer,
  98. int exponent,
  99. char* significant_buffer,
  100. int* significant_exponent) {
  101. for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
  102. significant_buffer[i] = buffer[i];
  103. }
  104. // The input buffer has been trimmed. Therefore the last digit must be
  105. // different from '0'.
  106. ASSERT(buffer[buffer.length() - 1] != '0');
  107. // Set the last digit to be non-zero. This is sufficient to guarantee
  108. // correct rounding.
  109. significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
  110. *significant_exponent =
  111. exponent + (buffer.length() - kMaxSignificantDecimalDigits);
  112. }
  113. // Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits.
  114. // If possible the input-buffer is reused, but if the buffer needs to be
  115. // modified (due to cutting), then the input needs to be copied into the
  116. // buffer_copy_space.
  117. static void TrimAndCut(Vector<const char> buffer, int exponent,
  118. char* buffer_copy_space, int space_size,
  119. Vector<const char>* trimmed, int* updated_exponent) {
  120. Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
  121. Vector<const char> right_trimmed = TrimTrailingZeros(left_trimmed);
  122. exponent += left_trimmed.length() - right_trimmed.length();
  123. if (right_trimmed.length() > kMaxSignificantDecimalDigits) {
  124. (void) space_size; // Mark variable as used.
  125. ASSERT(space_size >= kMaxSignificantDecimalDigits);
  126. CutToMaxSignificantDigits(right_trimmed, exponent,
  127. buffer_copy_space, updated_exponent);
  128. *trimmed = Vector<const char>(buffer_copy_space,
  129. kMaxSignificantDecimalDigits);
  130. } else {
  131. *trimmed = right_trimmed;
  132. *updated_exponent = exponent;
  133. }
  134. }
  135. // Reads digits from the buffer and converts them to a uint64.
  136. // Reads in as many digits as fit into a uint64.
  137. // When the string starts with "1844674407370955161" no further digit is read.
  138. // Since 2^64 = 18446744073709551616 it would still be possible read another
  139. // digit if it was less or equal than 6, but this would complicate the code.
  140. static uint64_t ReadUint64(Vector<const char> buffer,
  141. int* number_of_read_digits) {
  142. uint64_t result = 0;
  143. int i = 0;
  144. while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
  145. int digit = buffer[i++] - '0';
  146. ASSERT(0 <= digit && digit <= 9);
  147. result = 10 * result + digit;
  148. }
  149. *number_of_read_digits = i;
  150. return result;
  151. }
  152. // Reads a DiyFp from the buffer.
  153. // The returned DiyFp is not necessarily normalized.
  154. // If remaining_decimals is zero then the returned DiyFp is accurate.
  155. // Otherwise it has been rounded and has error of at most 1/2 ulp.
  156. static void ReadDiyFp(Vector<const char> buffer,
  157. DiyFp* result,
  158. int* remaining_decimals) {
  159. int read_digits;
  160. uint64_t significand = ReadUint64(buffer, &read_digits);
  161. if (buffer.length() == read_digits) {
  162. *result = DiyFp(significand, 0);
  163. *remaining_decimals = 0;
  164. } else {
  165. // Round the significand.
  166. if (buffer[read_digits] >= '5') {
  167. significand++;
  168. }
  169. // Compute the binary exponent.
  170. int exponent = 0;
  171. *result = DiyFp(significand, exponent);
  172. *remaining_decimals = buffer.length() - read_digits;
  173. }
  174. }
  175. static bool DoubleStrtod(Vector<const char> trimmed,
  176. int exponent,
  177. double* result) {
  178. #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS)
  179. // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
  180. // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
  181. // result is not accurate.
  182. // We know that Windows32 uses 64 bits and is therefore accurate.
  183. // Note that the ARM simulator is compiled for 32bits. It therefore exhibits
  184. // the same problem.
  185. return false;
  186. #else
  187. if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
  188. int read_digits;
  189. // The trimmed input fits into a double.
  190. // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
  191. // can compute the result-double simply by multiplying (resp. dividing) the
  192. // two numbers.
  193. // This is possible because IEEE guarantees that floating-point operations
  194. // return the best possible approximation.
  195. if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
  196. // 10^-exponent fits into a double.
  197. *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
  198. ASSERT(read_digits == trimmed.length());
  199. *result /= exact_powers_of_ten[-exponent];
  200. return true;
  201. }
  202. if (0 <= exponent && exponent < kExactPowersOfTenSize) {
  203. // 10^exponent fits into a double.
  204. *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
  205. ASSERT(read_digits == trimmed.length());
  206. *result *= exact_powers_of_ten[exponent];
  207. return true;
  208. }
  209. int remaining_digits =
  210. kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
  211. if ((0 <= exponent) &&
  212. (exponent - remaining_digits < kExactPowersOfTenSize)) {
  213. // The trimmed string was short and we can multiply it with
  214. // 10^remaining_digits. As a result the remaining exponent now fits
  215. // into a double too.
  216. *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
  217. ASSERT(read_digits == trimmed.length());
  218. *result *= exact_powers_of_ten[remaining_digits];
  219. *result *= exact_powers_of_ten[exponent - remaining_digits];
  220. return true;
  221. }
  222. }
  223. return false;
  224. #endif
  225. }
  226. // Returns 10^exponent as an exact DiyFp.
  227. // The given exponent must be in the range [1; kDecimalExponentDistance[.
  228. static DiyFp AdjustmentPowerOfTen(int exponent) {
  229. ASSERT(0 < exponent);
  230. ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
  231. // Simply hardcode the remaining powers for the given decimal exponent
  232. // distance.
  233. ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
  234. switch (exponent) {
  235. case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60);
  236. case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57);
  237. case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54);
  238. case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50);
  239. case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47);
  240. case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44);
  241. case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40);
  242. default:
  243. UNREACHABLE();
  244. }
  245. }
  246. // If the function returns true then the result is the correct double.
  247. // Otherwise it is either the correct double or the double that is just below
  248. // the correct double.
  249. static bool DiyFpStrtod(Vector<const char> buffer,
  250. int exponent,
  251. double* result) {
  252. DiyFp input;
  253. int remaining_decimals;
  254. ReadDiyFp(buffer, &input, &remaining_decimals);
  255. // Since we may have dropped some digits the input is not accurate.
  256. // If remaining_decimals is different than 0 than the error is at most
  257. // .5 ulp (unit in the last place).
  258. // We don't want to deal with fractions and therefore keep a common
  259. // denominator.
  260. const int kDenominatorLog = 3;
  261. const int kDenominator = 1 << kDenominatorLog;
  262. // Move the remaining decimals into the exponent.
  263. exponent += remaining_decimals;
  264. uint64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
  265. int old_e = input.e();
  266. input.Normalize();
  267. error <<= old_e - input.e();
  268. ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
  269. if (exponent < PowersOfTenCache::kMinDecimalExponent) {
  270. *result = 0.0;
  271. return true;
  272. }
  273. DiyFp cached_power;
  274. int cached_decimal_exponent;
  275. PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
  276. &cached_power,
  277. &cached_decimal_exponent);
  278. if (cached_decimal_exponent != exponent) {
  279. int adjustment_exponent = exponent - cached_decimal_exponent;
  280. DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
  281. input.Multiply(adjustment_power);
  282. if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
  283. // The product of input with the adjustment power fits into a 64 bit
  284. // integer.
  285. ASSERT(DiyFp::kSignificandSize == 64);
  286. } else {
  287. // The adjustment power is exact. There is hence only an error of 0.5.
  288. error += kDenominator / 2;
  289. }
  290. }
  291. input.Multiply(cached_power);
  292. // The error introduced by a multiplication of a*b equals
  293. // error_a + error_b + error_a*error_b/2^64 + 0.5
  294. // Substituting a with 'input' and b with 'cached_power' we have
  295. // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp),
  296. // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
  297. int error_b = kDenominator / 2;
  298. int error_ab = (error == 0 ? 0 : 1); // We round up to 1.
  299. int fixed_error = kDenominator / 2;
  300. error += error_b + error_ab + fixed_error;
  301. old_e = input.e();
  302. input.Normalize();
  303. error <<= old_e - input.e();
  304. // See if the double's significand changes if we add/subtract the error.
  305. int order_of_magnitude = DiyFp::kSignificandSize + input.e();
  306. int effective_significand_size =
  307. Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
  308. int precision_digits_count =
  309. DiyFp::kSignificandSize - effective_significand_size;
  310. if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
  311. // This can only happen for very small denormals. In this case the
  312. // half-way multiplied by the denominator exceeds the range of an uint64.
  313. // Simply shift everything to the right.
  314. int shift_amount = (precision_digits_count + kDenominatorLog) -
  315. DiyFp::kSignificandSize + 1;
  316. input.set_f(input.f() >> shift_amount);
  317. input.set_e(input.e() + shift_amount);
  318. // We add 1 for the lost precision of error, and kDenominator for
  319. // the lost precision of input.f().
  320. error = (error >> shift_amount) + 1 + kDenominator;
  321. precision_digits_count -= shift_amount;
  322. }
  323. // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
  324. ASSERT(DiyFp::kSignificandSize == 64);
  325. ASSERT(precision_digits_count < 64);
  326. uint64_t one64 = 1;
  327. uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
  328. uint64_t precision_bits = input.f() & precision_bits_mask;
  329. uint64_t half_way = one64 << (precision_digits_count - 1);
  330. precision_bits *= kDenominator;
  331. half_way *= kDenominator;
  332. DiyFp rounded_input(input.f() >> precision_digits_count,
  333. input.e() + precision_digits_count);
  334. if (precision_bits >= half_way + error) {
  335. rounded_input.set_f(rounded_input.f() + 1);
  336. }
  337. // If the last_bits are too close to the half-way case than we are too
  338. // inaccurate and round down. In this case we return false so that we can
  339. // fall back to a more precise algorithm.
  340. *result = Double(rounded_input).value();
  341. if (half_way - error < precision_bits && precision_bits < half_way + error) {
  342. // Too imprecise. The caller will have to fall back to a slower version.
  343. // However the returned number is guaranteed to be either the correct
  344. // double, or the next-lower double.
  345. return false;
  346. } else {
  347. return true;
  348. }
  349. }
  350. // Returns
  351. // - -1 if buffer*10^exponent < diy_fp.
  352. // - 0 if buffer*10^exponent == diy_fp.
  353. // - +1 if buffer*10^exponent > diy_fp.
  354. // Preconditions:
  355. // buffer.length() + exponent <= kMaxDecimalPower + 1
  356. // buffer.length() + exponent > kMinDecimalPower
  357. // buffer.length() <= kMaxDecimalSignificantDigits
  358. static int CompareBufferWithDiyFp(Vector<const char> buffer,
  359. int exponent,
  360. DiyFp diy_fp) {
  361. ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
  362. ASSERT(buffer.length() + exponent > kMinDecimalPower);
  363. ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
  364. // Make sure that the Bignum will be able to hold all our numbers.
  365. // Our Bignum implementation has a separate field for exponents. Shifts will
  366. // consume at most one bigit (< 64 bits).
  367. // ln(10) == 3.3219...
  368. ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
  369. Bignum buffer_bignum;
  370. Bignum diy_fp_bignum;
  371. buffer_bignum.AssignDecimalString(buffer);
  372. diy_fp_bignum.AssignUInt64(diy_fp.f());
  373. if (exponent >= 0) {
  374. buffer_bignum.MultiplyByPowerOfTen(exponent);
  375. } else {
  376. diy_fp_bignum.MultiplyByPowerOfTen(-exponent);
  377. }
  378. if (diy_fp.e() > 0) {
  379. diy_fp_bignum.ShiftLeft(diy_fp.e());
  380. } else {
  381. buffer_bignum.ShiftLeft(-diy_fp.e());
  382. }
  383. return Bignum::Compare(buffer_bignum, diy_fp_bignum);
  384. }
  385. // Returns true if the guess is the correct double.
  386. // Returns false, when guess is either correct or the next-lower double.
  387. static bool ComputeGuess(Vector<const char> trimmed, int exponent,
  388. double* guess) {
  389. if (trimmed.length() == 0) {
  390. *guess = 0.0;
  391. return true;
  392. }
  393. if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) {
  394. *guess = Double::Infinity();
  395. return true;
  396. }
  397. if (exponent + trimmed.length() <= kMinDecimalPower) {
  398. *guess = 0.0;
  399. return true;
  400. }
  401. if (DoubleStrtod(trimmed, exponent, guess) ||
  402. DiyFpStrtod(trimmed, exponent, guess)) {
  403. return true;
  404. }
  405. if (*guess == Double::Infinity()) {
  406. return true;
  407. }
  408. return false;
  409. }
  410. double Strtod(Vector<const char> buffer, int exponent) {
  411. char copy_buffer[kMaxSignificantDecimalDigits];
  412. Vector<const char> trimmed;
  413. int updated_exponent;
  414. TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
  415. &trimmed, &updated_exponent);
  416. exponent = updated_exponent;
  417. double guess;
  418. bool is_correct = ComputeGuess(trimmed, exponent, &guess);
  419. if (is_correct) return guess;
  420. DiyFp upper_boundary = Double(guess).UpperBoundary();
  421. int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
  422. if (comparison < 0) {
  423. return guess;
  424. } else if (comparison > 0) {
  425. return Double(guess).NextDouble();
  426. } else if ((Double(guess).Significand() & 1) == 0) {
  427. // Round towards even.
  428. return guess;
  429. } else {
  430. return Double(guess).NextDouble();
  431. }
  432. }
  433. static float SanitizedDoubletof(double d) {
  434. ASSERT(d >= 0.0);
  435. // ASAN has a sanitize check that disallows casting doubles to floats if
  436. // they are too big.
  437. // https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks
  438. // The behavior should be covered by IEEE 754, but some projects use this
  439. // flag, so work around it.
  440. float max_finite = 3.4028234663852885981170418348451692544e+38;
  441. // The half-way point between the max-finite and infinity value.
  442. // Since infinity has an even significand everything equal or greater than
  443. // this value should become infinity.
  444. double half_max_finite_infinity =
  445. 3.40282356779733661637539395458142568448e+38;
  446. if (d >= max_finite) {
  447. if (d >= half_max_finite_infinity) {
  448. return Single::Infinity();
  449. } else {
  450. return max_finite;
  451. }
  452. } else {
  453. return static_cast<float>(d);
  454. }
  455. }
  456. float Strtof(Vector<const char> buffer, int exponent) {
  457. char copy_buffer[kMaxSignificantDecimalDigits];
  458. Vector<const char> trimmed;
  459. int updated_exponent;
  460. TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
  461. &trimmed, &updated_exponent);
  462. exponent = updated_exponent;
  463. double double_guess;
  464. bool is_correct = ComputeGuess(trimmed, exponent, &double_guess);
  465. float float_guess = SanitizedDoubletof(double_guess);
  466. if (float_guess == double_guess) {
  467. // This shortcut triggers for integer values.
  468. return float_guess;
  469. }
  470. // We must catch double-rounding. Say the double has been rounded up, and is
  471. // now a boundary of a float, and rounds up again. This is why we have to
  472. // look at previous too.
  473. // Example (in decimal numbers):
  474. // input: 12349
  475. // high-precision (4 digits): 1235
  476. // low-precision (3 digits):
  477. // when read from input: 123
  478. // when rounded from high precision: 124.
  479. // To do this we simply look at the neigbors of the correct result and see
  480. // if they would round to the same float. If the guess is not correct we have
  481. // to look at four values (since two different doubles could be the correct
  482. // double).
  483. double double_next = Double(double_guess).NextDouble();
  484. double double_previous = Double(double_guess).PreviousDouble();
  485. float f1 = SanitizedDoubletof(double_previous);
  486. float f2 = float_guess;
  487. float f3 = SanitizedDoubletof(double_next);
  488. float f4;
  489. if (is_correct) {
  490. f4 = f3;
  491. } else {
  492. double double_next2 = Double(double_next).NextDouble();
  493. f4 = SanitizedDoubletof(double_next2);
  494. }
  495. (void) f2; // Mark variable as used.
  496. ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
  497. // If the guess doesn't lie near a single-precision boundary we can simply
  498. // return its float-value.
  499. if (f1 == f4) {
  500. return float_guess;
  501. }
  502. ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
  503. (f1 == f2 && f2 != f3 && f3 == f4) ||
  504. (f1 == f2 && f2 == f3 && f3 != f4));
  505. // guess and next are the two possible candidates (in the same way that
  506. // double_guess was the lower candidate for a double-precision guess).
  507. float guess = f1;
  508. float next = f4;
  509. DiyFp upper_boundary;
  510. if (guess == 0.0f) {
  511. float min_float = 1e-45f;
  512. upper_boundary = Double(static_cast<double>(min_float) / 2).AsDiyFp();
  513. } else {
  514. upper_boundary = Single(guess).UpperBoundary();
  515. }
  516. int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
  517. if (comparison < 0) {
  518. return guess;
  519. } else if (comparison > 0) {
  520. return next;
  521. } else if ((Single(guess).Significand() & 1) == 0) {
  522. // Round towards even.
  523. return guess;
  524. } else {
  525. return next;
  526. }
  527. }
  528. } // namespace double_conversion