blob: 6e11b9a757fb63e9d51a7739995e840869b7a3b9 [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)
Mark Dickinsonadcda342010-01-22 17:04:07 +0000238#define Etiny (-1074) /* smallest denormal is 2**Etiny */
Mark Dickinsonbb282852009-10-24 12:13:30 +0000239#define Exp_1 0x3ff00000
240#define Exp_11 0x3ff00000
241#define Ebits 11
242#define Frac_mask 0xfffff
243#define Frac_mask1 0xfffff
244#define Ten_pmax 22
245#define Bletch 0x10
246#define Bndry_mask 0xfffff
247#define Bndry_mask1 0xfffff
Mark Dickinsonbb282852009-10-24 12:13:30 +0000248#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 Dickinson4141d652010-01-20 17:36:31 +0000273 int 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
Mark Dickinson9481c572010-01-17 20:57:56 +0000311#ifndef Py_USING_MEMORY_DEBUGGER
312
Mark Dickinsonbb282852009-10-24 12:13:30 +0000313/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
314 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
315 1 << k. These pools are maintained as linked lists, with freelist[k]
316 pointing to the head of the list for pool k.
317
318 On allocation, if there's no free slot in the appropriate pool, MALLOC is
319 called to get more memory. This memory is not returned to the system until
320 Python quits. There's also a private memory pool that's allocated from
321 in preference to using MALLOC.
322
323 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
324 decimal digits), memory is directly allocated using MALLOC, and freed using
325 FREE.
326
327 XXX: it would be easy to bypass this memory-management system and
328 translate each call to Balloc into a call to PyMem_Malloc, and each
329 Bfree to PyMem_Free. Investigate whether this has any significant
330 performance on impact. */
331
332static Bigint *freelist[Kmax+1];
333
334/* Allocate space for a Bigint with up to 1<<k digits */
335
336static Bigint *
337Balloc(int k)
338{
339 int x;
340 Bigint *rv;
341 unsigned int len;
342
343 if (k <= Kmax && (rv = freelist[k]))
344 freelist[k] = rv->next;
345 else {
346 x = 1 << k;
347 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
348 /sizeof(double);
349 if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
350 rv = (Bigint*)pmem_next;
351 pmem_next += len;
352 }
353 else {
354 rv = (Bigint*)MALLOC(len*sizeof(double));
355 if (rv == NULL)
356 return NULL;
357 }
358 rv->k = k;
359 rv->maxwds = x;
360 }
361 rv->sign = rv->wds = 0;
362 return rv;
363}
364
365/* Free a Bigint allocated with Balloc */
366
367static void
368Bfree(Bigint *v)
369{
370 if (v) {
371 if (v->k > Kmax)
372 FREE((void*)v);
373 else {
374 v->next = freelist[v->k];
375 freelist[v->k] = v;
376 }
377 }
378}
379
Mark Dickinson9481c572010-01-17 20:57:56 +0000380#else
381
382/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
383 PyMem_Free directly in place of the custom memory allocation scheme above.
384 These are provided for the benefit of memory debugging tools like
385 Valgrind. */
386
387/* Allocate space for a Bigint with up to 1<<k digits */
388
389static Bigint *
390Balloc(int k)
391{
392 int x;
393 Bigint *rv;
394 unsigned int len;
395
396 x = 1 << k;
397 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
398 /sizeof(double);
399
400 rv = (Bigint*)MALLOC(len*sizeof(double));
401 if (rv == NULL)
402 return NULL;
403
404 rv->k = k;
405 rv->maxwds = x;
406 rv->sign = rv->wds = 0;
407 return rv;
408}
409
410/* Free a Bigint allocated with Balloc */
411
412static void
413Bfree(Bigint *v)
414{
415 if (v) {
416 FREE((void*)v);
417 }
418}
419
420#endif /* Py_USING_MEMORY_DEBUGGER */
421
Mark Dickinsonbb282852009-10-24 12:13:30 +0000422#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
423 y->wds*sizeof(Long) + 2*sizeof(int))
424
425/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
426 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
427 On failure, return NULL. In this case, b will have been already freed. */
428
429static Bigint *
430multadd(Bigint *b, int m, int a) /* multiply by m and add a */
431{
432 int i, wds;
433#ifdef ULLong
434 ULong *x;
435 ULLong carry, y;
436#else
437 ULong carry, *x, y;
438 ULong xi, z;
439#endif
440 Bigint *b1;
441
442 wds = b->wds;
443 x = b->x;
444 i = 0;
445 carry = a;
446 do {
447#ifdef ULLong
448 y = *x * (ULLong)m + carry;
449 carry = y >> 32;
450 *x++ = (ULong)(y & FFFFFFFF);
451#else
452 xi = *x;
453 y = (xi & 0xffff) * m + carry;
454 z = (xi >> 16) * m + (y >> 16);
455 carry = z >> 16;
456 *x++ = (z << 16) + (y & 0xffff);
457#endif
458 }
459 while(++i < wds);
460 if (carry) {
461 if (wds >= b->maxwds) {
462 b1 = Balloc(b->k+1);
463 if (b1 == NULL){
464 Bfree(b);
465 return NULL;
466 }
467 Bcopy(b1, b);
468 Bfree(b);
469 b = b1;
470 }
471 b->x[wds++] = (ULong)carry;
472 b->wds = wds;
473 }
474 return b;
475}
476
477/* convert a string s containing nd decimal digits (possibly containing a
478 decimal separator at position nd0, which is ignored) to a Bigint. This
479 function carries on where the parsing code in _Py_dg_strtod leaves off: on
480 entry, y9 contains the result of converting the first 9 digits. Returns
481 NULL on failure. */
482
483static Bigint *
Mark Dickinsond2a99402010-01-13 22:20:10 +0000484s2b(const char *s, int nd0, int nd, ULong y9)
Mark Dickinsonbb282852009-10-24 12:13:30 +0000485{
486 Bigint *b;
487 int i, k;
488 Long x, y;
489
490 x = (nd + 8) / 9;
491 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
492 b = Balloc(k);
493 if (b == NULL)
494 return NULL;
495 b->x[0] = y9;
496 b->wds = 1;
497
Mark Dickinsond2a99402010-01-13 22:20:10 +0000498 if (nd <= 9)
499 return b;
500
501 s += 9;
502 for (i = 9; i < nd0; i++) {
503 b = multadd(b, 10, *s++ - '0');
504 if (b == NULL)
505 return NULL;
Mark Dickinsonbb282852009-10-24 12:13:30 +0000506 }
Mark Dickinsond2a99402010-01-13 22:20:10 +0000507 s++;
Mark Dickinsonbb282852009-10-24 12:13:30 +0000508 for(; i < nd; i++) {
509 b = multadd(b, 10, *s++ - '0');
510 if (b == NULL)
511 return NULL;
512 }
513 return b;
514}
515
516/* count leading 0 bits in the 32-bit integer x. */
517
518static int
519hi0bits(ULong x)
520{
521 int k = 0;
522
523 if (!(x & 0xffff0000)) {
524 k = 16;
525 x <<= 16;
526 }
527 if (!(x & 0xff000000)) {
528 k += 8;
529 x <<= 8;
530 }
531 if (!(x & 0xf0000000)) {
532 k += 4;
533 x <<= 4;
534 }
535 if (!(x & 0xc0000000)) {
536 k += 2;
537 x <<= 2;
538 }
539 if (!(x & 0x80000000)) {
540 k++;
541 if (!(x & 0x40000000))
542 return 32;
543 }
544 return k;
545}
546
547/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
548 number of bits. */
549
550static int
551lo0bits(ULong *y)
552{
553 int k;
554 ULong x = *y;
555
556 if (x & 7) {
557 if (x & 1)
558 return 0;
559 if (x & 2) {
560 *y = x >> 1;
561 return 1;
562 }
563 *y = x >> 2;
564 return 2;
565 }
566 k = 0;
567 if (!(x & 0xffff)) {
568 k = 16;
569 x >>= 16;
570 }
571 if (!(x & 0xff)) {
572 k += 8;
573 x >>= 8;
574 }
575 if (!(x & 0xf)) {
576 k += 4;
577 x >>= 4;
578 }
579 if (!(x & 0x3)) {
580 k += 2;
581 x >>= 2;
582 }
583 if (!(x & 1)) {
584 k++;
585 x >>= 1;
586 if (!x)
587 return 32;
588 }
589 *y = x;
590 return k;
591}
592
593/* convert a small nonnegative integer to a Bigint */
594
595static Bigint *
596i2b(int i)
597{
598 Bigint *b;
599
600 b = Balloc(1);
601 if (b == NULL)
602 return NULL;
603 b->x[0] = i;
604 b->wds = 1;
605 return b;
606}
607
608/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
609 the signs of a and b. */
610
611static Bigint *
612mult(Bigint *a, Bigint *b)
613{
614 Bigint *c;
615 int k, wa, wb, wc;
616 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
617 ULong y;
618#ifdef ULLong
619 ULLong carry, z;
620#else
621 ULong carry, z;
622 ULong z2;
623#endif
624
625 if (a->wds < b->wds) {
626 c = a;
627 a = b;
628 b = c;
629 }
630 k = a->k;
631 wa = a->wds;
632 wb = b->wds;
633 wc = wa + wb;
634 if (wc > a->maxwds)
635 k++;
636 c = Balloc(k);
637 if (c == NULL)
638 return NULL;
639 for(x = c->x, xa = x + wc; x < xa; x++)
640 *x = 0;
641 xa = a->x;
642 xae = xa + wa;
643 xb = b->x;
644 xbe = xb + wb;
645 xc0 = c->x;
646#ifdef ULLong
647 for(; xb < xbe; xc0++) {
648 if ((y = *xb++)) {
649 x = xa;
650 xc = xc0;
651 carry = 0;
652 do {
653 z = *x++ * (ULLong)y + *xc + carry;
654 carry = z >> 32;
655 *xc++ = (ULong)(z & FFFFFFFF);
656 }
657 while(x < xae);
658 *xc = (ULong)carry;
659 }
660 }
661#else
662 for(; xb < xbe; xb++, xc0++) {
663 if (y = *xb & 0xffff) {
664 x = xa;
665 xc = xc0;
666 carry = 0;
667 do {
668 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
669 carry = z >> 16;
670 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
671 carry = z2 >> 16;
672 Storeinc(xc, z2, z);
673 }
674 while(x < xae);
675 *xc = carry;
676 }
677 if (y = *xb >> 16) {
678 x = xa;
679 xc = xc0;
680 carry = 0;
681 z2 = *xc;
682 do {
683 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
684 carry = z >> 16;
685 Storeinc(xc, z, z2);
686 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
687 carry = z2 >> 16;
688 }
689 while(x < xae);
690 *xc = z2;
691 }
692 }
693#endif
694 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
695 c->wds = wc;
696 return c;
697}
698
Mark Dickinson9481c572010-01-17 20:57:56 +0000699#ifndef Py_USING_MEMORY_DEBUGGER
700
Mark Dickinsonbb282852009-10-24 12:13:30 +0000701/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
702
703static Bigint *p5s;
704
705/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
706 failure; if the returned pointer is distinct from b then the original
707 Bigint b will have been Bfree'd. Ignores the sign of b. */
708
709static Bigint *
710pow5mult(Bigint *b, int k)
711{
712 Bigint *b1, *p5, *p51;
713 int i;
714 static int p05[3] = { 5, 25, 125 };
715
716 if ((i = k & 3)) {
717 b = multadd(b, p05[i-1], 0);
718 if (b == NULL)
719 return NULL;
720 }
721
722 if (!(k >>= 2))
723 return b;
724 p5 = p5s;
725 if (!p5) {
726 /* first time */
727 p5 = i2b(625);
728 if (p5 == NULL) {
729 Bfree(b);
730 return NULL;
731 }
732 p5s = p5;
733 p5->next = 0;
734 }
735 for(;;) {
736 if (k & 1) {
737 b1 = mult(b, p5);
738 Bfree(b);
739 b = b1;
740 if (b == NULL)
741 return NULL;
742 }
743 if (!(k >>= 1))
744 break;
745 p51 = p5->next;
746 if (!p51) {
747 p51 = mult(p5,p5);
748 if (p51 == NULL) {
749 Bfree(b);
750 return NULL;
751 }
752 p51->next = 0;
753 p5->next = p51;
754 }
755 p5 = p51;
756 }
757 return b;
758}
759
Mark Dickinson9481c572010-01-17 20:57:56 +0000760#else
761
762/* Version of pow5mult that doesn't cache powers of 5. Provided for
763 the benefit of memory debugging tools like Valgrind. */
764
765static Bigint *
766pow5mult(Bigint *b, int k)
767{
768 Bigint *b1, *p5, *p51;
769 int i;
770 static int p05[3] = { 5, 25, 125 };
771
772 if ((i = k & 3)) {
773 b = multadd(b, p05[i-1], 0);
774 if (b == NULL)
775 return NULL;
776 }
777
778 if (!(k >>= 2))
779 return b;
780 p5 = i2b(625);
781 if (p5 == NULL) {
782 Bfree(b);
783 return NULL;
784 }
785
786 for(;;) {
787 if (k & 1) {
788 b1 = mult(b, p5);
789 Bfree(b);
790 b = b1;
791 if (b == NULL) {
792 Bfree(p5);
793 return NULL;
794 }
795 }
796 if (!(k >>= 1))
797 break;
798 p51 = mult(p5, p5);
799 Bfree(p5);
800 p5 = p51;
801 if (p5 == NULL) {
802 Bfree(b);
803 return NULL;
804 }
805 }
806 Bfree(p5);
807 return b;
808}
809
810#endif /* Py_USING_MEMORY_DEBUGGER */
811
Mark Dickinsonbb282852009-10-24 12:13:30 +0000812/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
813 or NULL on failure. If the returned pointer is distinct from b then the
814 original b will have been Bfree'd. Ignores the sign of b. */
815
816static Bigint *
817lshift(Bigint *b, int k)
818{
819 int i, k1, n, n1;
820 Bigint *b1;
821 ULong *x, *x1, *xe, z;
822
823 n = k >> 5;
824 k1 = b->k;
825 n1 = n + b->wds + 1;
826 for(i = b->maxwds; n1 > i; i <<= 1)
827 k1++;
828 b1 = Balloc(k1);
829 if (b1 == NULL) {
830 Bfree(b);
831 return NULL;
832 }
833 x1 = b1->x;
834 for(i = 0; i < n; i++)
835 *x1++ = 0;
836 x = b->x;
837 xe = x + b->wds;
838 if (k &= 0x1f) {
839 k1 = 32 - k;
840 z = 0;
841 do {
842 *x1++ = *x << k | z;
843 z = *x++ >> k1;
844 }
845 while(x < xe);
846 if ((*x1 = z))
847 ++n1;
848 }
849 else do
850 *x1++ = *x++;
851 while(x < xe);
852 b1->wds = n1 - 1;
853 Bfree(b);
854 return b1;
855}
856
857/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
858 1 if a > b. Ignores signs of a and b. */
859
860static int
861cmp(Bigint *a, Bigint *b)
862{
863 ULong *xa, *xa0, *xb, *xb0;
864 int i, j;
865
866 i = a->wds;
867 j = b->wds;
868#ifdef DEBUG
869 if (i > 1 && !a->x[i-1])
870 Bug("cmp called with a->x[a->wds-1] == 0");
871 if (j > 1 && !b->x[j-1])
872 Bug("cmp called with b->x[b->wds-1] == 0");
873#endif
874 if (i -= j)
875 return i;
876 xa0 = a->x;
877 xa = xa0 + j;
878 xb0 = b->x;
879 xb = xb0 + j;
880 for(;;) {
881 if (*--xa != *--xb)
882 return *xa < *xb ? -1 : 1;
883 if (xa <= xa0)
884 break;
885 }
886 return 0;
887}
888
889/* Take the difference of Bigints a and b, returning a new Bigint. Returns
890 NULL on failure. The signs of a and b are ignored, but the sign of the
891 result is set appropriately. */
892
893static Bigint *
894diff(Bigint *a, Bigint *b)
895{
896 Bigint *c;
897 int i, wa, wb;
898 ULong *xa, *xae, *xb, *xbe, *xc;
899#ifdef ULLong
900 ULLong borrow, y;
901#else
902 ULong borrow, y;
903 ULong z;
904#endif
905
906 i = cmp(a,b);
907 if (!i) {
908 c = Balloc(0);
909 if (c == NULL)
910 return NULL;
911 c->wds = 1;
912 c->x[0] = 0;
913 return c;
914 }
915 if (i < 0) {
916 c = a;
917 a = b;
918 b = c;
919 i = 1;
920 }
921 else
922 i = 0;
923 c = Balloc(a->k);
924 if (c == NULL)
925 return NULL;
926 c->sign = i;
927 wa = a->wds;
928 xa = a->x;
929 xae = xa + wa;
930 wb = b->wds;
931 xb = b->x;
932 xbe = xb + wb;
933 xc = c->x;
934 borrow = 0;
935#ifdef ULLong
936 do {
937 y = (ULLong)*xa++ - *xb++ - borrow;
938 borrow = y >> 32 & (ULong)1;
939 *xc++ = (ULong)(y & FFFFFFFF);
940 }
941 while(xb < xbe);
942 while(xa < xae) {
943 y = *xa++ - borrow;
944 borrow = y >> 32 & (ULong)1;
945 *xc++ = (ULong)(y & FFFFFFFF);
946 }
947#else
948 do {
949 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
950 borrow = (y & 0x10000) >> 16;
951 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
952 borrow = (z & 0x10000) >> 16;
953 Storeinc(xc, z, y);
954 }
955 while(xb < xbe);
956 while(xa < xae) {
957 y = (*xa & 0xffff) - borrow;
958 borrow = (y & 0x10000) >> 16;
959 z = (*xa++ >> 16) - borrow;
960 borrow = (z & 0x10000) >> 16;
961 Storeinc(xc, z, y);
962 }
963#endif
964 while(!*--xc)
965 wa--;
966 c->wds = wa;
967 return c;
968}
969
Mark Dickinson4141d652010-01-20 17:36:31 +0000970/* Given a positive normal double x, return the difference between x and the
971 next double up. Doesn't give correct results for subnormals. */
Mark Dickinsonbb282852009-10-24 12:13:30 +0000972
973static double
974ulp(U *x)
975{
976 Long L;
977 U u;
978
979 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
980 word0(&u) = L;
981 word1(&u) = 0;
982 return dval(&u);
983}
984
985/* Convert a Bigint to a double plus an exponent */
986
987static double
988b2d(Bigint *a, int *e)
989{
990 ULong *xa, *xa0, w, y, z;
991 int k;
992 U d;
993
994 xa0 = a->x;
995 xa = xa0 + a->wds;
996 y = *--xa;
997#ifdef DEBUG
998 if (!y) Bug("zero y in b2d");
999#endif
1000 k = hi0bits(y);
1001 *e = 32 - k;
1002 if (k < Ebits) {
1003 word0(&d) = Exp_1 | y >> (Ebits - k);
1004 w = xa > xa0 ? *--xa : 0;
1005 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
1006 goto ret_d;
1007 }
1008 z = xa > xa0 ? *--xa : 0;
1009 if (k -= Ebits) {
1010 word0(&d) = Exp_1 | y << k | z >> (32 - k);
1011 y = xa > xa0 ? *--xa : 0;
1012 word1(&d) = z << k | y >> (32 - k);
1013 }
1014 else {
1015 word0(&d) = Exp_1 | y;
1016 word1(&d) = z;
1017 }
1018 ret_d:
1019 return dval(&d);
1020}
1021
Mark Dickinsonadcda342010-01-22 17:04:07 +00001022/* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
1023 except that it accepts the scale parameter used in _Py_dg_strtod (which
1024 should be either 0 or 2*P), and the normalization for the return value is
1025 different (see below). On input, d should be finite and nonnegative, and d
1026 / 2**scale should be exactly representable as an IEEE 754 double.
1027
1028 Returns a Bigint b and an integer e such that
1029
1030 dval(d) / 2**scale = b * 2**e.
1031
1032 Unlike d2b, b is not necessarily odd: b and e are normalized so
1033 that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
1034 and e == Etiny. This applies equally to an input of 0.0: in that
1035 case the return values are b = 0 and e = Etiny.
1036
1037 The above normalization ensures that for all possible inputs d,
1038 2**e gives ulp(d/2**scale).
1039
1040 Returns NULL on failure.
1041*/
1042
1043static Bigint *
1044sd2b(U *d, int scale, int *e)
1045{
1046 Bigint *b;
1047
1048 b = Balloc(1);
1049 if (b == NULL)
1050 return NULL;
1051
1052 /* First construct b and e assuming that scale == 0. */
1053 b->wds = 2;
1054 b->x[0] = word1(d);
1055 b->x[1] = word0(d) & Frac_mask;
1056 *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
1057 if (*e < Etiny)
1058 *e = Etiny;
1059 else
1060 b->x[1] |= Exp_msk1;
1061
1062 /* Now adjust for scale, provided that b != 0. */
1063 if (scale && (b->x[0] || b->x[1])) {
1064 *e -= scale;
1065 if (*e < Etiny) {
1066 scale = Etiny - *e;
1067 *e = Etiny;
1068 /* We can't shift more than P-1 bits without shifting out a 1. */
1069 assert(0 < scale && scale <= P - 1);
1070 if (scale >= 32) {
1071 /* The bits shifted out should all be zero. */
1072 assert(b->x[0] == 0);
1073 b->x[0] = b->x[1];
1074 b->x[1] = 0;
1075 scale -= 32;
1076 }
1077 if (scale) {
1078 /* The bits shifted out should all be zero. */
1079 assert(b->x[0] << (32 - scale) == 0);
1080 b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1081 b->x[1] >>= scale;
1082 }
1083 }
1084 }
1085 /* Ensure b is normalized. */
1086 if (!b->x[1])
1087 b->wds = 1;
1088
1089 return b;
1090}
1091
Mark Dickinsonbb282852009-10-24 12:13:30 +00001092/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
1093
1094 Given a finite nonzero double d, return an odd Bigint b and exponent *e
1095 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
Mark Dickinson2bcd1772010-01-04 21:32:02 +00001096 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
Mark Dickinsonbb282852009-10-24 12:13:30 +00001097
1098 If d is zero, then b == 0, *e == -1010, *bbits = 0.
1099 */
1100
Mark Dickinsonbb282852009-10-24 12:13:30 +00001101static Bigint *
1102d2b(U *d, int *e, int *bits)
1103{
1104 Bigint *b;
1105 int de, k;
1106 ULong *x, y, z;
1107 int i;
1108
1109 b = Balloc(1);
1110 if (b == NULL)
1111 return NULL;
1112 x = b->x;
1113
1114 z = word0(d) & Frac_mask;
1115 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
1116 if ((de = (int)(word0(d) >> Exp_shift)))
1117 z |= Exp_msk1;
1118 if ((y = word1(d))) {
1119 if ((k = lo0bits(&y))) {
1120 x[0] = y | z << (32 - k);
1121 z >>= k;
1122 }
1123 else
1124 x[0] = y;
1125 i =
1126 b->wds = (x[1] = z) ? 2 : 1;
1127 }
1128 else {
1129 k = lo0bits(&z);
1130 x[0] = z;
1131 i =
1132 b->wds = 1;
1133 k += 32;
1134 }
1135 if (de) {
1136 *e = de - Bias - (P-1) + k;
1137 *bits = P - k;
1138 }
1139 else {
1140 *e = de - Bias - (P-1) + 1 + k;
1141 *bits = 32*i - hi0bits(x[i-1]);
1142 }
1143 return b;
1144}
1145
1146/* Compute the ratio of two Bigints, as a double. The result may have an
1147 error of up to 2.5 ulps. */
1148
1149static double
1150ratio(Bigint *a, Bigint *b)
1151{
1152 U da, db;
1153 int k, ka, kb;
1154
1155 dval(&da) = b2d(a, &ka);
1156 dval(&db) = b2d(b, &kb);
1157 k = ka - kb + 32*(a->wds - b->wds);
1158 if (k > 0)
1159 word0(&da) += k*Exp_msk1;
1160 else {
1161 k = -k;
1162 word0(&db) += k*Exp_msk1;
1163 }
1164 return dval(&da) / dval(&db);
1165}
1166
1167static const double
1168tens[] = {
1169 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1170 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1171 1e20, 1e21, 1e22
1172};
1173
1174static const double
1175bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1176static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1177 9007199254740992.*9007199254740992.e-256
1178 /* = 2^106 * 1e-256 */
1179};
1180/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1181/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1182#define Scale_Bit 0x10
1183#define n_bigtens 5
1184
1185#define ULbits 32
1186#define kshift 5
1187#define kmask 31
1188
1189
1190static int
1191dshift(Bigint *b, int p2)
1192{
1193 int rv = hi0bits(b->x[b->wds-1]) - 4;
1194 if (p2 > 0)
1195 rv -= p2;
1196 return rv & kmask;
1197}
1198
1199/* special case of Bigint division. The quotient is always in the range 0 <=
1200 quotient < 10, and on entry the divisor S is normalized so that its top 4
1201 bits (28--31) are zero and bit 27 is set. */
1202
1203static int
1204quorem(Bigint *b, Bigint *S)
1205{
1206 int n;
1207 ULong *bx, *bxe, q, *sx, *sxe;
1208#ifdef ULLong
1209 ULLong borrow, carry, y, ys;
1210#else
1211 ULong borrow, carry, y, ys;
1212 ULong si, z, zs;
1213#endif
1214
1215 n = S->wds;
1216#ifdef DEBUG
1217 /*debug*/ if (b->wds > n)
1218 /*debug*/ Bug("oversize b in quorem");
1219#endif
1220 if (b->wds < n)
1221 return 0;
1222 sx = S->x;
1223 sxe = sx + --n;
1224 bx = b->x;
1225 bxe = bx + n;
1226 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1227#ifdef DEBUG
1228 /*debug*/ if (q > 9)
1229 /*debug*/ Bug("oversized quotient in quorem");
1230#endif
1231 if (q) {
1232 borrow = 0;
1233 carry = 0;
1234 do {
1235#ifdef ULLong
1236 ys = *sx++ * (ULLong)q + carry;
1237 carry = ys >> 32;
1238 y = *bx - (ys & FFFFFFFF) - borrow;
1239 borrow = y >> 32 & (ULong)1;
1240 *bx++ = (ULong)(y & FFFFFFFF);
1241#else
1242 si = *sx++;
1243 ys = (si & 0xffff) * q + carry;
1244 zs = (si >> 16) * q + (ys >> 16);
1245 carry = zs >> 16;
1246 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1247 borrow = (y & 0x10000) >> 16;
1248 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1249 borrow = (z & 0x10000) >> 16;
1250 Storeinc(bx, z, y);
1251#endif
1252 }
1253 while(sx <= sxe);
1254 if (!*bxe) {
1255 bx = b->x;
1256 while(--bxe > bx && !*bxe)
1257 --n;
1258 b->wds = n;
1259 }
1260 }
1261 if (cmp(b, S) >= 0) {
1262 q++;
1263 borrow = 0;
1264 carry = 0;
1265 bx = b->x;
1266 sx = S->x;
1267 do {
1268#ifdef ULLong
1269 ys = *sx++ + carry;
1270 carry = ys >> 32;
1271 y = *bx - (ys & FFFFFFFF) - borrow;
1272 borrow = y >> 32 & (ULong)1;
1273 *bx++ = (ULong)(y & FFFFFFFF);
1274#else
1275 si = *sx++;
1276 ys = (si & 0xffff) + carry;
1277 zs = (si >> 16) + (ys >> 16);
1278 carry = zs >> 16;
1279 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1280 borrow = (y & 0x10000) >> 16;
1281 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1282 borrow = (z & 0x10000) >> 16;
1283 Storeinc(bx, z, y);
1284#endif
1285 }
1286 while(sx <= sxe);
1287 bx = b->x;
1288 bxe = bx + n;
1289 if (!*bxe) {
1290 while(--bxe > bx && !*bxe)
1291 --n;
1292 b->wds = n;
1293 }
1294 }
1295 return q;
1296}
1297
Mark Dickinson5818e012010-01-13 19:02:37 +00001298/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001299
Mark Dickinson5818e012010-01-13 19:02:37 +00001300 Assuming that x is finite and nonnegative (positive zero is fine
1301 here) and x / 2^bc.scale is exactly representable as a double,
1302 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001303
1304static double
1305sulp(U *x, BCinfo *bc)
1306{
1307 U u;
1308
Mark Dickinson02139d72010-01-13 22:15:53 +00001309 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001310 /* rv/2^bc->scale is subnormal */
1311 word0(&u) = (P+2)*Exp_msk1;
1312 word1(&u) = 0;
1313 return u.d;
1314 }
Mark Dickinson5818e012010-01-13 19:02:37 +00001315 else {
1316 assert(word0(x) || word1(x)); /* x != 0.0 */
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001317 return ulp(x);
Mark Dickinson5818e012010-01-13 19:02:37 +00001318 }
Mark Dickinson5ff4f272010-01-12 22:55:51 +00001319}
Mark Dickinsonbb282852009-10-24 12:13:30 +00001320
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001321/* The bigcomp function handles some hard cases for strtod, for inputs
1322 with more than STRTOD_DIGLIM digits. It's called once an initial
1323 estimate for the double corresponding to the input string has
1324 already been obtained by the code in _Py_dg_strtod.
1325
1326 The bigcomp function is only called after _Py_dg_strtod has found a
1327 double value rv such that either rv or rv + 1ulp represents the
1328 correctly rounded value corresponding to the original string. It
1329 determines which of these two values is the correct one by
1330 computing the decimal digits of rv + 0.5ulp and comparing them with
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001331 the corresponding digits of s0.
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001332
1333 In the following, write dv for the absolute value of the number represented
1334 by the input string.
1335
1336 Inputs:
1337
1338 s0 points to the first significant digit of the input string.
1339
1340 rv is a (possibly scaled) estimate for the closest double value to the
1341 value represented by the original input to _Py_dg_strtod. If
1342 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1343 the input value.
1344
1345 bc is a struct containing information gathered during the parsing and
1346 estimation steps of _Py_dg_strtod. Description of fields follows:
1347
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001348 bc->e0 gives the exponent of the input value, such that dv = (integer
1349 given by the bd->nd digits of s0) * 10**e0
1350
Mark Dickinsond2a99402010-01-13 22:20:10 +00001351 bc->nd gives the total number of significant digits of s0. It will
1352 be at least 1.
Mark Dickinsonb26d56a2010-01-13 18:21:53 +00001353
1354 bc->nd0 gives the number of significant digits of s0 before the
1355 decimal separator. If there's no decimal separator, bc->nd0 ==
1356 bc->nd.
1357
1358 bc->scale is the value used to scale rv to avoid doing arithmetic with
1359 subnormal values. It's either 0 or 2*P (=106).
1360
1361 Outputs:
1362
1363 On successful exit, rv/2^(bc->scale) is the closest double to dv.
1364
1365 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001366
1367static int
1368bigcomp(U *rv, const char *s0, BCinfo *bc)
1369{
1370 Bigint *b, *d;
Mark Dickinsonadcda342010-01-22 17:04:07 +00001371 int b2, d2, dd, i, nd, nd0, odd, p2, p5;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001372
Mark Dickinsond2a99402010-01-13 22:20:10 +00001373 dd = 0; /* silence compiler warning about possibly unused variable */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001374 nd = bc->nd;
1375 nd0 = bc->nd0;
Mark Dickinson8efef5c2010-01-12 22:23:56 +00001376 p5 = nd + bc->e0;
Mark Dickinsonadcda342010-01-22 17:04:07 +00001377 b = sd2b(rv, bc->scale, &p2);
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001378 if (b == NULL)
1379 return -1;
Mark Dickinsonadcda342010-01-22 17:04:07 +00001380
Mark Dickinson50b60c62010-01-14 13:14:49 +00001381 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1382 case, this is used for round to even. */
Mark Dickinsonadcda342010-01-22 17:04:07 +00001383 odd = b->x[0] & 1;
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001384
Mark Dickinsonadcda342010-01-22 17:04:07 +00001385 /* left shift b by 1 bit and or a 1 into the least significant bit;
1386 this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1387 b = lshift(b, 1);
1388 if (b == NULL)
1389 return -1;
1390 b->x[0] |= 1;
1391 p2--;
1392
1393 p2 -= p5;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001394 d = i2b(1);
1395 if (d == NULL) {
1396 Bfree(b);
1397 return -1;
1398 }
1399 /* Arrange for convenient computation of quotients:
1400 * shift left if necessary so divisor has 4 leading 0 bits.
1401 */
1402 if (p5 > 0) {
1403 d = pow5mult(d, p5);
1404 if (d == NULL) {
1405 Bfree(b);
1406 return -1;
1407 }
1408 }
1409 else if (p5 < 0) {
1410 b = pow5mult(b, -p5);
1411 if (b == NULL) {
1412 Bfree(d);
1413 return -1;
1414 }
1415 }
1416 if (p2 > 0) {
1417 b2 = p2;
1418 d2 = 0;
1419 }
1420 else {
1421 b2 = 0;
1422 d2 = -p2;
1423 }
1424 i = dshift(d, d2);
1425 if ((b2 += i) > 0) {
1426 b = lshift(b, b2);
1427 if (b == NULL) {
1428 Bfree(d);
1429 return -1;
1430 }
1431 }
1432 if ((d2 += i) > 0) {
1433 d = lshift(d, d2);
1434 if (d == NULL) {
1435 Bfree(b);
1436 return -1;
1437 }
1438 }
1439
Mark Dickinson4141d652010-01-20 17:36:31 +00001440 /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1441 * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
1442 * a number in the range [0.1, 1). */
1443 if (cmp(b, d) >= 0)
1444 /* b/d >= 1 */
Mark Dickinson8efef5c2010-01-12 22:23:56 +00001445 dd = -1;
Mark Dickinson4141d652010-01-20 17:36:31 +00001446 else {
1447 i = 0;
1448 for(;;) {
1449 b = multadd(b, 10, 0);
1450 if (b == NULL) {
1451 Bfree(d);
1452 return -1;
1453 }
1454 dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1455 i++;
Mark Dickinson50b60c62010-01-14 13:14:49 +00001456
Mark Dickinson4141d652010-01-20 17:36:31 +00001457 if (dd)
1458 break;
1459 if (!b->x[0] && b->wds == 1) {
1460 /* b/d == 0 */
1461 dd = i < nd;
1462 break;
1463 }
1464 if (!(i < nd)) {
1465 /* b/d != 0, but digits of s0 exhausted */
1466 dd = -1;
1467 break;
1468 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001469 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001470 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001471 Bfree(b);
1472 Bfree(d);
Mark Dickinson50b60c62010-01-14 13:14:49 +00001473 if (dd > 0 || (dd == 0 && odd))
Mark Dickinson6e0d3d62010-01-13 20:55:03 +00001474 dval(rv) += sulp(rv, bc);
Mark Dickinsonbb282852009-10-24 12:13:30 +00001475 return 0;
1476}
1477
1478double
1479_Py_dg_strtod(const char *s00, char **se)
1480{
Mark Dickinsonadcda342010-01-22 17:04:07 +00001481 int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1482 int esign, i, j, k, lz, nd, nd0, odd, sign;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001483 const char *s, *s0, *s1;
1484 double aadj, aadj1;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001485 U aadj2, adj, rv, rv0;
Mark Dickinson4141d652010-01-20 17:36:31 +00001486 ULong y, z, abs_exp;
Mark Dickinson18a818b2010-01-16 18:06:17 +00001487 Long L;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001488 BCinfo bc;
1489 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1490
Mark Dickinsonbb282852009-10-24 12:13:30 +00001491 dval(&rv) = 0.;
Mark Dickinson4141d652010-01-20 17:36:31 +00001492
1493 /* Start parsing. */
1494 c = *(s = s00);
1495
1496 /* Parse optional sign, if present. */
1497 sign = 0;
1498 switch (c) {
1499 case '-':
1500 sign = 1;
1501 /* no break */
1502 case '+':
1503 c = *++s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001504 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001505
1506 /* Skip leading zeros: lz is true iff there were leading zeros. */
1507 s1 = s;
1508 while (c == '0')
1509 c = *++s;
1510 lz = s != s1;
1511
1512 /* Point s0 at the first nonzero digit (if any). nd0 will be the position
1513 of the point relative to s0. nd will be the total number of digits
1514 ignoring leading zeros. */
1515 s0 = s1 = s;
1516 while ('0' <= c && c <= '9')
1517 c = *++s;
1518 nd0 = nd = s - s1;
1519
1520 /* Parse decimal point and following digits. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001521 if (c == '.') {
1522 c = *++s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001523 if (!nd) {
Mark Dickinson4141d652010-01-20 17:36:31 +00001524 s1 = s;
1525 while (c == '0')
1526 c = *++s;
1527 lz = lz || s != s1;
1528 nd0 -= s - s1;
1529 s0 = s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001530 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001531 s1 = s;
1532 while ('0' <= c && c <= '9')
1533 c = *++s;
1534 nd += s - s1;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001535 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001536
1537 /* Now lz is true if and only if there were leading zero digits, and nd
1538 gives the total number of digits ignoring leading zeros. A valid input
1539 must have at least one digit. */
1540 if (!nd && !lz) {
Mark Dickinson19428062010-01-20 18:02:41 +00001541 if (se)
1542 *se = (char *)s00;
Mark Dickinson4141d652010-01-20 17:36:31 +00001543 goto parse_error;
1544 }
1545
1546 /* Parse exponent. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001547 e = 0;
1548 if (c == 'e' || c == 'E') {
Mark Dickinsonbb282852009-10-24 12:13:30 +00001549 s00 = s;
Mark Dickinson4141d652010-01-20 17:36:31 +00001550 c = *++s;
1551
1552 /* Exponent sign. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001553 esign = 0;
Mark Dickinson4141d652010-01-20 17:36:31 +00001554 switch (c) {
Mark Dickinsonbb282852009-10-24 12:13:30 +00001555 case '-':
1556 esign = 1;
Mark Dickinson4141d652010-01-20 17:36:31 +00001557 /* no break */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001558 case '+':
1559 c = *++s;
1560 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001561
1562 /* Skip zeros. lz is true iff there are leading zeros. */
1563 s1 = s;
1564 while (c == '0')
1565 c = *++s;
1566 lz = s != s1;
1567
1568 /* Get absolute value of the exponent. */
1569 s1 = s;
1570 abs_exp = 0;
1571 while ('0' <= c && c <= '9') {
1572 abs_exp = 10*abs_exp + (c - '0');
1573 c = *++s;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001574 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001575
1576 /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
1577 there are at most 9 significant exponent digits then overflow is
1578 impossible. */
1579 if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1580 e = (int)MAX_ABS_EXP;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001581 else
Mark Dickinson4141d652010-01-20 17:36:31 +00001582 e = (int)abs_exp;
1583 if (esign)
1584 e = -e;
1585
1586 /* A valid exponent must have at least one digit. */
1587 if (s == s1 && !lz)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001588 s = s00;
1589 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001590
1591 /* Adjust exponent to take into account position of the point. */
1592 e -= nd - nd0;
1593 if (nd0 <= 0)
Mark Dickinson811ff822010-01-16 17:57:49 +00001594 nd0 = nd;
1595
Mark Dickinson4141d652010-01-20 17:36:31 +00001596 /* Finished parsing. Set se to indicate how far we parsed */
1597 if (se)
1598 *se = (char *)s;
1599
1600 /* If all digits were zero, exit with return value +-0.0. Otherwise,
1601 strip trailing zeros: scan back until we hit a nonzero digit. */
1602 if (!nd)
1603 goto ret;
Mark Dickinson811ff822010-01-16 17:57:49 +00001604 for (i = nd; i > 0; ) {
Mark Dickinson811ff822010-01-16 17:57:49 +00001605 --i;
1606 if (s0[i < nd0 ? i : i+1] != '0') {
1607 ++i;
1608 break;
1609 }
1610 }
1611 e += nd - i;
1612 nd = i;
1613 if (nd0 > nd)
1614 nd0 = nd;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001615
Mark Dickinson4141d652010-01-20 17:36:31 +00001616 /* Summary of parsing results. After parsing, and dealing with zero
1617 * inputs, we have values s0, nd0, nd, e, sign, where:
Mark Dickinson476279f2010-01-16 10:44:00 +00001618 *
Mark Dickinson4141d652010-01-20 17:36:31 +00001619 * - s0 points to the first significant digit of the input string
Mark Dickinson476279f2010-01-16 10:44:00 +00001620 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001621 * - nd is the total number of significant digits (here, and
1622 * below, 'significant digits' means the set of digits of the
1623 * significand of the input that remain after ignoring leading
Mark Dickinson4141d652010-01-20 17:36:31 +00001624 * and trailing zeros).
Mark Dickinson476279f2010-01-16 10:44:00 +00001625 *
Mark Dickinson4141d652010-01-20 17:36:31 +00001626 * - nd0 indicates the position of the decimal point, if present; it
1627 * satisfies 1 <= nd0 <= nd. The nd significant digits are in
1628 * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1629 * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
1630 * nd0 == nd, then s0[nd0] could be any non-digit character.)
Mark Dickinson476279f2010-01-16 10:44:00 +00001631 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001632 * - e is the adjusted exponent: the absolute value of the number
1633 * represented by the original input string is n * 10**e, where
1634 * n is the integer represented by the concatenation of
1635 * s0[0:nd0] and s0[nd0+1:nd+1]
Mark Dickinson476279f2010-01-16 10:44:00 +00001636 *
Mark Dickinson811ff822010-01-16 17:57:49 +00001637 * - sign gives the sign of the input: 1 for negative, 0 for positive
1638 *
1639 * - the first and last significant digits are nonzero
1640 */
1641
1642 /* put first DBL_DIG+1 digits into integer y and z.
Mark Dickinson476279f2010-01-16 10:44:00 +00001643 *
1644 * - y contains the value represented by the first min(9, nd)
1645 * significant digits
1646 *
1647 * - if nd > 9, z contains the value represented by significant digits
1648 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1649 * gives the value represented by the first min(16, nd) sig. digits.
1650 */
1651
Mark Dickinson4141d652010-01-20 17:36:31 +00001652 bc.e0 = e1 = e;
Mark Dickinson811ff822010-01-16 17:57:49 +00001653 y = z = 0;
1654 for (i = 0; i < nd; i++) {
1655 if (i < 9)
1656 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1657 else if (i < DBL_DIG+1)
1658 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1659 else
1660 break;
1661 }
1662
Mark Dickinsonbb282852009-10-24 12:13:30 +00001663 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1664 dval(&rv) = y;
1665 if (k > 9) {
1666 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1667 }
1668 bd0 = 0;
1669 if (nd <= DBL_DIG
1670 && Flt_Rounds == 1
1671 ) {
1672 if (!e)
1673 goto ret;
1674 if (e > 0) {
1675 if (e <= Ten_pmax) {
1676 dval(&rv) *= tens[e];
1677 goto ret;
1678 }
1679 i = DBL_DIG - nd;
1680 if (e <= Ten_pmax + i) {
1681 /* A fancier test would sometimes let us do
1682 * this for larger i values.
1683 */
1684 e -= i;
1685 dval(&rv) *= tens[i];
1686 dval(&rv) *= tens[e];
1687 goto ret;
1688 }
1689 }
1690 else if (e >= -Ten_pmax) {
1691 dval(&rv) /= tens[-e];
1692 goto ret;
1693 }
1694 }
1695 e1 += nd - k;
1696
1697 bc.scale = 0;
1698
1699 /* Get starting approximation = rv * 10**e1 */
1700
1701 if (e1 > 0) {
1702 if ((i = e1 & 15))
1703 dval(&rv) *= tens[i];
1704 if (e1 &= ~15) {
Mark Dickinson4141d652010-01-20 17:36:31 +00001705 if (e1 > DBL_MAX_10_EXP)
1706 goto ovfl;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001707 e1 >>= 4;
1708 for(j = 0; e1 > 1; j++, e1 >>= 1)
1709 if (e1 & 1)
1710 dval(&rv) *= bigtens[j];
1711 /* The last multiplication could overflow. */
1712 word0(&rv) -= P*Exp_msk1;
1713 dval(&rv) *= bigtens[j];
1714 if ((z = word0(&rv) & Exp_mask)
1715 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1716 goto ovfl;
1717 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1718 /* set to largest number */
1719 /* (Can't trust DBL_MAX) */
1720 word0(&rv) = Big0;
1721 word1(&rv) = Big1;
1722 }
1723 else
1724 word0(&rv) += P*Exp_msk1;
1725 }
1726 }
1727 else if (e1 < 0) {
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001728 /* The input decimal value lies in [10**e1, 10**(e1+16)).
1729
1730 If e1 <= -512, underflow immediately.
1731 If e1 <= -256, set bc.scale to 2*P.
1732
1733 So for input value < 1e-256, bc.scale is always set;
1734 for input value >= 1e-240, bc.scale is never set.
1735 For input values in [1e-256, 1e-240), bc.scale may or may
1736 not be set. */
1737
Mark Dickinsonbb282852009-10-24 12:13:30 +00001738 e1 = -e1;
1739 if ((i = e1 & 15))
1740 dval(&rv) /= tens[i];
1741 if (e1 >>= 4) {
1742 if (e1 >= 1 << n_bigtens)
1743 goto undfl;
1744 if (e1 & Scale_Bit)
1745 bc.scale = 2*P;
1746 for(j = 0; e1 > 0; j++, e1 >>= 1)
1747 if (e1 & 1)
1748 dval(&rv) *= tinytens[j];
1749 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1750 >> Exp_shift)) > 0) {
1751 /* scaled rv is denormal; clear j low bits */
1752 if (j >= 32) {
1753 word1(&rv) = 0;
1754 if (j >= 53)
1755 word0(&rv) = (P+2)*Exp_msk1;
1756 else
1757 word0(&rv) &= 0xffffffff << (j-32);
1758 }
1759 else
1760 word1(&rv) &= 0xffffffff << j;
1761 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001762 if (!dval(&rv))
1763 goto undfl;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001764 }
1765 }
1766
1767 /* Now the hard part -- adjusting rv to the correct value.*/
1768
1769 /* Put digits into bd: true value = bd * 10^e */
1770
1771 bc.nd = nd;
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001772 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001773 /* to silence an erroneous warning about bc.nd0 */
1774 /* possibly not being initialized. */
Mark Dickinson5a0b3992010-01-10 13:06:31 +00001775 if (nd > STRTOD_DIGLIM) {
1776 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001777 /* minimum number of decimal digits to distinguish double values */
1778 /* in IEEE arithmetic. */
Mark Dickinson476279f2010-01-16 10:44:00 +00001779
1780 /* Truncate input to 18 significant digits, then discard any trailing
1781 zeros on the result by updating nd, nd0, e and y suitably. (There's
1782 no need to update z; it's not reused beyond this point.) */
1783 for (i = 18; i > 0; ) {
1784 /* scan back until we hit a nonzero digit. significant digit 'i'
1785 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001786 --i;
Mark Dickinson476279f2010-01-16 10:44:00 +00001787 if (s0[i < nd0 ? i : i+1] != '0') {
1788 ++i;
1789 break;
1790 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00001791 }
1792 e += nd - i;
1793 nd = i;
1794 if (nd0 > nd)
1795 nd0 = nd;
1796 if (nd < 9) { /* must recompute y */
1797 y = 0;
1798 for(i = 0; i < nd0; ++i)
1799 y = 10*y + s0[i] - '0';
Mark Dickinson476279f2010-01-16 10:44:00 +00001800 for(; i < nd; ++i)
1801 y = 10*y + s0[i+1] - '0';
Mark Dickinsonbb282852009-10-24 12:13:30 +00001802 }
1803 }
Mark Dickinsond2a99402010-01-13 22:20:10 +00001804 bd0 = s2b(s0, nd0, nd, y);
Mark Dickinsonbb282852009-10-24 12:13:30 +00001805 if (bd0 == NULL)
1806 goto failed_malloc;
1807
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001808 /* Notation for the comments below. Write:
1809
1810 - dv for the absolute value of the number represented by the original
1811 decimal input string.
1812
1813 - if we've truncated dv, write tdv for the truncated value.
1814 Otherwise, set tdv == dv.
1815
1816 - srv for the quantity rv/2^bc.scale; so srv is the current binary
1817 approximation to tdv (and dv). It should be exactly representable
1818 in an IEEE 754 double.
1819 */
1820
Mark Dickinsonbb282852009-10-24 12:13:30 +00001821 for(;;) {
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001822
1823 /* This is the main correction loop for _Py_dg_strtod.
1824
1825 We've got a decimal value tdv, and a floating-point approximation
1826 srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
1827 close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1828 approximation if not.
1829
1830 To determine whether srv is close enough to tdv, compute integers
1831 bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1832 respectively, and then use integer arithmetic to determine whether
1833 |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1834 */
1835
Mark Dickinsonbb282852009-10-24 12:13:30 +00001836 bd = Balloc(bd0->k);
1837 if (bd == NULL) {
1838 Bfree(bd0);
1839 goto failed_malloc;
1840 }
1841 Bcopy(bd, bd0);
Mark Dickinsonadcda342010-01-22 17:04:07 +00001842 bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001843 if (bb == NULL) {
1844 Bfree(bd);
1845 Bfree(bd0);
1846 goto failed_malloc;
1847 }
Mark Dickinsonadcda342010-01-22 17:04:07 +00001848 /* Record whether lsb of bb is odd, in case we need this
1849 for the round-to-even step later. */
1850 odd = bb->x[0] & 1;
1851
1852 /* tdv = bd * 10**e; srv = bb * 2**bbe */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001853 bs = i2b(1);
1854 if (bs == NULL) {
1855 Bfree(bb);
1856 Bfree(bd);
1857 Bfree(bd0);
1858 goto failed_malloc;
1859 }
1860
1861 if (e >= 0) {
1862 bb2 = bb5 = 0;
1863 bd2 = bd5 = e;
1864 }
1865 else {
1866 bb2 = bb5 = -e;
1867 bd2 = bd5 = 0;
1868 }
1869 if (bbe >= 0)
1870 bb2 += bbe;
1871 else
1872 bd2 -= bbe;
1873 bs2 = bb2;
Mark Dickinsonadcda342010-01-22 17:04:07 +00001874 bb2++;
1875 bd2++;
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001876
Mark Dickinsonadcda342010-01-22 17:04:07 +00001877 /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
1878 and bs == 1, so:
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001879
Mark Dickinsonadcda342010-01-22 17:04:07 +00001880 tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1881 srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
1882 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001883
Mark Dickinsonadcda342010-01-22 17:04:07 +00001884 It follows that:
1885
1886 M * tdv = bd * 2**bd2 * 5**bd5
1887 M * srv = bb * 2**bb2 * 5**bb5
1888 M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1889
1890 for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1891 this fact is not needed below.)
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001892 */
1893
Mark Dickinsonadcda342010-01-22 17:04:07 +00001894 /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001895 i = bb2 < bd2 ? bb2 : bd2;
1896 if (i > bs2)
1897 i = bs2;
1898 if (i > 0) {
1899 bb2 -= i;
1900 bd2 -= i;
1901 bs2 -= i;
1902 }
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001903
1904 /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
Mark Dickinsonbb282852009-10-24 12:13:30 +00001905 if (bb5 > 0) {
1906 bs = pow5mult(bs, bb5);
1907 if (bs == NULL) {
1908 Bfree(bb);
1909 Bfree(bd);
1910 Bfree(bd0);
1911 goto failed_malloc;
1912 }
1913 bb1 = mult(bs, bb);
1914 Bfree(bb);
1915 bb = bb1;
1916 if (bb == NULL) {
1917 Bfree(bs);
1918 Bfree(bd);
1919 Bfree(bd0);
1920 goto failed_malloc;
1921 }
1922 }
1923 if (bb2 > 0) {
1924 bb = lshift(bb, bb2);
1925 if (bb == NULL) {
1926 Bfree(bs);
1927 Bfree(bd);
1928 Bfree(bd0);
1929 goto failed_malloc;
1930 }
1931 }
1932 if (bd5 > 0) {
1933 bd = pow5mult(bd, bd5);
1934 if (bd == NULL) {
1935 Bfree(bb);
1936 Bfree(bs);
1937 Bfree(bd0);
1938 goto failed_malloc;
1939 }
1940 }
1941 if (bd2 > 0) {
1942 bd = lshift(bd, bd2);
1943 if (bd == NULL) {
1944 Bfree(bb);
1945 Bfree(bs);
1946 Bfree(bd0);
1947 goto failed_malloc;
1948 }
1949 }
1950 if (bs2 > 0) {
1951 bs = lshift(bs, bs2);
1952 if (bs == NULL) {
1953 Bfree(bb);
1954 Bfree(bd);
1955 Bfree(bd0);
1956 goto failed_malloc;
1957 }
1958 }
Mark Dickinsonca6ea562010-01-20 21:23:25 +00001959
1960 /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
1961 respectively. Compute the difference |tdv - srv|, and compare
1962 with 0.5 ulp(srv). */
1963
Mark Dickinsonbb282852009-10-24 12:13:30 +00001964 delta = diff(bb, bd);
1965 if (delta == NULL) {
1966 Bfree(bb);
1967 Bfree(bs);
1968 Bfree(bd);
1969 Bfree(bd0);
1970 goto failed_malloc;
1971 }
Mark Dickinson4141d652010-01-20 17:36:31 +00001972 dsign = delta->sign;
Mark Dickinsonbb282852009-10-24 12:13:30 +00001973 delta->sign = 0;
1974 i = cmp(delta, bs);
1975 if (bc.nd > nd && i <= 0) {
Mark Dickinson4141d652010-01-20 17:36:31 +00001976 if (dsign)
Mark Dickinsonbb282852009-10-24 12:13:30 +00001977 break; /* Must use bigcomp(). */
Mark Dickinsonf8747c12010-01-14 14:40:20 +00001978
1979 /* Here rv overestimates the truncated decimal value by at most
1980 0.5 ulp(rv). Hence rv either overestimates the true decimal
1981 value by <= 0.5 ulp(rv), or underestimates it by some small
1982 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1983 the true decimal value, so it's possible to exit.
1984
1985 Exception: if scaled rv is a normal exact power of 2, but not
1986 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1987 next double, so the correctly rounded result is either rv - 0.5
1988 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1989
1990 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1991 /* rv can't be 0, since it's an overestimate for some
1992 nonzero value. So rv is a normal power of 2. */
1993 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1994 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1995 rv / 2^bc.scale >= 2^-1021. */
1996 if (j - bc.scale >= 2) {
1997 dval(&rv) -= 0.5 * sulp(&rv, &bc);
Mark Dickinson4141d652010-01-20 17:36:31 +00001998 break; /* Use bigcomp. */
Mark Dickinsonf8747c12010-01-14 14:40:20 +00001999 }
2000 }
2001
Mark Dickinsonbb282852009-10-24 12:13:30 +00002002 {
2003 bc.nd = nd;
2004 i = -1; /* Discarded digits make delta smaller. */
2005 }
2006 }
2007
2008 if (i < 0) {
2009 /* Error is less than half an ulp -- check for
2010 * special case of mantissa a power of two.
2011 */
Mark Dickinson4141d652010-01-20 17:36:31 +00002012 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
Mark Dickinsonbb282852009-10-24 12:13:30 +00002013 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2014 ) {
2015 break;
2016 }
2017 if (!delta->x[0] && delta->wds <= 1) {
2018 /* exact result */
2019 break;
2020 }
2021 delta = lshift(delta,Log2P);
2022 if (delta == NULL) {
2023 Bfree(bb);
2024 Bfree(bs);
2025 Bfree(bd);
2026 Bfree(bd0);
2027 goto failed_malloc;
2028 }
2029 if (cmp(delta, bs) > 0)
2030 goto drop_down;
2031 break;
2032 }
2033 if (i == 0) {
2034 /* exactly half-way between */
Mark Dickinson4141d652010-01-20 17:36:31 +00002035 if (dsign) {
Mark Dickinsonbb282852009-10-24 12:13:30 +00002036 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
2037 && word1(&rv) == (
2038 (bc.scale &&
2039 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
2040 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2041 0xffffffff)) {
2042 /*boundary case -- increment exponent*/
2043 word0(&rv) = (word0(&rv) & Exp_mask)
2044 + Exp_msk1
2045 ;
2046 word1(&rv) = 0;
Mark Dickinson4141d652010-01-20 17:36:31 +00002047 dsign = 0;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002048 break;
2049 }
2050 }
2051 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
2052 drop_down:
2053 /* boundary case -- decrement exponent */
2054 if (bc.scale) {
2055 L = word0(&rv) & Exp_mask;
2056 if (L <= (2*P+1)*Exp_msk1) {
2057 if (L > (P+2)*Exp_msk1)
2058 /* round even ==> */
2059 /* accept rv */
2060 break;
2061 /* rv = smallest denormal */
Mark Dickinson4141d652010-01-20 17:36:31 +00002062 if (bc.nd > nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002063 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002064 goto undfl;
2065 }
2066 }
2067 L = (word0(&rv) & Exp_mask) - Exp_msk1;
2068 word0(&rv) = L | Bndry_mask1;
2069 word1(&rv) = 0xffffffff;
2070 break;
2071 }
Mark Dickinsonadcda342010-01-22 17:04:07 +00002072 if (!odd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002073 break;
Mark Dickinson4141d652010-01-20 17:36:31 +00002074 if (dsign)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002075 dval(&rv) += ulp(&rv);
2076 else {
2077 dval(&rv) -= ulp(&rv);
2078 if (!dval(&rv)) {
Mark Dickinson5a0b3992010-01-10 13:06:31 +00002079 if (bc.nd >nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002080 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002081 goto undfl;
2082 }
2083 }
Mark Dickinson4141d652010-01-20 17:36:31 +00002084 dsign = 1 - dsign;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002085 break;
2086 }
2087 if ((aadj = ratio(delta, bs)) <= 2.) {
Mark Dickinson4141d652010-01-20 17:36:31 +00002088 if (dsign)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002089 aadj = aadj1 = 1.;
2090 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
2091 if (word1(&rv) == Tiny1 && !word0(&rv)) {
Mark Dickinson5a0b3992010-01-10 13:06:31 +00002092 if (bc.nd >nd)
Mark Dickinsonbb282852009-10-24 12:13:30 +00002093 break;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002094 goto undfl;
2095 }
2096 aadj = 1.;
2097 aadj1 = -1.;
2098 }
2099 else {
2100 /* special case -- power of FLT_RADIX to be */
2101 /* rounded down... */
2102
2103 if (aadj < 2./FLT_RADIX)
2104 aadj = 1./FLT_RADIX;
2105 else
2106 aadj *= 0.5;
2107 aadj1 = -aadj;
2108 }
2109 }
2110 else {
2111 aadj *= 0.5;
Mark Dickinson4141d652010-01-20 17:36:31 +00002112 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002113 if (Flt_Rounds == 0)
2114 aadj1 += 0.5;
2115 }
2116 y = word0(&rv) & Exp_mask;
2117
2118 /* Check for overflow */
2119
2120 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2121 dval(&rv0) = dval(&rv);
2122 word0(&rv) -= P*Exp_msk1;
2123 adj.d = aadj1 * ulp(&rv);
2124 dval(&rv) += adj.d;
2125 if ((word0(&rv) & Exp_mask) >=
2126 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
Mark Dickinson23df3d22010-01-17 13:37:57 +00002127 if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
2128 Bfree(bb);
2129 Bfree(bd);
2130 Bfree(bs);
2131 Bfree(bd0);
2132 Bfree(delta);
Mark Dickinsonbb282852009-10-24 12:13:30 +00002133 goto ovfl;
Mark Dickinson23df3d22010-01-17 13:37:57 +00002134 }
Mark Dickinsonbb282852009-10-24 12:13:30 +00002135 word0(&rv) = Big0;
2136 word1(&rv) = Big1;
2137 goto cont;
2138 }
2139 else
2140 word0(&rv) += P*Exp_msk1;
2141 }
2142 else {
2143 if (bc.scale && y <= 2*P*Exp_msk1) {
2144 if (aadj <= 0x7fffffff) {
2145 if ((z = (ULong)aadj) <= 0)
2146 z = 1;
2147 aadj = z;
Mark Dickinson4141d652010-01-20 17:36:31 +00002148 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonbb282852009-10-24 12:13:30 +00002149 }
2150 dval(&aadj2) = aadj1;
2151 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2152 aadj1 = dval(&aadj2);
2153 }
2154 adj.d = aadj1 * ulp(&rv);
2155 dval(&rv) += adj.d;
2156 }
2157 z = word0(&rv) & Exp_mask;
2158 if (bc.nd == nd) {
2159 if (!bc.scale)
2160 if (y == z) {
2161 /* Can we stop now? */
2162 L = (Long)aadj;
2163 aadj -= L;
2164 /* The tolerances below are conservative. */
Mark Dickinson4141d652010-01-20 17:36:31 +00002165 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
Mark Dickinsonbb282852009-10-24 12:13:30 +00002166 if (aadj < .4999999 || aadj > .5000001)
2167 break;
2168 }
2169 else if (aadj < .4999999/FLT_RADIX)
2170 break;
2171 }
2172 }
2173 cont:
2174 Bfree(bb);
2175 Bfree(bd);
2176 Bfree(bs);
2177 Bfree(delta);
2178 }
2179 Bfree(bb);
2180 Bfree(bd);
2181 Bfree(bs);
2182 Bfree(bd0);
2183 Bfree(delta);
2184 if (bc.nd > nd) {
2185 error = bigcomp(&rv, s0, &bc);
2186 if (error)
2187 goto failed_malloc;
2188 }
2189
2190 if (bc.scale) {
2191 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2192 word1(&rv0) = 0;
2193 dval(&rv) *= dval(&rv0);
Mark Dickinsonbb282852009-10-24 12:13:30 +00002194 }
Mark Dickinson4141d652010-01-20 17:36:31 +00002195
Mark Dickinsonbb282852009-10-24 12:13:30 +00002196 ret:
Mark Dickinsonbb282852009-10-24 12:13:30 +00002197 return sign ? -dval(&rv) : dval(&rv);
2198
Mark Dickinson4141d652010-01-20 17:36:31 +00002199 parse_error:
2200 return 0.0;
2201
Mark Dickinsonbb282852009-10-24 12:13:30 +00002202 failed_malloc:
Mark Dickinsonbb282852009-10-24 12:13:30 +00002203 errno = ENOMEM;
2204 return -1.0;
Mark Dickinson4141d652010-01-20 17:36:31 +00002205
2206 undfl:
2207 return sign ? -0.0 : 0.0;
2208
2209 ovfl:
2210 errno = ERANGE;
2211 /* Can't trust HUGE_VAL */
2212 word0(&rv) = Exp_mask;
2213 word1(&rv) = 0;
2214 return sign ? -dval(&rv) : dval(&rv);
2215
Mark Dickinsonbb282852009-10-24 12:13:30 +00002216}
2217
2218static char *
2219rv_alloc(int i)
2220{
2221 int j, k, *r;
2222
2223 j = sizeof(ULong);
2224 for(k = 0;
2225 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2226 j <<= 1)
2227 k++;
2228 r = (int*)Balloc(k);
2229 if (r == NULL)
2230 return NULL;
2231 *r = k;
2232 return (char *)(r+1);
2233}
2234
2235static char *
2236nrv_alloc(char *s, char **rve, int n)
2237{
2238 char *rv, *t;
2239
2240 rv = rv_alloc(n);
2241 if (rv == NULL)
2242 return NULL;
2243 t = rv;
2244 while((*t = *s++)) t++;
2245 if (rve)
2246 *rve = t;
2247 return rv;
2248}
2249
2250/* freedtoa(s) must be used to free values s returned by dtoa
2251 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2252 * but for consistency with earlier versions of dtoa, it is optional
2253 * when MULTIPLE_THREADS is not defined.
2254 */
2255
2256void
2257_Py_dg_freedtoa(char *s)
2258{
2259 Bigint *b = (Bigint *)((int *)s - 1);
2260 b->maxwds = 1 << (b->k = *(int*)b);
2261 Bfree(b);
2262}
2263
2264/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2265 *
2266 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2267 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2268 *
2269 * Modifications:
2270 * 1. Rather than iterating, we use a simple numeric overestimate
2271 * to determine k = floor(log10(d)). We scale relevant
2272 * quantities using O(log2(k)) rather than O(k) multiplications.
2273 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2274 * try to generate digits strictly left to right. Instead, we
2275 * compute with fewer bits and propagate the carry if necessary
2276 * when rounding the final digit up. This is often faster.
2277 * 3. Under the assumption that input will be rounded nearest,
2278 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2279 * That is, we allow equality in stopping tests when the
2280 * round-nearest rule will give the same floating-point value
2281 * as would satisfaction of the stopping test with strict
2282 * inequality.
2283 * 4. We remove common factors of powers of 2 from relevant
2284 * quantities.
2285 * 5. When converting floating-point integers less than 1e16,
2286 * we use floating-point arithmetic rather than resorting
2287 * to multiple-precision integers.
2288 * 6. When asked to produce fewer than 15 digits, we first try
2289 * to get by with floating-point arithmetic; we resort to
2290 * multiple-precision integer arithmetic only if we cannot
2291 * guarantee that the floating-point calculation has given
2292 * the correctly rounded result. For k requested digits and
2293 * "uniformly" distributed input, the probability is
2294 * something like 10^(k-15) that we must resort to the Long
2295 * calculation.
2296 */
2297
2298/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2299 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2300 call to _Py_dg_freedtoa. */
2301
2302char *
2303_Py_dg_dtoa(double dd, int mode, int ndigits,
2304 int *decpt, int *sign, char **rve)
2305{
2306 /* Arguments ndigits, decpt, sign are similar to those
2307 of ecvt and fcvt; trailing zeros are suppressed from
2308 the returned string. If not null, *rve is set to point
2309 to the end of the return value. If d is +-Infinity or NaN,
2310 then *decpt is set to 9999.
2311
2312 mode:
2313 0 ==> shortest string that yields d when read in
2314 and rounded to nearest.
2315 1 ==> like 0, but with Steele & White stopping rule;
2316 e.g. with IEEE P754 arithmetic , mode 0 gives
2317 1e23 whereas mode 1 gives 9.999999999999999e22.
2318 2 ==> max(1,ndigits) significant digits. This gives a
2319 return value similar to that of ecvt, except
2320 that trailing zeros are suppressed.
2321 3 ==> through ndigits past the decimal point. This
2322 gives a return value similar to that from fcvt,
2323 except that trailing zeros are suppressed, and
2324 ndigits can be negative.
2325 4,5 ==> similar to 2 and 3, respectively, but (in
2326 round-nearest mode) with the tests of mode 0 to
2327 possibly return a shorter string that rounds to d.
2328 With IEEE arithmetic and compilation with
2329 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2330 as modes 2 and 3 when FLT_ROUNDS != 1.
2331 6-9 ==> Debugging modes similar to mode - 4: don't try
2332 fast floating-point estimate (if applicable).
2333
2334 Values of mode other than 0-9 are treated as mode 0.
2335
2336 Sufficient space is allocated to the return value
2337 to hold the suppressed trailing zeros.
2338 */
2339
2340 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2341 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2342 spec_case, try_quick;
2343 Long L;
2344 int denorm;
2345 ULong x;
2346 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2347 U d2, eps, u;
2348 double ds;
2349 char *s, *s0;
2350
2351 /* set pointers to NULL, to silence gcc compiler warnings and make
2352 cleanup easier on error */
2353 mlo = mhi = b = S = 0;
2354 s0 = 0;
2355
2356 u.d = dd;
2357 if (word0(&u) & Sign_bit) {
2358 /* set sign for everything, including 0's and NaNs */
2359 *sign = 1;
2360 word0(&u) &= ~Sign_bit; /* clear sign bit */
2361 }
2362 else
2363 *sign = 0;
2364
2365 /* quick return for Infinities, NaNs and zeros */
2366 if ((word0(&u) & Exp_mask) == Exp_mask)
2367 {
2368 /* Infinity or NaN */
2369 *decpt = 9999;
2370 if (!word1(&u) && !(word0(&u) & 0xfffff))
2371 return nrv_alloc("Infinity", rve, 8);
2372 return nrv_alloc("NaN", rve, 3);
2373 }
2374 if (!dval(&u)) {
2375 *decpt = 1;
2376 return nrv_alloc("0", rve, 1);
2377 }
2378
2379 /* compute k = floor(log10(d)). The computation may leave k
2380 one too large, but should never leave k too small. */
2381 b = d2b(&u, &be, &bbits);
2382 if (b == NULL)
2383 goto failed_malloc;
2384 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2385 dval(&d2) = dval(&u);
2386 word0(&d2) &= Frac_mask1;
2387 word0(&d2) |= Exp_11;
2388
2389 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2390 * log10(x) = log(x) / log(10)
2391 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2392 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2393 *
2394 * This suggests computing an approximation k to log10(d) by
2395 *
2396 * k = (i - Bias)*0.301029995663981
2397 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2398 *
2399 * We want k to be too large rather than too small.
2400 * The error in the first-order Taylor series approximation
2401 * is in our favor, so we just round up the constant enough
2402 * to compensate for any error in the multiplication of
2403 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2404 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2405 * adding 1e-13 to the constant term more than suffices.
2406 * Hence we adjust the constant term to 0.1760912590558.
2407 * (We could get a more accurate k by invoking log10,
2408 * but this is probably not worthwhile.)
2409 */
2410
2411 i -= Bias;
2412 denorm = 0;
2413 }
2414 else {
2415 /* d is denormalized */
2416
2417 i = bbits + be + (Bias + (P-1) - 1);
2418 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2419 : word1(&u) << (32 - i);
2420 dval(&d2) = x;
2421 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2422 i -= (Bias + (P-1) - 1) + 1;
2423 denorm = 1;
2424 }
2425 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2426 i*0.301029995663981;
2427 k = (int)ds;
2428 if (ds < 0. && ds != k)
2429 k--; /* want k = floor(ds) */
2430 k_check = 1;
2431 if (k >= 0 && k <= Ten_pmax) {
2432 if (dval(&u) < tens[k])
2433 k--;
2434 k_check = 0;
2435 }
2436 j = bbits - i - 1;
2437 if (j >= 0) {
2438 b2 = 0;
2439 s2 = j;
2440 }
2441 else {
2442 b2 = -j;
2443 s2 = 0;
2444 }
2445 if (k >= 0) {
2446 b5 = 0;
2447 s5 = k;
2448 s2 += k;
2449 }
2450 else {
2451 b2 -= k;
2452 b5 = -k;
2453 s5 = 0;
2454 }
2455 if (mode < 0 || mode > 9)
2456 mode = 0;
2457
2458 try_quick = 1;
2459
2460 if (mode > 5) {
2461 mode -= 4;
2462 try_quick = 0;
2463 }
2464 leftright = 1;
2465 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2466 /* silence erroneous "gcc -Wall" warning. */
2467 switch(mode) {
2468 case 0:
2469 case 1:
2470 i = 18;
2471 ndigits = 0;
2472 break;
2473 case 2:
2474 leftright = 0;
2475 /* no break */
2476 case 4:
2477 if (ndigits <= 0)
2478 ndigits = 1;
2479 ilim = ilim1 = i = ndigits;
2480 break;
2481 case 3:
2482 leftright = 0;
2483 /* no break */
2484 case 5:
2485 i = ndigits + k + 1;
2486 ilim = i;
2487 ilim1 = i - 1;
2488 if (i <= 0)
2489 i = 1;
2490 }
2491 s0 = rv_alloc(i);
2492 if (s0 == NULL)
2493 goto failed_malloc;
2494 s = s0;
2495
2496
2497 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2498
2499 /* Try to get by with floating-point arithmetic. */
2500
2501 i = 0;
2502 dval(&d2) = dval(&u);
2503 k0 = k;
2504 ilim0 = ilim;
2505 ieps = 2; /* conservative */
2506 if (k > 0) {
2507 ds = tens[k&0xf];
2508 j = k >> 4;
2509 if (j & Bletch) {
2510 /* prevent overflows */
2511 j &= Bletch - 1;
2512 dval(&u) /= bigtens[n_bigtens-1];
2513 ieps++;
2514 }
2515 for(; j; j >>= 1, i++)
2516 if (j & 1) {
2517 ieps++;
2518 ds *= bigtens[i];
2519 }
2520 dval(&u) /= ds;
2521 }
2522 else if ((j1 = -k)) {
2523 dval(&u) *= tens[j1 & 0xf];
2524 for(j = j1 >> 4; j; j >>= 1, i++)
2525 if (j & 1) {
2526 ieps++;
2527 dval(&u) *= bigtens[i];
2528 }
2529 }
2530 if (k_check && dval(&u) < 1. && ilim > 0) {
2531 if (ilim1 <= 0)
2532 goto fast_failed;
2533 ilim = ilim1;
2534 k--;
2535 dval(&u) *= 10.;
2536 ieps++;
2537 }
2538 dval(&eps) = ieps*dval(&u) + 7.;
2539 word0(&eps) -= (P-1)*Exp_msk1;
2540 if (ilim == 0) {
2541 S = mhi = 0;
2542 dval(&u) -= 5.;
2543 if (dval(&u) > dval(&eps))
2544 goto one_digit;
2545 if (dval(&u) < -dval(&eps))
2546 goto no_digits;
2547 goto fast_failed;
2548 }
2549 if (leftright) {
2550 /* Use Steele & White method of only
2551 * generating digits needed.
2552 */
2553 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2554 for(i = 0;;) {
2555 L = (Long)dval(&u);
2556 dval(&u) -= L;
2557 *s++ = '0' + (int)L;
2558 if (dval(&u) < dval(&eps))
2559 goto ret1;
2560 if (1. - dval(&u) < dval(&eps))
2561 goto bump_up;
2562 if (++i >= ilim)
2563 break;
2564 dval(&eps) *= 10.;
2565 dval(&u) *= 10.;
2566 }
2567 }
2568 else {
2569 /* Generate ilim digits, then fix them up. */
2570 dval(&eps) *= tens[ilim-1];
2571 for(i = 1;; i++, dval(&u) *= 10.) {
2572 L = (Long)(dval(&u));
2573 if (!(dval(&u) -= L))
2574 ilim = i;
2575 *s++ = '0' + (int)L;
2576 if (i == ilim) {
2577 if (dval(&u) > 0.5 + dval(&eps))
2578 goto bump_up;
2579 else if (dval(&u) < 0.5 - dval(&eps)) {
2580 while(*--s == '0');
2581 s++;
2582 goto ret1;
2583 }
2584 break;
2585 }
2586 }
2587 }
2588 fast_failed:
2589 s = s0;
2590 dval(&u) = dval(&d2);
2591 k = k0;
2592 ilim = ilim0;
2593 }
2594
2595 /* Do we have a "small" integer? */
2596
2597 if (be >= 0 && k <= Int_max) {
2598 /* Yes. */
2599 ds = tens[k];
2600 if (ndigits < 0 && ilim <= 0) {
2601 S = mhi = 0;
2602 if (ilim < 0 || dval(&u) <= 5*ds)
2603 goto no_digits;
2604 goto one_digit;
2605 }
2606 for(i = 1;; i++, dval(&u) *= 10.) {
2607 L = (Long)(dval(&u) / ds);
2608 dval(&u) -= L*ds;
2609 *s++ = '0' + (int)L;
2610 if (!dval(&u)) {
2611 break;
2612 }
2613 if (i == ilim) {
2614 dval(&u) += dval(&u);
2615 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2616 bump_up:
2617 while(*--s == '9')
2618 if (s == s0) {
2619 k++;
2620 *s = '0';
2621 break;
2622 }
2623 ++*s++;
2624 }
2625 break;
2626 }
2627 }
2628 goto ret1;
2629 }
2630
2631 m2 = b2;
2632 m5 = b5;
2633 if (leftright) {
2634 i =
2635 denorm ? be + (Bias + (P-1) - 1 + 1) :
2636 1 + P - bbits;
2637 b2 += i;
2638 s2 += i;
2639 mhi = i2b(1);
2640 if (mhi == NULL)
2641 goto failed_malloc;
2642 }
2643 if (m2 > 0 && s2 > 0) {
2644 i = m2 < s2 ? m2 : s2;
2645 b2 -= i;
2646 m2 -= i;
2647 s2 -= i;
2648 }
2649 if (b5 > 0) {
2650 if (leftright) {
2651 if (m5 > 0) {
2652 mhi = pow5mult(mhi, m5);
2653 if (mhi == NULL)
2654 goto failed_malloc;
2655 b1 = mult(mhi, b);
2656 Bfree(b);
2657 b = b1;
2658 if (b == NULL)
2659 goto failed_malloc;
2660 }
2661 if ((j = b5 - m5)) {
2662 b = pow5mult(b, j);
2663 if (b == NULL)
2664 goto failed_malloc;
2665 }
2666 }
2667 else {
2668 b = pow5mult(b, b5);
2669 if (b == NULL)
2670 goto failed_malloc;
2671 }
2672 }
2673 S = i2b(1);
2674 if (S == NULL)
2675 goto failed_malloc;
2676 if (s5 > 0) {
2677 S = pow5mult(S, s5);
2678 if (S == NULL)
2679 goto failed_malloc;
2680 }
2681
2682 /* Check for special case that d is a normalized power of 2. */
2683
2684 spec_case = 0;
2685 if ((mode < 2 || leftright)
2686 ) {
2687 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2688 && word0(&u) & (Exp_mask & ~Exp_msk1)
2689 ) {
2690 /* The special case */
2691 b2 += Log2P;
2692 s2 += Log2P;
2693 spec_case = 1;
2694 }
2695 }
2696
2697 /* Arrange for convenient computation of quotients:
2698 * shift left if necessary so divisor has 4 leading 0 bits.
2699 *
2700 * Perhaps we should just compute leading 28 bits of S once
2701 * and for all and pass them and a shift to quorem, so it
2702 * can do shifts and ors to compute the numerator for q.
2703 */
2704 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
2705 i = 32 - i;
2706#define iInc 28
2707 i = dshift(S, s2);
2708 b2 += i;
2709 m2 += i;
2710 s2 += i;
2711 if (b2 > 0) {
2712 b = lshift(b, b2);
2713 if (b == NULL)
2714 goto failed_malloc;
2715 }
2716 if (s2 > 0) {
2717 S = lshift(S, s2);
2718 if (S == NULL)
2719 goto failed_malloc;
2720 }
2721 if (k_check) {
2722 if (cmp(b,S) < 0) {
2723 k--;
2724 b = multadd(b, 10, 0); /* we botched the k estimate */
2725 if (b == NULL)
2726 goto failed_malloc;
2727 if (leftright) {
2728 mhi = multadd(mhi, 10, 0);
2729 if (mhi == NULL)
2730 goto failed_malloc;
2731 }
2732 ilim = ilim1;
2733 }
2734 }
2735 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2736 if (ilim < 0) {
2737 /* no digits, fcvt style */
2738 no_digits:
2739 k = -1 - ndigits;
2740 goto ret;
2741 }
2742 else {
2743 S = multadd(S, 5, 0);
2744 if (S == NULL)
2745 goto failed_malloc;
2746 if (cmp(b, S) <= 0)
2747 goto no_digits;
2748 }
2749 one_digit:
2750 *s++ = '1';
2751 k++;
2752 goto ret;
2753 }
2754 if (leftright) {
2755 if (m2 > 0) {
2756 mhi = lshift(mhi, m2);
2757 if (mhi == NULL)
2758 goto failed_malloc;
2759 }
2760
2761 /* Compute mlo -- check for special case
2762 * that d is a normalized power of 2.
2763 */
2764
2765 mlo = mhi;
2766 if (spec_case) {
2767 mhi = Balloc(mhi->k);
2768 if (mhi == NULL)
2769 goto failed_malloc;
2770 Bcopy(mhi, mlo);
2771 mhi = lshift(mhi, Log2P);
2772 if (mhi == NULL)
2773 goto failed_malloc;
2774 }
2775
2776 for(i = 1;;i++) {
2777 dig = quorem(b,S) + '0';
2778 /* Do we yet have the shortest decimal string
2779 * that will round to d?
2780 */
2781 j = cmp(b, mlo);
2782 delta = diff(S, mhi);
2783 if (delta == NULL)
2784 goto failed_malloc;
2785 j1 = delta->sign ? 1 : cmp(b, delta);
2786 Bfree(delta);
2787 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2788 ) {
2789 if (dig == '9')
2790 goto round_9_up;
2791 if (j > 0)
2792 dig++;
2793 *s++ = dig;
2794 goto ret;
2795 }
2796 if (j < 0 || (j == 0 && mode != 1
2797 && !(word1(&u) & 1)
2798 )) {
2799 if (!b->x[0] && b->wds <= 1) {
2800 goto accept_dig;
2801 }
2802 if (j1 > 0) {
2803 b = lshift(b, 1);
2804 if (b == NULL)
2805 goto failed_malloc;
2806 j1 = cmp(b, S);
2807 if ((j1 > 0 || (j1 == 0 && dig & 1))
2808 && dig++ == '9')
2809 goto round_9_up;
2810 }
2811 accept_dig:
2812 *s++ = dig;
2813 goto ret;
2814 }
2815 if (j1 > 0) {
2816 if (dig == '9') { /* possible if i == 1 */
2817 round_9_up:
2818 *s++ = '9';
2819 goto roundoff;
2820 }
2821 *s++ = dig + 1;
2822 goto ret;
2823 }
2824 *s++ = dig;
2825 if (i == ilim)
2826 break;
2827 b = multadd(b, 10, 0);
2828 if (b == NULL)
2829 goto failed_malloc;
2830 if (mlo == mhi) {
2831 mlo = mhi = multadd(mhi, 10, 0);
2832 if (mlo == NULL)
2833 goto failed_malloc;
2834 }
2835 else {
2836 mlo = multadd(mlo, 10, 0);
2837 if (mlo == NULL)
2838 goto failed_malloc;
2839 mhi = multadd(mhi, 10, 0);
2840 if (mhi == NULL)
2841 goto failed_malloc;
2842 }
2843 }
2844 }
2845 else
2846 for(i = 1;; i++) {
2847 *s++ = dig = quorem(b,S) + '0';
2848 if (!b->x[0] && b->wds <= 1) {
2849 goto ret;
2850 }
2851 if (i >= ilim)
2852 break;
2853 b = multadd(b, 10, 0);
2854 if (b == NULL)
2855 goto failed_malloc;
2856 }
2857
2858 /* Round off last digit */
2859
2860 b = lshift(b, 1);
2861 if (b == NULL)
2862 goto failed_malloc;
2863 j = cmp(b, S);
2864 if (j > 0 || (j == 0 && dig & 1)) {
2865 roundoff:
2866 while(*--s == '9')
2867 if (s == s0) {
2868 k++;
2869 *s++ = '1';
2870 goto ret;
2871 }
2872 ++*s++;
2873 }
2874 else {
2875 while(*--s == '0');
2876 s++;
2877 }
2878 ret:
2879 Bfree(S);
2880 if (mhi) {
2881 if (mlo && mlo != mhi)
2882 Bfree(mlo);
2883 Bfree(mhi);
2884 }
2885 ret1:
2886 Bfree(b);
2887 *s = 0;
2888 *decpt = k + 1;
2889 if (rve)
2890 *rve = s;
2891 return s0;
2892 failed_malloc:
2893 if (S)
2894 Bfree(S);
2895 if (mlo && mlo != mhi)
2896 Bfree(mlo);
2897 if (mhi)
2898 Bfree(mhi);
2899 if (b)
2900 Bfree(b);
2901 if (s0)
2902 _Py_dg_freedtoa(s0);
2903 return NULL;
2904}
2905#ifdef __cplusplus
2906}
2907#endif
2908
2909#endif /* PY_NO_SHORT_FLOAT_REPR */