blob: aa577f2ec2b7f7637b407c149395cb272a590194 [file] [log] [blame]
Mark Dickinsonbb282852009-10-24 12:13:30 +00001/****************************************************************
2 *
3 * The author of this software is David M. Gay.
4 *
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
12 *
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17 *
18 ***************************************************************/
19
20/****************************************************************
21 * This is dtoa.c by David M. Gay, downloaded from
22 * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
23 * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
24 *
25 * Please remember to check http://www.netlib.org/fp regularly (and especially
26 * before any Python release) for bugfixes and updates.
27 *
28 * The major modifications from Gay's original code are as follows:
29 *
30 * 0. The original code has been specialized to Python's needs by removing
31 * many of the #ifdef'd sections. In particular, code to support VAX and
32 * IBM floating-point formats, hex NaNs, hex floats, locale-aware
33 * treatment of the decimal point, and setting of the inexact flag have
34 * been removed.
35 *
36 * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
37 *
38 * 2. The public functions strtod, dtoa and freedtoa all now have
39 * a _Py_dg_ prefix.
40 *
41 * 3. Instead of assuming that PyMem_Malloc always succeeds, we thread
42 * PyMem_Malloc failures through the code. The functions
43 *
44 * Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
45 *
46 * of return type *Bigint all return NULL to indicate a malloc failure.
47 * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
48 * failure. bigcomp now has return type int (it used to be void) and
49 * returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL
50 * on failure. _Py_dg_strtod indicates failure due to malloc failure
51 * by returning -1.0, setting errno=ENOMEM and *se to s00.
52 *
53 * 4. The static variable dtoa_result has been removed. Callers of
54 * _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free
55 * the memory allocated by _Py_dg_dtoa.
56 *
57 * 5. The code has been reformatted to better fit with Python's
58 * C style guide (PEP 7).
59 *
60 * 6. A bug in the memory allocation has been fixed: to avoid FREEing memory
61 * that hasn't been MALLOC'ed, private_mem should only be used when k <=
62 * Kmax.
63 *
64 * 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
65 * leading whitespace.
66 *
67 ***************************************************************/
68
69/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
70 * at acm dot org, with " at " changed at "@" and " dot " changed to ".").
71 * Please report bugs for this modified version using the Python issue tracker
72 * (http://bugs.python.org). */
73
74/* On a machine with IEEE extended-precision registers, it is
75 * necessary to specify double-precision (53-bit) rounding precision
76 * before invoking strtod or dtoa. If the machine uses (the equivalent
77 * of) Intel 80x87 arithmetic, the call
78 * _control87(PC_53, MCW_PC);
79 * does this with many compilers. Whether this or another call is
80 * appropriate depends on the compiler; for this to work, it may be
81 * necessary to #include "float.h" or another system-dependent header
82 * file.
83 */
84
85/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
86 *
87 * This strtod returns a nearest machine number to the input decimal
88 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
89 * broken by the IEEE round-even rule. Otherwise ties are broken by
90 * biased rounding (add half and chop).
91 *
92 * Inspired loosely by William D. Clinger's paper "How to Read Floating
93 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
94 *
95 * Modifications:
96 *
97 * 1. We only require IEEE, IBM, or VAX double-precision
98 * arithmetic (not IEEE double-extended).
99 * 2. We get by with floating-point arithmetic in a case that
100 * Clinger missed -- when we're computing d * 10^n
101 * for a small integer d and the integer n is not too
102 * much larger than 22 (the maximum integer k for which
103 * we can represent 10^k exactly), we may be able to
104 * compute (d*10^k) * 10^(e-k) with just one roundoff.
105 * 3. Rather than a bit-at-a-time adjustment of the binary
106 * result in the hard case, we use floating-point
107 * arithmetic to determine the adjustment to within
108 * one bit; only in really hard cases do we need to
109 * compute a second residual.
110 * 4. Because of 3., we don't need a large table of powers of 10
111 * for ten-to-e (just some small tables, e.g. of 10^k
112 * for 0 <= k <= 22).
113 */
114
115/* Linking of Python's #defines to Gay's #defines starts here. */
116
117#include "Python.h"
118
Mark Dickinsonbb282852009-10-24 12:13:30 +0000119/* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
120 the following code */
121#ifndef PY_NO_SHORT_FLOAT_REPR
122
123#include "float.h"
124
125#define MALLOC PyMem_Malloc
126#define FREE PyMem_Free
127
128/* This code should also work for ARM mixed-endian format on little-endian
129 machines, where doubles have byte order 45670123 (in increasing address
130 order, 0 being the least significant byte). */
131#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
132# define IEEE_8087
133#endif
134#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
135 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
136# define IEEE_MC68k
137#endif
138#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
139#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
140#endif
141
142/* The code below assumes that the endianness of integers matches the
143 endianness of the two 32-bit words of a double. Check this. */
144#if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
145 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
146#error "doubles and ints have incompatible endianness"
147#endif
148
149#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
150#error "doubles and ints have incompatible endianness"
151#endif
152
153
154#if defined(HAVE_UINT32_T) && defined(HAVE_INT32_T)
155typedef PY_UINT32_T ULong;
156typedef PY_INT32_T Long;
157#else
158#error "Failed to find an exact-width 32-bit integer type"
159#endif
160
161#if defined(HAVE_UINT64_T)
162#define ULLong PY_UINT64_T
163#else
164#undef ULLong
165#endif
166
167#undef DEBUG
168#ifdef Py_DEBUG
169#define DEBUG
170#endif
171
172/* End Python #define linking */
173
174#ifdef DEBUG
175#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
176#endif
177
178#ifndef PRIVATE_MEM
179#define PRIVATE_MEM 2304
180#endif
181#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
182static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
183
184#ifdef __cplusplus
185extern "C" {
186#endif
187
188typedef union { double d; ULong L[2]; } U;
189
190#ifdef IEEE_8087
191#define word0(x) (x)->L[1]
192#define word1(x) (x)->L[0]
193#else
194#define word0(x) (x)->L[0]
195#define word1(x) (x)->L[1]
196#endif
197#define dval(x) (x)->d
198
199#ifndef STRTOD_DIGLIM
200#define STRTOD_DIGLIM 40
201#endif
202
Mark Dickinson0ca74522010-01-11 17:15:13 +0000203/* maximum permitted exponent value for strtod; exponents larger than
204 MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
205 should fit into an int. */
206#ifndef MAX_ABS_EXP
207#define MAX_ABS_EXP 19999U
208#endif
209
Mark Dickinsonbb282852009-10-24 12:13:30 +0000210/* The following definition of Storeinc is appropriate for MIPS processors.
211 * An alternative that might be better on some machines is
212 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
213 */
214#if defined(IEEE_8087)
215#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
216 ((unsigned short *)a)[0] = (unsigned short)c, a++)
217#else
218#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
219 ((unsigned short *)a)[1] = (unsigned short)c, a++)
220#endif
221
222/* #define P DBL_MANT_DIG */
223/* Ten_pmax = floor(P*log(2)/log(5)) */
224/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
225/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
226/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
227
228#define Exp_shift 20
229#define Exp_shift1 20
230#define Exp_msk1 0x100000
231#define Exp_msk11 0x100000
232#define Exp_mask 0x7ff00000
233#define P 53
234#define Nbits 53
235#define Bias 1023
236#define Emax 1023
237#define Emin (-1022)
238#define Exp_1 0x3ff00000
239#define Exp_11 0x3ff00000
240#define Ebits 11
241#define Frac_mask 0xfffff
242#define Frac_mask1 0xfffff
243#define Ten_pmax 22
244#define Bletch 0x10
245#define Bndry_mask 0xfffff
246#define Bndry_mask1 0xfffff
247#define LSB 1
248#define Sign_bit 0x80000000
249#define Log2P 1
250#define Tiny0 0
251#define Tiny1 1
252#define Quick_max 14
253#define Int_max 14
254
255#ifndef Flt_Rounds
256#ifdef FLT_ROUNDS
257#define Flt_Rounds FLT_ROUNDS
258#else
259#define Flt_Rounds 1
260#endif
261#endif /*Flt_Rounds*/
262
263#define Rounding Flt_Rounds
264
265#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
266#define Big1 0xffffffff
267
268/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
269
270typedef struct BCinfo BCinfo;
271struct
272BCinfo {
Mark Dickinsond2a99402010-01-13 22:20:10 +0000273 int dsign, e0, nd, nd0, scale;
Mark Dickinsonbb282852009-10-24 12:13:30 +0000274};
275
276#define FFFFFFFF 0xffffffffUL
277
278#define Kmax 7
279
280/* struct Bigint is used to represent arbitrary-precision integers. These
281 integers are stored in sign-magnitude format, with the magnitude stored as
282 an array of base 2**32 digits. Bigints are always normalized: if x is a
283 Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
284
285 The Bigint fields are as follows:
286
287 - next is a header used by Balloc and Bfree to keep track of lists
288 of freed Bigints; it's also used for the linked list of
289 powers of 5 of the form 5**2**i used by pow5mult.
290 - k indicates which pool this Bigint was allocated from
291 - maxwds is the maximum number of words space was allocated for
292 (usually maxwds == 2**k)
293 - sign is 1 for negative Bigints, 0 for positive. The sign is unused
294 (ignored on inputs, set to 0 on outputs) in almost all operations
295 involving Bigints: a notable exception is the diff function, which
296 ignores signs on inputs but sets the sign of the output correctly.
297 - wds is the actual number of significant words
298 - x contains the vector of words (digits) for this Bigint, from least
299 significant (x[0]) to most significant (x[wds-1]).
300*/
301
302struct
303Bigint {
304 struct Bigint *next;
305 int k, maxwds, sign, wds;
306 ULong x[1];
307};
308
309typedef struct Bigint Bigint;
310
311/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
312 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
313 1 << k. These pools are maintained as linked lists, with freelist[k]
314 pointing to the head of the list for pool k.
315
316 On allocation, if there's no free slot in the appropriate pool, MALLOC is
317 called to get more memory. This memory is not returned to the system until
318 Python quits. There's also a private memory pool that's allocated from
319 in preference to using MALLOC.
320
321 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
322 decimal digits), memory is directly allocated using MALLOC, and freed using
323 FREE.
324
325 XXX: it would be easy to bypass this memory-management system and
326 translate each call to Balloc into a call to PyMem_Malloc, and each
327 Bfree to PyMem_Free. Investigate whether this has any significant
328 performance on impact. */
329
330static Bigint *freelist[Kmax+1];
331
332/* Allocate space for a Bigint with up to 1<<k digits */
333
334static Bigint *
335Balloc(int k)
336{
337 int x;
338 Bigint *rv;
339 unsigned int len;
340
341 if (k <= Kmax && (rv = freelist[k]))
342 freelist[k] = rv->next;
343 else {
344 x = 1 << k;
345 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
346 /sizeof(double);
347 if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
348 rv = (Bigint*)pmem_next;
349 pmem_next += len;
350 }
351 else {
352 rv = (Bigint*)MALLOC(len*sizeof(double));
353 if (rv == NULL)
354 return NULL;
355 }
356 rv->k = k;
357 rv->maxwds = x;
358 }
359 rv->sign = rv->wds = 0;
360 return rv;
361}
362
363/* Free a Bigint allocated with Balloc */
364
365static void
366Bfree(Bigint *v)
367{
368 if (v) {
369 if (v->k > Kmax)
370 FREE((void*)v);
371 else {
372 v->next = freelist[v->k];
373 freelist[v->k] = v;
374 }
375 }
376}
377
378#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
379 y->wds*sizeof(Long) + 2*sizeof(int))
380
381/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
382 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
383 On failure, return NULL. In this case, b will have been already freed. */
384
385static Bigint *
386multadd(Bigint *b, int m, int a) /* multiply by m and add a */
387{
388 int i, wds;
389#ifdef ULLong
390 ULong *x;
391 ULLong carry, y;
392#else
393 ULong carry, *x, y;
394 ULong xi, z;
395#endif
396 Bigint *b1;
397
398 wds = b->wds;
399 x = b->x;
400 i = 0;
401 carry = a;
402 do {
403#ifdef ULLong
404 y = *x * (ULLong)m + carry;
405 carry = y >> 32;
406 *x++ = (ULong)(y & FFFFFFFF);
407#else
408 xi = *x;
409 y = (xi & 0xffff) * m + carry;
410 z = (xi >> 16) * m + (y >> 16);
411 carry = z >> 16;
412 *x++ = (z << 16) + (y & 0xffff);
413#endif
414 }
415 while(++i < wds);
416 if (carry) {
417 if (wds >= b->maxwds) {
418 b1 = Balloc(b->k+1);
419 if (b1 == NULL){
420 Bfree(b);
421 return NULL;
422 }
423 Bcopy(b1, b);
424 Bfree(b);
425 b = b1;
426 }
427 b->x[wds++] = (ULong)carry;
428 b->wds = wds;
429 }
430 return b;
431}
432
433/* convert a string s containing nd decimal digits (possibly containing a
434 decimal separator at position nd0, which is ignored) to a Bigint. This
435 function carries on where the parsing code in _Py_dg_strtod leaves off: on
436 entry, y9 contains the result of converting the first 9 digits. Returns
437 NULL on failure. */
438
439static Bigint *
Mark Dickinsond2a99402010-01-13 22:20:10 +0000440s2b(const char *s, int nd0, int nd, ULong y9)
Mark Dickinsonbb282852009-10-24 12:13:30 +0000441{
442 Bigint *b;
443 int i, k;
444 Long x, y;
445
446 x = (nd + 8) / 9;
447 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
448 b = Balloc(k);
449 if (b == NULL)
450 return NULL;
451 b->x[0] = y9;
452 b->wds = 1;
453
Mark Dickinsond2a99402010-01-13 22:20:10 +0000454 if (nd <= 9)
455 return b;
456
457 s += 9;
458 for (i = 9; i < nd0; i++) {
459 b = multadd(b, 10, *s++ - '0');
460 if (b == NULL)
461 return NULL;
Mark Dickinsonbb282852009-10-24 12:13:30 +0000462 }
Mark Dickinsond2a99402010-01-13 22:20:10 +0000463 s++;
Mark Dickinsonbb282852009-10-24 12:13:30 +0000464 for(; i < nd; i++) {
465 b = multadd(b, 10, *s++ - '0');
466 if (b == NULL)
467 return NULL;
468 }
469 return b;
470}
471
472/* count leading 0 bits in the 32-bit integer x. */
473
474static int
475hi0bits(ULong x)
476{
477 int k = 0;
478
479 if (!(x & 0xffff0000)) {
480 k = 16;
481 x <<= 16;
482 }
483 if (!(x & 0xff000000)) {
484 k += 8;
485 x <<= 8;
486 }
487 if (!(x & 0xf0000000)) {
488 k += 4;
489 x <<= 4;
490 }
491 if (!(x & 0xc0000000)) {
492 k += 2;
493 x <<= 2;
494 }
495 if (!(x & 0x80000000)) {
496 k++;
497 if (!(x & 0x40000000))
498 return 32;
499 }
500 return k;
501}
502
503/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
504 number of bits. */
505
506static int
507lo0bits(ULong *y)
508{
509 int k;
510 ULong x = *y;
511
512 if (x & 7) {
513 if (x & 1)
514 return 0;
515 if (x & 2) {
516 *y = x >> 1;
517 return 1;
518 }
519 *y = x >> 2;
520 return 2;
521 }
522 k = 0;
523 if (!(x & 0xffff)) {
524 k = 16;
525 x >>= 16;
526 }
527 if (!(x & 0xff)) {
528 k += 8;
529 x >>= 8;
530 }
531 if (!(x & 0xf)) {
532 k += 4;
533 x >>= 4;
534 }
535 if (!(x & 0x3)) {
536 k += 2;
537 x >>= 2;
538 }
539 if (!(x & 1)) {
540 k++;
541 x >>= 1;
542 if (!x)
543 return 32;
544 }
545 *y = x;
546 return k;
547}
548
549/* convert a small nonnegative integer to a Bigint */
550
551static Bigint *
552i2b(int i)
553{
554 Bigint *b;
555
556 b = Balloc(1);
557 if (b == NULL)
558 return NULL;
559 b->x[0] = i;
560 b->wds = 1;
561 return b;
562}
563
564/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
565 the signs of a and b. */
566
567static Bigint *
568mult(Bigint *a, Bigint *b)
569{
570 Bigint *c;
571 int k, wa, wb, wc;
572 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
573 ULong y;
574#ifdef ULLong
575 ULLong carry, z;
576#else
577 ULong carry, z;
578 ULong z2;
579#endif
580
581 if (a->wds < b->wds) {
582 c = a;
583 a = b;
584 b = c;
585 }
586 k = a->k;
587 wa = a->wds;
588 wb = b->wds;
589 wc = wa + wb;
590 if (wc > a->maxwds)
591 k++;
592 c = Balloc(k);
593 if (c == NULL)
594 return NULL;
595 for(x = c->x, xa = x + wc; x < xa; x++)
596 *x = 0;
597 xa = a->x;
598 xae = xa + wa;
599 xb = b->x;
600 xbe = xb + wb;
601 xc0 = c->x;
602#ifdef ULLong
603 for(; xb < xbe; xc0++) {
604 if ((y = *xb++)) {
605 x = xa;
606 xc = xc0;
607 carry = 0;
608 do {
609 z = *x++ * (ULLong)y + *xc + carry;
610 carry = z >> 32;
611 *xc++ = (ULong)(z & FFFFFFFF);
612 }
613 while(x < xae);
614 *xc = (ULong)carry;
615 }
616 }
617#else
618 for(; xb < xbe; xb++, xc0++) {
619 if (y = *xb & 0xffff) {
620 x = xa;
621 xc = xc0;
622 carry = 0;
623 do {
624 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
625 carry = z >> 16;
626 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
627 carry = z2 >> 16;
628 Storeinc(xc, z2, z);
629 }
630 while(x < xae);
631 *xc = carry;
632 }
633 if (y = *xb >> 16) {
634 x = xa;
635 xc = xc0;
636 carry = 0;
637 z2 = *xc;
638 do {
639 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
640 carry = z >> 16;
641 Storeinc(xc, z, z2);
642 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
643 carry = z2 >> 16;
644 }
645 while(x < xae);
646 *xc = z2;
647 }
648 }
649#endif
650 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
651 c->wds = wc;
652 return c;
653}
654
655/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
656
657static Bigint *p5s;
658
659/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
660 failure; if the returned pointer is distinct from b then the original
661 Bigint b will have been Bfree'd. Ignores the sign of b. */
662
663static Bigint *
664pow5mult(Bigint *b, int k)
665{
666 Bigint *b1, *p5, *p51;
667 int i;
668 static int p05[3] = { 5, 25, 125 };
669
670 if ((i = k & 3)) {
671 b = multadd(b, p05[i-1], 0);
672 if (b == NULL)
673 return NULL;
674 }
675
676 if (!(k >>= 2))
677 return b;
678 p5 = p5s;
679 if (!p5) {
680 /* first time */
681 p5 = i2b(625);
682 if (p5 == NULL) {
683 Bfree(b);
684 return NULL;
685 }
686 p5s = p5;
687 p5->next = 0;
688 }
689 for(;;) {
690 if (k & 1) {
691 b1 = mult(b, p5);
692 Bfree(b);
693 b = b1;
694 if (b == NULL)
695 return NULL;
696 }
697 if (!(k >>= 1))
698 break;
699 p51 = p5->next;
700 if (!p51) {
701 p51 = mult(p5,p5);
702 if (p51 == NULL) {
703 Bfree(b);
704 return NULL;
705 }
706 p51->next = 0;
707 p5->next = p51;
708 }
709 p5 = p51;
710 }
711 return b;
712}
713
714/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
715 or NULL on failure. If the returned pointer is distinct from b then the
716 original b will have been Bfree'd. Ignores the sign of b. */
717
718static Bigint *
719lshift(Bigint *b, int k)
720{
721 int i, k1, n, n1;
722 Bigint *b1;
723 ULong *x, *x1, *xe, z;
724
725 n = k >> 5;
726 k1 = b->k;
727 n1 = n + b->wds + 1;
728 for(i = b->maxwds; n1 > i; i <<= 1)
729 k1++;
730 b1 = Balloc(k1);
731 if (b1 == NULL) {
732 Bfree(b);
733 return NULL;
734 }
735 x1 = b1->x;
736 for(i = 0; i < n; i++)
737 *x1++ = 0;
738 x = b->x;
739 xe = x + b->wds;
740 if (k &= 0x1f) {
741 k1 = 32 - k;
742 z = 0;
743 do {
744 *x1++ = *x << k | z;
745 z = *x++ >> k1;
746 }
747 while(x < xe);
748 if ((*x1 = z))
749 ++n1;
750 }
751 else do
752 *x1++ = *x++;
753 while(x < xe);
754 b1->wds = n1 - 1;
755 Bfree(b);
756 return b1;
757}
758
759/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
760 1 if a > b. Ignores signs of a and b. */
761
762static int
763cmp(Bigint *a, Bigint *b)
764{
765 ULong *xa, *xa0, *xb, *xb0;
766 int i, j;
767
768 i = a->wds;
769 j = b->wds;
770#ifdef DEBUG
771 if (i > 1 && !a->x[i-1])
772 Bug("cmp called with a->x[a->wds-1] == 0");
773 if (j > 1 && !b->x[j-1])
774 Bug("cmp called with b->x[b->wds-1] == 0");
775#endif
776 if (i -= j)
777 return i;
778 xa0 = a->x;
779 xa = xa0 + j;
780 xb0 = b->x;
781 xb = xb0 + j;
782 for(;;) {
783 if (*--xa != *--xb)
784 return *xa < *xb ? -1 : 1;
785 if (xa <= xa0)
786 break;
787 }
788 return 0;
789}
790
791/* Take the difference of Bigints a and b, returning a new Bigint. Returns
792 NULL on failure. The signs of a and b are ignored, but the sign of the
793 result is set appropriately. */
794
795static Bigint *
796diff(Bigint *a, Bigint *b)
797{
798 Bigint *c;
799 int i, wa, wb;
800 ULong *xa, *xae, *xb, *xbe, *xc;
801#ifdef ULLong
802 ULLong borrow, y;
803#else
804 ULong borrow, y;
805 ULong z;
806#endif
807
808 i = cmp(a,b);
809 if (!i) {
810 c = Balloc(0);
811 if (c == NULL)
812 return NULL;
813 c->wds = 1;
814 c->x[0] = 0;
815 return c;
816 }
817 if (i < 0) {
818 c = a;
819 a = b;
820 b = c;
821 i = 1;
822 }
823 else
824 i = 0;
825 c = Balloc(a->k);
826 if (c == NULL)
827 return NULL;
828 c->sign = i;
829 wa = a->wds;
830 xa = a->x;
831 xae = xa + wa;
832 wb = b->wds;
833 xb = b->x;
834 xbe = xb + wb;
835 xc = c->x;
836 borrow = 0;
837#ifdef ULLong
838 do {
839 y = (ULLong)*xa++ - *xb++ - borrow;
840 borrow = y >> 32 & (ULong)1;
841 *xc++ = (ULong)(y & FFFFFFFF);
842 }
843 while(xb < xbe);
844 while(xa < xae) {
845 y = *xa++ - borrow;
846 borrow = y >> 32 & (ULong)1;
847 *xc++ = (ULong)(y & FFFFFFFF);
848 }
849#else
850 do {
851 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
852 borrow = (y & 0x10000) >> 16;
853 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
854 borrow = (z & 0x10000) >> 16;
855 Storeinc(xc, z, y);
856 }
857 while(xb < xbe);
858 while(xa < xae) {
859 y = (*xa & 0xffff) - borrow;
860 borrow = (y & 0x10000) >> 16;
861 z = (*xa++ >> 16) - borrow;
862 borrow = (z & 0x10000) >> 16;
863 Storeinc(xc, z, y);
864 }
865#endif
866 while(!*--xc)
867 wa--;
868 c->wds = wa;
869 return c;
870}
871
872/* Given a positive normal double x, return the difference between x and the next
873 double up. Doesn't give correct results for subnormals. */
874
875static double
876ulp(U *x)
877{
878 Long L;
879 U u;
880
881 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
882 word0(&u) = L;
883 word1(&u) = 0;
884 return dval(&u);
885}
886
887/* Convert a Bigint to a double plus an exponent */
888
889static double
890b2d(Bigint *a, int *e)
891{
892 ULong *xa, *xa0, w, y, z;
893 int k;
894 U d;
895
896 xa0 = a->x;
897 xa = xa0 + a->wds;
898 y = *--xa;
899#ifdef DEBUG
900 if (!y) Bug("zero y in b2d");
901#endif
902 k = hi0bits(y);
903 *e = 32 - k;
904 if (k < Ebits) {
905 word0(&d) = Exp_1 | y >> (Ebits - k);
906 w = xa > xa0 ? *--xa : 0;
907 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
908 goto ret_d;
909 }
910 z = xa > xa0 ? *--xa : 0;
911 if (k -= Ebits) {
912 word0(&d) = Exp_1 | y << k | z >> (32 - k);
913 y = xa > xa0 ? *--xa : 0;
914 word1(&d) = z << k | y >> (32 - k);
915 }
916 else {
917 word0(&d) = Exp_1 | y;
918 word1(&d) = z;
919 }
920 ret_d:
921 return dval(&d);
922}
923
924/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
925
926 Given a finite nonzero double d, return an odd Bigint b and exponent *e
927 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
Mark Dickinson2bcd1772010-01-04 21:32:02 +0000928 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
Mark Dickinsonbb282852009-10-24 12:13:30 +0000929
930 If d is zero, then b == 0, *e == -1010, *bbits = 0.
931 */
932
933
934static Bigint *
935d2b(U *d, int *e, int *bits)
936{
937 Bigint *b;
938 int de, k;
939 ULong *x, y, z;
940 int i;
941
942 b = Balloc(1);
943 if (b == NULL)
944 return NULL;
945 x = b->x;
946
947 z = word0(d) & Frac_mask;
948 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
949 if ((de = (int)(word0(d) >> Exp_shift)))
950 z |= Exp_msk1;
951 if ((y = word1(d))) {
952 if ((k = lo0bits(&y))) {
953 x[0] = y | z << (32 - k);
954 z >>= k;
955 }
956 else
957 x[0] = y;
958 i =
959 b->wds = (x[1] = z) ? 2 : 1;
960 }
961 else {
962 k = lo0bits(&z);
963 x[0] = z;
964 i =
965 b->wds = 1;
966 k += 32;
967 }
968 if (de) {
969 *e = de - Bias - (P-1) + k;
970 *bits = P - k;
971 }
972 else {
973 *e = de - Bias - (P-1) + 1 + k;
974 *bits = 32*i - hi0bits(x[i-1]);
975 }
976 return b;
977}
978
979/* Compute the ratio of two Bigints, as a double. The result may have an
980 error of up to 2.5 ulps. */
981
982static double
983ratio(Bigint *a, 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 k = ka - kb + 32*(a->wds - b->wds);
991 if (k > 0)
992 word0(&da) += k*Exp_msk1;
993 else {
994 k = -k;
995 word0(&db) += k*Exp_msk1;
996 }
997 return dval(&da) / dval(&db);
998}
999
1000static const double
1001tens[] = {
1002 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1003 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1004 1e20, 1e21, 1e22
1005};
1006
1007static const double
1008bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1009static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1010 9007199254740992.*9007199254740992.e-256
1011 /* = 2^106 * 1e-256 */
1012};
1013/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1014/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1015#define Scale_Bit 0x10
1016#define n_bigtens 5
1017
1018#define ULbits 32
1019#define kshift 5
1020#define kmask 31
1021
1022
1023static int
1024dshift(Bigint *b, int p2)
1025{
1026 int rv = hi0bits(b->x[b->wds-1]) - 4;
1027 if (p2 > 0)
1028 rv -= p2;
1029 return rv & kmask;
1030}
1031
1032/* special case of Bigint division. The quotient is always in the range 0 <=
1033 quotient < 10, and on entry the divisor S is normalized so that its top 4
1034 bits (28--31) are zero and bit 27 is set. */
1035
1036static int
1037quorem(Bigint *b, Bigint *S)
1038{
1039 int n;
1040 ULong *bx, *bxe, q, *sx, *sxe;
1041#ifdef ULLong
1042 ULLong borrow, carry, y, ys;
1043#else
1044 ULong borrow, carry, y, ys;
1045 ULong si, z, zs;
1046#endif
1047
1048 n = S->wds;
1049#ifdef DEBUG
1050 /*debug*/ if (b->wds > n)
1051 /*debug*/ Bug("oversize b in quorem");
1052#endif
1053 if (b->wds < n)
1054 return 0;
1055 sx = S->x;
1056 sxe = sx + --n;
1057 bx = b->x;
1058 bxe = bx + n;
1059 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1060#ifdef DEBUG
1061 /*debug*/ if (q > 9)
1062 /*debug*/ Bug("oversized quotient in quorem");
1063#endif
1064 if (q) {
1065 borrow = 0;
1066 carry = 0;
1067 do {
1068#ifdef ULLong
1069 ys = *sx++ * (ULLong)q + carry;
1070 carry = ys >> 32;
1071 y = *bx - (ys & FFFFFFFF) - borrow;
1072 borrow = y >> 32 & (ULong)1;
1073 *bx++ = (ULong)(y & FFFFFFFF);
1074#else
1075 si = *sx++;
1076 ys = (si & 0xffff) * q + carry;
1077 zs = (si >> 16) * q + (ys >> 16);
1078 carry = zs >> 16;
1079 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1080 borrow = (y & 0x10000) >> 16;
1081 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1082 borrow = (z & 0x10000) >> 16;
1083 Storeinc(bx, z, y);
1084#endif
1085 }
1086 while(sx <= sxe);
1087 if (!*bxe) {
1088 bx = b->x;
1089 while(--bxe > bx && !*bxe)
1090 --n;
1091 b->wds = n;
1092 }
1093 }
1094 if (cmp(b, S) >= 0) {
1095 q++;
1096 borrow = 0;
1097 carry = 0;
1098 bx = b->x;
1099 sx = S->x;
1100 do {
1101#ifdef ULLong
1102 ys = *sx++ + carry;
1103 carry = ys >> 32;
1104 y = *bx - (ys & FFFFFFFF) - borrow;
1105 borrow = y >> 32 & (ULong)1;
1106 *bx++ = (ULong)(y & FFFFFFFF);
1107#else
1108 si = *sx++;
1109 ys = (si & 0xffff) + carry;
1110 zs = (si >> 16) + (ys >> 16);
1111 carry = zs >> 16;
1112 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1113 borrow = (y & 0x10000) >> 16;
1114 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1115 borrow = (z & 0x10000) >> 16;
1116 Storeinc(bx, z, y);
1117#endif
1118 }
1119 while(sx <= sxe);
1120 bx = b->x;
1121 bxe = bx + n;
1122 if (!*bxe) {
1123 while(--bxe > bx && !*bxe)
1124 --n;
1125 b->wds = n;
1126 }
1127 }
1128 return q;
1129}
1130
Mark Dickinson5818e012010-01-13 19:02:37 +00001131/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001132
Mark Dickinson5818e012010-01-13 19:02:37 +00001133 Assuming that x is finite and nonnegative (positive zero is fine
1134 here) and x / 2^bc.scale is exactly representable as a double,
1135 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001136
1137static double
1138sulp(U *x, BCinfo *bc)
1139{
1140 U u;
1141
Mark Dickinson02139d72010-01-13 22:15:53 +00001142 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001143 /* rv/2^bc->scale is subnormal */
1144 word0(&u) = (P+2)*Exp_msk1;
1145 word1(&u) = 0;
1146 return u.d;
1147 }
Mark Dickinson5818e012010-01-13 19:02:37 +00001148 else {
1149 assert(word0(x) || word1(x)); /* x != 0.0 */
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001150 return ulp(x);
Mark Dickinson5818e012010-01-13 19:02:37 +00001151 }
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001152}
Mark Dickinsonbb282852009-10-24 12:13:30 +00001153
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001154/* The bigcomp function handles some hard cases for strtod, for inputs
1155 with more than STRTOD_DIGLIM digits. It's called once an initial
1156 estimate for the double corresponding to the input string has
1157 already been obtained by the code in _Py_dg_strtod.
1158
1159 The bigcomp function is only called after _Py_dg_strtod has found a
1160 double value rv such that either rv or rv + 1ulp represents the
1161 correctly rounded value corresponding to the original string. It
1162 determines which of these two values is the correct one by
1163 computing the decimal digits of rv + 0.5ulp and comparing them with
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001164 the corresponding digits of s0.
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001165
1166 In the following, write dv for the absolute value of the number represented
1167 by the input string.
1168
1169 Inputs:
1170
1171 s0 points to the first significant digit of the input string.
1172
1173 rv is a (possibly scaled) estimate for the closest double value to the
1174 value represented by the original input to _Py_dg_strtod. If
1175 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1176 the input value.
1177
1178 bc is a struct containing information gathered during the parsing and
1179 estimation steps of _Py_dg_strtod. Description of fields follows:
1180
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001181 bc->dsign is 1 if rv < decimal value, 0 if rv >= decimal value. In
1182 normal use, it should almost always be 1 when bigcomp is entered.
1183
1184 bc->e0 gives the exponent of the input value, such that dv = (integer
1185 given by the bd->nd digits of s0) * 10**e0
1186
Mark Dickinsond2a99402010-01-13 22:20:10 +00001187 bc->nd gives the total number of significant digits of s0. It will
1188 be at least 1.
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001189
1190 bc->nd0 gives the number of significant digits of s0 before the
1191 decimal separator. If there's no decimal separator, bc->nd0 ==
1192 bc->nd.
1193
1194 bc->scale is the value used to scale rv to avoid doing arithmetic with
1195 subnormal values. It's either 0 or 2*P (=106).
1196
1197 Outputs:
1198
1199 On successful exit, rv/2^(bc->scale) is the closest double to dv.
1200
1201 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001202
1203static int
1204bigcomp(U *rv, const char *s0, BCinfo *bc)
1205{
1206 Bigint *b, *d;
Mark Dickinson50b60c62010-01-14 13:14:49 +00001207 int b2, bbits, d2, dd, i, nd, nd0, odd, p2, p5;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001208
Mark Dickinsond2a99402010-01-13 22:20:10 +00001209 dd = 0; /* silence compiler warning about possibly unused variable */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001210 nd = bc->nd;
1211 nd0 = bc->nd0;
Mark Dickinson8efef5c2010-01-12 22:23:56 +00001212 p5 = nd + bc->e0;
Mark Dickinsond2a99402010-01-13 22:20:10 +00001213 if (rv->d == 0.) {
1214 /* special case because d2b doesn't handle 0.0 */
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001215 b = i2b(0);
Mark Dickinsonbb282852009-10-24 12:13:30 +00001216 if (b == NULL)
1217 return -1;
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001218 p2 = Emin - P + 1; /* = -1074 for IEEE 754 binary64 */
1219 bbits = 0;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001220 }
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001221 else {
Mark Dickinsonbb282852009-10-24 12:13:30 +00001222 b = d2b(rv, &p2, &bbits);
1223 if (b == NULL)
1224 return -1;
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001225 p2 -= bc->scale;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001226 }
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001227 /* now rv/2^(bc->scale) = b * 2**p2, and b has bbits significant bits */
1228
1229 /* Replace (b, p2) by (b << i, p2 - i), with i the largest integer such
1230 that b << i has at most P significant bits and p2 - i >= Emin - P +
1231 1. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001232 i = P - bbits;
Mark Dickinsond2a99402010-01-13 22:20:10 +00001233 if (i > p2 - (Emin - P + 1))
1234 i = p2 - (Emin - P + 1);
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001235 /* increment i so that we shift b by an extra bit; then or-ing a 1 into
1236 the lsb of b gives us rv/2^(bc->scale) + 0.5ulp. */
1237 b = lshift(b, ++i);
1238 if (b == NULL)
1239 return -1;
Mark Dickinson50b60c62010-01-14 13:14:49 +00001240 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1241 case, this is used for round to even. */
1242 odd = b->x[0] & 2;
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001243 b->x[0] |= 1;
1244
Mark Dickinsonbb282852009-10-24 12:13:30 +00001245 p2 -= p5 + i;
1246 d = i2b(1);
1247 if (d == NULL) {
1248 Bfree(b);
1249 return -1;
1250 }
1251 /* Arrange for convenient computation of quotients:
1252 * shift left if necessary so divisor has 4 leading 0 bits.
1253 */
1254 if (p5 > 0) {
1255 d = pow5mult(d, p5);
1256 if (d == NULL) {
1257 Bfree(b);
1258 return -1;
1259 }
1260 }
1261 else if (p5 < 0) {
1262 b = pow5mult(b, -p5);
1263 if (b == NULL) {
1264 Bfree(d);
1265 return -1;
1266 }
1267 }
1268 if (p2 > 0) {
1269 b2 = p2;
1270 d2 = 0;
1271 }
1272 else {
1273 b2 = 0;
1274 d2 = -p2;
1275 }
1276 i = dshift(d, d2);
1277 if ((b2 += i) > 0) {
1278 b = lshift(b, b2);
1279 if (b == NULL) {
1280 Bfree(d);
1281 return -1;
1282 }
1283 }
1284 if ((d2 += i) > 0) {
1285 d = lshift(d, d2);
1286 if (d == NULL) {
1287 Bfree(b);
1288 return -1;
1289 }
1290 }
1291
Mark Dickinsond2a99402010-01-13 22:20:10 +00001292 /* if b >= d, round down */
Mark Dickinson8efef5c2010-01-12 22:23:56 +00001293 if (cmp(b, d) >= 0) {
1294 dd = -1;
1295 goto ret;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001296 }
Mark Dickinson50b60c62010-01-14 13:14:49 +00001297
Mark Dickinsonbb282852009-10-24 12:13:30 +00001298 /* Compare b/d with s0 */
Mark Dickinsond2a99402010-01-13 22:20:10 +00001299 for(i = 0; i < nd0; i++) {
1300 b = multadd(b, 10, 0);
1301 if (b == NULL) {
1302 Bfree(d);
1303 return -1;
1304 }
1305 dd = *s0++ - '0' - quorem(b, d);
1306 if (dd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001307 goto ret;
1308 if (!b->x[0] && b->wds == 1) {
Mark Dickinson03774fa2010-01-14 13:02:36 +00001309 if (i < nd - 1)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001310 dd = 1;
1311 goto ret;
1312 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001313 }
Mark Dickinsond2a99402010-01-13 22:20:10 +00001314 s0++;
1315 for(; i < nd; i++) {
1316 b = multadd(b, 10, 0);
1317 if (b == NULL) {
1318 Bfree(d);
1319 return -1;
1320 }
1321 dd = *s0++ - '0' - quorem(b, d);
1322 if (dd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001323 goto ret;
1324 if (!b->x[0] && b->wds == 1) {
Mark Dickinson03774fa2010-01-14 13:02:36 +00001325 if (i < nd - 1)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001326 dd = 1;
1327 goto ret;
1328 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001329 }
1330 if (b->x[0] || b->wds > 1)
1331 dd = -1;
1332 ret:
1333 Bfree(b);
1334 Bfree(d);
Mark Dickinson50b60c62010-01-14 13:14:49 +00001335 if (dd > 0 || (dd == 0 && odd))
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001336 dval(rv) += sulp(rv, bc);
Mark Dickinsonbb282852009-10-24 12:13:30 +00001337 return 0;
1338}
1339
1340double
1341_Py_dg_strtod(const char *s00, char **se)
1342{
Mark Dickinson476279f2010-01-16 10:44:00 +00001343 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1, error;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001344 int esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1345 const char *s, *s0, *s1;
1346 double aadj, aadj1;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001347 U aadj2, adj, rv, rv0;
Mark Dickinson0ca74522010-01-11 17:15:13 +00001348 ULong y, z, L;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001349 BCinfo bc;
1350 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1351
Mark Dickinson476279f2010-01-16 10:44:00 +00001352 sign = nz0 = nz = 0;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001353 dval(&rv) = 0.;
1354 for(s = s00;;s++) switch(*s) {
1355 case '-':
1356 sign = 1;
1357 /* no break */
1358 case '+':
1359 if (*++s)
1360 goto break2;
1361 /* no break */
1362 case 0:
1363 goto ret0;
1364 /* modify original dtoa.c so that it doesn't accept leading whitespace
1365 case '\t':
1366 case '\n':
1367 case '\v':
1368 case '\f':
1369 case '\r':
1370 case ' ':
1371 continue;
1372 */
1373 default:
1374 goto break2;
1375 }
1376 break2:
1377 if (*s == '0') {
1378 nz0 = 1;
1379 while(*++s == '0') ;
1380 if (!*s)
1381 goto ret;
1382 }
1383 s0 = s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001384 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
Mark Dickinson811ff822010-01-16 17:57:49 +00001385 ;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001386 nd0 = nd;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001387 if (c == '.') {
1388 c = *++s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001389 if (!nd) {
1390 for(; c == '0'; c = *++s)
1391 nz++;
1392 if (c > '0' && c <= '9') {
1393 s0 = s;
1394 nf += nz;
1395 nz = 0;
1396 goto have_dig;
1397 }
1398 goto dig_done;
1399 }
1400 for(; c >= '0' && c <= '9'; c = *++s) {
1401 have_dig:
1402 nz++;
1403 if (c -= '0') {
1404 nf += nz;
Mark Dickinson811ff822010-01-16 17:57:49 +00001405 nd += nz;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001406 nz = 0;
1407 }
1408 }
1409 }
1410 dig_done:
1411 e = 0;
1412 if (c == 'e' || c == 'E') {
1413 if (!nd && !nz && !nz0) {
1414 goto ret0;
1415 }
1416 s00 = s;
1417 esign = 0;
1418 switch(c = *++s) {
1419 case '-':
1420 esign = 1;
1421 case '+':
1422 c = *++s;
1423 }
1424 if (c >= '0' && c <= '9') {
1425 while(c == '0')
1426 c = *++s;
1427 if (c > '0' && c <= '9') {
1428 L = c - '0';
1429 s1 = s;
1430 while((c = *++s) >= '0' && c <= '9')
1431 L = 10*L + c - '0';
Mark Dickinson0ca74522010-01-11 17:15:13 +00001432 if (s - s1 > 8 || L > MAX_ABS_EXP)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001433 /* Avoid confusion from exponents
1434 * so large that e might overflow.
1435 */
Mark Dickinson0ca74522010-01-11 17:15:13 +00001436 e = (int)MAX_ABS_EXP; /* safe for 16 bit ints */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001437 else
1438 e = (int)L;
1439 if (esign)
1440 e = -e;
1441 }
1442 else
1443 e = 0;
1444 }
1445 else
1446 s = s00;
1447 }
1448 if (!nd) {
1449 if (!nz && !nz0) {
1450 ret0:
1451 s = s00;
1452 sign = 0;
1453 }
1454 goto ret;
1455 }
Mark Dickinson811ff822010-01-16 17:57:49 +00001456 e -= nf;
1457 if (!nd0)
1458 nd0 = nd;
1459
1460 /* strip trailing zeros */
1461 for (i = nd; i > 0; ) {
1462 /* scan back until we hit a nonzero digit. significant digit 'i'
1463 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
1464 --i;
1465 if (s0[i < nd0 ? i : i+1] != '0') {
1466 ++i;
1467 break;
1468 }
1469 }
1470 e += nd - i;
1471 nd = i;
1472 if (nd0 > nd)
1473 nd0 = nd;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001474
1475 /* Now we have nd0 digits, starting at s0, followed by a
1476 * decimal point, followed by nd-nd0 digits. The number we're
1477 * after is the integer represented by those digits times
1478 * 10**e */
1479
Mark Dickinson811ff822010-01-16 17:57:49 +00001480 bc.e0 = e1 = e;
Mark Dickinson476279f2010-01-16 10:44:00 +00001481
1482 /* Summary of parsing results. The parsing stage gives values
Mark Dickinson811ff822010-01-16 17:57:49 +00001483 * s0, nd0, nd, e, sign, where:
Mark Dickinson476279f2010-01-16 10:44:00 +00001484 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001485 * - s0 points to the first significant digit of the input string s00;
Mark Dickinson476279f2010-01-16 10:44:00 +00001486 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001487 * - nd is the total number of significant digits (here, and
1488 * below, 'significant digits' means the set of digits of the
1489 * significand of the input that remain after ignoring leading
1490 * and trailing zeros.
Mark Dickinson476279f2010-01-16 10:44:00 +00001491 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001492 * - nd0 indicates the position of the decimal point (if
1493 * present): so the nd significant digits are in s0[0:nd0] and
1494 * s0[nd0+1:nd+1] using the usual Python half-open slice
1495 * notation. (If nd0 < nd, then s0[nd0] necessarily contains
1496 * a '.' character; if nd0 == nd, then it could be anything.)
Mark Dickinson476279f2010-01-16 10:44:00 +00001497 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001498 * - e is the adjusted exponent: the absolute value of the number
1499 * represented by the original input string is n * 10**e, where
1500 * n is the integer represented by the concatenation of
1501 * s0[0:nd0] and s0[nd0+1:nd+1]
Mark Dickinson476279f2010-01-16 10:44:00 +00001502 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001503 * - sign gives the sign of the input: 1 for negative, 0 for positive
1504 *
1505 * - the first and last significant digits are nonzero
1506 */
1507
1508 /* put first DBL_DIG+1 digits into integer y and z.
Mark Dickinson476279f2010-01-16 10:44:00 +00001509 *
1510 * - y contains the value represented by the first min(9, nd)
1511 * significant digits
1512 *
1513 * - if nd > 9, z contains the value represented by significant digits
1514 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1515 * gives the value represented by the first min(16, nd) sig. digits.
1516 */
1517
Mark Dickinson811ff822010-01-16 17:57:49 +00001518 y = z = 0;
1519 for (i = 0; i < nd; i++) {
1520 if (i < 9)
1521 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1522 else if (i < DBL_DIG+1)
1523 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1524 else
1525 break;
1526 }
1527
Mark Dickinsonbb282852009-10-24 12:13:30 +00001528 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1529 dval(&rv) = y;
1530 if (k > 9) {
1531 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1532 }
1533 bd0 = 0;
1534 if (nd <= DBL_DIG
1535 && Flt_Rounds == 1
1536 ) {
1537 if (!e)
1538 goto ret;
1539 if (e > 0) {
1540 if (e <= Ten_pmax) {
1541 dval(&rv) *= tens[e];
1542 goto ret;
1543 }
1544 i = DBL_DIG - nd;
1545 if (e <= Ten_pmax + i) {
1546 /* A fancier test would sometimes let us do
1547 * this for larger i values.
1548 */
1549 e -= i;
1550 dval(&rv) *= tens[i];
1551 dval(&rv) *= tens[e];
1552 goto ret;
1553 }
1554 }
1555 else if (e >= -Ten_pmax) {
1556 dval(&rv) /= tens[-e];
1557 goto ret;
1558 }
1559 }
1560 e1 += nd - k;
1561
1562 bc.scale = 0;
1563
1564 /* Get starting approximation = rv * 10**e1 */
1565
1566 if (e1 > 0) {
1567 if ((i = e1 & 15))
1568 dval(&rv) *= tens[i];
1569 if (e1 &= ~15) {
1570 if (e1 > DBL_MAX_10_EXP) {
1571 ovfl:
1572 errno = ERANGE;
1573 /* Can't trust HUGE_VAL */
1574 word0(&rv) = Exp_mask;
1575 word1(&rv) = 0;
1576 goto ret;
1577 }
1578 e1 >>= 4;
1579 for(j = 0; e1 > 1; j++, e1 >>= 1)
1580 if (e1 & 1)
1581 dval(&rv) *= bigtens[j];
1582 /* The last multiplication could overflow. */
1583 word0(&rv) -= P*Exp_msk1;
1584 dval(&rv) *= bigtens[j];
1585 if ((z = word0(&rv) & Exp_mask)
1586 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1587 goto ovfl;
1588 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1589 /* set to largest number */
1590 /* (Can't trust DBL_MAX) */
1591 word0(&rv) = Big0;
1592 word1(&rv) = Big1;
1593 }
1594 else
1595 word0(&rv) += P*Exp_msk1;
1596 }
1597 }
1598 else if (e1 < 0) {
1599 e1 = -e1;
1600 if ((i = e1 & 15))
1601 dval(&rv) /= tens[i];
1602 if (e1 >>= 4) {
1603 if (e1 >= 1 << n_bigtens)
1604 goto undfl;
1605 if (e1 & Scale_Bit)
1606 bc.scale = 2*P;
1607 for(j = 0; e1 > 0; j++, e1 >>= 1)
1608 if (e1 & 1)
1609 dval(&rv) *= tinytens[j];
1610 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1611 >> Exp_shift)) > 0) {
1612 /* scaled rv is denormal; clear j low bits */
1613 if (j >= 32) {
1614 word1(&rv) = 0;
1615 if (j >= 53)
1616 word0(&rv) = (P+2)*Exp_msk1;
1617 else
1618 word0(&rv) &= 0xffffffff << (j-32);
1619 }
1620 else
1621 word1(&rv) &= 0xffffffff << j;
1622 }
1623 if (!dval(&rv)) {
1624 undfl:
1625 dval(&rv) = 0.;
1626 errno = ERANGE;
1627 goto ret;
1628 }
1629 }
1630 }
1631
1632 /* Now the hard part -- adjusting rv to the correct value.*/
1633
1634 /* Put digits into bd: true value = bd * 10^e */
1635
1636 bc.nd = nd;
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001637 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001638 /* to silence an erroneous warning about bc.nd0 */
1639 /* possibly not being initialized. */
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001640 if (nd > STRTOD_DIGLIM) {
1641 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001642 /* minimum number of decimal digits to distinguish double values */
1643 /* in IEEE arithmetic. */
Mark Dickinson476279f2010-01-16 10:44:00 +00001644
1645 /* Truncate input to 18 significant digits, then discard any trailing
1646 zeros on the result by updating nd, nd0, e and y suitably. (There's
1647 no need to update z; it's not reused beyond this point.) */
1648 for (i = 18; i > 0; ) {
1649 /* scan back until we hit a nonzero digit. significant digit 'i'
1650 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001651 --i;
Mark Dickinson476279f2010-01-16 10:44:00 +00001652 if (s0[i < nd0 ? i : i+1] != '0') {
1653 ++i;
1654 break;
1655 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001656 }
1657 e += nd - i;
1658 nd = i;
1659 if (nd0 > nd)
1660 nd0 = nd;
1661 if (nd < 9) { /* must recompute y */
1662 y = 0;
1663 for(i = 0; i < nd0; ++i)
1664 y = 10*y + s0[i] - '0';
Mark Dickinson476279f2010-01-16 10:44:00 +00001665 for(; i < nd; ++i)
1666 y = 10*y + s0[i+1] - '0';
Mark Dickinsonbb282852009-10-24 12:13:30 +00001667 }
1668 }
Mark Dickinsond2a99402010-01-13 22:20:10 +00001669 bd0 = s2b(s0, nd0, nd, y);
Mark Dickinsonbb282852009-10-24 12:13:30 +00001670 if (bd0 == NULL)
1671 goto failed_malloc;
1672
1673 for(;;) {
1674 bd = Balloc(bd0->k);
1675 if (bd == NULL) {
1676 Bfree(bd0);
1677 goto failed_malloc;
1678 }
1679 Bcopy(bd, bd0);
1680 bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1681 if (bb == NULL) {
1682 Bfree(bd);
1683 Bfree(bd0);
1684 goto failed_malloc;
1685 }
1686 bs = i2b(1);
1687 if (bs == NULL) {
1688 Bfree(bb);
1689 Bfree(bd);
1690 Bfree(bd0);
1691 goto failed_malloc;
1692 }
1693
1694 if (e >= 0) {
1695 bb2 = bb5 = 0;
1696 bd2 = bd5 = e;
1697 }
1698 else {
1699 bb2 = bb5 = -e;
1700 bd2 = bd5 = 0;
1701 }
1702 if (bbe >= 0)
1703 bb2 += bbe;
1704 else
1705 bd2 -= bbe;
1706 bs2 = bb2;
1707 j = bbe - bc.scale;
1708 i = j + bbbits - 1; /* logb(rv) */
1709 if (i < Emin) /* denormal */
1710 j += P - Emin;
1711 else
1712 j = P + 1 - bbbits;
1713 bb2 += j;
1714 bd2 += j;
1715 bd2 += bc.scale;
1716 i = bb2 < bd2 ? bb2 : bd2;
1717 if (i > bs2)
1718 i = bs2;
1719 if (i > 0) {
1720 bb2 -= i;
1721 bd2 -= i;
1722 bs2 -= i;
1723 }
1724 if (bb5 > 0) {
1725 bs = pow5mult(bs, bb5);
1726 if (bs == NULL) {
1727 Bfree(bb);
1728 Bfree(bd);
1729 Bfree(bd0);
1730 goto failed_malloc;
1731 }
1732 bb1 = mult(bs, bb);
1733 Bfree(bb);
1734 bb = bb1;
1735 if (bb == NULL) {
1736 Bfree(bs);
1737 Bfree(bd);
1738 Bfree(bd0);
1739 goto failed_malloc;
1740 }
1741 }
1742 if (bb2 > 0) {
1743 bb = lshift(bb, bb2);
1744 if (bb == NULL) {
1745 Bfree(bs);
1746 Bfree(bd);
1747 Bfree(bd0);
1748 goto failed_malloc;
1749 }
1750 }
1751 if (bd5 > 0) {
1752 bd = pow5mult(bd, bd5);
1753 if (bd == NULL) {
1754 Bfree(bb);
1755 Bfree(bs);
1756 Bfree(bd0);
1757 goto failed_malloc;
1758 }
1759 }
1760 if (bd2 > 0) {
1761 bd = lshift(bd, bd2);
1762 if (bd == NULL) {
1763 Bfree(bb);
1764 Bfree(bs);
1765 Bfree(bd0);
1766 goto failed_malloc;
1767 }
1768 }
1769 if (bs2 > 0) {
1770 bs = lshift(bs, bs2);
1771 if (bs == NULL) {
1772 Bfree(bb);
1773 Bfree(bd);
1774 Bfree(bd0);
1775 goto failed_malloc;
1776 }
1777 }
1778 delta = diff(bb, bd);
1779 if (delta == NULL) {
1780 Bfree(bb);
1781 Bfree(bs);
1782 Bfree(bd);
1783 Bfree(bd0);
1784 goto failed_malloc;
1785 }
1786 bc.dsign = delta->sign;
1787 delta->sign = 0;
1788 i = cmp(delta, bs);
1789 if (bc.nd > nd && i <= 0) {
1790 if (bc.dsign)
1791 break; /* Must use bigcomp(). */
Mark Dickinsonf8747c12010-01-14 14:40:20 +00001792
1793 /* Here rv overestimates the truncated decimal value by at most
1794 0.5 ulp(rv). Hence rv either overestimates the true decimal
1795 value by <= 0.5 ulp(rv), or underestimates it by some small
1796 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1797 the true decimal value, so it's possible to exit.
1798
1799 Exception: if scaled rv is a normal exact power of 2, but not
1800 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1801 next double, so the correctly rounded result is either rv - 0.5
1802 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1803
1804 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1805 /* rv can't be 0, since it's an overestimate for some
1806 nonzero value. So rv is a normal power of 2. */
1807 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1808 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1809 rv / 2^bc.scale >= 2^-1021. */
1810 if (j - bc.scale >= 2) {
1811 dval(&rv) -= 0.5 * sulp(&rv, &bc);
1812 break;
1813 }
1814 }
1815
Mark Dickinsonbb282852009-10-24 12:13:30 +00001816 {
1817 bc.nd = nd;
1818 i = -1; /* Discarded digits make delta smaller. */
1819 }
1820 }
1821
1822 if (i < 0) {
1823 /* Error is less than half an ulp -- check for
1824 * special case of mantissa a power of two.
1825 */
1826 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
1827 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
1828 ) {
1829 break;
1830 }
1831 if (!delta->x[0] && delta->wds <= 1) {
1832 /* exact result */
1833 break;
1834 }
1835 delta = lshift(delta,Log2P);
1836 if (delta == NULL) {
1837 Bfree(bb);
1838 Bfree(bs);
1839 Bfree(bd);
1840 Bfree(bd0);
1841 goto failed_malloc;
1842 }
1843 if (cmp(delta, bs) > 0)
1844 goto drop_down;
1845 break;
1846 }
1847 if (i == 0) {
1848 /* exactly half-way between */
1849 if (bc.dsign) {
1850 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1851 && word1(&rv) == (
1852 (bc.scale &&
1853 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
1854 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1855 0xffffffff)) {
1856 /*boundary case -- increment exponent*/
1857 word0(&rv) = (word0(&rv) & Exp_mask)
1858 + Exp_msk1
1859 ;
1860 word1(&rv) = 0;
1861 bc.dsign = 0;
1862 break;
1863 }
1864 }
1865 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1866 drop_down:
1867 /* boundary case -- decrement exponent */
1868 if (bc.scale) {
1869 L = word0(&rv) & Exp_mask;
1870 if (L <= (2*P+1)*Exp_msk1) {
1871 if (L > (P+2)*Exp_msk1)
1872 /* round even ==> */
1873 /* accept rv */
1874 break;
1875 /* rv = smallest denormal */
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001876 if (bc.nd >nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001877 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001878 goto undfl;
1879 }
1880 }
1881 L = (word0(&rv) & Exp_mask) - Exp_msk1;
1882 word0(&rv) = L | Bndry_mask1;
1883 word1(&rv) = 0xffffffff;
1884 break;
1885 }
1886 if (!(word1(&rv) & LSB))
1887 break;
1888 if (bc.dsign)
1889 dval(&rv) += ulp(&rv);
1890 else {
1891 dval(&rv) -= ulp(&rv);
1892 if (!dval(&rv)) {
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001893 if (bc.nd >nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001894 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001895 goto undfl;
1896 }
1897 }
1898 bc.dsign = 1 - bc.dsign;
1899 break;
1900 }
1901 if ((aadj = ratio(delta, bs)) <= 2.) {
1902 if (bc.dsign)
1903 aadj = aadj1 = 1.;
1904 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
1905 if (word1(&rv) == Tiny1 && !word0(&rv)) {
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001906 if (bc.nd >nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001907 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001908 goto undfl;
1909 }
1910 aadj = 1.;
1911 aadj1 = -1.;
1912 }
1913 else {
1914 /* special case -- power of FLT_RADIX to be */
1915 /* rounded down... */
1916
1917 if (aadj < 2./FLT_RADIX)
1918 aadj = 1./FLT_RADIX;
1919 else
1920 aadj *= 0.5;
1921 aadj1 = -aadj;
1922 }
1923 }
1924 else {
1925 aadj *= 0.5;
1926 aadj1 = bc.dsign ? aadj : -aadj;
1927 if (Flt_Rounds == 0)
1928 aadj1 += 0.5;
1929 }
1930 y = word0(&rv) & Exp_mask;
1931
1932 /* Check for overflow */
1933
1934 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1935 dval(&rv0) = dval(&rv);
1936 word0(&rv) -= P*Exp_msk1;
1937 adj.d = aadj1 * ulp(&rv);
1938 dval(&rv) += adj.d;
1939 if ((word0(&rv) & Exp_mask) >=
1940 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1941 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
1942 goto ovfl;
1943 word0(&rv) = Big0;
1944 word1(&rv) = Big1;
1945 goto cont;
1946 }
1947 else
1948 word0(&rv) += P*Exp_msk1;
1949 }
1950 else {
1951 if (bc.scale && y <= 2*P*Exp_msk1) {
1952 if (aadj <= 0x7fffffff) {
1953 if ((z = (ULong)aadj) <= 0)
1954 z = 1;
1955 aadj = z;
1956 aadj1 = bc.dsign ? aadj : -aadj;
1957 }
1958 dval(&aadj2) = aadj1;
1959 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
1960 aadj1 = dval(&aadj2);
1961 }
1962 adj.d = aadj1 * ulp(&rv);
1963 dval(&rv) += adj.d;
1964 }
1965 z = word0(&rv) & Exp_mask;
1966 if (bc.nd == nd) {
1967 if (!bc.scale)
1968 if (y == z) {
1969 /* Can we stop now? */
1970 L = (Long)aadj;
1971 aadj -= L;
1972 /* The tolerances below are conservative. */
1973 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
1974 if (aadj < .4999999 || aadj > .5000001)
1975 break;
1976 }
1977 else if (aadj < .4999999/FLT_RADIX)
1978 break;
1979 }
1980 }
1981 cont:
1982 Bfree(bb);
1983 Bfree(bd);
1984 Bfree(bs);
1985 Bfree(delta);
1986 }
1987 Bfree(bb);
1988 Bfree(bd);
1989 Bfree(bs);
1990 Bfree(bd0);
1991 Bfree(delta);
1992 if (bc.nd > nd) {
1993 error = bigcomp(&rv, s0, &bc);
1994 if (error)
1995 goto failed_malloc;
1996 }
1997
1998 if (bc.scale) {
1999 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2000 word1(&rv0) = 0;
2001 dval(&rv) *= dval(&rv0);
2002 /* try to avoid the bug of testing an 8087 register value */
2003 if (!(word0(&rv) & Exp_mask))
2004 errno = ERANGE;
2005 }
2006 ret:
2007 if (se)
2008 *se = (char *)s;
2009 return sign ? -dval(&rv) : dval(&rv);
2010
2011 failed_malloc:
2012 if (se)
2013 *se = (char *)s00;
2014 errno = ENOMEM;
2015 return -1.0;
2016}
2017
2018static char *
2019rv_alloc(int i)
2020{
2021 int j, k, *r;
2022
2023 j = sizeof(ULong);
2024 for(k = 0;
2025 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2026 j <<= 1)
2027 k++;
2028 r = (int*)Balloc(k);
2029 if (r == NULL)
2030 return NULL;
2031 *r = k;
2032 return (char *)(r+1);
2033}
2034
2035static char *
2036nrv_alloc(char *s, char **rve, int n)
2037{
2038 char *rv, *t;
2039
2040 rv = rv_alloc(n);
2041 if (rv == NULL)
2042 return NULL;
2043 t = rv;
2044 while((*t = *s++)) t++;
2045 if (rve)
2046 *rve = t;
2047 return rv;
2048}
2049
2050/* freedtoa(s) must be used to free values s returned by dtoa
2051 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2052 * but for consistency with earlier versions of dtoa, it is optional
2053 * when MULTIPLE_THREADS is not defined.
2054 */
2055
2056void
2057_Py_dg_freedtoa(char *s)
2058{
2059 Bigint *b = (Bigint *)((int *)s - 1);
2060 b->maxwds = 1 << (b->k = *(int*)b);
2061 Bfree(b);
2062}
2063
2064/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2065 *
2066 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2067 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2068 *
2069 * Modifications:
2070 * 1. Rather than iterating, we use a simple numeric overestimate
2071 * to determine k = floor(log10(d)). We scale relevant
2072 * quantities using O(log2(k)) rather than O(k) multiplications.
2073 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2074 * try to generate digits strictly left to right. Instead, we
2075 * compute with fewer bits and propagate the carry if necessary
2076 * when rounding the final digit up. This is often faster.
2077 * 3. Under the assumption that input will be rounded nearest,
2078 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2079 * That is, we allow equality in stopping tests when the
2080 * round-nearest rule will give the same floating-point value
2081 * as would satisfaction of the stopping test with strict
2082 * inequality.
2083 * 4. We remove common factors of powers of 2 from relevant
2084 * quantities.
2085 * 5. When converting floating-point integers less than 1e16,
2086 * we use floating-point arithmetic rather than resorting
2087 * to multiple-precision integers.
2088 * 6. When asked to produce fewer than 15 digits, we first try
2089 * to get by with floating-point arithmetic; we resort to
2090 * multiple-precision integer arithmetic only if we cannot
2091 * guarantee that the floating-point calculation has given
2092 * the correctly rounded result. For k requested digits and
2093 * "uniformly" distributed input, the probability is
2094 * something like 10^(k-15) that we must resort to the Long
2095 * calculation.
2096 */
2097
2098/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2099 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2100 call to _Py_dg_freedtoa. */
2101
2102char *
2103_Py_dg_dtoa(double dd, int mode, int ndigits,
2104 int *decpt, int *sign, char **rve)
2105{
2106 /* Arguments ndigits, decpt, sign are similar to those
2107 of ecvt and fcvt; trailing zeros are suppressed from
2108 the returned string. If not null, *rve is set to point
2109 to the end of the return value. If d is +-Infinity or NaN,
2110 then *decpt is set to 9999.
2111
2112 mode:
2113 0 ==> shortest string that yields d when read in
2114 and rounded to nearest.
2115 1 ==> like 0, but with Steele & White stopping rule;
2116 e.g. with IEEE P754 arithmetic , mode 0 gives
2117 1e23 whereas mode 1 gives 9.999999999999999e22.
2118 2 ==> max(1,ndigits) significant digits. This gives a
2119 return value similar to that of ecvt, except
2120 that trailing zeros are suppressed.
2121 3 ==> through ndigits past the decimal point. This
2122 gives a return value similar to that from fcvt,
2123 except that trailing zeros are suppressed, and
2124 ndigits can be negative.
2125 4,5 ==> similar to 2 and 3, respectively, but (in
2126 round-nearest mode) with the tests of mode 0 to
2127 possibly return a shorter string that rounds to d.
2128 With IEEE arithmetic and compilation with
2129 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2130 as modes 2 and 3 when FLT_ROUNDS != 1.
2131 6-9 ==> Debugging modes similar to mode - 4: don't try
2132 fast floating-point estimate (if applicable).
2133
2134 Values of mode other than 0-9 are treated as mode 0.
2135
2136 Sufficient space is allocated to the return value
2137 to hold the suppressed trailing zeros.
2138 */
2139
2140 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2141 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2142 spec_case, try_quick;
2143 Long L;
2144 int denorm;
2145 ULong x;
2146 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2147 U d2, eps, u;
2148 double ds;
2149 char *s, *s0;
2150
2151 /* set pointers to NULL, to silence gcc compiler warnings and make
2152 cleanup easier on error */
2153 mlo = mhi = b = S = 0;
2154 s0 = 0;
2155
2156 u.d = dd;
2157 if (word0(&u) & Sign_bit) {
2158 /* set sign for everything, including 0's and NaNs */
2159 *sign = 1;
2160 word0(&u) &= ~Sign_bit; /* clear sign bit */
2161 }
2162 else
2163 *sign = 0;
2164
2165 /* quick return for Infinities, NaNs and zeros */
2166 if ((word0(&u) & Exp_mask) == Exp_mask)
2167 {
2168 /* Infinity or NaN */
2169 *decpt = 9999;
2170 if (!word1(&u) && !(word0(&u) & 0xfffff))
2171 return nrv_alloc("Infinity", rve, 8);
2172 return nrv_alloc("NaN", rve, 3);
2173 }
2174 if (!dval(&u)) {
2175 *decpt = 1;
2176 return nrv_alloc("0", rve, 1);
2177 }
2178
2179 /* compute k = floor(log10(d)). The computation may leave k
2180 one too large, but should never leave k too small. */
2181 b = d2b(&u, &be, &bbits);
2182 if (b == NULL)
2183 goto failed_malloc;
2184 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2185 dval(&d2) = dval(&u);
2186 word0(&d2) &= Frac_mask1;
2187 word0(&d2) |= Exp_11;
2188
2189 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2190 * log10(x) = log(x) / log(10)
2191 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2192 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2193 *
2194 * This suggests computing an approximation k to log10(d) by
2195 *
2196 * k = (i - Bias)*0.301029995663981
2197 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2198 *
2199 * We want k to be too large rather than too small.
2200 * The error in the first-order Taylor series approximation
2201 * is in our favor, so we just round up the constant enough
2202 * to compensate for any error in the multiplication of
2203 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2204 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2205 * adding 1e-13 to the constant term more than suffices.
2206 * Hence we adjust the constant term to 0.1760912590558.
2207 * (We could get a more accurate k by invoking log10,
2208 * but this is probably not worthwhile.)
2209 */
2210
2211 i -= Bias;
2212 denorm = 0;
2213 }
2214 else {
2215 /* d is denormalized */
2216
2217 i = bbits + be + (Bias + (P-1) - 1);
2218 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2219 : word1(&u) << (32 - i);
2220 dval(&d2) = x;
2221 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2222 i -= (Bias + (P-1) - 1) + 1;
2223 denorm = 1;
2224 }
2225 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2226 i*0.301029995663981;
2227 k = (int)ds;
2228 if (ds < 0. && ds != k)
2229 k--; /* want k = floor(ds) */
2230 k_check = 1;
2231 if (k >= 0 && k <= Ten_pmax) {
2232 if (dval(&u) < tens[k])
2233 k--;
2234 k_check = 0;
2235 }
2236 j = bbits - i - 1;
2237 if (j >= 0) {
2238 b2 = 0;
2239 s2 = j;
2240 }
2241 else {
2242 b2 = -j;
2243 s2 = 0;
2244 }
2245 if (k >= 0) {
2246 b5 = 0;
2247 s5 = k;
2248 s2 += k;
2249 }
2250 else {
2251 b2 -= k;
2252 b5 = -k;
2253 s5 = 0;
2254 }
2255 if (mode < 0 || mode > 9)
2256 mode = 0;
2257
2258 try_quick = 1;
2259
2260 if (mode > 5) {
2261 mode -= 4;
2262 try_quick = 0;
2263 }
2264 leftright = 1;
2265 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2266 /* silence erroneous "gcc -Wall" warning. */
2267 switch(mode) {
2268 case 0:
2269 case 1:
2270 i = 18;
2271 ndigits = 0;
2272 break;
2273 case 2:
2274 leftright = 0;
2275 /* no break */
2276 case 4:
2277 if (ndigits <= 0)
2278 ndigits = 1;
2279 ilim = ilim1 = i = ndigits;
2280 break;
2281 case 3:
2282 leftright = 0;
2283 /* no break */
2284 case 5:
2285 i = ndigits + k + 1;
2286 ilim = i;
2287 ilim1 = i - 1;
2288 if (i <= 0)
2289 i = 1;
2290 }
2291 s0 = rv_alloc(i);
2292 if (s0 == NULL)
2293 goto failed_malloc;
2294 s = s0;
2295
2296
2297 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2298
2299 /* Try to get by with floating-point arithmetic. */
2300
2301 i = 0;
2302 dval(&d2) = dval(&u);
2303 k0 = k;
2304 ilim0 = ilim;
2305 ieps = 2; /* conservative */
2306 if (k > 0) {
2307 ds = tens[k&0xf];
2308 j = k >> 4;
2309 if (j & Bletch) {
2310 /* prevent overflows */
2311 j &= Bletch - 1;
2312 dval(&u) /= bigtens[n_bigtens-1];
2313 ieps++;
2314 }
2315 for(; j; j >>= 1, i++)
2316 if (j & 1) {
2317 ieps++;
2318 ds *= bigtens[i];
2319 }
2320 dval(&u) /= ds;
2321 }
2322 else if ((j1 = -k)) {
2323 dval(&u) *= tens[j1 & 0xf];
2324 for(j = j1 >> 4; j; j >>= 1, i++)
2325 if (j & 1) {
2326 ieps++;
2327 dval(&u) *= bigtens[i];
2328 }
2329 }
2330 if (k_check && dval(&u) < 1. && ilim > 0) {
2331 if (ilim1 <= 0)
2332 goto fast_failed;
2333 ilim = ilim1;
2334 k--;
2335 dval(&u) *= 10.;
2336 ieps++;
2337 }
2338 dval(&eps) = ieps*dval(&u) + 7.;
2339 word0(&eps) -= (P-1)*Exp_msk1;
2340 if (ilim == 0) {
2341 S = mhi = 0;
2342 dval(&u) -= 5.;
2343 if (dval(&u) > dval(&eps))
2344 goto one_digit;
2345 if (dval(&u) < -dval(&eps))
2346 goto no_digits;
2347 goto fast_failed;
2348 }
2349 if (leftright) {
2350 /* Use Steele & White method of only
2351 * generating digits needed.
2352 */
2353 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2354 for(i = 0;;) {
2355 L = (Long)dval(&u);
2356 dval(&u) -= L;
2357 *s++ = '0' + (int)L;
2358 if (dval(&u) < dval(&eps))
2359 goto ret1;
2360 if (1. - dval(&u) < dval(&eps))
2361 goto bump_up;
2362 if (++i >= ilim)
2363 break;
2364 dval(&eps) *= 10.;
2365 dval(&u) *= 10.;
2366 }
2367 }
2368 else {
2369 /* Generate ilim digits, then fix them up. */
2370 dval(&eps) *= tens[ilim-1];
2371 for(i = 1;; i++, dval(&u) *= 10.) {
2372 L = (Long)(dval(&u));
2373 if (!(dval(&u) -= L))
2374 ilim = i;
2375 *s++ = '0' + (int)L;
2376 if (i == ilim) {
2377 if (dval(&u) > 0.5 + dval(&eps))
2378 goto bump_up;
2379 else if (dval(&u) < 0.5 - dval(&eps)) {
2380 while(*--s == '0');
2381 s++;
2382 goto ret1;
2383 }
2384 break;
2385 }
2386 }
2387 }
2388 fast_failed:
2389 s = s0;
2390 dval(&u) = dval(&d2);
2391 k = k0;
2392 ilim = ilim0;
2393 }
2394
2395 /* Do we have a "small" integer? */
2396
2397 if (be >= 0 && k <= Int_max) {
2398 /* Yes. */
2399 ds = tens[k];
2400 if (ndigits < 0 && ilim <= 0) {
2401 S = mhi = 0;
2402 if (ilim < 0 || dval(&u) <= 5*ds)
2403 goto no_digits;
2404 goto one_digit;
2405 }
2406 for(i = 1;; i++, dval(&u) *= 10.) {
2407 L = (Long)(dval(&u) / ds);
2408 dval(&u) -= L*ds;
2409 *s++ = '0' + (int)L;
2410 if (!dval(&u)) {
2411 break;
2412 }
2413 if (i == ilim) {
2414 dval(&u) += dval(&u);
2415 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2416 bump_up:
2417 while(*--s == '9')
2418 if (s == s0) {
2419 k++;
2420 *s = '0';
2421 break;
2422 }
2423 ++*s++;
2424 }
2425 break;
2426 }
2427 }
2428 goto ret1;
2429 }
2430
2431 m2 = b2;
2432 m5 = b5;
2433 if (leftright) {
2434 i =
2435 denorm ? be + (Bias + (P-1) - 1 + 1) :
2436 1 + P - bbits;
2437 b2 += i;
2438 s2 += i;
2439 mhi = i2b(1);
2440 if (mhi == NULL)
2441 goto failed_malloc;
2442 }
2443 if (m2 > 0 && s2 > 0) {
2444 i = m2 < s2 ? m2 : s2;
2445 b2 -= i;
2446 m2 -= i;
2447 s2 -= i;
2448 }
2449 if (b5 > 0) {
2450 if (leftright) {
2451 if (m5 > 0) {
2452 mhi = pow5mult(mhi, m5);
2453 if (mhi == NULL)
2454 goto failed_malloc;
2455 b1 = mult(mhi, b);
2456 Bfree(b);
2457 b = b1;
2458 if (b == NULL)
2459 goto failed_malloc;
2460 }
2461 if ((j = b5 - m5)) {
2462 b = pow5mult(b, j);
2463 if (b == NULL)
2464 goto failed_malloc;
2465 }
2466 }
2467 else {
2468 b = pow5mult(b, b5);
2469 if (b == NULL)
2470 goto failed_malloc;
2471 }
2472 }
2473 S = i2b(1);
2474 if (S == NULL)
2475 goto failed_malloc;
2476 if (s5 > 0) {
2477 S = pow5mult(S, s5);
2478 if (S == NULL)
2479 goto failed_malloc;
2480 }
2481
2482 /* Check for special case that d is a normalized power of 2. */
2483
2484 spec_case = 0;
2485 if ((mode < 2 || leftright)
2486 ) {
2487 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2488 && word0(&u) & (Exp_mask & ~Exp_msk1)
2489 ) {
2490 /* The special case */
2491 b2 += Log2P;
2492 s2 += Log2P;
2493 spec_case = 1;
2494 }
2495 }
2496
2497 /* Arrange for convenient computation of quotients:
2498 * shift left if necessary so divisor has 4 leading 0 bits.
2499 *
2500 * Perhaps we should just compute leading 28 bits of S once
2501 * and for all and pass them and a shift to quorem, so it
2502 * can do shifts and ors to compute the numerator for q.
2503 */
2504 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
2505 i = 32 - i;
2506#define iInc 28
2507 i = dshift(S, s2);
2508 b2 += i;
2509 m2 += i;
2510 s2 += i;
2511 if (b2 > 0) {
2512 b = lshift(b, b2);
2513 if (b == NULL)
2514 goto failed_malloc;
2515 }
2516 if (s2 > 0) {
2517 S = lshift(S, s2);
2518 if (S == NULL)
2519 goto failed_malloc;
2520 }
2521 if (k_check) {
2522 if (cmp(b,S) < 0) {
2523 k--;
2524 b = multadd(b, 10, 0); /* we botched the k estimate */
2525 if (b == NULL)
2526 goto failed_malloc;
2527 if (leftright) {
2528 mhi = multadd(mhi, 10, 0);
2529 if (mhi == NULL)
2530 goto failed_malloc;
2531 }
2532 ilim = ilim1;
2533 }
2534 }
2535 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2536 if (ilim < 0) {
2537 /* no digits, fcvt style */
2538 no_digits:
2539 k = -1 - ndigits;
2540 goto ret;
2541 }
2542 else {
2543 S = multadd(S, 5, 0);
2544 if (S == NULL)
2545 goto failed_malloc;
2546 if (cmp(b, S) <= 0)
2547 goto no_digits;
2548 }
2549 one_digit:
2550 *s++ = '1';
2551 k++;
2552 goto ret;
2553 }
2554 if (leftright) {
2555 if (m2 > 0) {
2556 mhi = lshift(mhi, m2);
2557 if (mhi == NULL)
2558 goto failed_malloc;
2559 }
2560
2561 /* Compute mlo -- check for special case
2562 * that d is a normalized power of 2.
2563 */
2564
2565 mlo = mhi;
2566 if (spec_case) {
2567 mhi = Balloc(mhi->k);
2568 if (mhi == NULL)
2569 goto failed_malloc;
2570 Bcopy(mhi, mlo);
2571 mhi = lshift(mhi, Log2P);
2572 if (mhi == NULL)
2573 goto failed_malloc;
2574 }
2575
2576 for(i = 1;;i++) {
2577 dig = quorem(b,S) + '0';
2578 /* Do we yet have the shortest decimal string
2579 * that will round to d?
2580 */
2581 j = cmp(b, mlo);
2582 delta = diff(S, mhi);
2583 if (delta == NULL)
2584 goto failed_malloc;
2585 j1 = delta->sign ? 1 : cmp(b, delta);
2586 Bfree(delta);
2587 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2588 ) {
2589 if (dig == '9')
2590 goto round_9_up;
2591 if (j > 0)
2592 dig++;
2593 *s++ = dig;
2594 goto ret;
2595 }
2596 if (j < 0 || (j == 0 && mode != 1
2597 && !(word1(&u) & 1)
2598 )) {
2599 if (!b->x[0] && b->wds <= 1) {
2600 goto accept_dig;
2601 }
2602 if (j1 > 0) {
2603 b = lshift(b, 1);
2604 if (b == NULL)
2605 goto failed_malloc;
2606 j1 = cmp(b, S);
2607 if ((j1 > 0 || (j1 == 0 && dig & 1))
2608 && dig++ == '9')
2609 goto round_9_up;
2610 }
2611 accept_dig:
2612 *s++ = dig;
2613 goto ret;
2614 }
2615 if (j1 > 0) {
2616 if (dig == '9') { /* possible if i == 1 */
2617 round_9_up:
2618 *s++ = '9';
2619 goto roundoff;
2620 }
2621 *s++ = dig + 1;
2622 goto ret;
2623 }
2624 *s++ = dig;
2625 if (i == ilim)
2626 break;
2627 b = multadd(b, 10, 0);
2628 if (b == NULL)
2629 goto failed_malloc;
2630 if (mlo == mhi) {
2631 mlo = mhi = multadd(mhi, 10, 0);
2632 if (mlo == NULL)
2633 goto failed_malloc;
2634 }
2635 else {
2636 mlo = multadd(mlo, 10, 0);
2637 if (mlo == NULL)
2638 goto failed_malloc;
2639 mhi = multadd(mhi, 10, 0);
2640 if (mhi == NULL)
2641 goto failed_malloc;
2642 }
2643 }
2644 }
2645 else
2646 for(i = 1;; i++) {
2647 *s++ = dig = quorem(b,S) + '0';
2648 if (!b->x[0] && b->wds <= 1) {
2649 goto ret;
2650 }
2651 if (i >= ilim)
2652 break;
2653 b = multadd(b, 10, 0);
2654 if (b == NULL)
2655 goto failed_malloc;
2656 }
2657
2658 /* Round off last digit */
2659
2660 b = lshift(b, 1);
2661 if (b == NULL)
2662 goto failed_malloc;
2663 j = cmp(b, S);
2664 if (j > 0 || (j == 0 && dig & 1)) {
2665 roundoff:
2666 while(*--s == '9')
2667 if (s == s0) {
2668 k++;
2669 *s++ = '1';
2670 goto ret;
2671 }
2672 ++*s++;
2673 }
2674 else {
2675 while(*--s == '0');
2676 s++;
2677 }
2678 ret:
2679 Bfree(S);
2680 if (mhi) {
2681 if (mlo && mlo != mhi)
2682 Bfree(mlo);
2683 Bfree(mhi);
2684 }
2685 ret1:
2686 Bfree(b);
2687 *s = 0;
2688 *decpt = k + 1;
2689 if (rve)
2690 *rve = s;
2691 return s0;
2692 failed_malloc:
2693 if (S)
2694 Bfree(S);
2695 if (mlo && mlo != mhi)
2696 Bfree(mlo);
2697 if (mhi)
2698 Bfree(mhi);
2699 if (b)
2700 Bfree(b);
2701 if (s0)
2702 _Py_dg_freedtoa(s0);
2703 return NULL;
2704}
2705#ifdef __cplusplus
2706}
2707#endif
2708
2709#endif /* PY_NO_SHORT_FLOAT_REPR */