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

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

Issue 3468003: Add bignum fall-back when the fast dtoa doesn't succeed. This removes Gay's d... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 1 month 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
OLDNEW
(Empty)
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
28 #include <math.h>
29
30 #include "v8.h"
31 #include "bignum-dtoa.h"
32
33 #include "bignum.h"
34 #include "double.h"
35
36 namespace v8 {
37 namespace internal {
38
39 // Returns an estimation of k such that 10^(k-1) <= v < 10^k.
William Hesse 2010/11/15 15:48:30 This is unclear. Explains that the input is the ex
Florian Loitsch 2010/11/16 14:32:06 Done.
40 // The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
41 // Note: the same is true for v + wiggle_plus (instead of simply v) (see
William Hesse 2010/11/15 15:48:30 What is wiggle_plus?
Florian Loitsch 2010/11/16 14:32:06 Changed to v+ (v's positive boundary).
42 // explanation below).
43 // Examples:
44 // EstimatePower(0) => 16
45 // EstimatePower(-52) => 0
46 //
47 // Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0.
48 static int EstimatePower(int exponent) {
49 // This function estimates log10 of v where v = f*2^e (with e == exponent).
50 // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)).
51 // Note that f is bounded by its container size. Let p = 53 (the double's
52 // significand size). Then 2^(p-1) <= f < 2^p.
53 //
54 // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close
55 // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)).
56 // The computed number undershoots by less than 0.631 (when we compute log3
57 // and not log10).
58 //
59 // Optimization: since we only need an approximated result this computation
60 // can be performed on 64 bit integers. On x86/x64 architecture the speedup is
61 // not really measurable, though.
62 //
63 // Since we want to avoid overshooting we decrement by 1e10 so that
64 // floating-point imprecisions don't affect us.
65 //
66 // Explanation for (v + wiggle_plus): the computation takes advantage of the
William Hesse 2010/11/15 15:48:30 Can the wiggle stuff be put where wiggle is used?
Florian Loitsch 2010/11/16 14:32:06 Reworked comment.
67 // fact that 2^(p-1) <= f < 2^p. However even after adding the wiggle this
68 // property is still true (even for denormals where the wiggle can be much
69 // more important).
70
71 const double k1Log10 = 0.30102999566398114; // 1/lg(10)
72
73 // For doubles len(f) == 53 (don't forget the hidden bit).
74 const int kSignificandSize = 53;
75 double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10);
76 return static_cast<int>(estimate);
77 }
78
79
80 // See comments for InitialScaledStartValues.
81 static void InitialScaledStartValuesPositiveExponent(
82 double v, int estimated_power, bool need_wiggles,
83 Bignum* numerator, Bignum* denominator,
84 Bignum* wiggle_minus, Bignum* wiggle_plus) {
85 // A positive exponent implies a positive power.
86 ASSERT(estimated_power >= 0);
87 // Since the estimated_power is positive we simply multiply the denominator
88 // by 10^estimated_power.
89
90 // The common case first. We later (10 lines below) correct the values for
91 // the special case where the boundaries are different.
92 // denominator = 2 * 10^estimated_power;
93 denominator->AssignPowerUInt16(10, estimated_power);
94 denominator->ShiftLeft(1);
95 // numerator = v * 2 (2 for the common denominator).
96 numerator->AssignUInt64(Double(v).Significand());
97 numerator->ShiftLeft(Double(v).Exponent() + 1);
98
99 if (need_wiggles) {
William Hesse 2010/11/15 15:48:30 Call these delta_plus and delta_minus, or somethin
Florian Loitsch 2010/11/16 14:32:06 Refactored (slightly) method and comments.
100 // Let v = f * 2^e, then wiggle_plus = 2^e;
101 wiggle_plus->AssignUInt16(1);
102 wiggle_plus->ShiftLeft(Double(v).Exponent());
103 // Same for wiggle_minus.
104 wiggle_minus->AssignUInt16(1);
105 wiggle_minus->ShiftLeft(Double(v).Exponent());
106
107 // If the significand (without the hidden bit) is 0, then the lower
108 // boundary is closer than just one ulp (unit in the last place).
109 // There is only one exception: if the next lower number is a denormal then
110 // the distance is 1 ulp. This cannot be the case for exponent >= 0 (but we
111 // have to test it in the other function where exponent < 0).
112 uint64_t v_bits = Double(v).AsUint64();
113 if ((v_bits & Double::kSignificandMask) == 0) {
114 // The lower boundary is closer at half the distance of "normal" numbers.
115 // Increase the denominator and adapt all but the wiggle_minus.
116 denominator->ShiftLeft(1); // *2
117 numerator->ShiftLeft(1); // *2
118 wiggle_plus->ShiftLeft(1); // *2
119 }
120 }
121 }
122
123
124 // See comments for InitialScaledStartValues
125 static void InitialScaledStartValuesNegativeExponentPositivePower(
126 double v, int estimated_power, bool need_wiggles,
127 Bignum* numerator, Bignum* denominator,
128 Bignum* wiggle_minus, Bignum* wiggle_plus) {
129 uint64_t significand = Double(v).Significand();
130 int exponent = Double(v).Exponent();
131 // v = f * 2^e with e < 0, and with estimated_power >= 0.
132 // This means that e is close to 0 (have a look at how estimated_power is
133 // computed).
134
135 // The common case first. We later (10 lines below) correct the values for
136 // the special case where the boundaries are different.
137 // denominator = 10^estimated_power * 2 * 2^-exponent (with exponent < 0)
138 denominator->AssignPowerUInt16(10, estimated_power);
139 denominator->ShiftLeft(1 - exponent);
140 // numerator = 2 * significand
141 // since v = significand * 2^exponent this is equivalent to
142 // numerator = v * 2 / 2^-exponent
143 numerator->AssignUInt64(significand);
144 numerator->ShiftLeft(1);
145
146 if (need_wiggles) {
147 // Given that the denominator already includes v's exponent the wiggle
148 // room is simply 1.
149 wiggle_plus->AssignUInt16(1);
150 // Same for wiggle_minus.
151 wiggle_minus->AssignUInt16(1);
152
153 // If the significand (without the hidden bit) is 0, then the lower
154 // boundary is closer than just one ulp (unit in the last place).
155 // There is only one exception: if the next lower number is a denormal
156 // then the distance is 1 ulp. Since the exponent is close to zero
157 // (otherwise estimated_power would have been negative) this cannot happen
158 // here either.
159 uint64_t v_bits = Double(v).AsUint64();
160 if ((v_bits & Double::kSignificandMask) == 0) {
161 // The lower boundary is closer at half the distance of "normal" numbers.
162 // Increase the denominator and adapt all but the wiggle_minus.
163 denominator->ShiftLeft(1); // *2
164 numerator->ShiftLeft(1); // *2
165 wiggle_plus->ShiftLeft(1); // *2
166 }
167 }
168 }
169
170
171 // See comments for InitialScaledStartValues
172 static void InitialScaledStartValuesNegativeExponentNegativePower(
173 double v, int estimated_power, bool need_wiggles,
174 Bignum* numerator, Bignum* denominator,
175 Bignum* wiggle_minus, Bignum* wiggle_plus) {
176 const uint64_t kMinimalNormalizedExponent =
177 V8_2PART_UINT64_C(0x00100000, 00000000);
178 uint64_t significand = Double(v).Significand();
179 int exponent = Double(v).Exponent();
180 // Instead of multiplying the denominator with 10^estimated_power we
181 // multiply all values (numerator and wiggles) by 10^-estimated_power.
182
183 // Use numerator as temporary container for power_ten
184 Bignum* power_ten = numerator;
185 power_ten->AssignPowerUInt16(10, -estimated_power);
186
187 // The common case first. The special case is handled soon.
188
189 if (need_wiggles) {
190 // wiggle_plus = wiggle_minus = 10^estimated_power
191 wiggle_plus->AssignBignum(*power_ten);
192 wiggle_minus->AssignBignum(*power_ten);
193 }
194 // denominator = 2 * 2^-exponent with exponent < 0.
195 denominator->AssignUInt16(1);
196 denominator->ShiftLeft(1 - exponent);
197 // numerator = significand * 2 * 10^-estimated_power
198 // since v = significand * 2^exponent this is equivalent to
199 // numerator = v * 10^-estimated_power * 2 * 2^-exponent.
200 // Remember: numerator has been abused as power_ten. So no need to assign it
201 // to itself.
202 numerator->MultiplyByUInt64(significand);
203 numerator->ShiftLeft(1);
204
205 if (need_wiggles) {
206 // The special case where the lower boundary is twice as close.
207 // This time we have to look out for the exception too.
208 uint64_t v_bits = Double(v).AsUint64();
209 if ((v_bits & Double::kSignificandMask) == 0 &&
210 // The only exception where a significand == 0 has its boundaries at
211 // "normal" distances:
212 (v_bits & Double::kExponentMask) != kMinimalNormalizedExponent) {
213 numerator->ShiftLeft(1); // *2
214 denominator->ShiftLeft(1); // *2
215 wiggle_plus->ShiftLeft(1); // *2
216 }
217 }
218 }
219
220
221 // v = significand * 2^exponent
222 // The initial start values consist of:
223 // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power.
224 // - a scaled (common) denominator.
225 // - the scaled range a double has towards -infinity while still being
226 // considered to be equal to v. In other words: the difference between v and
227 // its lower boundary.
228 // - the same room towards +infinity.
229 // The scaling consist of multiplying the numerator by 10^estimated_power, or
230 // (if the estimated_power is negative) by multiplying the denominator
231 // by 10^-estimated_power.
232 // Note that the wiggle-room is scaled too. If the common denominator has been
233 // scaled, then the wiggles are automatically scaled. Otherwise they are
234 // multiplied by the scaling factor, too.
235 //
236 // Let ep == estimated_power, then the returned values will satisfy:
237 // v / 10^ep = numerator / denominator.
238 // v's boundarys m- and m+:
239 // m- / 10^ep == v / 10^ep - wiggle_minus / denominator
240 // m+ / 10^ep == v / 10^ep + wiggle_plus / denominator
241 // Or in other words:
242 // m- == v - wiggle_minus * 10^ep / denominator;
243 // m+ == v + wiggle_plus * 10^ep / denominator;
244 //
245 // Since 10^(k-1) <= v < 10^k (with k == estimated_power)
246 // or 10^k <= v < 10^(k+1)
247 // we then have 0.1 <= numerator/denominator < 1
248 // or 1 <= numerator/denominator < 10
249 //
250 // It is then easy to kickstart the digit-generation routine.
251 static void InitialScaledStartValues(double v, int estimated_power,
252 bool need_wiggles,
253 Bignum* numerator, Bignum* denominator,
254 Bignum* wiggle_minus,
255 Bignum* wiggle_plus) {
256 if (Double(v).Exponent() >= 0) {
257 InitialScaledStartValuesPositiveExponent(
258 v, estimated_power, need_wiggles,
259 numerator, denominator, wiggle_minus, wiggle_plus);
260 } else if (estimated_power >= 0) {
261 InitialScaledStartValuesNegativeExponentPositivePower(
262 v, estimated_power, need_wiggles,
263 numerator, denominator, wiggle_minus, wiggle_plus);
264 } else {
265 InitialScaledStartValuesNegativeExponentNegativePower(
266 v, estimated_power, need_wiggles,
267 numerator, denominator, wiggle_minus, wiggle_plus);
268 }
269 }
270
271
272 // This routine multiplies numerator/denominator so that its values lies in the
273 // range 1-10. That is after a call to this function we have:
274 // 1 <= (numerator + wiggle_plus) /denominator < 10.
275 // In some cases estimated_power was too low, and this is already the case. We
276 // then simply adjust estimated_power so that 10^(k-1) <= v < 10^k (with k ==
277 // estimated_power) but do not touch the numerator or denominator.
278 // Otherwise the routine multiplies the numerator and the wiggles by 10.
279 static void FixupMultiply10(int estimated_power, bool is_even,
280 int* power,
281 Bignum* numerator, Bignum* denominator,
282 Bignum* wiggle_minus, Bignum* wiggle_plus) {
283 bool in_range;
284 if (is_even) {
285 // For IEEE doubles half-way cases (in decimal system numbers ending with 5)
286 // are rounded to the closest floating-point number with even significand.
287 in_range = Bignum::PlusCompare(*numerator, *wiggle_plus, *denominator) >= 0;
288 } else {
289 in_range = Bignum::PlusCompare(*numerator, *wiggle_plus, *denominator) > 0;
290 }
291 if (in_range) {
292 // Since numerator + wiggle_plus >= denominator we already have
293 // 1 <= numerator/denominator < 10. Simply update the estimated_power.
294 *power = estimated_power + 1;
295 } else {
296 *power = estimated_power;
297 numerator->Times10();
298 if (Bignum::Equal(*wiggle_minus, *wiggle_plus)) {
299 wiggle_minus->Times10();
300 wiggle_plus->AssignBignum(*wiggle_minus);
301 } else {
302 wiggle_minus->Times10();
303 wiggle_plus->Times10();
304 }
305 }
306 }
307
308
309 // Precondition: 0 <= (numerator+wiggle_plus) / denominator < 10.
310 // If 1 <= (numerator+wiggle_plus) / denominator < 10 then no leading 0 digit
311 // will be produced. This should be the standard precondition.
312 // Produces the least amount of digits so that the result lies in the wiggle
William Hesse 2010/11/15 15:48:30 Give more big picture comments - result lying in t
Florian Loitsch 2010/11/16 14:32:06 Done.
313 // room. Let V the value written in the buffer, and
314 // m- := (numerator - wiggle_minus) / denominator
315 // m+ := (numerator + wiggle_plus) / denominator
316 // <? := '<=' if is_even and '<' otherwise, then
317 // m- <? V <? m+
318 // In other words the written buffer would read as the input number.
319 static void GenerateDigits(Bignum* numerator, Bignum* denominator,
William Hesse 2010/11/15 15:48:30 GenerateShortestDigits?
Florian Loitsch 2010/11/16 14:32:06 Done.
320 Bignum* wiggle_minus, Bignum* wiggle_plus,
321 bool is_even,
322 Vector<char> buffer, int* length) {
323 // Small optimization: if wiggle_minus and wiggle_plus are the same just reuse
324 // one of the two bignums.
325 if (Bignum::Equal(*wiggle_minus, *wiggle_plus)) {
326 wiggle_plus = wiggle_minus;
327 }
328 *length = 0;
329 while (true) {
330 uint16_t digit;
331 digit = numerator->DivideModuloIntBignum(*denominator);
332 // digit = numerator / denominator (integer division).
333 // numerator = numerator % denominator.
334 buffer[(*length)++] = digit + '0';
335
336 // Can we stop already?
337 bool in_wiggle_room_minus;
338 bool in_wiggle_room_plus;
339 if (is_even) {
340 in_wiggle_room_minus = Bignum::LessEqual(*numerator, *wiggle_minus);
341 } else {
342 in_wiggle_room_minus = Bignum::Less(*numerator, *wiggle_minus);
343 }
344 if (is_even) {
345 in_wiggle_room_plus =
346 Bignum::PlusCompare(*numerator, *wiggle_plus, *denominator) >= 0;
347 } else {
348 in_wiggle_room_plus =
349 Bignum::PlusCompare(*numerator, *wiggle_plus, *denominator) > 0;
350 }
351 if (!in_wiggle_room_minus && !in_wiggle_room_plus) {
352 // Prepare for next iteration.
353 numerator->Times10();
354 wiggle_minus->Times10();
355 // We optimized wiggle_plus to be equal to wiggle_minus (if they share the
356 // same value). So don't multiply wiggle_plus if they point to the same
357 // object.
358 if (wiggle_minus != wiggle_plus) {
359 wiggle_plus->Times10();
360 }
361 } else if (in_wiggle_room_minus && in_wiggle_room_plus) {
362 // Let's see if 2*numerator < denominator.
363 // If yes, then the next digit would be < 5 and we can round down.
364 int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator);
365 if (compare < 0) {
366 // Remaining digits are less than .5. -> Round down (== do nothing).
367 } else if (compare > 0) {
368 // Remaining digits are more than .5 of denominator. -> Round up.
369 // Note that the last digit could not be a '9' as otherwise the whole
370 // loop would have stopped earlier.
371 // We still have an assert here in case the preconditions were not
372 // satisfied.
373 ASSERT(buffer[(*length) - 1] != '9');
374 buffer[(*length) - 1]++;
375 } else {
376 // Halfway case.
377 // TODO(floitsch): need a way to solve half-way cases.
378 // For now let's round towards even (since this is what Gay seems to
379 // do).
380
381 if ((buffer[(*length) - 1] - '0') % 2 == 0) {
382 // Round down => Do nothing.
383 } else {
384 ASSERT(buffer[(*length) - 1] != '9');
385 buffer[(*length) - 1]++;
386 }
387 }
388 return;
389 } else if (in_wiggle_room_minus) {
390 // Round down (== do nothing).
391 return;
392 } else { // in_wiggle_room_plus
393 // Round up.
394 // Note again that the last digit could not be '9' since this would have
395 // stopped the loop earlier.
396 // We still have an ASSERT here, in case the preconditions were not
397 // satisfied.
398 ASSERT(buffer[(*length) -1] != '9');
399 buffer[(*length) - 1]++;
400 return;
401 }
402 }
403 }
404
405
406 static int NormalizedExponent(uint64_t significand, int exponent) {
407 ASSERT(significand != 0);
408 while ((significand & Double::kHiddenBit) == 0) {
409 significand = significand << 1;
410 exponent = exponent - 1;
411 }
412 return exponent;
413 }
414
415
416 static void GenerateCountedDigits(int count, int* decimal_point,
417 Bignum* numerator, Bignum* denominator,
418 Vector<char>(buffer), int* length) {
419 ASSERT(count >= 0);
420 for (int i = 0; i < count - 1; ++i) {
421 uint16_t digit;
422 digit = numerator->DivideModuloIntBignum(*denominator);
423 // digit = numerator / denominator (integer division).
William Hesse 2010/11/15 15:48:30 Include "Assumes numerator / denominator < 10" (or
Florian Loitsch 2010/11/16 14:32:06 added ASSERT.
424 // numerator = numerator % denominator.
425 buffer[i] = digit + '0';
426 // Prepare for next iteration.
427 numerator->Times10();
428 }
429 // Generate the last digit.
430 uint16_t digit;
431 digit = numerator->DivideModuloIntBignum(*denominator);
432 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
433 digit++;
434 }
435 buffer[count - 1] = digit + '0';
436 // Correct bad digits (in case we had a sequence of '9's).
William Hesse 2010/11/15 15:48:30 Propagate the carry until we hit a non-'9' or til
Florian Loitsch 2010/11/16 14:32:06 Done.
437 for (int i = count - 1; i > 0; --i) {
438 if (buffer[i] != '0' + 10) break;
439 buffer[i] = '0';
440 buffer[i - 1]++;
441 }
442 if (buffer[0] == '0' + 10) {
William Hesse 2010/11/15 15:48:30 Propagate a carry past the top place.
Florian Loitsch 2010/11/16 14:32:06 Done.
443 buffer[0] = '1';
444 (*decimal_point)++;
445 }
446 *length = count;
447 }
448
449
450 static void BignumToFixed(int requested_digits, int* decimal_point,
451 Bignum* numerator, Bignum* denominator,
452 Vector<char>(buffer), int* length) {
453 // The requested digits correspond to the digits after the point.
454 // The variable 'needed_digits' includes the digits before the point.
455 int needed_digits;
456 // Note that we have to look at more than just the requested_digits, since
457 // a number could be rounded up. Example: v=0.5 with requested_digits=0.
458 // Even though the power of v equals 0 we can't just stop here.
459 if (-(*decimal_point) > requested_digits) {
William Hesse 2010/11/15 15:48:30 Explain this. People don't know what all these qu
Florian Loitsch 2010/11/16 14:32:06 Done.
460 *decimal_point = -requested_digits;
461 *length = 0;
462 return;
463 } else if (-(*decimal_point) == requested_digits) {
464 *decimal_point = -requested_digits;
465 denominator->Times10(); // Bring fraction back to range 0.1 - 1.
William Hesse 2010/11/15 15:48:30 Why does this bring the fraction to that range?
Florian Loitsch 2010/11/16 14:32:06 Done.
466 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
467 // If the fraction is >= 0.5 then we have to include the rounded
468 // digit.
469 buffer[0] = '1';
470 *length = 1;
471 (*decimal_point)++;
472 } else {
473 // Note that we caught most of similar cases earlier.
474 *length = 0;
475 }
476 return;
477 } else {
478 needed_digits = (*decimal_point) + requested_digits;
479 }
480 GenerateCountedDigits(needed_digits, decimal_point,
481 numerator, denominator,
482 buffer, length);
483 }
484
485
486 void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
487 Vector<char> buffer, int* length, int* decimal_point) {
488 ASSERT(v > 0);
489 ASSERT(!Double(v).IsSpecial());
490 uint64_t significand = Double(v).Significand();
491 bool is_even = (significand & 1) == 0;
492 int exponent = Double(v).Exponent();
493 int normalized_exponent = NormalizedExponent(significand, exponent);
494 // estimated_power might be too low by 1.
495 int estimated_power = EstimatePower(normalized_exponent);
496 bool need_wiggles = (mode == BIGNUM_DTOA_SHORTEST);
William Hesse 2010/11/15 15:48:30 bool compute_shortest_approximation =, instead of
Florian Loitsch 2010/11/16 14:32:06 renamed to need_boundary_deltas.
497
498 // Shortcut for Fixed.
499 // The requested digits correspond to the digits after the point. If the
500 // number is much too small, then there is no need in trying to get any
501 // digits.
502 if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) {
503 buffer[0] = '\0';
504 *length = 0;
505 // Set decimal-point to -requested_digits. This is what Gay does.
506 // Note that it should not have any effect anyways since the string is
507 // empty.
508 *decimal_point = -requested_digits;
509 return;
510 }
511
512 Bignum numerator;
513 Bignum denominator;
514 Bignum wiggle_minus;
515 Bignum wiggle_plus;
516 // Make sure the bignum can grow large enough. The smallest double equals
517 // 4e-324. In this case the denominator needs less than 324*4 binary digits.
William Hesse 2010/11/15 15:48:30 fewer than
Florian Loitsch 2010/11/16 14:32:06 Done.
518 // The maximum double is 1.7976931348623157e308 which needs less than
William Hesse 2010/11/15 15:48:30 fewer
Florian Loitsch 2010/11/16 14:32:06 Done.
519 // 308*4 binary digits.
520 ASSERT(Bignum::kMaxSignificantBits >= 324*4);
521 InitialScaledStartValues(v, estimated_power, need_wiggles,
522 &numerator, &denominator,
523 &wiggle_minus, &wiggle_plus);
524 FixupMultiply10(estimated_power, is_even, decimal_point,
525 &numerator, &denominator,
526 &wiggle_minus, &wiggle_plus);
527 switch (mode) {
528 case BIGNUM_DTOA_SHORTEST:
529 GenerateDigits(&numerator, &denominator,
530 &wiggle_minus, &wiggle_plus,
531 is_even, buffer, length);
532 break;
533 case BIGNUM_DTOA_FIXED:
534 BignumToFixed(requested_digits, decimal_point,
535 &numerator, &denominator,
536 buffer, length);
537 break;
538 case BIGNUM_DTOA_PRECISION:
539 GenerateCountedDigits(requested_digits, decimal_point,
540 &numerator, &denominator,
541 buffer, length);
542 break;
543 default:
544 UNREACHABLE();
545 }
546 buffer[*length] = '\0';
547 }
548
549 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698