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

Side by Side Diff: src/fast-dtoa.cc

Issue 2000004: Added precision mode to fast-dtoa. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 3 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 | « src/fast-dtoa.h ('k') | test/cctest/SConscript » ('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 24 matching lines...) Expand all
35 35
36 namespace v8 { 36 namespace v8 {
37 namespace internal { 37 namespace internal {
38 38
39 // The minimal and maximal target exponent define the range of w's binary 39 // The minimal and maximal target exponent define the range of w's binary
40 // exponent, where 'w' is the result of multiplying the input by a cached power 40 // exponent, where 'w' is the result of multiplying the input by a cached power
41 // of ten. 41 // of ten.
42 // 42 //
43 // A different range might be chosen on a different platform, to optimize digit 43 // A different range might be chosen on a different platform, to optimize digit
44 // generation, but a smaller range requires more powers of ten to be cached. 44 // generation, but a smaller range requires more powers of ten to be cached.
45 static const int minimal_target_exponent = -60; 45 static const int kMinimalTargetExponent = -60;
46 static const int maximal_target_exponent = -32; 46 static const int kMaximalTargetExponent = -32;
47 47
48 48
49 // Adjusts the last digit of the generated number, and screens out generated 49 // Adjusts the last digit of the generated number, and screens out generated
50 // solutions that may be inaccurate. A solution may be inaccurate if it is 50 // solutions that may be inaccurate. A solution may be inaccurate if it is
51 // outside the safe interval, or if we ctannot prove that it is closer to the 51 // outside the safe interval, or if we ctannot prove that it is closer to the
52 // input than a neighboring representation of the same length. 52 // input than a neighboring representation of the same length.
53 // 53 //
54 // Input: * buffer containing the digits of too_high / 10^kappa 54 // Input: * buffer containing the digits of too_high / 10^kappa
55 // * the buffer's length 55 // * the buffer's length
56 // * distance_too_high_w == (too_high - w).f() * unit 56 // * distance_too_high_w == (too_high - w).f() * unit
57 // * unsafe_interval == (too_high - too_low).f() * unit 57 // * unsafe_interval == (too_high - too_low).f() * unit
58 // * rest = (too_high - buffer * 10^kappa).f() * unit 58 // * rest = (too_high - buffer * 10^kappa).f() * unit
59 // * ten_kappa = 10^kappa * unit 59 // * ten_kappa = 10^kappa * unit
60 // * unit = the common multiplier 60 // * unit = the common multiplier
61 // Output: returns true if the buffer is guaranteed to contain the closest 61 // Output: returns true if the buffer is guaranteed to contain the closest
62 // representable number to the input. 62 // representable number to the input.
63 // Modifies the generated digits in the buffer to approach (round towards) w. 63 // Modifies the generated digits in the buffer to approach (round towards) w.
64 bool RoundWeed(Vector<char> buffer, 64 static bool RoundWeed(Vector<char> buffer,
65 int length, 65 int length,
66 uint64_t distance_too_high_w, 66 uint64_t distance_too_high_w,
67 uint64_t unsafe_interval, 67 uint64_t unsafe_interval,
68 uint64_t rest, 68 uint64_t rest,
69 uint64_t ten_kappa, 69 uint64_t ten_kappa,
70 uint64_t unit) { 70 uint64_t unit) {
71 uint64_t small_distance = distance_too_high_w - unit; 71 uint64_t small_distance = distance_too_high_w - unit;
72 uint64_t big_distance = distance_too_high_w + unit; 72 uint64_t big_distance = distance_too_high_w + unit;
73 // Let w_low = too_high - big_distance, and 73 // Let w_low = too_high - big_distance, and
74 // w_high = too_high - small_distance. 74 // w_high = too_high - small_distance.
75 // Note: w_low < w < w_high 75 // Note: w_low < w < w_high
76 // 76 //
77 // The real w (* unit) must lie somewhere inside the interval 77 // The real w (* unit) must lie somewhere inside the interval
78 // ]w_low; w_low[ (often written as "(w_low; w_low)") 78 // ]w_low; w_high[ (often written as "(w_low; w_high)")
79 79
80 // Basically the buffer currently contains a number in the unsafe interval 80 // Basically the buffer currently contains a number in the unsafe interval
81 // ]too_low; too_high[ with too_low < w < too_high 81 // ]too_low; too_high[ with too_low < w < too_high
82 // 82 //
83 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 83 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84 // ^v 1 unit ^ ^ ^ ^ 84 // ^v 1 unit ^ ^ ^ ^
85 // boundary_high --------------------- . . . . 85 // boundary_high --------------------- . . . .
86 // ^v 1 unit . . . . 86 // ^v 1 unit . . . .
87 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . . 87 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
88 // . . ^ . . 88 // . . ^ . .
(...skipping 26 matching lines...) Expand all
115 // In fact the error is guaranteed to be strictly less than one unit. 115 // In fact the error is guaranteed to be strictly less than one unit.
116 // 116 //
117 // Anything that lies outside the unsafe interval is guaranteed not to round 117 // Anything that lies outside the unsafe interval is guaranteed not to round
118 // to v when read again. 118 // to v when read again.
119 // Anything that lies inside the safe interval is guaranteed to round to v 119 // Anything that lies inside the safe interval is guaranteed to round to v
120 // when read again. 120 // when read again.
121 // If the number inside the buffer lies inside the unsafe interval but not 121 // If the number inside the buffer lies inside the unsafe interval but not
122 // inside the safe interval then we simply do not know and bail out (returning 122 // inside the safe interval then we simply do not know and bail out (returning
123 // false). 123 // false).
124 // 124 //
125 // Similarly we have to take into account the imprecision of 'w' when rounding 125 // Similarly we have to take into account the imprecision of 'w' when finding
126 // the buffer. If we have two potential representations we need to make sure 126 // the closest representation of 'w'. If we have two potential
127 // that the chosen one is closer to w_low and w_high since v can be anywhere 127 // representations, and one is closer to both w_low and w_high, then we know
128 // between them. 128 // it is closer to the actual value v.
129 // 129 //
130 // By generating the digits of too_high we got the largest (closest to 130 // By generating the digits of too_high we got the largest (closest to
131 // too_high) buffer that is still in the unsafe interval. In the case where 131 // too_high) buffer that is still in the unsafe interval. In the case where
132 // w_high < buffer < too_high we try to decrement the buffer. 132 // w_high < buffer < too_high we try to decrement the buffer.
133 // This way the buffer approaches (rounds towards) w. 133 // This way the buffer approaches (rounds towards) w.
134 // There are 3 conditions that stop the decrementation process: 134 // There are 3 conditions that stop the decrementation process:
135 // 1) the buffer is already below w_high 135 // 1) the buffer is already below w_high
136 // 2) decrementing the buffer would make it leave the unsafe interval 136 // 2) decrementing the buffer would make it leave the unsafe interval
137 // 3) decrementing the buffer would yield a number below w_high and farther 137 // 3) decrementing the buffer would yield a number below w_high and farther
138 // away than the current number. In other words: 138 // away than the current number. In other words:
139 // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high 139 // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
140 // Instead of using the buffer directly we use its distance to too_high. 140 // Instead of using the buffer directly we use its distance to too_high.
141 // Conceptually rest ~= too_high - buffer 141 // Conceptually rest ~= too_high - buffer
142 // We need to do the following tests in this order to avoid over- and
143 // underflows.
144 ASSERT(rest <= unsafe_interval);
142 while (rest < small_distance && // Negated condition 1 145 while (rest < small_distance && // Negated condition 1
143 unsafe_interval - rest >= ten_kappa && // Negated condition 2 146 unsafe_interval - rest >= ten_kappa && // Negated condition 2
144 (rest + ten_kappa < small_distance || // buffer{-1} > w_high 147 (rest + ten_kappa < small_distance || // buffer{-1} > w_high
145 small_distance - rest >= rest + ten_kappa - small_distance)) { 148 small_distance - rest >= rest + ten_kappa - small_distance)) {
146 buffer[length - 1]--; 149 buffer[length - 1]--;
147 rest += ten_kappa; 150 rest += ten_kappa;
148 } 151 }
149 152
150 // We have approached w+ as much as possible. We now test if approaching w- 153 // We have approached w+ as much as possible. We now test if approaching w-
151 // would require changing the buffer. If yes, then we have two possible 154 // would require changing the buffer. If yes, then we have two possible
152 // representations close to w, but we cannot decide which one is closer. 155 // representations close to w, but we cannot decide which one is closer.
153 if (rest < big_distance && 156 if (rest < big_distance &&
154 unsafe_interval - rest >= ten_kappa && 157 unsafe_interval - rest >= ten_kappa &&
155 (rest + ten_kappa < big_distance || 158 (rest + ten_kappa < big_distance ||
156 big_distance - rest > rest + ten_kappa - big_distance)) { 159 big_distance - rest > rest + ten_kappa - big_distance)) {
157 return false; 160 return false;
158 } 161 }
159 162
160 // Weeding test. 163 // Weeding test.
161 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp] 164 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
162 // Since too_low = too_high - unsafe_interval this is equivalent to 165 // Since too_low = too_high - unsafe_interval this is equivalent to
163 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp] 166 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
164 // Conceptually we have: rest ~= too_high - buffer 167 // Conceptually we have: rest ~= too_high - buffer
165 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit); 168 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
166 } 169 }
167 170
168 171
172 // Rounds the buffer upwards if the result is closer to v by possibly adding
173 // 1 to the buffer. If the precision of the calculation is not sufficient to
174 // round correctly, return false.
175 // The rounding might shift the whole buffer in which case the kappa is
176 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
177 //
178 // If 2*rest > ten_kappa then the buffer needs to be round up.
179 // rest can have an error of +/- 1 unit. This function accounts for the
180 // imprecision and returns false, if the rounding direction cannot be
181 // unambiguously determined.
182 //
183 // Precondition: rest < ten_kappa.
184 static bool RoundWeedCounted(Vector<char> buffer,
185 int length,
186 uint64_t rest,
187 uint64_t ten_kappa,
188 uint64_t unit,
189 int* kappa) {
190 ASSERT(rest < ten_kappa);
191 // The following tests are done in a specific order to avoid overflows. They
192 // will work correctly with any uint64 values of rest < ten_kappa and unit.
193 //
194 // If the unit is too big, then we don't know which way to round. For example
195 // a unit of 50 means that the real number lies within rest +/- 50. If
196 // 10^kappa == 40 then there is no way to tell which way to round.
197 if (unit >= ten_kappa) return false;
198 // Even if unit is just half the size of 10^kappa we are already completely
199 // lost. (And after the previous test we know that the expression will not
200 // over/underflow.)
201 if (ten_kappa - unit <= unit) return false;
202 // If 2 * (rest + unit) <= 10^kappa we can safely round down.
203 if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
204 return true;
205 }
206 // If 2 * (rest - unit) >= 10^kappa, then we can safely round up.
207 if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
208 // Increment the last digit recursively until we find a non '9' digit.
209 buffer[length - 1]++;
210 for (int i = length - 1; i > 0; --i) {
211 if (buffer[i] != '0' + 10) break;
212 buffer[i] = '0';
213 buffer[i - 1]++;
214 }
215 // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the
216 // exception of the first digit all digits are now '0'. Simply switch the
217 // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and
218 // the power (the kappa) is increased.
219 if (buffer[0] == '0' + 10) {
220 buffer[0] = '1';
221 (*kappa) += 1;
222 }
223 return true;
224 }
225 return false;
226 }
227
169 228
170 static const uint32_t kTen4 = 10000; 229 static const uint32_t kTen4 = 10000;
171 static const uint32_t kTen5 = 100000; 230 static const uint32_t kTen5 = 100000;
172 static const uint32_t kTen6 = 1000000; 231 static const uint32_t kTen6 = 1000000;
173 static const uint32_t kTen7 = 10000000; 232 static const uint32_t kTen7 = 10000000;
174 static const uint32_t kTen8 = 100000000; 233 static const uint32_t kTen8 = 100000000;
175 static const uint32_t kTen9 = 1000000000; 234 static const uint32_t kTen9 = 1000000000;
176 235
177 // Returns the biggest power of ten that is less than or equal than the given 236 // Returns the biggest power of ten that is less than or equal than the given
178 // number. We furthermore receive the maximum number of bits 'number' has. 237 // number. We furthermore receive the maximum number of bits 'number' has.
179 // If number_bits == 0 then 0^-1 is returned 238 // If number_bits == 0 then 0^-1 is returned
180 // The number of bits must be <= 32. 239 // The number of bits must be <= 32.
181 // Precondition: (1 << number_bits) <= number < (1 << (number_bits + 1)). 240 // Precondition: number < (1 << (number_bits + 1)).
182 static void BiggestPowerTen(uint32_t number, 241 static void BiggestPowerTen(uint32_t number,
183 int number_bits, 242 int number_bits,
184 uint32_t* power, 243 uint32_t* power,
185 int* exponent) { 244 int* exponent) {
186 switch (number_bits) { 245 switch (number_bits) {
187 case 32: 246 case 32:
188 case 31: 247 case 31:
189 case 30: 248 case 30:
190 if (kTen9 <= number) { 249 if (kTen9 <= number) {
191 *power = kTen9; 250 *power = kTen9;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Following assignments are here to silence compiler warnings. 333 // Following assignments are here to silence compiler warnings.
275 *power = 0; 334 *power = 0;
276 *exponent = 0; 335 *exponent = 0;
277 UNREACHABLE(); 336 UNREACHABLE();
278 } 337 }
279 } 338 }
280 339
281 340
282 // Generates the digits of input number w. 341 // Generates the digits of input number w.
283 // w is a floating-point number (DiyFp), consisting of a significand and an 342 // w is a floating-point number (DiyFp), consisting of a significand and an
284 // exponent. Its exponent is bounded by minimal_target_exponent and 343 // exponent. Its exponent is bounded by kMinimalTargetExponent and
285 // maximal_target_exponent. 344 // kMaximalTargetExponent.
286 // Hence -60 <= w.e() <= -32. 345 // Hence -60 <= w.e() <= -32.
287 // 346 //
288 // Returns false if it fails, in which case the generated digits in the buffer 347 // Returns false if it fails, in which case the generated digits in the buffer
289 // should not be used. 348 // should not be used.
290 // Preconditions: 349 // Preconditions:
291 // * low, w and high are correct up to 1 ulp (unit in the last place). That 350 // * low, w and high are correct up to 1 ulp (unit in the last place). That
292 // is, their error must be less that a unit of their last digits. 351 // is, their error must be less than a unit of their last digits.
293 // * low.e() == w.e() == high.e() 352 // * low.e() == w.e() == high.e()
294 // * low < w < high, and taking into account their error: low~ <= high~ 353 // * low < w < high, and taking into account their error: low~ <= high~
295 // * minimal_target_exponent <= w.e() <= maximal_target_exponent 354 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
296 // Postconditions: returns false if procedure fails. 355 // Postconditions: returns false if procedure fails.
297 // otherwise: 356 // otherwise:
298 // * buffer is not null-terminated, but len contains the number of digits. 357 // * buffer is not null-terminated, but len contains the number of digits.
299 // * buffer contains the shortest possible decimal digit-sequence 358 // * buffer contains the shortest possible decimal digit-sequence
300 // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the 359 // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
301 // correct values of low and high (without their error). 360 // correct values of low and high (without their error).
302 // * if more than one decimal representation gives the minimal number of 361 // * if more than one decimal representation gives the minimal number of
303 // decimal digits then the one closest to W (where W is the correct value 362 // decimal digits then the one closest to W (where W is the correct value
304 // of w) is chosen. 363 // of w) is chosen.
305 // Remark: this procedure takes into account the imprecision of its input 364 // Remark: this procedure takes into account the imprecision of its input
306 // numbers. If the precision is not enough to guarantee all the postconditions 365 // numbers. If the precision is not enough to guarantee all the postconditions
307 // then false is returned. This usually happens rarely (~0.5%). 366 // then false is returned. This usually happens rarely (~0.5%).
308 // 367 //
309 // Say, for the sake of example, that 368 // Say, for the sake of example, that
310 // w.e() == -48, and w.f() == 0x1234567890abcdef 369 // w.e() == -48, and w.f() == 0x1234567890abcdef
311 // w's value can be computed by w.f() * 2^w.e() 370 // w's value can be computed by w.f() * 2^w.e()
312 // We can obtain w's integral digits by simply shifting w.f() by -w.e(). 371 // We can obtain w's integral digits by simply shifting w.f() by -w.e().
313 // -> w's integral part is 0x1234 372 // -> w's integral part is 0x1234
314 // w's fractional part is therefore 0x567890abcdef. 373 // w's fractional part is therefore 0x567890abcdef.
315 // Printing w's integral part is easy (simply print 0x1234 in decimal). 374 // Printing w's integral part is easy (simply print 0x1234 in decimal).
316 // In order to print its fraction we repeatedly multiply the fraction by 10 and 375 // In order to print its fraction we repeatedly multiply the fraction by 10 and
317 // get each digit. Example the first digit after the point would be computed by 376 // get each digit. Example the first digit after the point would be computed by
318 // (0x567890abcdef * 10) >> 48. -> 3 377 // (0x567890abcdef * 10) >> 48. -> 3
319 // The whole thing becomes slightly more complicated because we want to stop 378 // The whole thing becomes slightly more complicated because we want to stop
320 // once we have enough digits. That is, once the digits inside the buffer 379 // once we have enough digits. That is, once the digits inside the buffer
321 // represent 'w' we can stop. Everything inside the interval low - high 380 // represent 'w' we can stop. Everything inside the interval low - high
322 // represents w. However we have to pay attention to low, high and w's 381 // represents w. However we have to pay attention to low, high and w's
323 // imprecision. 382 // imprecision.
324 bool DigitGen(DiyFp low, 383 static bool DigitGen(DiyFp low,
325 DiyFp w, 384 DiyFp w,
326 DiyFp high, 385 DiyFp high,
327 Vector<char> buffer, 386 Vector<char> buffer,
328 int* length, 387 int* length,
329 int* kappa) { 388 int* kappa) {
330 ASSERT(low.e() == w.e() && w.e() == high.e()); 389 ASSERT(low.e() == w.e() && w.e() == high.e());
331 ASSERT(low.f() + 1 <= high.f() - 1); 390 ASSERT(low.f() + 1 <= high.f() - 1);
332 ASSERT(minimal_target_exponent <= w.e() && w.e() <= maximal_target_exponent); 391 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
333 // low, w and high are imprecise, but by less than one ulp (unit in the last 392 // low, w and high are imprecise, but by less than one ulp (unit in the last
334 // place). 393 // place).
335 // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that 394 // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
336 // the new numbers are outside of the interval we want the final 395 // the new numbers are outside of the interval we want the final
337 // representation to lie in. 396 // representation to lie in.
338 // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield 397 // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
339 // numbers that are certain to lie in the interval. We will use this fact 398 // numbers that are certain to lie in the interval. We will use this fact
340 // later on. 399 // later on.
341 // We will now start by generating the digits within the uncertain 400 // We will now start by generating the digits within the uncertain
342 // interval. Later we will weed out representations that lie outside the safe 401 // interval. Later we will weed out representations that lie outside the safe
343 // interval and thus _might_ lie outside the correct interval. 402 // interval and thus _might_ lie outside the correct interval.
344 uint64_t unit = 1; 403 uint64_t unit = 1;
345 DiyFp too_low = DiyFp(low.f() - unit, low.e()); 404 DiyFp too_low = DiyFp(low.f() - unit, low.e());
346 DiyFp too_high = DiyFp(high.f() + unit, high.e()); 405 DiyFp too_high = DiyFp(high.f() + unit, high.e());
347 // too_low and too_high are guaranteed to lie outside the interval we want the 406 // too_low and too_high are guaranteed to lie outside the interval we want the
348 // generated number in. 407 // generated number in.
349 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low); 408 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
350 // We now cut the input number into two parts: the integral digits and the 409 // We now cut the input number into two parts: the integral digits and the
351 // fractionals. We will not write any decimal separator though, but adapt 410 // fractionals. We will not write any decimal separator though, but adapt
352 // kappa instead. 411 // kappa instead.
353 // Reminder: we are currently computing the digits (stored inside the buffer) 412 // Reminder: we are currently computing the digits (stored inside the buffer)
354 // such that: too_low < buffer * 10^kappa < too_high 413 // such that: too_low < buffer * 10^kappa < too_high
355 // We use too_high for the digit_generation and stop as soon as possible. 414 // We use too_high for the digit_generation and stop as soon as possible.
356 // If we stop early we effectively round down. 415 // If we stop early we effectively round down.
357 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e()); 416 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
358 // Division by one is a shift. 417 // Division by one is a shift.
359 uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e()); 418 uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
360 // Modulo by one is an and. 419 // Modulo by one is an and.
361 uint64_t fractionals = too_high.f() & (one.f() - 1); 420 uint64_t fractionals = too_high.f() & (one.f() - 1);
362 uint32_t divider; 421 uint32_t divisor;
363 int divider_exponent; 422 int divisor_exponent;
364 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), 423 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
365 &divider, &divider_exponent); 424 &divisor, &divisor_exponent);
366 *kappa = divider_exponent + 1; 425 *kappa = divisor_exponent + 1;
367 *length = 0; 426 *length = 0;
368 // Loop invariant: buffer = too_high / 10^kappa (integer division) 427 // Loop invariant: buffer = too_high / 10^kappa (integer division)
369 // The invariant holds for the first iteration: kappa has been initialized 428 // The invariant holds for the first iteration: kappa has been initialized
370 // with the divider exponent + 1. And the divider is the biggest power of ten 429 // with the divisor exponent + 1. And the divisor is the biggest power of ten
371 // that is smaller than integrals. 430 // that is smaller than integrals.
372 while (*kappa > 0) { 431 while (*kappa > 0) {
373 int digit = integrals / divider; 432 int digit = integrals / divisor;
374 buffer[*length] = '0' + digit; 433 buffer[*length] = '0' + digit;
375 (*length)++; 434 (*length)++;
376 integrals %= divider; 435 integrals %= divisor;
377 (*kappa)--; 436 (*kappa)--;
378 // Note that kappa now equals the exponent of the divider and that the 437 // Note that kappa now equals the exponent of the divisor and that the
379 // invariant thus holds again. 438 // invariant thus holds again.
380 uint64_t rest = 439 uint64_t rest =
381 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; 440 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
382 // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e()) 441 // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
383 // Reminder: unsafe_interval.e() == one.e() 442 // Reminder: unsafe_interval.e() == one.e()
384 if (rest < unsafe_interval.f()) { 443 if (rest < unsafe_interval.f()) {
385 // Rounding down (by not emitting the remaining digits) yields a number 444 // Rounding down (by not emitting the remaining digits) yields a number
386 // that lies within the unsafe interval. 445 // that lies within the unsafe interval.
387 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(), 446 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
388 unsafe_interval.f(), rest, 447 unsafe_interval.f(), rest,
389 static_cast<uint64_t>(divider) << -one.e(), unit); 448 static_cast<uint64_t>(divisor) << -one.e(), unit);
390 } 449 }
391 divider /= 10; 450 divisor /= 10;
392 } 451 }
393 452
394 // The integrals have been generated. We are at the point of the decimal 453 // The integrals have been generated. We are at the point of the decimal
395 // separator. In the following loop we simply multiply the remaining digits by 454 // separator. In the following loop we simply multiply the remaining digits by
396 // 10 and divide by one. We just need to pay attention to multiply associated 455 // 10 and divide by one. We just need to pay attention to multiply associated
397 // data (like the interval or 'unit'), too. 456 // data (like the interval or 'unit'), too.
398 // Instead of multiplying by 10 we multiply by 5 (cheaper operation) and 457 // Note that the multiplication by 10 does not overflow, because w.e >= -60
399 // increase its (imaginary) exponent. At the same time we decrease the 458 // and thus one.e >= -60.
400 // divider's (one's) exponent and shift its significand. 459 ASSERT(one.e() >= -60);
401 // Basically, if fractionals was a DiyFp (with fractionals.e == one.e): 460 ASSERT(fractionals < one.f());
402 // fractionals.f *= 10; 461 ASSERT(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
403 // fractionals.f >>= 1; fractionals.e++; // value remains unchanged.
404 // one.f >>= 1; one.e++; // value remains unchanged.
405 // and we have again fractionals.e == one.e which allows us to divide
406 // fractionals.f() by one.f()
407 // We simply combine the *= 10 and the >>= 1.
408 while (true) { 462 while (true) {
409 fractionals *= 5; 463 fractionals *= 10;
410 unit *= 5; 464 unit *= 10;
411 unsafe_interval.set_f(unsafe_interval.f() * 5); 465 unsafe_interval.set_f(unsafe_interval.f() * 10);
412 unsafe_interval.set_e(unsafe_interval.e() + 1); // Will be optimized out.
413 one.set_f(one.f() >> 1);
414 one.set_e(one.e() + 1);
415 // Integer division by one. 466 // Integer division by one.
416 int digit = static_cast<int>(fractionals >> -one.e()); 467 int digit = static_cast<int>(fractionals >> -one.e());
417 buffer[*length] = '0' + digit; 468 buffer[*length] = '0' + digit;
418 (*length)++; 469 (*length)++;
419 fractionals &= one.f() - 1; // Modulo by one. 470 fractionals &= one.f() - 1; // Modulo by one.
420 (*kappa)--; 471 (*kappa)--;
421 if (fractionals < unsafe_interval.f()) { 472 if (fractionals < unsafe_interval.f()) {
422 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit, 473 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
423 unsafe_interval.f(), fractionals, one.f(), unit); 474 unsafe_interval.f(), fractionals, one.f(), unit);
424 } 475 }
425 } 476 }
426 } 477 }
427 478
428 479
480
481 // Generates (at most) requested_digits of input number w.
482 // w is a floating-point number (DiyFp), consisting of a significand and an
483 // exponent. Its exponent is bounded by kMinimalTargetExponent and
484 // kMaximalTargetExponent.
485 // Hence -60 <= w.e() <= -32.
486 //
487 // Returns false if it fails, in which case the generated digits in the buffer
488 // should not be used.
489 // Preconditions:
490 // * w is correct up to 1 ulp (unit in the last place). That
491 // is, its error must be strictly less than a unit of its last digit.
492 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
493 //
494 // Postconditions: returns false if procedure fails.
495 // otherwise:
496 // * buffer is not null-terminated, but length contains the number of
497 // digits.
498 // * the representation in buffer is the most precise representation of
499 // requested_digits digits.
500 // * buffer contains at most requested_digits digits of w. If there are less
501 // than requested_digits digits then some trailing '0's have been removed.
502 // * kappa is such that
503 // w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2.
504 //
505 // Remark: This procedure takes into account the imprecision of its input
506 // numbers. If the precision is not enough to guarantee all the postconditions
507 // then false is returned. This usually happens rarely, but the failure-rate
508 // increases with higher requested_digits.
509 static bool DigitGenCounted(DiyFp w,
510 int requested_digits,
511 Vector<char> buffer,
512 int* length,
513 int* kappa) {
514 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
515 ASSERT(kMinimalTargetExponent >= -60);
516 ASSERT(kMaximalTargetExponent <= -32);
517 // w is assumed to have an error less than 1 unit. Whenever w is scaled we
518 // also scale its error.
519 uint64_t w_error = 1;
520 // We cut the input number into two parts: the integral digits and the
521 // fractional digits. We don't emit any decimal separator, but adapt kappa
522 // instead. Example: instead of writing "1.2" we put "12" into the buffer and
523 // increase kappa by 1.
524 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
525 // Division by one is a shift.
526 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
527 // Modulo by one is an and.
528 uint64_t fractionals = w.f() & (one.f() - 1);
529 uint32_t divisor;
530 int divisor_exponent;
531 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
532 &divisor, &divisor_exponent);
533 *kappa = divisor_exponent + 1;
534 *length = 0;
535
536 // Loop invariant: buffer = w / 10^kappa (integer division)
537 // The invariant holds for the first iteration: kappa has been initialized
538 // with the divisor exponent + 1. And the divisor is the biggest power of ten
539 // that is smaller than 'integrals'.
540 while (*kappa > 0) {
541 int digit = integrals / divisor;
542 buffer[*length] = '0' + digit;
543 (*length)++;
544 requested_digits--;
545 integrals %= divisor;
546 (*kappa)--;
547 // Note that kappa now equals the exponent of the divisor and that the
548 // invariant thus holds again.
549 if (requested_digits == 0) break;
550 divisor /= 10;
551 }
552
553 if (requested_digits == 0) {
554 uint64_t rest =
555 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
556 return RoundWeedCounted(buffer, *length, rest,
557 static_cast<uint64_t>(divisor) << -one.e(), w_error,
558 kappa);
559 }
560
561 // The integrals have been generated. We are at the point of the decimal
562 // separator. In the following loop we simply multiply the remaining digits by
563 // 10 and divide by one. We just need to pay attention to multiply associated
564 // data (the 'unit'), too.
565 // Note that the multiplication by 10 does not overflow, because w.e >= -60
566 // and thus one.e >= -60.
567 ASSERT(one.e() >= -60);
568 ASSERT(fractionals < one.f());
569 ASSERT(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
570 while (requested_digits > 0 && fractionals > w_error) {
571 fractionals *= 10;
572 w_error *= 10;
573 // Integer division by one.
574 int digit = static_cast<int>(fractionals >> -one.e());
575 buffer[*length] = '0' + digit;
576 (*length)++;
577 requested_digits--;
578 fractionals &= one.f() - 1; // Modulo by one.
579 (*kappa)--;
580 }
581 if (requested_digits != 0) return false;
582 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
583 kappa);
584 }
585
586
429 // Provides a decimal representation of v. 587 // Provides a decimal representation of v.
430 // Returns true if it succeeds, otherwise the result cannot be trusted. 588 // Returns true if it succeeds, otherwise the result cannot be trusted.
431 // There will be *length digits inside the buffer (not null-terminated). 589 // There will be *length digits inside the buffer (not null-terminated).
432 // If the function returns true then 590 // If the function returns true then
433 // v == (double) (buffer * 10^decimal_exponent). 591 // v == (double) (buffer * 10^decimal_exponent).
434 // The digits in the buffer are the shortest representation possible: no 592 // The digits in the buffer are the shortest representation possible: no
435 // 0.09999999999999999 instead of 0.1. The shorter representation will even be 593 // 0.09999999999999999 instead of 0.1. The shorter representation will even be
436 // chosen even if the longer one would be closer to v. 594 // chosen even if the longer one would be closer to v.
437 // The last digit will be closest to the actual v. That is, even if several 595 // The last digit will be closest to the actual v. That is, even if several
438 // digits might correctly yield 'v' when read again, the closest will be 596 // digits might correctly yield 'v' when read again, the closest will be
439 // computed. 597 // computed.
440 bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) { 598 static bool Grisu3(double v,
599 Vector<char> buffer,
600 int* length,
601 int* decimal_exponent) {
441 DiyFp w = Double(v).AsNormalizedDiyFp(); 602 DiyFp w = Double(v).AsNormalizedDiyFp();
442 // boundary_minus and boundary_plus are the boundaries between v and its 603 // boundary_minus and boundary_plus are the boundaries between v and its
443 // closest floating-point neighbors. Any number strictly between 604 // closest floating-point neighbors. Any number strictly between
444 // boundary_minus and boundary_plus will round to v when convert to a double. 605 // boundary_minus and boundary_plus will round to v when convert to a double.
445 // Grisu3 will never output representations that lie exactly on a boundary. 606 // Grisu3 will never output representations that lie exactly on a boundary.
446 DiyFp boundary_minus, boundary_plus; 607 DiyFp boundary_minus, boundary_plus;
447 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus); 608 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
448 ASSERT(boundary_plus.e() == w.e()); 609 ASSERT(boundary_plus.e() == w.e());
449 DiyFp ten_mk; // Cached power of ten: 10^-k 610 DiyFp ten_mk; // Cached power of ten: 10^-k
450 int mk; // -k 611 int mk; // -k
451 GetCachedPower(w.e() + DiyFp::kSignificandSize, minimal_target_exponent, 612 GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
452 maximal_target_exponent, &mk, &ten_mk); 613 kMaximalTargetExponent, &mk, &ten_mk);
453 ASSERT(minimal_target_exponent <= w.e() + ten_mk.e() + 614 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
454 DiyFp::kSignificandSize && 615 DiyFp::kSignificandSize) &&
455 maximal_target_exponent >= w.e() + ten_mk.e() + 616 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
456 DiyFp::kSignificandSize); 617 DiyFp::kSignificandSize));
457 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a 618 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
458 // 64 bit significand and ten_mk is thus only precise up to 64 bits. 619 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
459 620
460 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated 621 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
461 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now 622 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
462 // off by a small amount. 623 // off by a small amount.
463 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. 624 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
464 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then 625 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
465 // (f-1) * 2^e < w*10^k < (f+1) * 2^e 626 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
466 DiyFp scaled_w = DiyFp::Times(w, ten_mk); 627 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
(...skipping 14 matching lines...) Expand all
481 // the buffer will be filled with "123" und the decimal_exponent will be 642 // the buffer will be filled with "123" und the decimal_exponent will be
482 // decreased by 2. 643 // decreased by 2.
483 int kappa; 644 int kappa;
484 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus, 645 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
485 buffer, length, &kappa); 646 buffer, length, &kappa);
486 *decimal_exponent = -mk + kappa; 647 *decimal_exponent = -mk + kappa;
487 return result; 648 return result;
488 } 649 }
489 650
490 651
652 // The "counted" version of grisu3 (see above) only generates requested_digits
653 // number of digits. This version does not generate the shortest representation,
654 // and with enough requested digits 0.1 will at some point print as 0.9999999...
655 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
656 // therefore the rounding strategy for halfway cases is irrelevant.
657 static bool Grisu3Counted(double v,
658 int requested_digits,
659 Vector<char> buffer,
660 int* length,
661 int* decimal_exponent) {
662 DiyFp w = Double(v).AsNormalizedDiyFp();
663 DiyFp ten_mk; // Cached power of ten: 10^-k
664 int mk; // -k
665 GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
666 kMaximalTargetExponent, &mk, &ten_mk);
667 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
668 DiyFp::kSignificandSize) &&
669 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
670 DiyFp::kSignificandSize));
671 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
672 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
673
674 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
675 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
676 // off by a small amount.
677 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
678 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
679 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
680 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
681
682 // We now have (double) (scaled_w * 10^-mk).
683 // DigitGen will generate the first requested_digits digits of scaled_w and
684 // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
685 // will not always be exactly the same since DigitGenCounted only produces a
686 // limited number of digits.)
687 int kappa;
688 bool result = DigitGenCounted(scaled_w, requested_digits,
689 buffer, length, &kappa);
690 *decimal_exponent = -mk + kappa;
691 return result;
692 }
693
694
491 bool FastDtoa(double v, 695 bool FastDtoa(double v,
696 FastDtoaMode mode,
697 int requested_digits,
492 Vector<char> buffer, 698 Vector<char> buffer,
493 int* length, 699 int* length,
494 int* point) { 700 int* decimal_point) {
495 ASSERT(v > 0); 701 ASSERT(v > 0);
496 ASSERT(!Double(v).IsSpecial()); 702 ASSERT(!Double(v).IsSpecial());
497 703
704 bool result = false;
498 int decimal_exponent; 705 int decimal_exponent;
499 bool result = grisu3(v, buffer, length, &decimal_exponent); 706 switch (mode) {
500 *point = *length + decimal_exponent; 707 case FAST_DTOA_SHORTEST:
501 buffer[*length] = '\0'; 708 result = Grisu3(v, buffer, length, &decimal_exponent);
709 break;
710 case FAST_DTOA_PRECISION:
711 result = Grisu3Counted(v, requested_digits,
712 buffer, length, &decimal_exponent);
713 break;
714 }
715 if (result) {
716 *decimal_point = *length + decimal_exponent;
717 buffer[*length] = '\0';
718 }
502 return result; 719 return result;
503 } 720 }
504 721
505 } } // namespace v8::internal 722 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/fast-dtoa.h ('k') | test/cctest/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698