Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: Source/wtf/dtoa/fast-dtoa.cc

Issue 20652002: Fix trailing whitespace in scripts and misc. files (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Don't change literal diff. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/dtoa/double-conversion.cc ('k') | Source/wtf/dtoa/fixed-dtoa.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 18 matching lines...) Expand all
29 29
30 #include "fast-dtoa.h" 30 #include "fast-dtoa.h"
31 31
32 #include "cached-powers.h" 32 #include "cached-powers.h"
33 #include "diy-fp.h" 33 #include "diy-fp.h"
34 #include "double.h" 34 #include "double.h"
35 35
36 namespace WTF { 36 namespace WTF {
37 37
38 namespace double_conversion { 38 namespace double_conversion {
39 39
40 // The minimal and maximal target exponent define the range of w's binary 40 // The minimal and maximal target exponent define the range of w's binary
41 // exponent, where 'w' is the result of multiplying the input by a cached po wer 41 // exponent, where 'w' is the result of multiplying the input by a cached po wer
42 // of ten. 42 // of ten.
43 // 43 //
44 // A different range might be chosen on a different platform, to optimize di git 44 // A different range might be chosen on a different platform, to optimize di git
45 // generation, but a smaller range requires more powers of ten to be cached. 45 // generation, but a smaller range requires more powers of ten to be cached.
46 static const int kMinimalTargetExponent = -60; 46 static const int kMinimalTargetExponent = -60;
47 static const int kMaximalTargetExponent = -32; 47 static const int kMaximalTargetExponent = -32;
48 48
49 49
50 // Adjusts the last digit of the generated number, and screens out generated 50 // Adjusts the last digit of the generated number, and screens out generated
51 // solutions that may be inaccurate. A solution may be inaccurate if it is 51 // solutions that may be inaccurate. A solution may be inaccurate if it is
52 // outside the safe interval, or if we cannot prove that it is closer to the 52 // outside the safe interval, or if we cannot prove that it is closer to the
53 // input than a neighboring representation of the same length. 53 // input than a neighboring representation of the same length.
54 // 54 //
55 // Input: * buffer containing the digits of too_high / 10^kappa 55 // Input: * buffer containing the digits of too_high / 10^kappa
56 // * the buffer's length 56 // * the buffer's length
57 // * distance_too_high_w == (too_high - w).f() * unit 57 // * distance_too_high_w == (too_high - w).f() * unit
58 // * unsafe_interval == (too_high - too_low).f() * unit 58 // * unsafe_interval == (too_high - too_low).f() * unit
59 // * rest = (too_high - buffer * 10^kappa).f() * unit 59 // * rest = (too_high - buffer * 10^kappa).f() * unit
(...skipping 10 matching lines...) Expand all
70 uint64_t ten_kappa, 70 uint64_t ten_kappa,
71 uint64_t unit) { 71 uint64_t unit) {
72 uint64_t small_distance = distance_too_high_w - unit; 72 uint64_t small_distance = distance_too_high_w - unit;
73 uint64_t big_distance = distance_too_high_w + unit; 73 uint64_t big_distance = distance_too_high_w + unit;
74 // Let w_low = too_high - big_distance, and 74 // Let w_low = too_high - big_distance, and
75 // w_high = too_high - small_distance. 75 // w_high = too_high - small_distance.
76 // Note: w_low < w < w_high 76 // Note: w_low < w < w_high
77 // 77 //
78 // The real w (* unit) must lie somewhere inside the interval 78 // The real w (* unit) must lie somewhere inside the interval
79 // ]w_low; w_high[ (often written as "(w_low; w_high)") 79 // ]w_low; w_high[ (often written as "(w_low; w_high)")
80 80
81 // Basically the buffer currently contains a number in the unsafe interv al 81 // Basically the buffer currently contains a number in the unsafe interv al
82 // ]too_low; too_high[ with too_low < w < too_high 82 // ]too_low; too_high[ with too_low < w < too_high
83 // 83 //
84 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 84 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85 // ^v 1 unit ^ ^ ^ ^ 85 // ^v 1 unit ^ ^ ^ ^
86 // boundary_high --------------------- . . . . 86 // boundary_high --------------------- . . . .
87 // ^v 1 unit . . . . 87 // ^v 1 unit . . . .
88 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . . 88 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
89 // . . ^ . . 89 // . . ^ . .
90 // . big_distance . . . 90 // . big_distance . . .
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // We need to do the following tests in this order to avoid over- and 143 // We need to do the following tests in this order to avoid over- and
144 // underflows. 144 // underflows.
145 ASSERT(rest <= unsafe_interval); 145 ASSERT(rest <= unsafe_interval);
146 while (rest < small_distance && // Negated condition 1 146 while (rest < small_distance && // Negated condition 1
147 unsafe_interval - rest >= ten_kappa && // Negated condition 2 147 unsafe_interval - rest >= ten_kappa && // Negated condition 2
148 (rest + ten_kappa < small_distance || // buffer{-1} > w_high 148 (rest + ten_kappa < small_distance || // buffer{-1} > w_high
149 small_distance - rest >= rest + ten_kappa - small_distance)) { 149 small_distance - rest >= rest + ten_kappa - small_distance)) {
150 buffer[length - 1]--; 150 buffer[length - 1]--;
151 rest += ten_kappa; 151 rest += ten_kappa;
152 } 152 }
153 153
154 // We have approached w+ as much as possible. We now test if approaching w- 154 // We have approached w+ as much as possible. We now test if approaching w-
155 // would require changing the buffer. If yes, then we have two possible 155 // would require changing the buffer. If yes, then we have two possible
156 // representations close to w, but we cannot decide which one is closer. 156 // representations close to w, but we cannot decide which one is closer.
157 if (rest < big_distance && 157 if (rest < big_distance &&
158 unsafe_interval - rest >= ten_kappa && 158 unsafe_interval - rest >= ten_kappa &&
159 (rest + ten_kappa < big_distance || 159 (rest + ten_kappa < big_distance ||
160 big_distance - rest > rest + ten_kappa - big_distance)) { 160 big_distance - rest > rest + ten_kappa - big_distance)) {
161 return false; 161 return false;
162 } 162 }
163 163
164 // Weeding test. 164 // Weeding test.
165 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp] 165 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
166 // Since too_low = too_high - unsafe_interval this is equivalent to 166 // Since too_low = too_high - unsafe_interval this is equivalent to
167 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp] 167 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
168 // Conceptually we have: rest ~= too_high - buffer 168 // Conceptually we have: rest ~= too_high - buffer
169 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit); 169 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
170 } 170 }
171 171
172 172
173 // Rounds the buffer upwards if the result is closer to v by possibly adding 173 // Rounds the buffer upwards if the result is closer to v by possibly adding
174 // 1 to the buffer. If the precision of the calculation is not sufficient to 174 // 1 to the buffer. If the precision of the calculation is not sufficient to
175 // round correctly, return false. 175 // round correctly, return false.
176 // The rounding might shift the whole buffer in which case the kappa is 176 // The rounding might shift the whole buffer in which case the kappa is
177 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4. 177 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
178 // 178 //
179 // If 2*rest > ten_kappa then the buffer needs to be round up. 179 // If 2*rest > ten_kappa then the buffer needs to be round up.
180 // rest can have an error of +/- 1 unit. This function accounts for the 180 // rest can have an error of +/- 1 unit. This function accounts for the
181 // imprecision and returns false, if the rounding direction cannot be 181 // imprecision and returns false, if the rounding direction cannot be
182 // unambiguously determined. 182 // unambiguously determined.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 // first digit to '1' and adjust the kappa. Example: "99" becomes "1 0" and 218 // first digit to '1' and adjust the kappa. Example: "99" becomes "1 0" and
219 // the power (the kappa) is increased. 219 // the power (the kappa) is increased.
220 if (buffer[0] == '0' + 10) { 220 if (buffer[0] == '0' + 10) {
221 buffer[0] = '1'; 221 buffer[0] = '1';
222 (*kappa) += 1; 222 (*kappa) += 1;
223 } 223 }
224 return true; 224 return true;
225 } 225 }
226 return false; 226 return false;
227 } 227 }
228 228
229 229
230 static const uint32_t kTen4 = 10000; 230 static const uint32_t kTen4 = 10000;
231 static const uint32_t kTen5 = 100000; 231 static const uint32_t kTen5 = 100000;
232 static const uint32_t kTen6 = 1000000; 232 static const uint32_t kTen6 = 1000000;
233 static const uint32_t kTen7 = 10000000; 233 static const uint32_t kTen7 = 10000000;
234 static const uint32_t kTen8 = 100000000; 234 static const uint32_t kTen8 = 100000000;
235 static const uint32_t kTen9 = 1000000000; 235 static const uint32_t kTen9 = 1000000000;
236 236
237 // Returns the biggest power of ten that is less than or equal to the given 237 // Returns the biggest power of ten that is less than or equal to the given
238 // number. We furthermore receive the maximum number of bits 'number' has. 238 // number. We furthermore receive the maximum number of bits 'number' has.
239 // If number_bits == 0 then 0^-1 is returned 239 // If number_bits == 0 then 0^-1 is returned
240 // The number of bits must be <= 32. 240 // The number of bits must be <= 32.
241 // Precondition: number < (1 << (number_bits + 1)). 241 // Precondition: number < (1 << (number_bits + 1)).
242 static void BiggestPowerTen(uint32_t number, 242 static void BiggestPowerTen(uint32_t number,
243 int number_bits, 243 int number_bits,
244 uint32_t* power, 244 uint32_t* power,
245 int* exponent) { 245 int* exponent) {
246 ASSERT(number < (uint32_t)(1 << (number_bits + 1))); 246 ASSERT(number < (uint32_t)(1 << (number_bits + 1)));
247 247
248 switch (number_bits) { 248 switch (number_bits) {
249 case 32: 249 case 32:
250 case 31: 250 case 31:
251 case 30: 251 case 30:
252 if (kTen9 <= number) { 252 if (kTen9 <= number) {
253 *power = kTen9; 253 *power = kTen9;
254 *exponent = 9; 254 *exponent = 9;
255 break; 255 break;
256 } // else fallthrough 256 } // else fallthrough
257 case 29: 257 case 29:
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 *power = 0; 332 *power = 0;
333 *exponent = -1; 333 *exponent = -1;
334 break; 334 break;
335 default: 335 default:
336 // Following assignments are here to silence compiler warnings. 336 // Following assignments are here to silence compiler warnings.
337 *power = 0; 337 *power = 0;
338 *exponent = 0; 338 *exponent = 0;
339 UNREACHABLE(); 339 UNREACHABLE();
340 } 340 }
341 } 341 }
342 342
343 343
344 // Generates the digits of input number w. 344 // Generates the digits of input number w.
345 // w is a floating-point number (DiyFp), consisting of a significand and an 345 // w is a floating-point number (DiyFp), consisting of a significand and an
346 // exponent. Its exponent is bounded by kMinimalTargetExponent and 346 // exponent. Its exponent is bounded by kMinimalTargetExponent and
347 // kMaximalTargetExponent. 347 // kMaximalTargetExponent.
348 // Hence -60 <= w.e() <= -32. 348 // Hence -60 <= w.e() <= -32.
349 // 349 //
350 // Returns false if it fails, in which case the generated digits in the buff er 350 // Returns false if it fails, in which case the generated digits in the buff er
351 // should not be used. 351 // should not be used.
352 // Preconditions: 352 // Preconditions:
353 // * low, w and high are correct up to 1 ulp (unit in the last place). That 353 // * low, w and high are correct up to 1 ulp (unit in the last place). That
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 // Reminder: unsafe_interval.e() == one.e() 445 // Reminder: unsafe_interval.e() == one.e()
446 if (rest < unsafe_interval.f()) { 446 if (rest < unsafe_interval.f()) {
447 // Rounding down (by not emitting the remaining digits) yields a number 447 // Rounding down (by not emitting the remaining digits) yields a number
448 // that lies within the unsafe interval. 448 // that lies within the unsafe interval.
449 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(), 449 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
450 unsafe_interval.f(), rest, 450 unsafe_interval.f(), rest,
451 static_cast<uint64_t>(divisor) << -one.e(), uni t); 451 static_cast<uint64_t>(divisor) << -one.e(), uni t);
452 } 452 }
453 divisor /= 10; 453 divisor /= 10;
454 } 454 }
455 455
456 // The integrals have been generated. We are at the point of the decimal 456 // The integrals have been generated. We are at the point of the decimal
457 // separator. In the following loop we simply multiply the remaining dig its by 457 // separator. In the following loop we simply multiply the remaining dig its by
458 // 10 and divide by one. We just need to pay attention to multiply assoc iated 458 // 10 and divide by one. We just need to pay attention to multiply assoc iated
459 // data (like the interval or 'unit'), too. 459 // data (like the interval or 'unit'), too.
460 // Note that the multiplication by 10 does not overflow, because w.e >= -60 460 // Note that the multiplication by 10 does not overflow, because w.e >= -60
461 // and thus one.e >= -60. 461 // and thus one.e >= -60.
462 ASSERT(one.e() >= -60); 462 ASSERT(one.e() >= -60);
463 ASSERT(fractionals < one.f()); 463 ASSERT(fractionals < one.f());
464 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); 464 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
465 while (true) { 465 while (true) {
466 fractionals *= 10; 466 fractionals *= 10;
467 unit *= 10; 467 unit *= 10;
468 unsafe_interval.set_f(unsafe_interval.f() * 10); 468 unsafe_interval.set_f(unsafe_interval.f() * 10);
469 // Integer division by one. 469 // Integer division by one.
470 int digit = static_cast<int>(fractionals >> -one.e()); 470 int digit = static_cast<int>(fractionals >> -one.e());
471 buffer[*length] = '0' + digit; 471 buffer[*length] = '0' + digit;
472 (*length)++; 472 (*length)++;
473 fractionals &= one.f() - 1; // Modulo by one. 473 fractionals &= one.f() - 1; // Modulo by one.
474 (*kappa)--; 474 (*kappa)--;
475 if (fractionals < unsafe_interval.f()) { 475 if (fractionals < unsafe_interval.f()) {
476 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit, 476 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
477 unsafe_interval.f(), fractionals, one.f(), unit ); 477 unsafe_interval.f(), fractionals, one.f(), unit );
478 } 478 }
479 } 479 }
480 } 480 }
481 481
482 482
483 483
484 // Generates (at most) requested_digits digits of input number w. 484 // Generates (at most) requested_digits digits of input number w.
485 // w is a floating-point number (DiyFp), consisting of a significand and an 485 // w is a floating-point number (DiyFp), consisting of a significand and an
486 // exponent. Its exponent is bounded by kMinimalTargetExponent and 486 // exponent. Its exponent is bounded by kMinimalTargetExponent and
487 // kMaximalTargetExponent. 487 // kMaximalTargetExponent.
488 // Hence -60 <= w.e() <= -32. 488 // Hence -60 <= w.e() <= -32.
489 // 489 //
490 // Returns false if it fails, in which case the generated digits in the buff er 490 // Returns false if it fails, in which case the generated digits in the buff er
491 // should not be used. 491 // should not be used.
492 // Preconditions: 492 // Preconditions:
493 // * w is correct up to 1 ulp (unit in the last place). That 493 // * w is correct up to 1 ulp (unit in the last place). That
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 // Division by one is a shift. 528 // Division by one is a shift.
529 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e()); 529 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
530 // Modulo by one is an and. 530 // Modulo by one is an and.
531 uint64_t fractionals = w.f() & (one.f() - 1); 531 uint64_t fractionals = w.f() & (one.f() - 1);
532 uint32_t divisor; 532 uint32_t divisor;
533 int divisor_exponent; 533 int divisor_exponent;
534 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), 534 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
535 &divisor, &divisor_exponent); 535 &divisor, &divisor_exponent);
536 *kappa = divisor_exponent + 1; 536 *kappa = divisor_exponent + 1;
537 *length = 0; 537 *length = 0;
538 538
539 // Loop invariant: buffer = w / 10^kappa (integer division) 539 // Loop invariant: buffer = w / 10^kappa (integer division)
540 // The invariant holds for the first iteration: kappa has been initializ ed 540 // The invariant holds for the first iteration: kappa has been initializ ed
541 // with the divisor exponent + 1. And the divisor is the biggest power o f ten 541 // with the divisor exponent + 1. And the divisor is the biggest power o f ten
542 // that is smaller than 'integrals'. 542 // that is smaller than 'integrals'.
543 while (*kappa > 0) { 543 while (*kappa > 0) {
544 int digit = integrals / divisor; 544 int digit = integrals / divisor;
545 buffer[*length] = '0' + digit; 545 buffer[*length] = '0' + digit;
546 (*length)++; 546 (*length)++;
547 requested_digits--; 547 requested_digits--;
548 integrals %= divisor; 548 integrals %= divisor;
549 (*kappa)--; 549 (*kappa)--;
550 // Note that kappa now equals the exponent of the divisor and that t he 550 // Note that kappa now equals the exponent of the divisor and that t he
551 // invariant thus holds again. 551 // invariant thus holds again.
552 if (requested_digits == 0) break; 552 if (requested_digits == 0) break;
553 divisor /= 10; 553 divisor /= 10;
554 } 554 }
555 555
556 if (requested_digits == 0) { 556 if (requested_digits == 0) {
557 uint64_t rest = 557 uint64_t rest =
558 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; 558 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
559 return RoundWeedCounted(buffer, *length, rest, 559 return RoundWeedCounted(buffer, *length, rest,
560 static_cast<uint64_t>(divisor) << -one.e(), w_error, 560 static_cast<uint64_t>(divisor) << -one.e(), w_error,
561 kappa); 561 kappa);
562 } 562 }
563 563
564 // The integrals have been generated. We are at the point of the decimal 564 // The integrals have been generated. We are at the point of the decimal
565 // separator. In the following loop we simply multiply the remaining dig its by 565 // separator. In the following loop we simply multiply the remaining dig its by
566 // 10 and divide by one. We just need to pay attention to multiply assoc iated 566 // 10 and divide by one. We just need to pay attention to multiply assoc iated
567 // data (the 'unit'), too. 567 // data (the 'unit'), too.
568 // Note that the multiplication by 10 does not overflow, because w.e >= -60 568 // Note that the multiplication by 10 does not overflow, because w.e >= -60
569 // and thus one.e >= -60. 569 // and thus one.e >= -60.
570 ASSERT(one.e() >= -60); 570 ASSERT(one.e() >= -60);
571 ASSERT(fractionals < one.f()); 571 ASSERT(fractionals < one.f());
572 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); 572 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
573 while (requested_digits > 0 && fractionals > w_error) { 573 while (requested_digits > 0 && fractionals > w_error) {
574 fractionals *= 10; 574 fractionals *= 10;
575 w_error *= 10; 575 w_error *= 10;
576 // Integer division by one. 576 // Integer division by one.
577 int digit = static_cast<int>(fractionals >> -one.e()); 577 int digit = static_cast<int>(fractionals >> -one.e());
578 buffer[*length] = '0' + digit; 578 buffer[*length] = '0' + digit;
579 (*length)++; 579 (*length)++;
580 requested_digits--; 580 requested_digits--;
581 fractionals &= one.f() - 1; // Modulo by one. 581 fractionals &= one.f() - 1; // Modulo by one.
582 (*kappa)--; 582 (*kappa)--;
583 } 583 }
584 if (requested_digits != 0) return false; 584 if (requested_digits != 0) return false;
585 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error, 585 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
586 kappa); 586 kappa);
587 } 587 }
588 588
589 589
590 // Provides a decimal representation of v. 590 // Provides a decimal representation of v.
591 // Returns true if it succeeds, otherwise the result cannot be trusted. 591 // Returns true if it succeeds, otherwise the result cannot be trusted.
592 // There will be *length digits inside the buffer (not null-terminated). 592 // There will be *length digits inside the buffer (not null-terminated).
593 // If the function returns true then 593 // If the function returns true then
594 // v == (double) (buffer * 10^decimal_exponent). 594 // v == (double) (buffer * 10^decimal_exponent).
595 // The digits in the buffer are the shortest representation possible: no 595 // The digits in the buffer are the shortest representation possible: no
596 // 0.09999999999999999 instead of 0.1. The shorter representation will even be 596 // 0.09999999999999999 instead of 0.1. The shorter representation will even be
597 // chosen even if the longer one would be closer to v. 597 // chosen even if the longer one would be closer to v.
598 // The last digit will be closest to the actual v. That is, even if several 598 // The last digit will be closest to the actual v. That is, even if several
599 // digits might correctly yield 'v' when read again, the closest will be 599 // digits might correctly yield 'v' when read again, the closest will be
(...skipping 19 matching lines...) Expand all
619 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( 619 PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
620 ten_mk_minimal_bi nary_exponent, 620 ten_mk_minimal_bi nary_exponent,
621 ten_mk_maximal_bi nary_exponent, 621 ten_mk_maximal_bi nary_exponent,
622 &ten_mk, &mk); 622 &ten_mk, &mk);
623 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + 623 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
624 DiyFp::kSignificandSize) && 624 DiyFp::kSignificandSize) &&
625 (kMaximalTargetExponent >= w.e() + ten_mk.e() + 625 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
626 DiyFp::kSignificandSize)); 626 DiyFp::kSignificandSize));
627 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont ains a 627 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont ains a
628 // 64 bit significand and ten_mk is thus only precise up to 64 bits. 628 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
629 629
630 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima ted 630 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima ted
631 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar e now 631 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar e now
632 // off by a small amount. 632 // off by a small amount.
633 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_ w. 633 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_ w.
634 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then 634 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
635 // (f-1) * 2^e < w*10^k < (f+1) * 2^e 635 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
636 DiyFp scaled_w = DiyFp::Times(w, ten_mk); 636 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
637 ASSERT(scaled_w.e() == 637 ASSERT(scaled_w.e() ==
638 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize); 638 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
639 // In theory it would be possible to avoid some recomputations by comput ing 639 // In theory it would be possible to avoid some recomputations by comput ing
640 // the difference between w and boundary_minus/plus (a power of 2) and t o 640 // the difference between w and boundary_minus/plus (a power of 2) and t o
641 // compute scaled_boundary_minus/plus by subtracting/adding from 641 // compute scaled_boundary_minus/plus by subtracting/adding from
642 // scaled_w. However the code becomes much less readable and the speed 642 // scaled_w. However the code becomes much less readable and the speed
643 // enhancements are not terriffic. 643 // enhancements are not terriffic.
644 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk); 644 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
645 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk); 645 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk);
646 646
647 // DigitGen will generate the digits of scaled_w. Therefore we have 647 // DigitGen will generate the digits of scaled_w. Therefore we have
648 // v == (double) (scaled_w * 10^-mk). 648 // v == (double) (scaled_w * 10^-mk).
649 // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is n ot an 649 // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is n ot an
650 // integer than it will be updated. For instance if scaled_w == 1.23 the n 650 // integer than it will be updated. For instance if scaled_w == 1.23 the n
651 // the buffer will be filled with "123" und the decimal_exponent will be 651 // the buffer will be filled with "123" und the decimal_exponent will be
652 // decreased by 2. 652 // decreased by 2.
653 int kappa; 653 int kappa;
654 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_ plus, 654 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_ plus,
655 buffer, length, &kappa); 655 buffer, length, &kappa);
656 *decimal_exponent = -mk + kappa; 656 *decimal_exponent = -mk + kappa;
657 return result; 657 return result;
658 } 658 }
659 659
660 660
661 // The "counted" version of grisu3 (see above) only generates requested_digi ts 661 // The "counted" version of grisu3 (see above) only generates requested_digi ts
662 // number of digits. This version does not generate the shortest representat ion, 662 // number of digits. This version does not generate the shortest representat ion,
663 // and with enough requested digits 0.1 will at some point print as 0.999999 9... 663 // and with enough requested digits 0.1 will at some point print as 0.999999 9...
664 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and 664 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
665 // therefore the rounding strategy for halfway cases is irrelevant. 665 // therefore the rounding strategy for halfway cases is irrelevant.
666 static bool Grisu3Counted(double v, 666 static bool Grisu3Counted(double v,
667 int requested_digits, 667 int requested_digits,
668 Vector<char> buffer, 668 Vector<char> buffer,
669 int* length, 669 int* length,
670 int* decimal_exponent) { 670 int* decimal_exponent) {
671 DiyFp w = Double(v).AsNormalizedDiyFp(); 671 DiyFp w = Double(v).AsNormalizedDiyFp();
672 DiyFp ten_mk; // Cached power of ten: 10^-k 672 DiyFp ten_mk; // Cached power of ten: 10^-k
673 int mk; // -k 673 int mk; // -k
674 int ten_mk_minimal_binary_exponent = 674 int ten_mk_minimal_binary_exponent =
675 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); 675 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
676 int ten_mk_maximal_binary_exponent = 676 int ten_mk_maximal_binary_exponent =
677 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); 677 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
678 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( 678 PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
679 ten_mk_minimal_bi nary_exponent, 679 ten_mk_minimal_bi nary_exponent,
680 ten_mk_maximal_bi nary_exponent, 680 ten_mk_maximal_bi nary_exponent,
681 &ten_mk, &mk); 681 &ten_mk, &mk);
682 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + 682 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
683 DiyFp::kSignificandSize) && 683 DiyFp::kSignificandSize) &&
684 (kMaximalTargetExponent >= w.e() + ten_mk.e() + 684 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
685 DiyFp::kSignificandSize)); 685 DiyFp::kSignificandSize));
686 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont ains a 686 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont ains a
687 // 64 bit significand and ten_mk is thus only precise up to 64 bits. 687 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
688 688
689 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima ted 689 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima ted
690 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar e now 690 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar e now
691 // off by a small amount. 691 // off by a small amount.
692 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_ w. 692 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_ w.
693 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then 693 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
694 // (f-1) * 2^e < w*10^k < (f+1) * 2^e 694 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
695 DiyFp scaled_w = DiyFp::Times(w, ten_mk); 695 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
696 696
697 // We now have (double) (scaled_w * 10^-mk). 697 // We now have (double) (scaled_w * 10^-mk).
698 // DigitGen will generate the first requested_digits digits of scaled_w and 698 // DigitGen will generate the first requested_digits digits of scaled_w and
699 // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It 699 // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
700 // will not always be exactly the same since DigitGenCounted only produc es a 700 // will not always be exactly the same since DigitGenCounted only produc es a
701 // limited number of digits.) 701 // limited number of digits.)
702 int kappa; 702 int kappa;
703 bool result = DigitGenCounted(scaled_w, requested_digits, 703 bool result = DigitGenCounted(scaled_w, requested_digits,
704 buffer, length, &kappa); 704 buffer, length, &kappa);
705 *decimal_exponent = -mk + kappa; 705 *decimal_exponent = -mk + kappa;
706 return result; 706 return result;
707 } 707 }
708 708
709 709
710 bool FastDtoa(double v, 710 bool FastDtoa(double v,
711 FastDtoaMode mode, 711 FastDtoaMode mode,
712 int requested_digits, 712 int requested_digits,
713 Vector<char> buffer, 713 Vector<char> buffer,
714 int* length, 714 int* length,
715 int* decimal_point) { 715 int* decimal_point) {
716 ASSERT(v > 0); 716 ASSERT(v > 0);
717 ASSERT(!Double(v).IsSpecial()); 717 ASSERT(!Double(v).IsSpecial());
718 718
719 bool result = false; 719 bool result = false;
720 int decimal_exponent = 0; 720 int decimal_exponent = 0;
721 switch (mode) { 721 switch (mode) {
722 case FAST_DTOA_SHORTEST: 722 case FAST_DTOA_SHORTEST:
723 result = Grisu3(v, buffer, length, &decimal_exponent); 723 result = Grisu3(v, buffer, length, &decimal_exponent);
724 break; 724 break;
725 case FAST_DTOA_PRECISION: 725 case FAST_DTOA_PRECISION:
726 result = Grisu3Counted(v, requested_digits, 726 result = Grisu3Counted(v, requested_digits,
727 buffer, length, &decimal_exponent); 727 buffer, length, &decimal_exponent);
728 break; 728 break;
729 default: 729 default:
730 UNREACHABLE(); 730 UNREACHABLE();
731 } 731 }
732 if (result) { 732 if (result) {
733 *decimal_point = *length + decimal_exponent; 733 *decimal_point = *length + decimal_exponent;
734 buffer[*length] = '\0'; 734 buffer[*length] = '\0';
735 } 735 }
736 return result; 736 return result;
737 } 737 }
738 738
739 } // namespace double_conversion 739 } // namespace double_conversion
740 740
741 } // namespace WTF 741 } // namespace WTF
OLDNEW
« no previous file with comments | « Source/wtf/dtoa/double-conversion.cc ('k') | Source/wtf/dtoa/fixed-dtoa.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698