source: webkit/trunk/JavaScriptCore/wtf/dtoa.cpp@ 65588

Last change on this file since 65588 was 65588, checked in by [email protected], 15 years ago

Bug 44146 - Remove toDouble/toUInt32 methods from UString.

Reviewed by Sam Weinig.

JavaScriptCore:

These methods all implement JavaScript language specific behaviour, and as such
are not suited to being on a generic string object. They are also inefficient
and incorrectly used, refactor & cleanup. Uses of these methods really divide
out into two cases.

ToNumber:
Uses of toDouble from JSString and from parseFloat are implementing ecma's
ToNumber conversion from strings (see ecma-262 9.3.1), so UString::toDouble
should largely just be moved out to a global jsToNumber function. ToNumber is
capable of recognizing either decimal or hexadecimal numbers, but parseFloat
should only recognize decimal values. This is currently handled by testing for
hexadecimal before calling toDouble, which should unnecessary - instead we can
just split out the two parts to the grammar into separate functions. Also,
strtod recognizes a set of literals (nan, inf, and infinity - all with any
capitalization) - which are not defined by any of the specs we are implementing.
To handle this we need to perform additional work in toDouble to convert the
unsupported cases of infinities back to NaNs. Instead we should simply remove
support for this literals from strtod. This should provide a more desirable
behaviour for all clients of strtod.

Indexed properties:
Uses of the toStrictUInt32 methods are were all converting property names to
indices, and all uses of toUInt32 were incorrect; in all cases we should have
been calling toUInt32. This error results in some incorrect behaviour in the
DOM (accessing property "0 " of a NodeList should fail; it currently does not).
Move this method onto Identifier (our canonical property name), and make it
always perform a strict conversion. Add a layout test to check NodeList does
convert indexed property names correctly.

(JSC::Arguments::getOwnPropertySlot):
(JSC::Arguments::getOwnPropertyDescriptor):
(JSC::Arguments::put):
(JSC::Arguments::deleteProperty):

  • runtime/Identifier.cpp:

(JSC::Identifier::toUInt32):

  • runtime/Identifier.h:

(JSC::Identifier::toUInt32):

  • runtime/JSArray.cpp:

(JSC::JSArray::getOwnPropertySlot):
(JSC::JSArray::getOwnPropertyDescriptor):
(JSC::JSArray::put):
(JSC::JSArray::deleteProperty):

  • runtime/JSArray.h:

(JSC::Identifier::toArrayIndex):

  • runtime/JSByteArray.cpp:

(JSC::JSByteArray::getOwnPropertySlot):
(JSC::JSByteArray::getOwnPropertyDescriptor):
(JSC::JSByteArray::put):

  • runtime/JSGlobalObjectFunctions.cpp:

(JSC::isInfinity):
(JSC::jsHexIntegerLiteral):
(JSC::jsStrDecimalLiteral):
(JSC::jsToNumber):
(JSC::parseFloat):

  • runtime/JSGlobalObjectFunctions.h:
  • runtime/JSString.cpp:

(JSC::JSString::getPrimitiveNumber):
(JSC::JSString::toNumber):
(JSC::JSString::getStringPropertyDescriptor):

  • runtime/JSString.h:

(JSC::JSString::getStringPropertySlot):

  • runtime/ObjectPrototype.cpp:

(JSC::ObjectPrototype::put):

  • runtime/StringObject.cpp:

(JSC::StringObject::deleteProperty):

  • runtime/UString.cpp:
  • runtime/UString.h:
  • wtf/dtoa.cpp:

(WTF::strtod):

WebCore:

These methods all implement JavaScript language specific behaviour, and as such
are not suited to being on a generic string object. They are also inefficient
and incorrectly used, refactor & cleanup. Uses of these methods really divide
out into two cases.

ToNumber:
Uses of toDouble from JSString and from parseFloat are implementing ecma's
ToNumber conversion from strings (see ecma-262 9.3.1), so UString::toDouble
should largely just be moved out to a global jsToNumber function. ToNumber is
capable of recognizing either decimal or hexadecimal numbers, but parseFloat
should only recognize decimal values. This is currently handled by testing for
hexadecimal before calling toDouble, which should unnecessary - instead we can
just split out the two parts to the grammar into separate functions. Also,
strtod recognizes a set of literals (nan, inf, and infinity - all with any
capitalization) - which are not defined by any of the specs we are implementing.
To handle this we need to perform additional work in toDouble to convert the
unsupported cases of infinities back to NaNs. Instead we should simply remove
support for this literals from strtod. This should provide a more desirable
behaviour for all clients of strtod.

Indexed properties:
Uses of the toStrictUInt32 methods are were all converting property names to
indices, and all uses of toUInt32 were incorrect; in all cases we should have
been calling toUInt32. This error results in some incorrect behaviour in the
DOM (accessing property "0 " of a NodeList should fail; it currently does not).
Move this method onto Identifier (our canonical property name), and make it
always perform a strict conversion. Add a layout test to check NodeList does
convert indexed property names correctly.

Test: fast/dom/NodeList/nodelist-item-with-index.html

  • WebCore.xcodeproj/project.pbxproj:
  • bindings/js/JSDOMWindowCustom.cpp:

(WebCore::JSDOMWindow::getOwnPropertySlot):
(WebCore::JSDOMWindow::getOwnPropertyDescriptor):

  • bindings/js/JSHTMLAllCollectionCustom.cpp:

(WebCore::callHTMLAllCollection):
(WebCore::JSHTMLAllCollection::item):

  • bindings/js/JSHTMLCollectionCustom.cpp:

(WebCore::callHTMLCollection):
(WebCore::JSHTMLCollection::item):

  • bindings/js/JSNodeListCustom.cpp:

(WebCore::callNodeList):

  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/runtime_array.cpp:

(JSC::RuntimeArray::getOwnPropertySlot):
(JSC::RuntimeArray::getOwnPropertyDescriptor):
(JSC::RuntimeArray::put):

LayoutTests:

Test that indexing into nodelists works correctly, particularly
wrt indices passed as strings that contain whitespace.

  • fast/dom/NodeList/nodelist-item-with-index-expected.txt: Added.
  • fast/dom/NodeList/nodelist-item-with-index.html: Added.
  • Property allow-tabs set to x
  • Property svn:eol-style set to native
File size: 61.8 KB
Line 
1/****************************************************************
2 *
3 * The author of this software is David M. Gay.
4 *
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose without fee is hereby granted, provided that this entire notice
10 * is included in all copies of any software which is or includes a copy
11 * or modification of this software and in all copies of the supporting
12 * documentation for such software.
13 *
14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
17 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 *
19 ***************************************************************/
20
21/* Please send bug reports to
22 David M. Gay
23 Bell Laboratories, Room 2C-463
24 600 Mountain Avenue
25 Murray Hill, NJ 07974-0636
26 U.S.A.
27 [email protected]
28 */
29
30/* On a machine with IEEE extended-precision registers, it is
31 * necessary to specify double-precision (53-bit) rounding precision
32 * before invoking strtod or dtoa. If the machine uses (the equivalent
33 * of) Intel 80x87 arithmetic, the call
34 * _control87(PC_53, MCW_PC);
35 * does this with many compilers. Whether this or another call is
36 * appropriate depends on the compiler; for this to work, it may be
37 * necessary to #include "float.h" or another system-dependent header
38 * file.
39 */
40
41/* strtod for IEEE-arithmetic machines.
42 *
43 * This strtod returns a nearest machine number to the input decimal
44 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
45 * broken by the IEEE round-even rule. Otherwise ties are broken by
46 * biased rounding (add half and chop).
47 *
48 * Inspired loosely by William D. Clinger's paper "How to Read Floating
49 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
50 *
51 * Modifications:
52 *
53 * 1. We only require IEEE.
54 * 2. We get by with floating-point arithmetic in a case that
55 * Clinger missed -- when we're computing d * 10^n
56 * for a small integer d and the integer n is not too
57 * much larger than 22 (the maximum integer k for which
58 * we can represent 10^k exactly), we may be able to
59 * compute (d*10^k) * 10^(e-k) with just one roundoff.
60 * 3. Rather than a bit-at-a-time adjustment of the binary
61 * result in the hard case, we use floating-point
62 * arithmetic to determine the adjustment to within
63 * one bit; only in really hard cases do we need to
64 * compute a second residual.
65 * 4. Because of 3., we don't need a large table of powers of 10
66 * for ten-to-e (just some small tables, e.g. of 10^k
67 * for 0 <= k <= 22).
68 */
69
70/*
71 * #define IEEE_8087 for IEEE-arithmetic machines where the least
72 * significant byte has the lowest address.
73 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
74 * significant byte has the lowest address.
75 * #define No_leftright to omit left-right logic in fast floating-point
76 * computation of dtoa.
77 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
78 * and Honor_FLT_ROUNDS is not #defined.
79 * #define Inaccurate_Divide for IEEE-format with correctly rounded
80 * products but inaccurate quotients, e.g., for Intel i860.
81 * #define USE_LONG_LONG on machines that have a "long long"
82 * integer type (of >= 64 bits), and performance testing shows that
83 * it is faster than 32-bit fallback (which is often not the case
84 * on 32-bit machines). On such machines, you can #define Just_16
85 * to store 16 bits per 32-bit int32_t when doing high-precision integer
86 * arithmetic. Whether this speeds things up or slows things down
87 * depends on the machine and the number being converted.
88 * #define Bad_float_h if your system lacks a float.h or if it does not
89 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
90 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
91 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
92 * avoids underflows on inputs whose result does not underflow.
93 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
94 * floating-point numbers and flushes underflows to zero rather
95 * than implementing gradual underflow, then you must also #define
96 * Sudden_Underflow.
97 * #define YES_ALIAS to permit aliasing certain double values with
98 * arrays of ULongs. This leads to slightly better code with
99 * some compilers and was always used prior to 19990916, but it
100 * is not strictly legal and can cause trouble with aggressively
101 * optimizing compilers (e.g., gcc 2.95.1 under -O2).
102 * #define SET_INEXACT if IEEE arithmetic is being used and extra
103 * computation should be done to set the inexact flag when the
104 * result is inexact and avoid setting inexact when the result
105 * is exact. In this case, dtoa.c must be compiled in
106 * an environment, perhaps provided by #include "dtoa.c" in a
107 * suitable wrapper, that defines two functions,
108 * int get_inexact(void);
109 * void clear_inexact(void);
110 * such that get_inexact() returns a nonzero value if the
111 * inexact bit is already set, and clear_inexact() sets the
112 * inexact bit to 0. When SET_INEXACT is #defined, strtod
113 * also does extra computations to set the underflow and overflow
114 * flags when appropriate (i.e., when the result is tiny and
115 * inexact or when it is a numeric value rounded to +-infinity).
116 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
117 * the result overflows to +-Infinity or underflows to 0.
118 */
119
120#include "config.h"
121#include "dtoa.h"
122
123#if HAVE(ERRNO_H)
124#include <errno.h>
125#else
126#define NO_ERRNO
127#endif
128#include <math.h>
129#include <stdint.h>
130#include <stdio.h>
131#include <stdlib.h>
132#include <string.h>
133#include <wtf/AlwaysInline.h>
134#include <wtf/Assertions.h>
135#include <wtf/FastMalloc.h>
136#include <wtf/MathExtras.h>
137#include <wtf/Threading.h>
138#include <wtf/Vector.h>
139
140#if COMPILER(MSVC)
141#pragma warning(disable: 4244)
142#pragma warning(disable: 4245)
143#pragma warning(disable: 4554)
144#endif
145
146#if CPU(BIG_ENDIAN)
147#define IEEE_MC68k
148#elif CPU(MIDDLE_ENDIAN)
149#define IEEE_ARM
150#else
151#define IEEE_8087
152#endif
153
154#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
155Exactly one of IEEE_8087, IEEE_ARM or IEEE_MC68k should be defined.
156#endif
157
158namespace WTF {
159
160#if ENABLE(JSC_MULTIPLE_THREADS)
161Mutex* s_dtoaP5Mutex;
162#endif
163
164typedef union {
165 double d;
166 uint32_t L[2];
167} U;
168
169#ifdef YES_ALIAS
170#define dval(x) x
171#ifdef IEEE_8087
172#define word0(x) ((uint32_t*)&x)[1]
173#define word1(x) ((uint32_t*)&x)[0]
174#else
175#define word0(x) ((uint32_t*)&x)[0]
176#define word1(x) ((uint32_t*)&x)[1]
177#endif
178#else
179#ifdef IEEE_8087
180#define word0(x) (x)->L[1]
181#define word1(x) (x)->L[0]
182#else
183#define word0(x) (x)->L[0]
184#define word1(x) (x)->L[1]
185#endif
186#define dval(x) (x)->d
187#endif
188
189/* The following definition of Storeinc is appropriate for MIPS processors.
190 * An alternative that might be better on some machines is
191 * *p++ = high << 16 | low & 0xffff;
192 */
193static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low)
194{
195 uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
196#if defined(IEEE_8087) || defined(IEEE_ARM)
197 p16[1] = high;
198 p16[0] = low;
199#else
200 p16[0] = high;
201 p16[1] = low;
202#endif
203 return p + 1;
204}
205
206#define Exp_shift 20
207#define Exp_shift1 20
208#define Exp_msk1 0x100000
209#define Exp_msk11 0x100000
210#define Exp_mask 0x7ff00000
211#define P 53
212#define Bias 1023
213#define Emin (-1022)
214#define Exp_1 0x3ff00000
215#define Exp_11 0x3ff00000
216#define Ebits 11
217#define Frac_mask 0xfffff
218#define Frac_mask1 0xfffff
219#define Ten_pmax 22
220#define Bletch 0x10
221#define Bndry_mask 0xfffff
222#define Bndry_mask1 0xfffff
223#define LSB 1
224#define Sign_bit 0x80000000
225#define Log2P 1
226#define Tiny0 0
227#define Tiny1 1
228#define Quick_max 14
229#define Int_max 14
230
231#if !defined(NO_IEEE_Scale)
232#undef Avoid_Underflow
233#define Avoid_Underflow
234#endif
235
236#if !defined(Flt_Rounds)
237#if defined(FLT_ROUNDS)
238#define Flt_Rounds FLT_ROUNDS
239#else
240#define Flt_Rounds 1
241#endif
242#endif /* Flt_Rounds */
243
244
245#define rounded_product(a, b) a *= b
246#define rounded_quotient(a, b) a /= b
247
248#define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
249#define Big1 0xffffffff
250
251
252// FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
253#ifndef Pack_32
254#define Pack_32
255#endif
256
257#if CPU(PPC64) || CPU(X86_64)
258// FIXME: should we enable this on all 64-bit CPUs?
259// 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
260#define USE_LONG_LONG
261#endif
262
263#ifndef USE_LONG_LONG
264#ifdef Just_16
265#undef Pack_32
266/* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
267 * This makes some inner loops simpler and sometimes saves work
268 * during multiplications, but it often seems to make things slightly
269 * slower. Hence the default is now to store 32 bits per int32_t.
270 */
271#endif
272#endif
273
274#define Kmax 15
275
276struct BigInt {
277 BigInt() : sign(0) { }
278 int sign;
279
280 void clear()
281 {
282 sign = 0;
283 m_words.clear();
284 }
285
286 size_t size() const
287 {
288 return m_words.size();
289 }
290
291 void resize(size_t s)
292 {
293 m_words.resize(s);
294 }
295
296 uint32_t* words()
297 {
298 return m_words.data();
299 }
300
301 const uint32_t* words() const
302 {
303 return m_words.data();
304 }
305
306 void append(uint32_t w)
307 {
308 m_words.append(w);
309 }
310
311 Vector<uint32_t, 16> m_words;
312};
313
314static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */
315{
316#ifdef USE_LONG_LONG
317 unsigned long long carry;
318#else
319 uint32_t carry;
320#endif
321
322 int wds = b.size();
323 uint32_t* x = b.words();
324 int i = 0;
325 carry = a;
326 do {
327#ifdef USE_LONG_LONG
328 unsigned long long y = *x * (unsigned long long)m + carry;
329 carry = y >> 32;
330 *x++ = (uint32_t)y & 0xffffffffUL;
331#else
332#ifdef Pack_32
333 uint32_t xi = *x;
334 uint32_t y = (xi & 0xffff) * m + carry;
335 uint32_t z = (xi >> 16) * m + (y >> 16);
336 carry = z >> 16;
337 *x++ = (z << 16) + (y & 0xffff);
338#else
339 uint32_t y = *x * m + carry;
340 carry = y >> 16;
341 *x++ = y & 0xffff;
342#endif
343#endif
344 } while (++i < wds);
345
346 if (carry)
347 b.append((uint32_t)carry);
348}
349
350static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9)
351{
352 int k;
353 int32_t y;
354 int32_t x = (nd + 8) / 9;
355
356 for (k = 0, y = 1; x > y; y <<= 1, k++) { }
357#ifdef Pack_32
358 b.sign = 0;
359 b.resize(1);
360 b.words()[0] = y9;
361#else
362 b.sign = 0;
363 b.resize((b->x[1] = y9 >> 16) ? 2 : 1);
364 b.words()[0] = y9 & 0xffff;
365#endif
366
367 int i = 9;
368 if (9 < nd0) {
369 s += 9;
370 do {
371 multadd(b, 10, *s++ - '0');
372 } while (++i < nd0);
373 s++;
374 } else
375 s += 10;
376 for (; i < nd; i++)
377 multadd(b, 10, *s++ - '0');
378}
379
380static int hi0bits(uint32_t x)
381{
382 int k = 0;
383
384 if (!(x & 0xffff0000)) {
385 k = 16;
386 x <<= 16;
387 }
388 if (!(x & 0xff000000)) {
389 k += 8;
390 x <<= 8;
391 }
392 if (!(x & 0xf0000000)) {
393 k += 4;
394 x <<= 4;
395 }
396 if (!(x & 0xc0000000)) {
397 k += 2;
398 x <<= 2;
399 }
400 if (!(x & 0x80000000)) {
401 k++;
402 if (!(x & 0x40000000))
403 return 32;
404 }
405 return k;
406}
407
408static int lo0bits(uint32_t* y)
409{
410 int k;
411 uint32_t x = *y;
412
413 if (x & 7) {
414 if (x & 1)
415 return 0;
416 if (x & 2) {
417 *y = x >> 1;
418 return 1;
419 }
420 *y = x >> 2;
421 return 2;
422 }
423 k = 0;
424 if (!(x & 0xffff)) {
425 k = 16;
426 x >>= 16;
427 }
428 if (!(x & 0xff)) {
429 k += 8;
430 x >>= 8;
431 }
432 if (!(x & 0xf)) {
433 k += 4;
434 x >>= 4;
435 }
436 if (!(x & 0x3)) {
437 k += 2;
438 x >>= 2;
439 }
440 if (!(x & 1)) {
441 k++;
442 x >>= 1;
443 if (!x & 1)
444 return 32;
445 }
446 *y = x;
447 return k;
448}
449
450static void i2b(BigInt& b, int i)
451{
452 b.sign = 0;
453 b.resize(1);
454 b.words()[0] = i;
455}
456
457static void mult(BigInt& aRef, const BigInt& bRef)
458{
459 const BigInt* a = &aRef;
460 const BigInt* b = &bRef;
461 BigInt c;
462 int wa, wb, wc;
463 const uint32_t* x = 0;
464 const uint32_t* xa;
465 const uint32_t* xb;
466 const uint32_t* xae;
467 const uint32_t* xbe;
468 uint32_t* xc;
469 uint32_t* xc0;
470 uint32_t y;
471#ifdef USE_LONG_LONG
472 unsigned long long carry, z;
473#else
474 uint32_t carry, z;
475#endif
476
477 if (a->size() < b->size()) {
478 const BigInt* tmp = a;
479 a = b;
480 b = tmp;
481 }
482
483 wa = a->size();
484 wb = b->size();
485 wc = wa + wb;
486 c.resize(wc);
487
488 for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
489 *xc = 0;
490 xa = a->words();
491 xae = xa + wa;
492 xb = b->words();
493 xbe = xb + wb;
494 xc0 = c.words();
495#ifdef USE_LONG_LONG
496 for (; xb < xbe; xc0++) {
497 if ((y = *xb++)) {
498 x = xa;
499 xc = xc0;
500 carry = 0;
501 do {
502 z = *x++ * (unsigned long long)y + *xc + carry;
503 carry = z >> 32;
504 *xc++ = (uint32_t)z & 0xffffffffUL;
505 } while (x < xae);
506 *xc = (uint32_t)carry;
507 }
508 }
509#else
510#ifdef Pack_32
511 for (; xb < xbe; xb++, xc0++) {
512 if ((y = *xb & 0xffff)) {
513 x = xa;
514 xc = xc0;
515 carry = 0;
516 do {
517 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
518 carry = z >> 16;
519 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
520 carry = z2 >> 16;
521 xc = storeInc(xc, z2, z);
522 } while (x < xae);
523 *xc = carry;
524 }
525 if ((y = *xb >> 16)) {
526 x = xa;
527 xc = xc0;
528 carry = 0;
529 uint32_t z2 = *xc;
530 do {
531 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
532 carry = z >> 16;
533 xc = storeInc(xc, z, z2);
534 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
535 carry = z2 >> 16;
536 } while (x < xae);
537 *xc = z2;
538 }
539 }
540#else
541 for (; xb < xbe; xc0++) {
542 if ((y = *xb++)) {
543 x = xa;
544 xc = xc0;
545 carry = 0;
546 do {
547 z = *x++ * y + *xc + carry;
548 carry = z >> 16;
549 *xc++ = z & 0xffff;
550 } while (x < xae);
551 *xc = carry;
552 }
553 }
554#endif
555#endif
556 for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
557 c.resize(wc);
558 aRef = c;
559}
560
561struct P5Node : Noncopyable {
562 BigInt val;
563 P5Node* next;
564};
565
566static P5Node* p5s;
567static int p5sCount;
568
569static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
570{
571 static int p05[3] = { 5, 25, 125 };
572
573 if (int i = k & 3)
574 multadd(b, p05[i - 1], 0);
575
576 if (!(k >>= 2))
577 return;
578
579#if ENABLE(JSC_MULTIPLE_THREADS)
580 s_dtoaP5Mutex->lock();
581#endif
582 P5Node* p5 = p5s;
583
584 if (!p5) {
585 /* first time */
586 p5 = new P5Node;
587 i2b(p5->val, 625);
588 p5->next = 0;
589 p5s = p5;
590 p5sCount = 1;
591 }
592
593 int p5sCountLocal = p5sCount;
594#if ENABLE(JSC_MULTIPLE_THREADS)
595 s_dtoaP5Mutex->unlock();
596#endif
597 int p5sUsed = 0;
598
599 for (;;) {
600 if (k & 1)
601 mult(b, p5->val);
602
603 if (!(k >>= 1))
604 break;
605
606 if (++p5sUsed == p5sCountLocal) {
607#if ENABLE(JSC_MULTIPLE_THREADS)
608 s_dtoaP5Mutex->lock();
609#endif
610 if (p5sUsed == p5sCount) {
611 ASSERT(!p5->next);
612 p5->next = new P5Node;
613 p5->next->next = 0;
614 p5->next->val = p5->val;
615 mult(p5->next->val, p5->next->val);
616 ++p5sCount;
617 }
618
619 p5sCountLocal = p5sCount;
620#if ENABLE(JSC_MULTIPLE_THREADS)
621 s_dtoaP5Mutex->unlock();
622#endif
623 }
624 p5 = p5->next;
625 }
626}
627
628static ALWAYS_INLINE void lshift(BigInt& b, int k)
629{
630#ifdef Pack_32
631 int n = k >> 5;
632#else
633 int n = k >> 4;
634#endif
635
636 int origSize = b.size();
637 int n1 = n + origSize + 1;
638
639 if (k &= 0x1f)
640 b.resize(b.size() + n + 1);
641 else
642 b.resize(b.size() + n);
643
644 const uint32_t* srcStart = b.words();
645 uint32_t* dstStart = b.words();
646 const uint32_t* src = srcStart + origSize - 1;
647 uint32_t* dst = dstStart + n1 - 1;
648#ifdef Pack_32
649 if (k) {
650 uint32_t hiSubword = 0;
651 int s = 32 - k;
652 for (; src >= srcStart; --src) {
653 *dst-- = hiSubword | *src >> s;
654 hiSubword = *src << k;
655 }
656 *dst = hiSubword;
657 ASSERT(dst == dstStart + n);
658
659 b.resize(origSize + n + !!b.words()[n1 - 1]);
660 }
661#else
662 if (k &= 0xf) {
663 uint32_t hiSubword = 0;
664 int s = 16 - k;
665 for (; src >= srcStart; --src) {
666 *dst-- = hiSubword | *src >> s;
667 hiSubword = (*src << k) & 0xffff;
668 }
669 *dst = hiSubword;
670 ASSERT(dst == dstStart + n);
671 result->wds = b->wds + n + !!result->x[n1 - 1];
672 }
673#endif
674 else {
675 do {
676 *--dst = *src--;
677 } while (src >= srcStart);
678 }
679 for (dst = dstStart + n; dst != dstStart; )
680 *--dst = 0;
681
682 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
683}
684
685static int cmp(const BigInt& a, const BigInt& b)
686{
687 const uint32_t *xa, *xa0, *xb, *xb0;
688 int i, j;
689
690 i = a.size();
691 j = b.size();
692 ASSERT(i <= 1 || a.words()[i - 1]);
693 ASSERT(j <= 1 || b.words()[j - 1]);
694 if (i -= j)
695 return i;
696 xa0 = a.words();
697 xa = xa0 + j;
698 xb0 = b.words();
699 xb = xb0 + j;
700 for (;;) {
701 if (*--xa != *--xb)
702 return *xa < *xb ? -1 : 1;
703 if (xa <= xa0)
704 break;
705 }
706 return 0;
707}
708
709static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
710{
711 const BigInt* a = &aRef;
712 const BigInt* b = &bRef;
713 int i, wa, wb;
714 uint32_t* xc;
715
716 i = cmp(*a, *b);
717 if (!i) {
718 c.sign = 0;
719 c.resize(1);
720 c.words()[0] = 0;
721 return;
722 }
723 if (i < 0) {
724 const BigInt* tmp = a;
725 a = b;
726 b = tmp;
727 i = 1;
728 } else
729 i = 0;
730
731 wa = a->size();
732 const uint32_t* xa = a->words();
733 const uint32_t* xae = xa + wa;
734 wb = b->size();
735 const uint32_t* xb = b->words();
736 const uint32_t* xbe = xb + wb;
737
738 c.resize(wa);
739 c.sign = i;
740 xc = c.words();
741#ifdef USE_LONG_LONG
742 unsigned long long borrow = 0;
743 do {
744 unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
745 borrow = y >> 32 & (uint32_t)1;
746 *xc++ = (uint32_t)y & 0xffffffffUL;
747 } while (xb < xbe);
748 while (xa < xae) {
749 unsigned long long y = *xa++ - borrow;
750 borrow = y >> 32 & (uint32_t)1;
751 *xc++ = (uint32_t)y & 0xffffffffUL;
752 }
753#else
754 uint32_t borrow = 0;
755#ifdef Pack_32
756 do {
757 uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
758 borrow = (y & 0x10000) >> 16;
759 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
760 borrow = (z & 0x10000) >> 16;
761 xc = storeInc(xc, z, y);
762 } while (xb < xbe);
763 while (xa < xae) {
764 uint32_t y = (*xa & 0xffff) - borrow;
765 borrow = (y & 0x10000) >> 16;
766 uint32_t z = (*xa++ >> 16) - borrow;
767 borrow = (z & 0x10000) >> 16;
768 xc = storeInc(xc, z, y);
769 }
770#else
771 do {
772 uint32_t y = *xa++ - *xb++ - borrow;
773 borrow = (y & 0x10000) >> 16;
774 *xc++ = y & 0xffff;
775 } while (xb < xbe);
776 while (xa < xae) {
777 uint32_t y = *xa++ - borrow;
778 borrow = (y & 0x10000) >> 16;
779 *xc++ = y & 0xffff;
780 }
781#endif
782#endif
783 while (!*--xc)
784 wa--;
785 c.resize(wa);
786}
787
788static double ulp(U *x)
789{
790 register int32_t L;
791 U u;
792
793 L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;
794#ifndef Avoid_Underflow
795#ifndef Sudden_Underflow
796 if (L > 0) {
797#endif
798#endif
799 word0(&u) = L;
800 word1(&u) = 0;
801#ifndef Avoid_Underflow
802#ifndef Sudden_Underflow
803 } else {
804 L = -L >> Exp_shift;
805 if (L < Exp_shift) {
806 word0(&u) = 0x80000 >> L;
807 word1(&u) = 0;
808 } else {
809 word0(&u) = 0;
810 L -= Exp_shift;
811 word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
812 }
813 }
814#endif
815#endif
816 return dval(&u);
817}
818
819static double b2d(const BigInt& a, int* e)
820{
821 const uint32_t* xa;
822 const uint32_t* xa0;
823 uint32_t w;
824 uint32_t y;
825 uint32_t z;
826 int k;
827 U d;
828
829#define d0 word0(&d)
830#define d1 word1(&d)
831
832 xa0 = a.words();
833 xa = xa0 + a.size();
834 y = *--xa;
835 ASSERT(y);
836 k = hi0bits(y);
837 *e = 32 - k;
838#ifdef Pack_32
839 if (k < Ebits) {
840 d0 = Exp_1 | (y >> (Ebits - k));
841 w = xa > xa0 ? *--xa : 0;
842 d1 = (y << (32 - Ebits + k)) | (w >> (Ebits - k));
843 goto returnD;
844 }
845 z = xa > xa0 ? *--xa : 0;
846 if (k -= Ebits) {
847 d0 = Exp_1 | (y << k) | (z >> (32 - k));
848 y = xa > xa0 ? *--xa : 0;
849 d1 = (z << k) | (y >> (32 - k));
850 } else {
851 d0 = Exp_1 | y;
852 d1 = z;
853 }
854#else
855 if (k < Ebits + 16) {
856 z = xa > xa0 ? *--xa : 0;
857 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
858 w = xa > xa0 ? *--xa : 0;
859 y = xa > xa0 ? *--xa : 0;
860 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
861 goto returnD;
862 }
863 z = xa > xa0 ? *--xa : 0;
864 w = xa > xa0 ? *--xa : 0;
865 k -= Ebits + 16;
866 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
867 y = xa > xa0 ? *--xa : 0;
868 d1 = w << k + 16 | y << k;
869#endif
870returnD:
871#undef d0
872#undef d1
873 return dval(&d);
874}
875
876static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
877{
878 int de, k;
879 uint32_t* x;
880 uint32_t y, z;
881#ifndef Sudden_Underflow
882 int i;
883#endif
884#define d0 word0(d)
885#define d1 word1(d)
886
887 b.sign = 0;
888#ifdef Pack_32
889 b.resize(1);
890#else
891 b.resize(2);
892#endif
893 x = b.words();
894
895 z = d0 & Frac_mask;
896 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
897#ifdef Sudden_Underflow
898 de = (int)(d0 >> Exp_shift);
899#else
900 if ((de = (int)(d0 >> Exp_shift)))
901 z |= Exp_msk1;
902#endif
903#ifdef Pack_32
904 if ((y = d1)) {
905 if ((k = lo0bits(&y))) {
906 x[0] = y | (z << (32 - k));
907 z >>= k;
908 } else
909 x[0] = y;
910 if (z) {
911 b.resize(2);
912 x[1] = z;
913 }
914
915#ifndef Sudden_Underflow
916 i = b.size();
917#endif
918 } else {
919 k = lo0bits(&z);
920 x[0] = z;
921#ifndef Sudden_Underflow
922 i = 1;
923#endif
924 b.resize(1);
925 k += 32;
926 }
927#else
928 if ((y = d1)) {
929 if ((k = lo0bits(&y))) {
930 if (k >= 16) {
931 x[0] = y | z << 32 - k & 0xffff;
932 x[1] = z >> k - 16 & 0xffff;
933 x[2] = z >> k;
934 i = 2;
935 } else {
936 x[0] = y & 0xffff;
937 x[1] = y >> 16 | z << 16 - k & 0xffff;
938 x[2] = z >> k & 0xffff;
939 x[3] = z >> k + 16;
940 i = 3;
941 }
942 } else {
943 x[0] = y & 0xffff;
944 x[1] = y >> 16;
945 x[2] = z & 0xffff;
946 x[3] = z >> 16;
947 i = 3;
948 }
949 } else {
950 k = lo0bits(&z);
951 if (k >= 16) {
952 x[0] = z;
953 i = 0;
954 } else {
955 x[0] = z & 0xffff;
956 x[1] = z >> 16;
957 i = 1;
958 }
959 k += 32;
960 } while (!x[i])
961 --i;
962 b->resize(i + 1);
963#endif
964#ifndef Sudden_Underflow
965 if (de) {
966#endif
967 *e = de - Bias - (P - 1) + k;
968 *bits = P - k;
969#ifndef Sudden_Underflow
970 } else {
971 *e = de - Bias - (P - 1) + 1 + k;
972#ifdef Pack_32
973 *bits = (32 * i) - hi0bits(x[i - 1]);
974#else
975 *bits = (i + 2) * 16 - hi0bits(x[i]);
976#endif
977 }
978#endif
979}
980#undef d0
981#undef d1
982
983static double ratio(const BigInt& a, const BigInt& b)
984{
985 U da, db;
986 int k, ka, kb;
987
988 dval(&da) = b2d(a, &ka);
989 dval(&db) = b2d(b, &kb);
990#ifdef Pack_32
991 k = ka - kb + 32 * (a.size() - b.size());
992#else
993 k = ka - kb + 16 * (a.size() - b.size());
994#endif
995 if (k > 0)
996 word0(&da) += k * Exp_msk1;
997 else {
998 k = -k;
999 word0(&db) += k * Exp_msk1;
1000 }
1001 return dval(&da) / dval(&db);
1002}
1003
1004static const double tens[] = {
1005 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1006 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1007 1e20, 1e21, 1e22
1008};
1009
1010static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1011static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1012#ifdef Avoid_Underflow
1013 9007199254740992. * 9007199254740992.e-256
1014 /* = 2^106 * 1e-53 */
1015#else
1016 1e-256
1017#endif
1018};
1019
1020/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1021/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1022#define Scale_Bit 0x10
1023#define n_bigtens 5
1024
1025double strtod(const char* s00, char** se)
1026{
1027#ifdef Avoid_Underflow
1028 int scale;
1029#endif
1030 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1031 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1032 const char *s, *s0, *s1;
1033 double aadj, aadj1;
1034 U aadj2, adj, rv, rv0;
1035 int32_t L;
1036 uint32_t y, z;
1037 BigInt bb, bb1, bd, bd0, bs, delta;
1038#ifdef SET_INEXACT
1039 int inexact, oldinexact;
1040#endif
1041
1042 sign = nz0 = nz = 0;
1043 dval(&rv) = 0;
1044 for (s = s00; ; s++) {
1045 switch (*s) {
1046 case '-':
1047 sign = 1;
1048 /* no break */
1049 case '+':
1050 if (*++s)
1051 goto break2;
1052 /* no break */
1053 case 0:
1054 goto ret0;
1055 case '\t':
1056 case '\n':
1057 case '\v':
1058 case '\f':
1059 case '\r':
1060 case ' ':
1061 continue;
1062 default:
1063 goto break2;
1064 }
1065 }
1066break2:
1067 if (*s == '0') {
1068 nz0 = 1;
1069 while (*++s == '0') { }
1070 if (!*s)
1071 goto ret;
1072 }
1073 s0 = s;
1074 y = z = 0;
1075 for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1076 if (nd < 9)
1077 y = (10 * y) + c - '0';
1078 else if (nd < 16)
1079 z = (10 * z) + c - '0';
1080 nd0 = nd;
1081 if (c == '.') {
1082 c = *++s;
1083 if (!nd) {
1084 for (; c == '0'; c = *++s)
1085 nz++;
1086 if (c > '0' && c <= '9') {
1087 s0 = s;
1088 nf += nz;
1089 nz = 0;
1090 goto haveDig;
1091 }
1092 goto digDone;
1093 }
1094 for (; c >= '0' && c <= '9'; c = *++s) {
1095haveDig:
1096 nz++;
1097 if (c -= '0') {
1098 nf += nz;
1099 for (i = 1; i < nz; i++)
1100 if (nd++ < 9)
1101 y *= 10;
1102 else if (nd <= DBL_DIG + 1)
1103 z *= 10;
1104 if (nd++ < 9)
1105 y = (10 * y) + c;
1106 else if (nd <= DBL_DIG + 1)
1107 z = (10 * z) + c;
1108 nz = 0;
1109 }
1110 }
1111 }
1112digDone:
1113 e = 0;
1114 if (c == 'e' || c == 'E') {
1115 if (!nd && !nz && !nz0)
1116 goto ret0;
1117 s00 = s;
1118 esign = 0;
1119 switch (c = *++s) {
1120 case '-':
1121 esign = 1;
1122 case '+':
1123 c = *++s;
1124 }
1125 if (c >= '0' && c <= '9') {
1126 while (c == '0')
1127 c = *++s;
1128 if (c > '0' && c <= '9') {
1129 L = c - '0';
1130 s1 = s;
1131 while ((c = *++s) >= '0' && c <= '9')
1132 L = (10 * L) + c - '0';
1133 if (s - s1 > 8 || L > 19999)
1134 /* Avoid confusion from exponents
1135 * so large that e might overflow.
1136 */
1137 e = 19999; /* safe for 16 bit ints */
1138 else
1139 e = (int)L;
1140 if (esign)
1141 e = -e;
1142 } else
1143 e = 0;
1144 } else
1145 s = s00;
1146 }
1147 if (!nd) {
1148 if (!nz && !nz0) {
1149ret0:
1150 s = s00;
1151 sign = 0;
1152 }
1153 goto ret;
1154 }
1155 e1 = e -= nf;
1156
1157 /* Now we have nd0 digits, starting at s0, followed by a
1158 * decimal point, followed by nd-nd0 digits. The number we're
1159 * after is the integer represented by those digits times
1160 * 10**e */
1161
1162 if (!nd0)
1163 nd0 = nd;
1164 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1165 dval(&rv) = y;
1166 if (k > 9) {
1167#ifdef SET_INEXACT
1168 if (k > DBL_DIG)
1169 oldinexact = get_inexact();
1170#endif
1171 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1172 }
1173 if (nd <= DBL_DIG && Flt_Rounds == 1) {
1174 if (!e)
1175 goto ret;
1176 if (e > 0) {
1177 if (e <= Ten_pmax) {
1178 /* rv = */ rounded_product(dval(&rv), tens[e]);
1179 goto ret;
1180 }
1181 i = DBL_DIG - nd;
1182 if (e <= Ten_pmax + i) {
1183 /* A fancier test would sometimes let us do
1184 * this for larger i values.
1185 */
1186 e -= i;
1187 dval(&rv) *= tens[i];
1188 /* rv = */ rounded_product(dval(&rv), tens[e]);
1189 goto ret;
1190 }
1191 }
1192#ifndef Inaccurate_Divide
1193 else if (e >= -Ten_pmax) {
1194 /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
1195 goto ret;
1196 }
1197#endif
1198 }
1199 e1 += nd - k;
1200
1201#ifdef SET_INEXACT
1202 inexact = 1;
1203 if (k <= DBL_DIG)
1204 oldinexact = get_inexact();
1205#endif
1206#ifdef Avoid_Underflow
1207 scale = 0;
1208#endif
1209
1210 /* Get starting approximation = rv * 10**e1 */
1211
1212 if (e1 > 0) {
1213 if ((i = e1 & 15))
1214 dval(&rv) *= tens[i];
1215 if (e1 &= ~15) {
1216 if (e1 > DBL_MAX_10_EXP) {
1217ovfl:
1218#ifndef NO_ERRNO
1219 errno = ERANGE;
1220#endif
1221 /* Can't trust HUGE_VAL */
1222 word0(&rv) = Exp_mask;
1223 word1(&rv) = 0;
1224#ifdef SET_INEXACT
1225 /* set overflow bit */
1226 dval(&rv0) = 1e300;
1227 dval(&rv0) *= dval(&rv0);
1228#endif
1229 goto ret;
1230 }
1231 e1 >>= 4;
1232 for (j = 0; e1 > 1; j++, e1 >>= 1)
1233 if (e1 & 1)
1234 dval(&rv) *= bigtens[j];
1235 /* The last multiplication could overflow. */
1236 word0(&rv) -= P * Exp_msk1;
1237 dval(&rv) *= bigtens[j];
1238 if ((z = word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1239 goto ovfl;
1240 if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P)) {
1241 /* set to largest number */
1242 /* (Can't trust DBL_MAX) */
1243 word0(&rv) = Big0;
1244 word1(&rv) = Big1;
1245 } else
1246 word0(&rv) += P * Exp_msk1;
1247 }
1248 } else if (e1 < 0) {
1249 e1 = -e1;
1250 if ((i = e1 & 15))
1251 dval(&rv) /= tens[i];
1252 if (e1 >>= 4) {
1253 if (e1 >= 1 << n_bigtens)
1254 goto undfl;
1255#ifdef Avoid_Underflow
1256 if (e1 & Scale_Bit)
1257 scale = 2 * P;
1258 for (j = 0; e1 > 0; j++, e1 >>= 1)
1259 if (e1 & 1)
1260 dval(&rv) *= tinytens[j];
1261 if (scale && (j = (2 * P) + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0) {
1262 /* scaled rv is denormal; zap j low bits */
1263 if (j >= 32) {
1264 word1(&rv) = 0;
1265 if (j >= 53)
1266 word0(&rv) = (P + 2) * Exp_msk1;
1267 else
1268 word0(&rv) &= 0xffffffff << (j - 32);
1269 } else
1270 word1(&rv) &= 0xffffffff << j;
1271 }
1272#else
1273 for (j = 0; e1 > 1; j++, e1 >>= 1)
1274 if (e1 & 1)
1275 dval(&rv) *= tinytens[j];
1276 /* The last multiplication could underflow. */
1277 dval(&rv0) = dval(&rv);
1278 dval(&rv) *= tinytens[j];
1279 if (!dval(&rv)) {
1280 dval(&rv) = 2. * dval(&rv0);
1281 dval(&rv) *= tinytens[j];
1282#endif
1283 if (!dval(&rv)) {
1284undfl:
1285 dval(&rv) = 0.;
1286#ifndef NO_ERRNO
1287 errno = ERANGE;
1288#endif
1289 goto ret;
1290 }
1291#ifndef Avoid_Underflow
1292 word0(&rv) = Tiny0;
1293 word1(&rv) = Tiny1;
1294 /* The refinement below will clean
1295 * this approximation up.
1296 */
1297 }
1298#endif
1299 }
1300 }
1301
1302 /* Now the hard part -- adjusting rv to the correct value.*/
1303
1304 /* Put digits into bd: true value = bd * 10^e */
1305
1306 s2b(bd0, s0, nd0, nd, y);
1307
1308 for (;;) {
1309 bd = bd0;
1310 d2b(bb, &rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1311 i2b(bs, 1);
1312
1313 if (e >= 0) {
1314 bb2 = bb5 = 0;
1315 bd2 = bd5 = e;
1316 } else {
1317 bb2 = bb5 = -e;
1318 bd2 = bd5 = 0;
1319 }
1320 if (bbe >= 0)
1321 bb2 += bbe;
1322 else
1323 bd2 -= bbe;
1324 bs2 = bb2;
1325#ifdef Avoid_Underflow
1326 j = bbe - scale;
1327 i = j + bbbits - 1; /* logb(rv) */
1328 if (i < Emin) /* denormal */
1329 j += P - Emin;
1330 else
1331 j = P + 1 - bbbits;
1332#else /*Avoid_Underflow*/
1333#ifdef Sudden_Underflow
1334 j = P + 1 - bbbits;
1335#else /*Sudden_Underflow*/
1336 j = bbe;
1337 i = j + bbbits - 1; /* logb(rv) */
1338 if (i < Emin) /* denormal */
1339 j += P - Emin;
1340 else
1341 j = P + 1 - bbbits;
1342#endif /*Sudden_Underflow*/
1343#endif /*Avoid_Underflow*/
1344 bb2 += j;
1345 bd2 += j;
1346#ifdef Avoid_Underflow
1347 bd2 += scale;
1348#endif
1349 i = bb2 < bd2 ? bb2 : bd2;
1350 if (i > bs2)
1351 i = bs2;
1352 if (i > 0) {
1353 bb2 -= i;
1354 bd2 -= i;
1355 bs2 -= i;
1356 }
1357 if (bb5 > 0) {
1358 pow5mult(bs, bb5);
1359 mult(bb, bs);
1360 }
1361 if (bb2 > 0)
1362 lshift(bb, bb2);
1363 if (bd5 > 0)
1364 pow5mult(bd, bd5);
1365 if (bd2 > 0)
1366 lshift(bd, bd2);
1367 if (bs2 > 0)
1368 lshift(bs, bs2);
1369 diff(delta, bb, bd);
1370 dsign = delta.sign;
1371 delta.sign = 0;
1372 i = cmp(delta, bs);
1373
1374 if (i < 0) {
1375 /* Error is less than half an ulp -- check for
1376 * special case of mantissa a power of two.
1377 */
1378 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
1379#ifdef Avoid_Underflow
1380 || (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1
1381#else
1382 || (word0(&rv) & Exp_mask) <= Exp_msk1
1383#endif
1384 ) {
1385#ifdef SET_INEXACT
1386 if (!delta->words()[0] && delta->size() <= 1)
1387 inexact = 0;
1388#endif
1389 break;
1390 }
1391 if (!delta.words()[0] && delta.size() <= 1) {
1392 /* exact result */
1393#ifdef SET_INEXACT
1394 inexact = 0;
1395#endif
1396 break;
1397 }
1398 lshift(delta, Log2P);
1399 if (cmp(delta, bs) > 0)
1400 goto dropDown;
1401 break;
1402 }
1403 if (!i) {
1404 /* exactly half-way between */
1405 if (dsign) {
1406 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1407 && word1(&rv) == (
1408#ifdef Avoid_Underflow
1409 (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1410 ? (0xffffffff & (0xffffffff << (2 * P + 1 - (y >> Exp_shift)))) :
1411#endif
1412 0xffffffff)) {
1413 /*boundary case -- increment exponent*/
1414 word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1;
1415 word1(&rv) = 0;
1416#ifdef Avoid_Underflow
1417 dsign = 0;
1418#endif
1419 break;
1420 }
1421 } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1422dropDown:
1423 /* boundary case -- decrement exponent */
1424#ifdef Sudden_Underflow /*{{*/
1425 L = word0(&rv) & Exp_mask;
1426#ifdef Avoid_Underflow
1427 if (L <= (scale ? (2 * P + 1) * Exp_msk1 : Exp_msk1))
1428#else
1429 if (L <= Exp_msk1)
1430#endif /*Avoid_Underflow*/
1431 goto undfl;
1432 L -= Exp_msk1;
1433#else /*Sudden_Underflow}{*/
1434#ifdef Avoid_Underflow
1435 if (scale) {
1436 L = word0(&rv) & Exp_mask;
1437 if (L <= (2 * P + 1) * Exp_msk1) {
1438 if (L > (P + 2) * Exp_msk1)
1439 /* round even ==> */
1440 /* accept rv */
1441 break;
1442 /* rv = smallest denormal */
1443 goto undfl;
1444 }
1445 }
1446#endif /*Avoid_Underflow*/
1447 L = (word0(&rv) & Exp_mask) - Exp_msk1;
1448#endif /*Sudden_Underflow}}*/
1449 word0(&rv) = L | Bndry_mask1;
1450 word1(&rv) = 0xffffffff;
1451 break;
1452 }
1453 if (!(word1(&rv) & LSB))
1454 break;
1455 if (dsign)
1456 dval(&rv) += ulp(&rv);
1457 else {
1458 dval(&rv) -= ulp(&rv);
1459#ifndef Sudden_Underflow
1460 if (!dval(&rv))
1461 goto undfl;
1462#endif
1463 }
1464#ifdef Avoid_Underflow
1465 dsign = 1 - dsign;
1466#endif
1467 break;
1468 }
1469 if ((aadj = ratio(delta, bs)) <= 2.) {
1470 if (dsign)
1471 aadj = aadj1 = 1.;
1472 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
1473#ifndef Sudden_Underflow
1474 if (word1(&rv) == Tiny1 && !word0(&rv))
1475 goto undfl;
1476#endif
1477 aadj = 1.;
1478 aadj1 = -1.;
1479 } else {
1480 /* special case -- power of FLT_RADIX to be */
1481 /* rounded down... */
1482
1483 if (aadj < 2. / FLT_RADIX)
1484 aadj = 1. / FLT_RADIX;
1485 else
1486 aadj *= 0.5;
1487 aadj1 = -aadj;
1488 }
1489 } else {
1490 aadj *= 0.5;
1491 aadj1 = dsign ? aadj : -aadj;
1492#ifdef Check_FLT_ROUNDS
1493 switch (Rounding) {
1494 case 2: /* towards +infinity */
1495 aadj1 -= 0.5;
1496 break;
1497 case 0: /* towards 0 */
1498 case 3: /* towards -infinity */
1499 aadj1 += 0.5;
1500 }
1501#else
1502 if (!Flt_Rounds)
1503 aadj1 += 0.5;
1504#endif /*Check_FLT_ROUNDS*/
1505 }
1506 y = word0(&rv) & Exp_mask;
1507
1508 /* Check for overflow */
1509
1510 if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) {
1511 dval(&rv0) = dval(&rv);
1512 word0(&rv) -= P * Exp_msk1;
1513 adj.d = aadj1 * ulp(&rv);
1514 dval(&rv) += adj.d;
1515 if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P)) {
1516 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
1517 goto ovfl;
1518 word0(&rv) = Big0;
1519 word1(&rv) = Big1;
1520 goto cont;
1521 }
1522 word0(&rv) += P * Exp_msk1;
1523 } else {
1524#ifdef Avoid_Underflow
1525 if (scale && y <= 2 * P * Exp_msk1) {
1526 if (aadj <= 0x7fffffff) {
1527 if ((z = (uint32_t)aadj) <= 0)
1528 z = 1;
1529 aadj = z;
1530 aadj1 = dsign ? aadj : -aadj;
1531 }
1532 dval(&aadj2) = aadj1;
1533 word0(&aadj2) += (2 * P + 1) * Exp_msk1 - y;
1534 aadj1 = dval(&aadj2);
1535 }
1536 adj.d = aadj1 * ulp(&rv);
1537 dval(&rv) += adj.d;
1538#else
1539#ifdef Sudden_Underflow
1540 if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) {
1541 dval(&rv0) = dval(&rv);
1542 word0(&rv) += P * Exp_msk1;
1543 adj.d = aadj1 * ulp(&rv);
1544 dval(&rv) += adj.d;
1545 if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) {
1546 if (word0(&rv0) == Tiny0 && word1(&rv0) == Tiny1)
1547 goto undfl;
1548 word0(&rv) = Tiny0;
1549 word1(&rv) = Tiny1;
1550 goto cont;
1551 }
1552 word0(&rv) -= P * Exp_msk1;
1553 } else {
1554 adj.d = aadj1 * ulp(&rv);
1555 dval(&rv) += adj.d;
1556 }
1557#else /*Sudden_Underflow*/
1558 /* Compute adj so that the IEEE rounding rules will
1559 * correctly round rv + adj in some half-way cases.
1560 * If rv * ulp(rv) is denormalized (i.e.,
1561 * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
1562 * trouble from bits lost to denormalization;
1563 * example: 1.2e-307 .
1564 */
1565 if (y <= (P - 1) * Exp_msk1 && aadj > 1.) {
1566 aadj1 = (double)(int)(aadj + 0.5);
1567 if (!dsign)
1568 aadj1 = -aadj1;
1569 }
1570 adj.d = aadj1 * ulp(&rv);
1571 dval(&rv) += adj.d;
1572#endif /*Sudden_Underflow*/
1573#endif /*Avoid_Underflow*/
1574 }
1575 z = word0(&rv) & Exp_mask;
1576#ifndef SET_INEXACT
1577#ifdef Avoid_Underflow
1578 if (!scale)
1579#endif
1580 if (y == z) {
1581 /* Can we stop now? */
1582 L = (int32_t)aadj;
1583 aadj -= L;
1584 /* The tolerances below are conservative. */
1585 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
1586 if (aadj < .4999999 || aadj > .5000001)
1587 break;
1588 } else if (aadj < .4999999 / FLT_RADIX)
1589 break;
1590 }
1591#endif
1592cont:
1593 {}
1594 }
1595#ifdef SET_INEXACT
1596 if (inexact) {
1597 if (!oldinexact) {
1598 word0(&rv0) = Exp_1 + (70 << Exp_shift);
1599 word1(&rv0) = 0;
1600 dval(&rv0) += 1.;
1601 }
1602 } else if (!oldinexact)
1603 clear_inexact();
1604#endif
1605#ifdef Avoid_Underflow
1606 if (scale) {
1607 word0(&rv0) = Exp_1 - 2 * P * Exp_msk1;
1608 word1(&rv0) = 0;
1609 dval(&rv) *= dval(&rv0);
1610#ifndef NO_ERRNO
1611 /* try to avoid the bug of testing an 8087 register value */
1612 if (!word0(&rv) && !word1(&rv))
1613 errno = ERANGE;
1614#endif
1615 }
1616#endif /* Avoid_Underflow */
1617#ifdef SET_INEXACT
1618 if (inexact && !(word0(&rv) & Exp_mask)) {
1619 /* set underflow bit */
1620 dval(&rv0) = 1e-300;
1621 dval(&rv0) *= dval(&rv0);
1622 }
1623#endif
1624ret:
1625 if (se)
1626 *se = const_cast<char*>(s);
1627 return sign ? -dval(&rv) : dval(&rv);
1628}
1629
1630static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
1631{
1632 size_t n;
1633 uint32_t* bx;
1634 uint32_t* bxe;
1635 uint32_t q;
1636 uint32_t* sx;
1637 uint32_t* sxe;
1638#ifdef USE_LONG_LONG
1639 unsigned long long borrow, carry, y, ys;
1640#else
1641 uint32_t borrow, carry, y, ys;
1642#ifdef Pack_32
1643 uint32_t si, z, zs;
1644#endif
1645#endif
1646 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
1647 ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
1648
1649 n = S.size();
1650 ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
1651 if (b.size() < n)
1652 return 0;
1653 sx = S.words();
1654 sxe = sx + --n;
1655 bx = b.words();
1656 bxe = bx + n;
1657 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1658 ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
1659 if (q) {
1660 borrow = 0;
1661 carry = 0;
1662 do {
1663#ifdef USE_LONG_LONG
1664 ys = *sx++ * (unsigned long long)q + carry;
1665 carry = ys >> 32;
1666 y = *bx - (ys & 0xffffffffUL) - borrow;
1667 borrow = y >> 32 & (uint32_t)1;
1668 *bx++ = (uint32_t)y & 0xffffffffUL;
1669#else
1670#ifdef Pack_32
1671 si = *sx++;
1672 ys = (si & 0xffff) * q + carry;
1673 zs = (si >> 16) * q + (ys >> 16);
1674 carry = zs >> 16;
1675 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1676 borrow = (y & 0x10000) >> 16;
1677 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1678 borrow = (z & 0x10000) >> 16;
1679 bx = storeInc(bx, z, y);
1680#else
1681 ys = *sx++ * q + carry;
1682 carry = ys >> 16;
1683 y = *bx - (ys & 0xffff) - borrow;
1684 borrow = (y & 0x10000) >> 16;
1685 *bx++ = y & 0xffff;
1686#endif
1687#endif
1688 } while (sx <= sxe);
1689 if (!*bxe) {
1690 bx = b.words();
1691 while (--bxe > bx && !*bxe)
1692 --n;
1693 b.resize(n);
1694 }
1695 }
1696 if (cmp(b, S) >= 0) {
1697 q++;
1698 borrow = 0;
1699 carry = 0;
1700 bx = b.words();
1701 sx = S.words();
1702 do {
1703#ifdef USE_LONG_LONG
1704 ys = *sx++ + carry;
1705 carry = ys >> 32;
1706 y = *bx - (ys & 0xffffffffUL) - borrow;
1707 borrow = y >> 32 & (uint32_t)1;
1708 *bx++ = (uint32_t)y & 0xffffffffUL;
1709#else
1710#ifdef Pack_32
1711 si = *sx++;
1712 ys = (si & 0xffff) + carry;
1713 zs = (si >> 16) + (ys >> 16);
1714 carry = zs >> 16;
1715 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1716 borrow = (y & 0x10000) >> 16;
1717 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1718 borrow = (z & 0x10000) >> 16;
1719 bx = storeInc(bx, z, y);
1720#else
1721 ys = *sx++ + carry;
1722 carry = ys >> 16;
1723 y = *bx - (ys & 0xffff) - borrow;
1724 borrow = (y & 0x10000) >> 16;
1725 *bx++ = y & 0xffff;
1726#endif
1727#endif
1728 } while (sx <= sxe);
1729 bx = b.words();
1730 bxe = bx + n;
1731 if (!*bxe) {
1732 while (--bxe > bx && !*bxe)
1733 --n;
1734 b.resize(n);
1735 }
1736 }
1737 return q;
1738}
1739
1740/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1741 *
1742 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1743 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1744 *
1745 * Modifications:
1746 * 1. Rather than iterating, we use a simple numeric overestimate
1747 * to determine k = floor(log10(d)). We scale relevant
1748 * quantities using O(log2(k)) rather than O(k) multiplications.
1749 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1750 * try to generate digits strictly left to right. Instead, we
1751 * compute with fewer bits and propagate the carry if necessary
1752 * when rounding the final digit up. This is often faster.
1753 * 3. Under the assumption that input will be rounded nearest,
1754 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1755 * That is, we allow equality in stopping tests when the
1756 * round-nearest rule will give the same floating-point value
1757 * as would satisfaction of the stopping test with strict
1758 * inequality.
1759 * 4. We remove common factors of powers of 2 from relevant
1760 * quantities.
1761 * 5. When converting floating-point integers less than 1e16,
1762 * we use floating-point arithmetic rather than resorting
1763 * to multiple-precision integers.
1764 * 6. When asked to produce fewer than 15 digits, we first try
1765 * to get by with floating-point arithmetic; we resort to
1766 * multiple-precision integer arithmetic only if we cannot
1767 * guarantee that the floating-point calculation has given
1768 * the correctly rounded result. For k requested digits and
1769 * "uniformly" distributed input, the probability is
1770 * something like 10^(k-15) that we must resort to the int32_t
1771 * calculation.
1772 */
1773
1774void dtoa(DtoaBuffer result, double dd, int ndigits, int* decpt, int* sign, char** rve)
1775{
1776 /*
1777 Arguments ndigits, decpt, sign are similar to those
1778 of ecvt and fcvt; trailing zeros are suppressed from
1779 the returned string. If not null, *rve is set to point
1780 to the end of the return value. If d is +-Infinity or NaN,
1781 then *decpt is set to 9999.
1782
1783 */
1784
1785 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
1786 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1787 spec_case, try_quick;
1788 int32_t L;
1789#ifndef Sudden_Underflow
1790 int denorm;
1791 uint32_t x;
1792#endif
1793 BigInt b, b1, delta, mlo, mhi, S;
1794 U d2, eps, u;
1795 double ds;
1796 char* s;
1797 char* s0;
1798#ifdef SET_INEXACT
1799 int inexact, oldinexact;
1800#endif
1801
1802 u.d = dd;
1803 if (word0(&u) & Sign_bit) {
1804 /* set sign for everything, including 0's and NaNs */
1805 *sign = 1;
1806 word0(&u) &= ~Sign_bit; /* clear sign bit */
1807 } else
1808 *sign = 0;
1809
1810 if ((word0(&u) & Exp_mask) == Exp_mask) {
1811 /* Infinity or NaN */
1812 *decpt = 9999;
1813 if (!word1(&u) && !(word0(&u) & 0xfffff)) {
1814 strcpy(result, "Infinity");
1815 if (rve)
1816 *rve = result + 8;
1817 } else {
1818 strcpy(result, "NaN");
1819 if (rve)
1820 *rve = result + 3;
1821 }
1822 return;
1823 }
1824 if (!dval(&u)) {
1825 *decpt = 1;
1826 result[0] = '0';
1827 result[1] = '\0';
1828 if (rve)
1829 *rve = result + 1;
1830 return;
1831 }
1832
1833#ifdef SET_INEXACT
1834 try_quick = oldinexact = get_inexact();
1835 inexact = 1;
1836#endif
1837
1838 d2b(b, &u, &be, &bbits);
1839#ifdef Sudden_Underflow
1840 i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
1841#else
1842 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
1843#endif
1844 dval(&d2) = dval(&u);
1845 word0(&d2) &= Frac_mask1;
1846 word0(&d2) |= Exp_11;
1847
1848 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1849 * log10(x) = log(x) / log(10)
1850 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1851 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1852 *
1853 * This suggests computing an approximation k to log10(d) by
1854 *
1855 * k = (i - Bias)*0.301029995663981
1856 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1857 *
1858 * We want k to be too large rather than too small.
1859 * The error in the first-order Taylor series approximation
1860 * is in our favor, so we just round up the constant enough
1861 * to compensate for any error in the multiplication of
1862 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1863 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1864 * adding 1e-13 to the constant term more than suffices.
1865 * Hence we adjust the constant term to 0.1760912590558.
1866 * (We could get a more accurate k by invoking log10,
1867 * but this is probably not worthwhile.)
1868 */
1869
1870 i -= Bias;
1871#ifndef Sudden_Underflow
1872 denorm = 0;
1873 } else {
1874 /* d is denormalized */
1875
1876 i = bbits + be + (Bias + (P - 1) - 1);
1877 x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
1878 : word1(&u) << (32 - i);
1879 dval(&d2) = x;
1880 word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
1881 i -= (Bias + (P - 1) - 1) + 1;
1882 denorm = 1;
1883 }
1884#endif
1885 ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
1886 k = (int)ds;
1887 if (ds < 0. && ds != k)
1888 k--; /* want k = floor(ds) */
1889 k_check = 1;
1890 if (k >= 0 && k <= Ten_pmax) {
1891 if (dval(&u) < tens[k])
1892 k--;
1893 k_check = 0;
1894 }
1895 j = bbits - i - 1;
1896 if (j >= 0) {
1897 b2 = 0;
1898 s2 = j;
1899 } else {
1900 b2 = -j;
1901 s2 = 0;
1902 }
1903 if (k >= 0) {
1904 b5 = 0;
1905 s5 = k;
1906 s2 += k;
1907 } else {
1908 b2 -= k;
1909 b5 = -k;
1910 s5 = 0;
1911 }
1912
1913#ifndef SET_INEXACT
1914#ifdef Check_FLT_ROUNDS
1915 try_quick = Rounding == 1;
1916#else
1917 try_quick = 1;
1918#endif
1919#endif /*SET_INEXACT*/
1920
1921 leftright = 1;
1922 ilim = ilim1 = -1;
1923 i = 18;
1924 ndigits = 0;
1925 s = s0 = result;
1926
1927 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
1928
1929 /* Try to get by with floating-point arithmetic. */
1930
1931 i = 0;
1932 dval(&d2) = dval(&u);
1933 k0 = k;
1934 ilim0 = ilim;
1935 ieps = 2; /* conservative */
1936 if (k > 0) {
1937 ds = tens[k & 0xf];
1938 j = k >> 4;
1939 if (j & Bletch) {
1940 /* prevent overflows */
1941 j &= Bletch - 1;
1942 dval(&u) /= bigtens[n_bigtens - 1];
1943 ieps++;
1944 }
1945 for (; j; j >>= 1, i++) {
1946 if (j & 1) {
1947 ieps++;
1948 ds *= bigtens[i];
1949 }
1950 }
1951 dval(&u) /= ds;
1952 } else if ((j1 = -k)) {
1953 dval(&u) *= tens[j1 & 0xf];
1954 for (j = j1 >> 4; j; j >>= 1, i++) {
1955 if (j & 1) {
1956 ieps++;
1957 dval(&u) *= bigtens[i];
1958 }
1959 }
1960 }
1961 if (k_check && dval(&u) < 1. && ilim > 0) {
1962 if (ilim1 <= 0)
1963 goto fastFailed;
1964 ilim = ilim1;
1965 k--;
1966 dval(&u) *= 10.;
1967 ieps++;
1968 }
1969 dval(&eps) = (ieps * dval(&u)) + 7.;
1970 word0(&eps) -= (P - 1) * Exp_msk1;
1971 if (!ilim) {
1972 S.clear();
1973 mhi.clear();
1974 dval(&u) -= 5.;
1975 if (dval(&u) > dval(&eps))
1976 goto oneDigit;
1977 if (dval(&u) < -dval(&eps))
1978 goto noDigits;
1979 goto fastFailed;
1980 }
1981#ifndef No_leftright
1982 if (leftright) {
1983 /* Use Steele & White method of only
1984 * generating digits needed.
1985 */
1986 dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
1987 for (i = 0;;) {
1988 L = (long int)dval(&u);
1989 dval(&u) -= L;
1990 *s++ = '0' + (int)L;
1991 if (dval(&u) < dval(&eps))
1992 goto ret;
1993 if (1. - dval(&u) < dval(&eps))
1994 goto bumpUp;
1995 if (++i >= ilim)
1996 break;
1997 dval(&eps) *= 10.;
1998 dval(&u) *= 10.;
1999 }
2000 } else {
2001#endif
2002 /* Generate ilim digits, then fix them up. */
2003 dval(&eps) *= tens[ilim - 1];
2004 for (i = 1;; i++, dval(&u) *= 10.) {
2005 L = (int32_t)(dval(&u));
2006 if (!(dval(&u) -= L))
2007 ilim = i;
2008 *s++ = '0' + (int)L;
2009 if (i == ilim) {
2010 if (dval(&u) > 0.5 + dval(&eps))
2011 goto bumpUp;
2012 if (dval(&u) < 0.5 - dval(&eps)) {
2013 while (*--s == '0') { }
2014 s++;
2015 goto ret;
2016 }
2017 break;
2018 }
2019 }
2020#ifndef No_leftright
2021 }
2022#endif
2023fastFailed:
2024 s = s0;
2025 dval(&u) = dval(&d2);
2026 k = k0;
2027 ilim = ilim0;
2028 }
2029
2030 /* Do we have a "small" integer? */
2031
2032 if (be >= 0 && k <= Int_max) {
2033 /* Yes. */
2034 ds = tens[k];
2035 if (ndigits < 0 && ilim <= 0) {
2036 S.clear();
2037 mhi.clear();
2038 if (ilim < 0 || dval(&u) <= 5 * ds)
2039 goto noDigits;
2040 goto oneDigit;
2041 }
2042 for (i = 1;; i++, dval(&u) *= 10.) {
2043 L = (int32_t)(dval(&u) / ds);
2044 dval(&u) -= L * ds;
2045#ifdef Check_FLT_ROUNDS
2046 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2047 if (dval(&u) < 0) {
2048 L--;
2049 dval(&u) += ds;
2050 }
2051#endif
2052 *s++ = '0' + (int)L;
2053 if (!dval(&u)) {
2054#ifdef SET_INEXACT
2055 inexact = 0;
2056#endif
2057 break;
2058 }
2059 if (i == ilim) {
2060 dval(&u) += dval(&u);
2061 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
2062bumpUp:
2063 while (*--s == '9')
2064 if (s == s0) {
2065 k++;
2066 *s = '0';
2067 break;
2068 }
2069 ++*s++;
2070 }
2071 break;
2072 }
2073 }
2074 goto ret;
2075 }
2076
2077 m2 = b2;
2078 m5 = b5;
2079 mhi.clear();
2080 mlo.clear();
2081 if (leftright) {
2082 i =
2083#ifndef Sudden_Underflow
2084 denorm ? be + (Bias + (P - 1) - 1 + 1) :
2085#endif
2086 1 + P - bbits;
2087 b2 += i;
2088 s2 += i;
2089 i2b(mhi, 1);
2090 }
2091 if (m2 > 0 && s2 > 0) {
2092 i = m2 < s2 ? m2 : s2;
2093 b2 -= i;
2094 m2 -= i;
2095 s2 -= i;
2096 }
2097 if (b5 > 0) {
2098 if (leftright) {
2099 if (m5 > 0) {
2100 pow5mult(mhi, m5);
2101 mult(b, mhi);
2102 }
2103 if ((j = b5 - m5))
2104 pow5mult(b, j);
2105 } else
2106 pow5mult(b, b5);
2107 }
2108 i2b(S, 1);
2109 if (s5 > 0)
2110 pow5mult(S, s5);
2111
2112 /* Check for special case that d is a normalized power of 2. */
2113
2114 spec_case = 0;
2115 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2116#ifndef Sudden_Underflow
2117 && word0(&u) & (Exp_mask & ~Exp_msk1)
2118#endif
2119 ) {
2120 /* The special case */
2121 b2 += Log2P;
2122 s2 += Log2P;
2123 spec_case = 1;
2124 }
2125
2126 /* Arrange for convenient computation of quotients:
2127 * shift left if necessary so divisor has 4 leading 0 bits.
2128 *
2129 * Perhaps we should just compute leading 28 bits of S once
2130 * and for all and pass them and a shift to quorem, so it
2131 * can do shifts and ors to compute the numerator for q.
2132 */
2133#ifdef Pack_32
2134 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
2135 i = 32 - i;
2136#else
2137 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0xf))
2138 i = 16 - i;
2139#endif
2140 if (i > 4) {
2141 i -= 4;
2142 b2 += i;
2143 m2 += i;
2144 s2 += i;
2145 } else if (i < 4) {
2146 i += 28;
2147 b2 += i;
2148 m2 += i;
2149 s2 += i;
2150 }
2151 if (b2 > 0)
2152 lshift(b, b2);
2153 if (s2 > 0)
2154 lshift(S, s2);
2155 if (k_check) {
2156 if (cmp(b, S) < 0) {
2157 k--;
2158 multadd(b, 10, 0); /* we botched the k estimate */
2159 if (leftright)
2160 multadd(mhi, 10, 0);
2161 ilim = ilim1;
2162 }
2163 }
2164
2165 if (leftright) {
2166 if (m2 > 0)
2167 lshift(mhi, m2);
2168
2169 /* Compute mlo -- check for special case
2170 * that d is a normalized power of 2.
2171 */
2172
2173 mlo = mhi;
2174 if (spec_case) {
2175 mhi = mlo;
2176 lshift(mhi, Log2P);
2177 }
2178
2179 for (i = 1;;i++) {
2180 dig = quorem(b, S) + '0';
2181 /* Do we yet have the shortest decimal string
2182 * that will round to d?
2183 */
2184 j = cmp(b, mlo);
2185 diff(delta, S, mhi);
2186 j1 = delta.sign ? 1 : cmp(b, delta);
2187 if (!j1 && !(word1(&u) & 1)) {
2188 if (dig == '9')
2189 goto round9up;
2190 if (j > 0)
2191 dig++;
2192#ifdef SET_INEXACT
2193 else if (!b->x[0] && b->wds <= 1)
2194 inexact = 0;
2195#endif
2196 *s++ = dig;
2197 goto ret;
2198 }
2199 if (j < 0 || (!j && !(word1(&u) & 1))) {
2200 if (!b.words()[0] && b.size() <= 1) {
2201#ifdef SET_INEXACT
2202 inexact = 0;
2203#endif
2204 goto acceptDig;
2205 }
2206 if (j1 > 0) {
2207 lshift(b, 1);
2208 j1 = cmp(b, S);
2209 if ((j1 > 0 || (!j1 && (dig & 1))) && dig++ == '9')
2210 goto round9up;
2211 }
2212acceptDig:
2213 *s++ = dig;
2214 goto ret;
2215 }
2216 if (j1 > 0) {
2217 if (dig == '9') { /* possible if i == 1 */
2218round9up:
2219 *s++ = '9';
2220 goto roundoff;
2221 }
2222 *s++ = dig + 1;
2223 goto ret;
2224 }
2225 *s++ = dig;
2226 if (i == ilim)
2227 break;
2228 multadd(b, 10, 0);
2229 multadd(mlo, 10, 0);
2230 multadd(mhi, 10, 0);
2231 }
2232 } else
2233 for (i = 1;; i++) {
2234 *s++ = dig = quorem(b, S) + '0';
2235 if (!b.words()[0] && b.size() <= 1) {
2236#ifdef SET_INEXACT
2237 inexact = 0;
2238#endif
2239 goto ret;
2240 }
2241 if (i >= ilim)
2242 break;
2243 multadd(b, 10, 0);
2244 }
2245
2246 /* Round off last digit */
2247
2248 lshift(b, 1);
2249 j = cmp(b, S);
2250 if (j > 0 || (!j && (dig & 1))) {
2251roundoff:
2252 while (*--s == '9')
2253 if (s == s0) {
2254 k++;
2255 *s++ = '1';
2256 goto ret;
2257 }
2258 ++*s++;
2259 } else {
2260 while (*--s == '0') { }
2261 s++;
2262 }
2263 goto ret;
2264noDigits:
2265 k = -1 - ndigits;
2266 goto ret;
2267oneDigit:
2268 *s++ = '1';
2269 k++;
2270 goto ret;
2271ret:
2272#ifdef SET_INEXACT
2273 if (inexact) {
2274 if (!oldinexact) {
2275 word0(&u) = Exp_1 + (70 << Exp_shift);
2276 word1(&u) = 0;
2277 dval(&u) += 1.;
2278 }
2279 } else if (!oldinexact)
2280 clear_inexact();
2281#endif
2282 *s = 0;
2283 *decpt = k + 1;
2284 if (rve)
2285 *rve = s;
2286}
2287
2288static ALWAYS_INLINE void append(char*& next, const char* src, unsigned size)
2289{
2290 for (unsigned i = 0; i < size; ++i)
2291 *next++ = *src++;
2292}
2293
2294void doubleToStringInJavaScriptFormat(double d, DtoaBuffer buffer, unsigned* resultLength)
2295{
2296 ASSERT(buffer);
2297
2298 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
2299 if (isnan(d)) {
2300 append(buffer, "NaN", 3);
2301 if (resultLength)
2302 *resultLength = 3;
2303 return;
2304 }
2305 // -0 -> "0"
2306 if (!d) {
2307 buffer[0] = '0';
2308 if (resultLength)
2309 *resultLength = 1;
2310 return;
2311 }
2312
2313 int decimalPoint;
2314 int sign;
2315
2316 DtoaBuffer result;
2317 char* resultEnd = 0;
2318 WTF::dtoa(result, d, 0, &decimalPoint, &sign, &resultEnd);
2319 int length = resultEnd - result;
2320
2321 char* next = buffer;
2322 if (sign)
2323 *next++ = '-';
2324
2325 if (decimalPoint <= 0 && decimalPoint > -6) {
2326 *next++ = '0';
2327 *next++ = '.';
2328 for (int j = decimalPoint; j < 0; j++)
2329 *next++ = '0';
2330 append(next, result, length);
2331 } else if (decimalPoint <= 21 && decimalPoint > 0) {
2332 if (length <= decimalPoint) {
2333 append(next, result, length);
2334 for (int j = 0; j < decimalPoint - length; j++)
2335 *next++ = '0';
2336 } else {
2337 append(next, result, decimalPoint);
2338 *next++ = '.';
2339 append(next, result + decimalPoint, length - decimalPoint);
2340 }
2341 } else if (result[0] < '0' || result[0] > '9')
2342 append(next, result, length);
2343 else {
2344 *next++ = result[0];
2345 if (length > 1) {
2346 *next++ = '.';
2347 append(next, result + 1, length - 1);
2348 }
2349
2350 *next++ = 'e';
2351 *next++ = (decimalPoint >= 0) ? '+' : '-';
2352 // decimalPoint can't be more than 3 digits decimal given the
2353 // nature of float representation
2354 int exponential = decimalPoint - 1;
2355 if (exponential < 0)
2356 exponential = -exponential;
2357 if (exponential >= 100)
2358 *next++ = static_cast<char>('0' + exponential / 100);
2359 if (exponential >= 10)
2360 *next++ = static_cast<char>('0' + (exponential % 100) / 10);
2361 *next++ = static_cast<char>('0' + exponential % 10);
2362 }
2363 if (resultLength)
2364 *resultLength = next - buffer;
2365}
2366
2367} // namespace WTF
Note: See TracBrowser for help on using the repository browser.