blob: 94eb2676e2480e4a6ec9919af8756271dd8b6bc6 [file] [log] [blame]
Mark Dickinsonb08a53a2009-04-16 19:52:09 +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.
Mark Dickinson7f0ea322009-04-17 16:06:28 +000024 *
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:
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000029 *
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 *
Mark Dickinson7f0ea322009-04-17 16:06:28 +000060 * 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 *
Mark Dickinson725bfd82009-05-03 20:33:40 +000064 * 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
65 * leading whitespace.
66 *
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000067 ***************************************************************/
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
119/* 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
Benjamin Peterson4fe55102016-09-06 11:58:01 -0700154typedef uint32_t ULong;
155typedef int32_t Long;
156typedef uint64_t ULLong;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000157
158#undef DEBUG
159#ifdef Py_DEBUG
160#define DEBUG
161#endif
162
163/* End Python #define linking */
164
165#ifdef DEBUG
166#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
167#endif
168
169#ifndef PRIVATE_MEM
170#define PRIVATE_MEM 2304
171#endif
172#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
173static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
174
175#ifdef __cplusplus
176extern "C" {
177#endif
178
179typedef union { double d; ULong L[2]; } U;
180
181#ifdef IEEE_8087
182#define word0(x) (x)->L[1]
183#define word1(x) (x)->L[0]
184#else
185#define word0(x) (x)->L[0]
186#define word1(x) (x)->L[1]
187#endif
188#define dval(x) (x)->d
189
190#ifndef STRTOD_DIGLIM
191#define STRTOD_DIGLIM 40
192#endif
193
Mark Dickinson81612e82010-01-12 23:04:19 +0000194/* maximum permitted exponent value for strtod; exponents larger than
195 MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
196 should fit into an int. */
197#ifndef MAX_ABS_EXP
Mark Dickinsonf45bbb62013-11-26 16:19:13 +0000198#define MAX_ABS_EXP 1100000000U
199#endif
200/* Bound on length of pieces of input strings in _Py_dg_strtod; specifically,
201 this is used to bound the total number of digits ignoring leading zeros and
202 the number of digits that follow the decimal point. Ideally, MAX_DIGITS
203 should satisfy MAX_DIGITS + 400 < MAX_ABS_EXP; that ensures that the
204 exponent clipping in _Py_dg_strtod can't affect the value of the output. */
205#ifndef MAX_DIGITS
206#define MAX_DIGITS 1000000000U
207#endif
208
209/* Guard against trying to use the above values on unusual platforms with ints
210 * of width less than 32 bits. */
211#if MAX_ABS_EXP > INT_MAX
212#error "MAX_ABS_EXP should fit in an int"
213#endif
214#if MAX_DIGITS > INT_MAX
215#error "MAX_DIGITS should fit in an int"
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000216#endif
217
218/* The following definition of Storeinc is appropriate for MIPS processors.
219 * An alternative that might be better on some machines is
220 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
221 */
222#if defined(IEEE_8087)
223#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
224 ((unsigned short *)a)[0] = (unsigned short)c, a++)
225#else
226#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
227 ((unsigned short *)a)[1] = (unsigned short)c, a++)
228#endif
229
230/* #define P DBL_MANT_DIG */
231/* Ten_pmax = floor(P*log(2)/log(5)) */
232/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
233/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
234/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
235
236#define Exp_shift 20
237#define Exp_shift1 20
238#define Exp_msk1 0x100000
239#define Exp_msk11 0x100000
240#define Exp_mask 0x7ff00000
241#define P 53
242#define Nbits 53
243#define Bias 1023
244#define Emax 1023
245#define Emin (-1022)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000246#define Etiny (-1074) /* smallest denormal is 2**Etiny */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000247#define Exp_1 0x3ff00000
248#define Exp_11 0x3ff00000
249#define Ebits 11
250#define Frac_mask 0xfffff
251#define Frac_mask1 0xfffff
252#define Ten_pmax 22
253#define Bletch 0x10
254#define Bndry_mask 0xfffff
255#define Bndry_mask1 0xfffff
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000256#define Sign_bit 0x80000000
257#define Log2P 1
258#define Tiny0 0
259#define Tiny1 1
260#define Quick_max 14
261#define Int_max 14
262
263#ifndef Flt_Rounds
264#ifdef FLT_ROUNDS
265#define Flt_Rounds FLT_ROUNDS
266#else
267#define Flt_Rounds 1
268#endif
269#endif /*Flt_Rounds*/
270
271#define Rounding Flt_Rounds
272
273#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
274#define Big1 0xffffffff
275
Mark Dickinsone383e822012-04-29 15:31:56 +0100276/* Standard NaN used by _Py_dg_stdnan. */
277
278#define NAN_WORD0 0x7ff80000
279#define NAN_WORD1 0
280
281/* Bits of the representation of positive infinity. */
282
283#define POSINF_WORD0 0x7ff00000
284#define POSINF_WORD1 0
285
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000286/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
287
288typedef struct BCinfo BCinfo;
289struct
290BCinfo {
Mark Dickinsonadd28232010-01-21 19:51:08 +0000291 int e0, nd, nd0, scale;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000292};
293
294#define FFFFFFFF 0xffffffffUL
295
296#define Kmax 7
297
298/* struct Bigint is used to represent arbitrary-precision integers. These
299 integers are stored in sign-magnitude format, with the magnitude stored as
300 an array of base 2**32 digits. Bigints are always normalized: if x is a
301 Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
302
303 The Bigint fields are as follows:
304
305 - next is a header used by Balloc and Bfree to keep track of lists
306 of freed Bigints; it's also used for the linked list of
307 powers of 5 of the form 5**2**i used by pow5mult.
308 - k indicates which pool this Bigint was allocated from
309 - maxwds is the maximum number of words space was allocated for
310 (usually maxwds == 2**k)
311 - sign is 1 for negative Bigints, 0 for positive. The sign is unused
312 (ignored on inputs, set to 0 on outputs) in almost all operations
313 involving Bigints: a notable exception is the diff function, which
314 ignores signs on inputs but sets the sign of the output correctly.
315 - wds is the actual number of significant words
316 - x contains the vector of words (digits) for this Bigint, from least
317 significant (x[0]) to most significant (x[wds-1]).
318*/
319
320struct
321Bigint {
322 struct Bigint *next;
323 int k, maxwds, sign, wds;
324 ULong x[1];
325};
326
327typedef struct Bigint Bigint;
328
Mark Dickinsonde508002010-01-17 21:02:55 +0000329#ifndef Py_USING_MEMORY_DEBUGGER
330
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000331/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
332 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
333 1 << k. These pools are maintained as linked lists, with freelist[k]
334 pointing to the head of the list for pool k.
335
336 On allocation, if there's no free slot in the appropriate pool, MALLOC is
337 called to get more memory. This memory is not returned to the system until
338 Python quits. There's also a private memory pool that's allocated from
339 in preference to using MALLOC.
340
341 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
342 decimal digits), memory is directly allocated using MALLOC, and freed using
343 FREE.
344
345 XXX: it would be easy to bypass this memory-management system and
346 translate each call to Balloc into a call to PyMem_Malloc, and each
347 Bfree to PyMem_Free. Investigate whether this has any significant
348 performance on impact. */
349
350static Bigint *freelist[Kmax+1];
351
352/* Allocate space for a Bigint with up to 1<<k digits */
353
354static Bigint *
355Balloc(int k)
356{
357 int x;
358 Bigint *rv;
359 unsigned int len;
360
361 if (k <= Kmax && (rv = freelist[k]))
362 freelist[k] = rv->next;
363 else {
364 x = 1 << k;
365 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
366 /sizeof(double);
Victor Stinner938b0b92015-03-18 15:01:44 +0100367 if (k <= Kmax && pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000368 rv = (Bigint*)pmem_next;
369 pmem_next += len;
370 }
371 else {
372 rv = (Bigint*)MALLOC(len*sizeof(double));
373 if (rv == NULL)
374 return NULL;
375 }
376 rv->k = k;
377 rv->maxwds = x;
378 }
379 rv->sign = rv->wds = 0;
380 return rv;
381}
382
383/* Free a Bigint allocated with Balloc */
384
385static void
386Bfree(Bigint *v)
387{
388 if (v) {
389 if (v->k > Kmax)
390 FREE((void*)v);
391 else {
392 v->next = freelist[v->k];
393 freelist[v->k] = v;
394 }
395 }
396}
397
Mark Dickinsonde508002010-01-17 21:02:55 +0000398#else
399
400/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
401 PyMem_Free directly in place of the custom memory allocation scheme above.
402 These are provided for the benefit of memory debugging tools like
403 Valgrind. */
404
405/* Allocate space for a Bigint with up to 1<<k digits */
406
407static Bigint *
408Balloc(int k)
409{
410 int x;
411 Bigint *rv;
412 unsigned int len;
413
414 x = 1 << k;
415 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
416 /sizeof(double);
417
418 rv = (Bigint*)MALLOC(len*sizeof(double));
419 if (rv == NULL)
420 return NULL;
421
422 rv->k = k;
423 rv->maxwds = x;
424 rv->sign = rv->wds = 0;
425 return rv;
426}
427
428/* Free a Bigint allocated with Balloc */
429
430static void
431Bfree(Bigint *v)
432{
433 if (v) {
434 FREE((void*)v);
435 }
436}
437
438#endif /* Py_USING_MEMORY_DEBUGGER */
439
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000440#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
441 y->wds*sizeof(Long) + 2*sizeof(int))
442
443/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
444 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
445 On failure, return NULL. In this case, b will have been already freed. */
446
447static Bigint *
448multadd(Bigint *b, int m, int a) /* multiply by m and add a */
449{
450 int i, wds;
451#ifdef ULLong
452 ULong *x;
453 ULLong carry, y;
454#else
455 ULong carry, *x, y;
456 ULong xi, z;
457#endif
458 Bigint *b1;
459
460 wds = b->wds;
461 x = b->x;
462 i = 0;
463 carry = a;
464 do {
465#ifdef ULLong
466 y = *x * (ULLong)m + carry;
467 carry = y >> 32;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000468 *x++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000469#else
470 xi = *x;
471 y = (xi & 0xffff) * m + carry;
472 z = (xi >> 16) * m + (y >> 16);
473 carry = z >> 16;
474 *x++ = (z << 16) + (y & 0xffff);
475#endif
476 }
477 while(++i < wds);
478 if (carry) {
479 if (wds >= b->maxwds) {
480 b1 = Balloc(b->k+1);
481 if (b1 == NULL){
482 Bfree(b);
483 return NULL;
484 }
485 Bcopy(b1, b);
486 Bfree(b);
487 b = b1;
488 }
489 b->x[wds++] = (ULong)carry;
490 b->wds = wds;
491 }
492 return b;
493}
494
495/* convert a string s containing nd decimal digits (possibly containing a
496 decimal separator at position nd0, which is ignored) to a Bigint. This
497 function carries on where the parsing code in _Py_dg_strtod leaves off: on
498 entry, y9 contains the result of converting the first 9 digits. Returns
499 NULL on failure. */
500
501static Bigint *
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000502s2b(const char *s, int nd0, int nd, ULong y9)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000503{
504 Bigint *b;
505 int i, k;
506 Long x, y;
507
508 x = (nd + 8) / 9;
509 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
510 b = Balloc(k);
511 if (b == NULL)
512 return NULL;
513 b->x[0] = y9;
514 b->wds = 1;
515
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000516 if (nd <= 9)
517 return b;
518
519 s += 9;
520 for (i = 9; i < nd0; i++) {
521 b = multadd(b, 10, *s++ - '0');
522 if (b == NULL)
523 return NULL;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000524 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000525 s++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000526 for(; i < nd; i++) {
527 b = multadd(b, 10, *s++ - '0');
528 if (b == NULL)
529 return NULL;
530 }
531 return b;
532}
533
534/* count leading 0 bits in the 32-bit integer x. */
535
536static int
537hi0bits(ULong x)
538{
539 int k = 0;
540
541 if (!(x & 0xffff0000)) {
542 k = 16;
543 x <<= 16;
544 }
545 if (!(x & 0xff000000)) {
546 k += 8;
547 x <<= 8;
548 }
549 if (!(x & 0xf0000000)) {
550 k += 4;
551 x <<= 4;
552 }
553 if (!(x & 0xc0000000)) {
554 k += 2;
555 x <<= 2;
556 }
557 if (!(x & 0x80000000)) {
558 k++;
559 if (!(x & 0x40000000))
560 return 32;
561 }
562 return k;
563}
564
565/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
566 number of bits. */
567
568static int
569lo0bits(ULong *y)
570{
571 int k;
572 ULong x = *y;
573
574 if (x & 7) {
575 if (x & 1)
576 return 0;
577 if (x & 2) {
578 *y = x >> 1;
579 return 1;
580 }
581 *y = x >> 2;
582 return 2;
583 }
584 k = 0;
585 if (!(x & 0xffff)) {
586 k = 16;
587 x >>= 16;
588 }
589 if (!(x & 0xff)) {
590 k += 8;
591 x >>= 8;
592 }
593 if (!(x & 0xf)) {
594 k += 4;
595 x >>= 4;
596 }
597 if (!(x & 0x3)) {
598 k += 2;
599 x >>= 2;
600 }
601 if (!(x & 1)) {
602 k++;
603 x >>= 1;
604 if (!x)
605 return 32;
606 }
607 *y = x;
608 return k;
609}
610
611/* convert a small nonnegative integer to a Bigint */
612
613static Bigint *
614i2b(int i)
615{
616 Bigint *b;
617
618 b = Balloc(1);
619 if (b == NULL)
620 return NULL;
621 b->x[0] = i;
622 b->wds = 1;
623 return b;
624}
625
626/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
627 the signs of a and b. */
628
629static Bigint *
630mult(Bigint *a, Bigint *b)
631{
632 Bigint *c;
633 int k, wa, wb, wc;
634 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
635 ULong y;
636#ifdef ULLong
637 ULLong carry, z;
638#else
639 ULong carry, z;
640 ULong z2;
641#endif
642
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000643 if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
644 c = Balloc(0);
645 if (c == NULL)
646 return NULL;
647 c->wds = 1;
648 c->x[0] = 0;
649 return c;
650 }
651
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000652 if (a->wds < b->wds) {
653 c = a;
654 a = b;
655 b = c;
656 }
657 k = a->k;
658 wa = a->wds;
659 wb = b->wds;
660 wc = wa + wb;
661 if (wc > a->maxwds)
662 k++;
663 c = Balloc(k);
664 if (c == NULL)
665 return NULL;
666 for(x = c->x, xa = x + wc; x < xa; x++)
667 *x = 0;
668 xa = a->x;
669 xae = xa + wa;
670 xb = b->x;
671 xbe = xb + wb;
672 xc0 = c->x;
673#ifdef ULLong
674 for(; xb < xbe; xc0++) {
675 if ((y = *xb++)) {
676 x = xa;
677 xc = xc0;
678 carry = 0;
679 do {
680 z = *x++ * (ULLong)y + *xc + carry;
681 carry = z >> 32;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000682 *xc++ = (ULong)(z & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000683 }
684 while(x < xae);
685 *xc = (ULong)carry;
686 }
687 }
688#else
689 for(; xb < xbe; xb++, xc0++) {
690 if (y = *xb & 0xffff) {
691 x = xa;
692 xc = xc0;
693 carry = 0;
694 do {
695 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
696 carry = z >> 16;
697 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
698 carry = z2 >> 16;
699 Storeinc(xc, z2, z);
700 }
701 while(x < xae);
702 *xc = carry;
703 }
704 if (y = *xb >> 16) {
705 x = xa;
706 xc = xc0;
707 carry = 0;
708 z2 = *xc;
709 do {
710 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
711 carry = z >> 16;
712 Storeinc(xc, z, z2);
713 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
714 carry = z2 >> 16;
715 }
716 while(x < xae);
717 *xc = z2;
718 }
719 }
720#endif
721 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
722 c->wds = wc;
723 return c;
724}
725
Mark Dickinsonde508002010-01-17 21:02:55 +0000726#ifndef Py_USING_MEMORY_DEBUGGER
727
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000728/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
729
730static Bigint *p5s;
731
732/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
733 failure; if the returned pointer is distinct from b then the original
734 Bigint b will have been Bfree'd. Ignores the sign of b. */
735
736static Bigint *
737pow5mult(Bigint *b, int k)
738{
739 Bigint *b1, *p5, *p51;
740 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200741 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000742
743 if ((i = k & 3)) {
744 b = multadd(b, p05[i-1], 0);
745 if (b == NULL)
746 return NULL;
747 }
748
749 if (!(k >>= 2))
750 return b;
751 p5 = p5s;
752 if (!p5) {
753 /* first time */
754 p5 = i2b(625);
755 if (p5 == NULL) {
756 Bfree(b);
757 return NULL;
758 }
759 p5s = p5;
760 p5->next = 0;
761 }
762 for(;;) {
763 if (k & 1) {
764 b1 = mult(b, p5);
765 Bfree(b);
766 b = b1;
767 if (b == NULL)
768 return NULL;
769 }
770 if (!(k >>= 1))
771 break;
772 p51 = p5->next;
773 if (!p51) {
774 p51 = mult(p5,p5);
775 if (p51 == NULL) {
776 Bfree(b);
777 return NULL;
778 }
779 p51->next = 0;
780 p5->next = p51;
781 }
782 p5 = p51;
783 }
784 return b;
785}
786
Mark Dickinsonde508002010-01-17 21:02:55 +0000787#else
788
789/* Version of pow5mult that doesn't cache powers of 5. Provided for
790 the benefit of memory debugging tools like Valgrind. */
791
792static Bigint *
793pow5mult(Bigint *b, int k)
794{
795 Bigint *b1, *p5, *p51;
796 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200797 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonde508002010-01-17 21:02:55 +0000798
799 if ((i = k & 3)) {
800 b = multadd(b, p05[i-1], 0);
801 if (b == NULL)
802 return NULL;
803 }
804
805 if (!(k >>= 2))
806 return b;
807 p5 = i2b(625);
808 if (p5 == NULL) {
809 Bfree(b);
810 return NULL;
811 }
812
813 for(;;) {
814 if (k & 1) {
815 b1 = mult(b, p5);
816 Bfree(b);
817 b = b1;
818 if (b == NULL) {
819 Bfree(p5);
820 return NULL;
821 }
822 }
823 if (!(k >>= 1))
824 break;
825 p51 = mult(p5, p5);
826 Bfree(p5);
827 p5 = p51;
828 if (p5 == NULL) {
829 Bfree(b);
830 return NULL;
831 }
832 }
833 Bfree(p5);
834 return b;
835}
836
837#endif /* Py_USING_MEMORY_DEBUGGER */
838
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000839/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
840 or NULL on failure. If the returned pointer is distinct from b then the
841 original b will have been Bfree'd. Ignores the sign of b. */
842
843static Bigint *
844lshift(Bigint *b, int k)
845{
846 int i, k1, n, n1;
847 Bigint *b1;
848 ULong *x, *x1, *xe, z;
849
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000850 if (!k || (!b->x[0] && b->wds == 1))
851 return b;
852
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000853 n = k >> 5;
854 k1 = b->k;
855 n1 = n + b->wds + 1;
856 for(i = b->maxwds; n1 > i; i <<= 1)
857 k1++;
858 b1 = Balloc(k1);
859 if (b1 == NULL) {
860 Bfree(b);
861 return NULL;
862 }
863 x1 = b1->x;
864 for(i = 0; i < n; i++)
865 *x1++ = 0;
866 x = b->x;
867 xe = x + b->wds;
868 if (k &= 0x1f) {
869 k1 = 32 - k;
870 z = 0;
871 do {
872 *x1++ = *x << k | z;
873 z = *x++ >> k1;
874 }
875 while(x < xe);
876 if ((*x1 = z))
877 ++n1;
878 }
879 else do
880 *x1++ = *x++;
881 while(x < xe);
882 b1->wds = n1 - 1;
883 Bfree(b);
884 return b1;
885}
886
887/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
888 1 if a > b. Ignores signs of a and b. */
889
890static int
891cmp(Bigint *a, Bigint *b)
892{
893 ULong *xa, *xa0, *xb, *xb0;
894 int i, j;
895
896 i = a->wds;
897 j = b->wds;
898#ifdef DEBUG
899 if (i > 1 && !a->x[i-1])
900 Bug("cmp called with a->x[a->wds-1] == 0");
901 if (j > 1 && !b->x[j-1])
902 Bug("cmp called with b->x[b->wds-1] == 0");
903#endif
904 if (i -= j)
905 return i;
906 xa0 = a->x;
907 xa = xa0 + j;
908 xb0 = b->x;
909 xb = xb0 + j;
910 for(;;) {
911 if (*--xa != *--xb)
912 return *xa < *xb ? -1 : 1;
913 if (xa <= xa0)
914 break;
915 }
916 return 0;
917}
918
919/* Take the difference of Bigints a and b, returning a new Bigint. Returns
920 NULL on failure. The signs of a and b are ignored, but the sign of the
921 result is set appropriately. */
922
923static Bigint *
924diff(Bigint *a, Bigint *b)
925{
926 Bigint *c;
927 int i, wa, wb;
928 ULong *xa, *xae, *xb, *xbe, *xc;
929#ifdef ULLong
930 ULLong borrow, y;
931#else
932 ULong borrow, y;
933 ULong z;
934#endif
935
936 i = cmp(a,b);
937 if (!i) {
938 c = Balloc(0);
939 if (c == NULL)
940 return NULL;
941 c->wds = 1;
942 c->x[0] = 0;
943 return c;
944 }
945 if (i < 0) {
946 c = a;
947 a = b;
948 b = c;
949 i = 1;
950 }
951 else
952 i = 0;
953 c = Balloc(a->k);
954 if (c == NULL)
955 return NULL;
956 c->sign = i;
957 wa = a->wds;
958 xa = a->x;
959 xae = xa + wa;
960 wb = b->wds;
961 xb = b->x;
962 xbe = xb + wb;
963 xc = c->x;
964 borrow = 0;
965#ifdef ULLong
966 do {
967 y = (ULLong)*xa++ - *xb++ - borrow;
968 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000969 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000970 }
971 while(xb < xbe);
972 while(xa < xae) {
973 y = *xa++ - borrow;
974 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000975 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000976 }
977#else
978 do {
979 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
980 borrow = (y & 0x10000) >> 16;
981 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
982 borrow = (z & 0x10000) >> 16;
983 Storeinc(xc, z, y);
984 }
985 while(xb < xbe);
986 while(xa < xae) {
987 y = (*xa & 0xffff) - borrow;
988 borrow = (y & 0x10000) >> 16;
989 z = (*xa++ >> 16) - borrow;
990 borrow = (z & 0x10000) >> 16;
991 Storeinc(xc, z, y);
992 }
993#endif
994 while(!*--xc)
995 wa--;
996 c->wds = wa;
997 return c;
998}
999
Mark Dickinsonadd28232010-01-21 19:51:08 +00001000/* Given a positive normal double x, return the difference between x and the
1001 next double up. Doesn't give correct results for subnormals. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001002
1003static double
1004ulp(U *x)
1005{
1006 Long L;
1007 U u;
1008
1009 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1010 word0(&u) = L;
1011 word1(&u) = 0;
1012 return dval(&u);
1013}
1014
1015/* Convert a Bigint to a double plus an exponent */
1016
1017static double
1018b2d(Bigint *a, int *e)
1019{
1020 ULong *xa, *xa0, w, y, z;
1021 int k;
1022 U d;
1023
1024 xa0 = a->x;
1025 xa = xa0 + a->wds;
1026 y = *--xa;
1027#ifdef DEBUG
1028 if (!y) Bug("zero y in b2d");
1029#endif
1030 k = hi0bits(y);
1031 *e = 32 - k;
1032 if (k < Ebits) {
1033 word0(&d) = Exp_1 | y >> (Ebits - k);
1034 w = xa > xa0 ? *--xa : 0;
1035 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
1036 goto ret_d;
1037 }
1038 z = xa > xa0 ? *--xa : 0;
1039 if (k -= Ebits) {
1040 word0(&d) = Exp_1 | y << k | z >> (32 - k);
1041 y = xa > xa0 ? *--xa : 0;
1042 word1(&d) = z << k | y >> (32 - k);
1043 }
1044 else {
1045 word0(&d) = Exp_1 | y;
1046 word1(&d) = z;
1047 }
1048 ret_d:
1049 return dval(&d);
1050}
1051
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001052/* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
1053 except that it accepts the scale parameter used in _Py_dg_strtod (which
1054 should be either 0 or 2*P), and the normalization for the return value is
1055 different (see below). On input, d should be finite and nonnegative, and d
1056 / 2**scale should be exactly representable as an IEEE 754 double.
1057
1058 Returns a Bigint b and an integer e such that
1059
1060 dval(d) / 2**scale = b * 2**e.
1061
1062 Unlike d2b, b is not necessarily odd: b and e are normalized so
1063 that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
1064 and e == Etiny. This applies equally to an input of 0.0: in that
1065 case the return values are b = 0 and e = Etiny.
1066
1067 The above normalization ensures that for all possible inputs d,
1068 2**e gives ulp(d/2**scale).
1069
1070 Returns NULL on failure.
1071*/
1072
1073static Bigint *
1074sd2b(U *d, int scale, int *e)
1075{
1076 Bigint *b;
1077
1078 b = Balloc(1);
1079 if (b == NULL)
1080 return NULL;
Victor Stinner938b0b92015-03-18 15:01:44 +01001081
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001082 /* First construct b and e assuming that scale == 0. */
1083 b->wds = 2;
1084 b->x[0] = word1(d);
1085 b->x[1] = word0(d) & Frac_mask;
1086 *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
1087 if (*e < Etiny)
1088 *e = Etiny;
1089 else
1090 b->x[1] |= Exp_msk1;
1091
1092 /* Now adjust for scale, provided that b != 0. */
1093 if (scale && (b->x[0] || b->x[1])) {
1094 *e -= scale;
1095 if (*e < Etiny) {
1096 scale = Etiny - *e;
1097 *e = Etiny;
1098 /* We can't shift more than P-1 bits without shifting out a 1. */
1099 assert(0 < scale && scale <= P - 1);
1100 if (scale >= 32) {
1101 /* The bits shifted out should all be zero. */
1102 assert(b->x[0] == 0);
1103 b->x[0] = b->x[1];
1104 b->x[1] = 0;
1105 scale -= 32;
1106 }
1107 if (scale) {
1108 /* The bits shifted out should all be zero. */
1109 assert(b->x[0] << (32 - scale) == 0);
1110 b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1111 b->x[1] >>= scale;
1112 }
1113 }
1114 }
1115 /* Ensure b is normalized. */
1116 if (!b->x[1])
1117 b->wds = 1;
1118
1119 return b;
1120}
1121
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001122/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
1123
1124 Given a finite nonzero double d, return an odd Bigint b and exponent *e
1125 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
Mark Dickinson180e4cd2010-01-04 21:33:31 +00001126 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001127
1128 If d is zero, then b == 0, *e == -1010, *bbits = 0.
1129 */
1130
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001131static Bigint *
1132d2b(U *d, int *e, int *bits)
1133{
1134 Bigint *b;
1135 int de, k;
1136 ULong *x, y, z;
1137 int i;
1138
1139 b = Balloc(1);
1140 if (b == NULL)
1141 return NULL;
1142 x = b->x;
1143
1144 z = word0(d) & Frac_mask;
1145 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
1146 if ((de = (int)(word0(d) >> Exp_shift)))
1147 z |= Exp_msk1;
1148 if ((y = word1(d))) {
1149 if ((k = lo0bits(&y))) {
1150 x[0] = y | z << (32 - k);
1151 z >>= k;
1152 }
1153 else
1154 x[0] = y;
1155 i =
1156 b->wds = (x[1] = z) ? 2 : 1;
1157 }
1158 else {
1159 k = lo0bits(&z);
1160 x[0] = z;
1161 i =
1162 b->wds = 1;
1163 k += 32;
1164 }
1165 if (de) {
1166 *e = de - Bias - (P-1) + k;
1167 *bits = P - k;
1168 }
1169 else {
1170 *e = de - Bias - (P-1) + 1 + k;
1171 *bits = 32*i - hi0bits(x[i-1]);
1172 }
1173 return b;
1174}
1175
1176/* Compute the ratio of two Bigints, as a double. The result may have an
1177 error of up to 2.5 ulps. */
1178
1179static double
1180ratio(Bigint *a, Bigint *b)
1181{
1182 U da, db;
1183 int k, ka, kb;
1184
1185 dval(&da) = b2d(a, &ka);
1186 dval(&db) = b2d(b, &kb);
1187 k = ka - kb + 32*(a->wds - b->wds);
1188 if (k > 0)
1189 word0(&da) += k*Exp_msk1;
1190 else {
1191 k = -k;
1192 word0(&db) += k*Exp_msk1;
1193 }
1194 return dval(&da) / dval(&db);
1195}
1196
1197static const double
1198tens[] = {
1199 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1200 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1201 1e20, 1e21, 1e22
1202};
1203
1204static const double
1205bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1206static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1207 9007199254740992.*9007199254740992.e-256
1208 /* = 2^106 * 1e-256 */
1209};
1210/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1211/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1212#define Scale_Bit 0x10
1213#define n_bigtens 5
1214
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001215#define ULbits 32
1216#define kshift 5
1217#define kmask 31
1218
1219
1220static int
1221dshift(Bigint *b, int p2)
1222{
1223 int rv = hi0bits(b->x[b->wds-1]) - 4;
1224 if (p2 > 0)
1225 rv -= p2;
1226 return rv & kmask;
1227}
1228
1229/* special case of Bigint division. The quotient is always in the range 0 <=
1230 quotient < 10, and on entry the divisor S is normalized so that its top 4
1231 bits (28--31) are zero and bit 27 is set. */
1232
1233static int
1234quorem(Bigint *b, Bigint *S)
1235{
1236 int n;
1237 ULong *bx, *bxe, q, *sx, *sxe;
1238#ifdef ULLong
1239 ULLong borrow, carry, y, ys;
1240#else
1241 ULong borrow, carry, y, ys;
1242 ULong si, z, zs;
1243#endif
1244
1245 n = S->wds;
1246#ifdef DEBUG
1247 /*debug*/ if (b->wds > n)
1248 /*debug*/ Bug("oversize b in quorem");
1249#endif
1250 if (b->wds < n)
1251 return 0;
1252 sx = S->x;
1253 sxe = sx + --n;
1254 bx = b->x;
1255 bxe = bx + n;
1256 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1257#ifdef DEBUG
1258 /*debug*/ if (q > 9)
1259 /*debug*/ Bug("oversized quotient in quorem");
1260#endif
1261 if (q) {
1262 borrow = 0;
1263 carry = 0;
1264 do {
1265#ifdef ULLong
1266 ys = *sx++ * (ULLong)q + carry;
1267 carry = ys >> 32;
1268 y = *bx - (ys & FFFFFFFF) - borrow;
1269 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001270 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001271#else
1272 si = *sx++;
1273 ys = (si & 0xffff) * q + carry;
1274 zs = (si >> 16) * q + (ys >> 16);
1275 carry = zs >> 16;
1276 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1277 borrow = (y & 0x10000) >> 16;
1278 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1279 borrow = (z & 0x10000) >> 16;
1280 Storeinc(bx, z, y);
1281#endif
1282 }
1283 while(sx <= sxe);
1284 if (!*bxe) {
1285 bx = b->x;
1286 while(--bxe > bx && !*bxe)
1287 --n;
1288 b->wds = n;
1289 }
1290 }
1291 if (cmp(b, S) >= 0) {
1292 q++;
1293 borrow = 0;
1294 carry = 0;
1295 bx = b->x;
1296 sx = S->x;
1297 do {
1298#ifdef ULLong
1299 ys = *sx++ + carry;
1300 carry = ys >> 32;
1301 y = *bx - (ys & FFFFFFFF) - borrow;
1302 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001303 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001304#else
1305 si = *sx++;
1306 ys = (si & 0xffff) + carry;
1307 zs = (si >> 16) + (ys >> 16);
1308 carry = zs >> 16;
1309 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1310 borrow = (y & 0x10000) >> 16;
1311 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1312 borrow = (z & 0x10000) >> 16;
1313 Storeinc(bx, z, y);
1314#endif
1315 }
1316 while(sx <= sxe);
1317 bx = b->x;
1318 bxe = bx + n;
1319 if (!*bxe) {
1320 while(--bxe > bx && !*bxe)
1321 --n;
1322 b->wds = n;
1323 }
1324 }
1325 return q;
1326}
1327
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001328/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
Mark Dickinson81612e82010-01-12 23:04:19 +00001329
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001330 Assuming that x is finite and nonnegative (positive zero is fine
1331 here) and x / 2^bc.scale is exactly representable as a double,
1332 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
Mark Dickinson81612e82010-01-12 23:04:19 +00001333
1334static double
1335sulp(U *x, BCinfo *bc)
1336{
1337 U u;
1338
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001339 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00001340 /* rv/2^bc->scale is subnormal */
1341 word0(&u) = (P+2)*Exp_msk1;
1342 word1(&u) = 0;
1343 return u.d;
1344 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001345 else {
1346 assert(word0(x) || word1(x)); /* x != 0.0 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001347 return ulp(x);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001348 }
Mark Dickinson81612e82010-01-12 23:04:19 +00001349}
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001350
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001351/* The bigcomp function handles some hard cases for strtod, for inputs
1352 with more than STRTOD_DIGLIM digits. It's called once an initial
1353 estimate for the double corresponding to the input string has
1354 already been obtained by the code in _Py_dg_strtod.
1355
1356 The bigcomp function is only called after _Py_dg_strtod has found a
1357 double value rv such that either rv or rv + 1ulp represents the
1358 correctly rounded value corresponding to the original string. It
1359 determines which of these two values is the correct one by
1360 computing the decimal digits of rv + 0.5ulp and comparing them with
1361 the corresponding digits of s0.
1362
1363 In the following, write dv for the absolute value of the number represented
1364 by the input string.
1365
1366 Inputs:
1367
1368 s0 points to the first significant digit of the input string.
1369
1370 rv is a (possibly scaled) estimate for the closest double value to the
1371 value represented by the original input to _Py_dg_strtod. If
1372 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1373 the input value.
1374
1375 bc is a struct containing information gathered during the parsing and
1376 estimation steps of _Py_dg_strtod. Description of fields follows:
1377
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001378 bc->e0 gives the exponent of the input value, such that dv = (integer
1379 given by the bd->nd digits of s0) * 10**e0
1380
1381 bc->nd gives the total number of significant digits of s0. It will
1382 be at least 1.
1383
1384 bc->nd0 gives the number of significant digits of s0 before the
1385 decimal separator. If there's no decimal separator, bc->nd0 ==
1386 bc->nd.
1387
1388 bc->scale is the value used to scale rv to avoid doing arithmetic with
1389 subnormal values. It's either 0 or 2*P (=106).
1390
1391 Outputs:
1392
1393 On successful exit, rv/2^(bc->scale) is the closest double to dv.
1394
1395 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001396
1397static int
1398bigcomp(U *rv, const char *s0, BCinfo *bc)
1399{
1400 Bigint *b, *d;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001401 int b2, d2, dd, i, nd, nd0, odd, p2, p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001402
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001403 nd = bc->nd;
1404 nd0 = bc->nd0;
Mark Dickinson81612e82010-01-12 23:04:19 +00001405 p5 = nd + bc->e0;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001406 b = sd2b(rv, bc->scale, &p2);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001407 if (b == NULL)
1408 return -1;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001409
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001410 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1411 case, this is used for round to even. */
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001412 odd = b->x[0] & 1;
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001413
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001414 /* left shift b by 1 bit and or a 1 into the least significant bit;
1415 this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1416 b = lshift(b, 1);
1417 if (b == NULL)
1418 return -1;
1419 b->x[0] |= 1;
1420 p2--;
1421
1422 p2 -= p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001423 d = i2b(1);
1424 if (d == NULL) {
1425 Bfree(b);
1426 return -1;
1427 }
1428 /* Arrange for convenient computation of quotients:
1429 * shift left if necessary so divisor has 4 leading 0 bits.
1430 */
1431 if (p5 > 0) {
1432 d = pow5mult(d, p5);
1433 if (d == NULL) {
1434 Bfree(b);
1435 return -1;
1436 }
1437 }
1438 else if (p5 < 0) {
1439 b = pow5mult(b, -p5);
1440 if (b == NULL) {
1441 Bfree(d);
1442 return -1;
1443 }
1444 }
1445 if (p2 > 0) {
1446 b2 = p2;
1447 d2 = 0;
1448 }
1449 else {
1450 b2 = 0;
1451 d2 = -p2;
1452 }
1453 i = dshift(d, d2);
1454 if ((b2 += i) > 0) {
1455 b = lshift(b, b2);
1456 if (b == NULL) {
1457 Bfree(d);
1458 return -1;
1459 }
1460 }
1461 if ((d2 += i) > 0) {
1462 d = lshift(d, d2);
1463 if (d == NULL) {
1464 Bfree(b);
1465 return -1;
1466 }
1467 }
1468
Mark Dickinsonadd28232010-01-21 19:51:08 +00001469 /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1470 * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
1471 * a number in the range [0.1, 1). */
1472 if (cmp(b, d) >= 0)
1473 /* b/d >= 1 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001474 dd = -1;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001475 else {
1476 i = 0;
1477 for(;;) {
1478 b = multadd(b, 10, 0);
1479 if (b == NULL) {
1480 Bfree(d);
1481 return -1;
1482 }
1483 dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1484 i++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001485
Mark Dickinsonadd28232010-01-21 19:51:08 +00001486 if (dd)
1487 break;
1488 if (!b->x[0] && b->wds == 1) {
1489 /* b/d == 0 */
1490 dd = i < nd;
1491 break;
1492 }
1493 if (!(i < nd)) {
1494 /* b/d != 0, but digits of s0 exhausted */
1495 dd = -1;
1496 break;
1497 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001498 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001499 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001500 Bfree(b);
1501 Bfree(d);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001502 if (dd > 0 || (dd == 0 && odd))
1503 dval(rv) += sulp(rv, bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001504 return 0;
1505}
1506
Mark Dickinsone383e822012-04-29 15:31:56 +01001507/* Return a 'standard' NaN value.
1508
1509 There are exactly two quiet NaNs that don't arise by 'quieting' signaling
1510 NaNs (see IEEE 754-2008, section 6.2.1). If sign == 0, return the one whose
1511 sign bit is cleared. Otherwise, return the one whose sign bit is set.
1512*/
1513
1514double
1515_Py_dg_stdnan(int sign)
1516{
1517 U rv;
1518 word0(&rv) = NAN_WORD0;
1519 word1(&rv) = NAN_WORD1;
1520 if (sign)
1521 word0(&rv) |= Sign_bit;
1522 return dval(&rv);
1523}
1524
1525/* Return positive or negative infinity, according to the given sign (0 for
1526 * positive infinity, 1 for negative infinity). */
1527
1528double
1529_Py_dg_infinity(int sign)
1530{
1531 U rv;
1532 word0(&rv) = POSINF_WORD0;
1533 word1(&rv) = POSINF_WORD1;
1534 return sign ? -dval(&rv) : dval(&rv);
1535}
1536
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001537double
1538_Py_dg_strtod(const char *s00, char **se)
1539{
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001540 int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1541 int esign, i, j, k, lz, nd, nd0, odd, sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001542 const char *s, *s0, *s1;
1543 double aadj, aadj1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001544 U aadj2, adj, rv, rv0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001545 ULong y, z, abs_exp;
Mark Dickinson45b63652010-01-16 18:10:25 +00001546 Long L;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001547 BCinfo bc;
1548 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001549 size_t ndigits, fraclen;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001550
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001551 dval(&rv) = 0.;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001552
1553 /* Start parsing. */
1554 c = *(s = s00);
1555
1556 /* Parse optional sign, if present. */
1557 sign = 0;
1558 switch (c) {
1559 case '-':
1560 sign = 1;
1561 /* no break */
1562 case '+':
1563 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001564 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001565
1566 /* Skip leading zeros: lz is true iff there were leading zeros. */
1567 s1 = s;
1568 while (c == '0')
1569 c = *++s;
1570 lz = s != s1;
1571
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001572 /* Point s0 at the first nonzero digit (if any). fraclen will be the
1573 number of digits between the decimal point and the end of the
1574 digit string. ndigits will be the total number of digits ignoring
1575 leading zeros. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001576 s0 = s1 = s;
1577 while ('0' <= c && c <= '9')
1578 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001579 ndigits = s - s1;
1580 fraclen = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001581
1582 /* Parse decimal point and following digits. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001583 if (c == '.') {
1584 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001585 if (!ndigits) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001586 s1 = s;
1587 while (c == '0')
1588 c = *++s;
1589 lz = lz || s != s1;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001590 fraclen += (s - s1);
Mark Dickinsonadd28232010-01-21 19:51:08 +00001591 s0 = s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001592 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001593 s1 = s;
1594 while ('0' <= c && c <= '9')
1595 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001596 ndigits += s - s1;
1597 fraclen += s - s1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001598 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001599
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001600 /* Now lz is true if and only if there were leading zero digits, and
1601 ndigits gives the total number of digits ignoring leading zeros. A
1602 valid input must have at least one digit. */
1603 if (!ndigits && !lz) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001604 if (se)
1605 *se = (char *)s00;
1606 goto parse_error;
1607 }
1608
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001609 /* Range check ndigits and fraclen to make sure that they, and values
1610 computed with them, can safely fit in an int. */
1611 if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) {
1612 if (se)
1613 *se = (char *)s00;
1614 goto parse_error;
1615 }
1616 nd = (int)ndigits;
1617 nd0 = (int)ndigits - (int)fraclen;
1618
Mark Dickinsonadd28232010-01-21 19:51:08 +00001619 /* Parse exponent. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001620 e = 0;
1621 if (c == 'e' || c == 'E') {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001622 s00 = s;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001623 c = *++s;
1624
1625 /* Exponent sign. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001626 esign = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001627 switch (c) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001628 case '-':
1629 esign = 1;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001630 /* no break */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001631 case '+':
1632 c = *++s;
1633 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001634
1635 /* Skip zeros. lz is true iff there are leading zeros. */
1636 s1 = s;
1637 while (c == '0')
1638 c = *++s;
1639 lz = s != s1;
1640
1641 /* Get absolute value of the exponent. */
1642 s1 = s;
1643 abs_exp = 0;
1644 while ('0' <= c && c <= '9') {
1645 abs_exp = 10*abs_exp + (c - '0');
1646 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001647 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001648
1649 /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
1650 there are at most 9 significant exponent digits then overflow is
1651 impossible. */
1652 if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1653 e = (int)MAX_ABS_EXP;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001654 else
Mark Dickinsonadd28232010-01-21 19:51:08 +00001655 e = (int)abs_exp;
1656 if (esign)
1657 e = -e;
1658
1659 /* A valid exponent must have at least one digit. */
1660 if (s == s1 && !lz)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001661 s = s00;
1662 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001663
1664 /* Adjust exponent to take into account position of the point. */
1665 e -= nd - nd0;
1666 if (nd0 <= 0)
Mark Dickinson45b63652010-01-16 18:10:25 +00001667 nd0 = nd;
1668
Mark Dickinsonadd28232010-01-21 19:51:08 +00001669 /* Finished parsing. Set se to indicate how far we parsed */
1670 if (se)
1671 *se = (char *)s;
1672
1673 /* If all digits were zero, exit with return value +-0.0. Otherwise,
1674 strip trailing zeros: scan back until we hit a nonzero digit. */
1675 if (!nd)
1676 goto ret;
Mark Dickinson45b63652010-01-16 18:10:25 +00001677 for (i = nd; i > 0; ) {
Mark Dickinson45b63652010-01-16 18:10:25 +00001678 --i;
1679 if (s0[i < nd0 ? i : i+1] != '0') {
1680 ++i;
1681 break;
1682 }
1683 }
1684 e += nd - i;
1685 nd = i;
1686 if (nd0 > nd)
1687 nd0 = nd;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001688
Mark Dickinsonadd28232010-01-21 19:51:08 +00001689 /* Summary of parsing results. After parsing, and dealing with zero
1690 * inputs, we have values s0, nd0, nd, e, sign, where:
Mark Dickinson45b63652010-01-16 18:10:25 +00001691 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001692 * - s0 points to the first significant digit of the input string
Mark Dickinson45b63652010-01-16 18:10:25 +00001693 *
1694 * - nd is the total number of significant digits (here, and
1695 * below, 'significant digits' means the set of digits of the
1696 * significand of the input that remain after ignoring leading
Mark Dickinsonadd28232010-01-21 19:51:08 +00001697 * and trailing zeros).
Mark Dickinson45b63652010-01-16 18:10:25 +00001698 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001699 * - nd0 indicates the position of the decimal point, if present; it
1700 * satisfies 1 <= nd0 <= nd. The nd significant digits are in
1701 * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1702 * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
1703 * nd0 == nd, then s0[nd0] could be any non-digit character.)
Mark Dickinson45b63652010-01-16 18:10:25 +00001704 *
1705 * - e is the adjusted exponent: the absolute value of the number
1706 * represented by the original input string is n * 10**e, where
1707 * n is the integer represented by the concatenation of
1708 * s0[0:nd0] and s0[nd0+1:nd+1]
1709 *
1710 * - sign gives the sign of the input: 1 for negative, 0 for positive
1711 *
1712 * - the first and last significant digits are nonzero
1713 */
1714
1715 /* put first DBL_DIG+1 digits into integer y and z.
1716 *
1717 * - y contains the value represented by the first min(9, nd)
1718 * significant digits
1719 *
1720 * - if nd > 9, z contains the value represented by significant digits
1721 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1722 * gives the value represented by the first min(16, nd) sig. digits.
1723 */
1724
Mark Dickinsonadd28232010-01-21 19:51:08 +00001725 bc.e0 = e1 = e;
Mark Dickinson45b63652010-01-16 18:10:25 +00001726 y = z = 0;
1727 for (i = 0; i < nd; i++) {
1728 if (i < 9)
1729 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1730 else if (i < DBL_DIG+1)
1731 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1732 else
1733 break;
1734 }
1735
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001736 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1737 dval(&rv) = y;
1738 if (k > 9) {
1739 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1740 }
1741 bd0 = 0;
1742 if (nd <= DBL_DIG
1743 && Flt_Rounds == 1
1744 ) {
1745 if (!e)
1746 goto ret;
1747 if (e > 0) {
1748 if (e <= Ten_pmax) {
1749 dval(&rv) *= tens[e];
1750 goto ret;
1751 }
1752 i = DBL_DIG - nd;
1753 if (e <= Ten_pmax + i) {
1754 /* A fancier test would sometimes let us do
1755 * this for larger i values.
1756 */
1757 e -= i;
1758 dval(&rv) *= tens[i];
1759 dval(&rv) *= tens[e];
1760 goto ret;
1761 }
1762 }
1763 else if (e >= -Ten_pmax) {
1764 dval(&rv) /= tens[-e];
1765 goto ret;
1766 }
1767 }
1768 e1 += nd - k;
1769
1770 bc.scale = 0;
1771
1772 /* Get starting approximation = rv * 10**e1 */
1773
1774 if (e1 > 0) {
1775 if ((i = e1 & 15))
1776 dval(&rv) *= tens[i];
1777 if (e1 &= ~15) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001778 if (e1 > DBL_MAX_10_EXP)
1779 goto ovfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001780 e1 >>= 4;
1781 for(j = 0; e1 > 1; j++, e1 >>= 1)
1782 if (e1 & 1)
1783 dval(&rv) *= bigtens[j];
1784 /* The last multiplication could overflow. */
1785 word0(&rv) -= P*Exp_msk1;
1786 dval(&rv) *= bigtens[j];
1787 if ((z = word0(&rv) & Exp_mask)
1788 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1789 goto ovfl;
1790 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1791 /* set to largest number */
1792 /* (Can't trust DBL_MAX) */
1793 word0(&rv) = Big0;
1794 word1(&rv) = Big1;
1795 }
1796 else
1797 word0(&rv) += P*Exp_msk1;
1798 }
1799 }
1800 else if (e1 < 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001801 /* The input decimal value lies in [10**e1, 10**(e1+16)).
1802
1803 If e1 <= -512, underflow immediately.
1804 If e1 <= -256, set bc.scale to 2*P.
1805
1806 So for input value < 1e-256, bc.scale is always set;
1807 for input value >= 1e-240, bc.scale is never set.
1808 For input values in [1e-256, 1e-240), bc.scale may or may
1809 not be set. */
1810
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001811 e1 = -e1;
1812 if ((i = e1 & 15))
1813 dval(&rv) /= tens[i];
1814 if (e1 >>= 4) {
1815 if (e1 >= 1 << n_bigtens)
1816 goto undfl;
1817 if (e1 & Scale_Bit)
1818 bc.scale = 2*P;
1819 for(j = 0; e1 > 0; j++, e1 >>= 1)
1820 if (e1 & 1)
1821 dval(&rv) *= tinytens[j];
1822 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1823 >> Exp_shift)) > 0) {
1824 /* scaled rv is denormal; clear j low bits */
1825 if (j >= 32) {
1826 word1(&rv) = 0;
1827 if (j >= 53)
1828 word0(&rv) = (P+2)*Exp_msk1;
1829 else
1830 word0(&rv) &= 0xffffffff << (j-32);
1831 }
1832 else
1833 word1(&rv) &= 0xffffffff << j;
1834 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001835 if (!dval(&rv))
1836 goto undfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001837 }
1838 }
1839
1840 /* Now the hard part -- adjusting rv to the correct value.*/
1841
1842 /* Put digits into bd: true value = bd * 10^e */
1843
1844 bc.nd = nd;
Mark Dickinson81612e82010-01-12 23:04:19 +00001845 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001846 /* to silence an erroneous warning about bc.nd0 */
1847 /* possibly not being initialized. */
Mark Dickinson81612e82010-01-12 23:04:19 +00001848 if (nd > STRTOD_DIGLIM) {
1849 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001850 /* minimum number of decimal digits to distinguish double values */
1851 /* in IEEE arithmetic. */
Mark Dickinson45b63652010-01-16 18:10:25 +00001852
1853 /* Truncate input to 18 significant digits, then discard any trailing
1854 zeros on the result by updating nd, nd0, e and y suitably. (There's
1855 no need to update z; it's not reused beyond this point.) */
1856 for (i = 18; i > 0; ) {
1857 /* scan back until we hit a nonzero digit. significant digit 'i'
1858 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001859 --i;
Mark Dickinson45b63652010-01-16 18:10:25 +00001860 if (s0[i < nd0 ? i : i+1] != '0') {
1861 ++i;
1862 break;
1863 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001864 }
1865 e += nd - i;
1866 nd = i;
1867 if (nd0 > nd)
1868 nd0 = nd;
1869 if (nd < 9) { /* must recompute y */
1870 y = 0;
1871 for(i = 0; i < nd0; ++i)
1872 y = 10*y + s0[i] - '0';
Mark Dickinson45b63652010-01-16 18:10:25 +00001873 for(; i < nd; ++i)
1874 y = 10*y + s0[i+1] - '0';
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001875 }
1876 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001877 bd0 = s2b(s0, nd0, nd, y);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001878 if (bd0 == NULL)
1879 goto failed_malloc;
1880
Mark Dickinsonadd28232010-01-21 19:51:08 +00001881 /* Notation for the comments below. Write:
1882
1883 - dv for the absolute value of the number represented by the original
1884 decimal input string.
1885
1886 - if we've truncated dv, write tdv for the truncated value.
1887 Otherwise, set tdv == dv.
1888
1889 - srv for the quantity rv/2^bc.scale; so srv is the current binary
1890 approximation to tdv (and dv). It should be exactly representable
1891 in an IEEE 754 double.
1892 */
1893
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001894 for(;;) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001895
1896 /* This is the main correction loop for _Py_dg_strtod.
1897
1898 We've got a decimal value tdv, and a floating-point approximation
1899 srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
1900 close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1901 approximation if not.
1902
1903 To determine whether srv is close enough to tdv, compute integers
1904 bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1905 respectively, and then use integer arithmetic to determine whether
1906 |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1907 */
1908
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001909 bd = Balloc(bd0->k);
1910 if (bd == NULL) {
1911 Bfree(bd0);
1912 goto failed_malloc;
1913 }
1914 Bcopy(bd, bd0);
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001915 bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001916 if (bb == NULL) {
1917 Bfree(bd);
1918 Bfree(bd0);
1919 goto failed_malloc;
1920 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001921 /* Record whether lsb of bb is odd, in case we need this
1922 for the round-to-even step later. */
1923 odd = bb->x[0] & 1;
1924
1925 /* tdv = bd * 10**e; srv = bb * 2**bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001926 bs = i2b(1);
1927 if (bs == NULL) {
1928 Bfree(bb);
1929 Bfree(bd);
1930 Bfree(bd0);
1931 goto failed_malloc;
1932 }
1933
1934 if (e >= 0) {
1935 bb2 = bb5 = 0;
1936 bd2 = bd5 = e;
1937 }
1938 else {
1939 bb2 = bb5 = -e;
1940 bd2 = bd5 = 0;
1941 }
1942 if (bbe >= 0)
1943 bb2 += bbe;
1944 else
1945 bd2 -= bbe;
1946 bs2 = bb2;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001947 bb2++;
1948 bd2++;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001949
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001950 /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
Mark Dickinsone383e822012-04-29 15:31:56 +01001951 and bs == 1, so:
Mark Dickinsonadd28232010-01-21 19:51:08 +00001952
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001953 tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1954 srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
Mark Dickinsone383e822012-04-29 15:31:56 +01001955 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001956
Mark Dickinsone383e822012-04-29 15:31:56 +01001957 It follows that:
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001958
1959 M * tdv = bd * 2**bd2 * 5**bd5
1960 M * srv = bb * 2**bb2 * 5**bb5
1961 M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1962
Mark Dickinsone383e822012-04-29 15:31:56 +01001963 for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1964 this fact is not needed below.)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001965 */
1966
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001967 /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001968 i = bb2 < bd2 ? bb2 : bd2;
1969 if (i > bs2)
1970 i = bs2;
1971 if (i > 0) {
1972 bb2 -= i;
1973 bd2 -= i;
1974 bs2 -= i;
1975 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001976
1977 /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001978 if (bb5 > 0) {
1979 bs = pow5mult(bs, bb5);
1980 if (bs == NULL) {
1981 Bfree(bb);
1982 Bfree(bd);
1983 Bfree(bd0);
1984 goto failed_malloc;
1985 }
1986 bb1 = mult(bs, bb);
1987 Bfree(bb);
1988 bb = bb1;
1989 if (bb == NULL) {
1990 Bfree(bs);
1991 Bfree(bd);
1992 Bfree(bd0);
1993 goto failed_malloc;
1994 }
1995 }
1996 if (bb2 > 0) {
1997 bb = lshift(bb, bb2);
1998 if (bb == NULL) {
1999 Bfree(bs);
2000 Bfree(bd);
2001 Bfree(bd0);
2002 goto failed_malloc;
2003 }
2004 }
2005 if (bd5 > 0) {
2006 bd = pow5mult(bd, bd5);
2007 if (bd == NULL) {
2008 Bfree(bb);
2009 Bfree(bs);
2010 Bfree(bd0);
2011 goto failed_malloc;
2012 }
2013 }
2014 if (bd2 > 0) {
2015 bd = lshift(bd, bd2);
2016 if (bd == NULL) {
2017 Bfree(bb);
2018 Bfree(bs);
2019 Bfree(bd0);
2020 goto failed_malloc;
2021 }
2022 }
2023 if (bs2 > 0) {
2024 bs = lshift(bs, bs2);
2025 if (bs == NULL) {
2026 Bfree(bb);
2027 Bfree(bd);
2028 Bfree(bd0);
2029 goto failed_malloc;
2030 }
2031 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00002032
2033 /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
2034 respectively. Compute the difference |tdv - srv|, and compare
2035 with 0.5 ulp(srv). */
2036
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002037 delta = diff(bb, bd);
2038 if (delta == NULL) {
2039 Bfree(bb);
2040 Bfree(bs);
2041 Bfree(bd);
2042 Bfree(bd0);
2043 goto failed_malloc;
2044 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00002045 dsign = delta->sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002046 delta->sign = 0;
2047 i = cmp(delta, bs);
2048 if (bc.nd > nd && i <= 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00002049 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002050 break; /* Must use bigcomp(). */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00002051
2052 /* Here rv overestimates the truncated decimal value by at most
2053 0.5 ulp(rv). Hence rv either overestimates the true decimal
2054 value by <= 0.5 ulp(rv), or underestimates it by some small
2055 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
2056 the true decimal value, so it's possible to exit.
2057
2058 Exception: if scaled rv is a normal exact power of 2, but not
2059 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
2060 next double, so the correctly rounded result is either rv - 0.5
2061 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
2062
2063 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
2064 /* rv can't be 0, since it's an overestimate for some
2065 nonzero value. So rv is a normal power of 2. */
2066 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
2067 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
2068 rv / 2^bc.scale >= 2^-1021. */
2069 if (j - bc.scale >= 2) {
2070 dval(&rv) -= 0.5 * sulp(&rv, &bc);
Mark Dickinsonadd28232010-01-21 19:51:08 +00002071 break; /* Use bigcomp. */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00002072 }
2073 }
2074
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002075 {
2076 bc.nd = nd;
2077 i = -1; /* Discarded digits make delta smaller. */
2078 }
2079 }
2080
2081 if (i < 0) {
2082 /* Error is less than half an ulp -- check for
2083 * special case of mantissa a power of two.
2084 */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002085 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002086 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2087 ) {
2088 break;
2089 }
2090 if (!delta->x[0] && delta->wds <= 1) {
2091 /* exact result */
2092 break;
2093 }
2094 delta = lshift(delta,Log2P);
2095 if (delta == NULL) {
2096 Bfree(bb);
2097 Bfree(bs);
2098 Bfree(bd);
2099 Bfree(bd0);
2100 goto failed_malloc;
2101 }
2102 if (cmp(delta, bs) > 0)
2103 goto drop_down;
2104 break;
2105 }
2106 if (i == 0) {
2107 /* exactly half-way between */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002108 if (dsign) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002109 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
2110 && word1(&rv) == (
2111 (bc.scale &&
2112 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
2113 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2114 0xffffffff)) {
2115 /*boundary case -- increment exponent*/
2116 word0(&rv) = (word0(&rv) & Exp_mask)
2117 + Exp_msk1
2118 ;
2119 word1(&rv) = 0;
Brett Cannonb94767f2011-02-22 20:15:44 +00002120 /* dsign = 0; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002121 break;
2122 }
2123 }
2124 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
2125 drop_down:
2126 /* boundary case -- decrement exponent */
2127 if (bc.scale) {
2128 L = word0(&rv) & Exp_mask;
2129 if (L <= (2*P+1)*Exp_msk1) {
2130 if (L > (P+2)*Exp_msk1)
2131 /* round even ==> */
2132 /* accept rv */
2133 break;
2134 /* rv = smallest denormal */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002135 if (bc.nd > nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002136 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002137 goto undfl;
2138 }
2139 }
2140 L = (word0(&rv) & Exp_mask) - Exp_msk1;
2141 word0(&rv) = L | Bndry_mask1;
2142 word1(&rv) = 0xffffffff;
2143 break;
2144 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002145 if (!odd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002146 break;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002147 if (dsign)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002148 dval(&rv) += sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002149 else {
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002150 dval(&rv) -= sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002151 if (!dval(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002152 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002153 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002154 goto undfl;
2155 }
2156 }
Brett Cannonb94767f2011-02-22 20:15:44 +00002157 /* dsign = 1 - dsign; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002158 break;
2159 }
2160 if ((aadj = ratio(delta, bs)) <= 2.) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00002161 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002162 aadj = aadj1 = 1.;
2163 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
2164 if (word1(&rv) == Tiny1 && !word0(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002165 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002166 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002167 goto undfl;
2168 }
2169 aadj = 1.;
2170 aadj1 = -1.;
2171 }
2172 else {
2173 /* special case -- power of FLT_RADIX to be */
2174 /* rounded down... */
2175
2176 if (aadj < 2./FLT_RADIX)
2177 aadj = 1./FLT_RADIX;
2178 else
2179 aadj *= 0.5;
2180 aadj1 = -aadj;
2181 }
2182 }
2183 else {
2184 aadj *= 0.5;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002185 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002186 if (Flt_Rounds == 0)
2187 aadj1 += 0.5;
2188 }
2189 y = word0(&rv) & Exp_mask;
2190
2191 /* Check for overflow */
2192
2193 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2194 dval(&rv0) = dval(&rv);
2195 word0(&rv) -= P*Exp_msk1;
2196 adj.d = aadj1 * ulp(&rv);
2197 dval(&rv) += adj.d;
2198 if ((word0(&rv) & Exp_mask) >=
2199 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002200 if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
2201 Bfree(bb);
2202 Bfree(bd);
2203 Bfree(bs);
2204 Bfree(bd0);
2205 Bfree(delta);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002206 goto ovfl;
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002207 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002208 word0(&rv) = Big0;
2209 word1(&rv) = Big1;
2210 goto cont;
2211 }
2212 else
2213 word0(&rv) += P*Exp_msk1;
2214 }
2215 else {
2216 if (bc.scale && y <= 2*P*Exp_msk1) {
2217 if (aadj <= 0x7fffffff) {
2218 if ((z = (ULong)aadj) <= 0)
2219 z = 1;
2220 aadj = z;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002221 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002222 }
2223 dval(&aadj2) = aadj1;
2224 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2225 aadj1 = dval(&aadj2);
2226 }
2227 adj.d = aadj1 * ulp(&rv);
2228 dval(&rv) += adj.d;
2229 }
2230 z = word0(&rv) & Exp_mask;
2231 if (bc.nd == nd) {
2232 if (!bc.scale)
2233 if (y == z) {
2234 /* Can we stop now? */
2235 L = (Long)aadj;
2236 aadj -= L;
2237 /* The tolerances below are conservative. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002238 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002239 if (aadj < .4999999 || aadj > .5000001)
2240 break;
2241 }
2242 else if (aadj < .4999999/FLT_RADIX)
2243 break;
2244 }
2245 }
2246 cont:
2247 Bfree(bb);
2248 Bfree(bd);
2249 Bfree(bs);
2250 Bfree(delta);
2251 }
2252 Bfree(bb);
2253 Bfree(bd);
2254 Bfree(bs);
2255 Bfree(bd0);
2256 Bfree(delta);
2257 if (bc.nd > nd) {
2258 error = bigcomp(&rv, s0, &bc);
2259 if (error)
2260 goto failed_malloc;
2261 }
2262
2263 if (bc.scale) {
2264 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2265 word1(&rv0) = 0;
2266 dval(&rv) *= dval(&rv0);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002267 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00002268
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002269 ret:
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002270 return sign ? -dval(&rv) : dval(&rv);
2271
Mark Dickinsonadd28232010-01-21 19:51:08 +00002272 parse_error:
2273 return 0.0;
2274
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002275 failed_malloc:
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002276 errno = ENOMEM;
2277 return -1.0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002278
2279 undfl:
2280 return sign ? -0.0 : 0.0;
2281
2282 ovfl:
2283 errno = ERANGE;
2284 /* Can't trust HUGE_VAL */
2285 word0(&rv) = Exp_mask;
2286 word1(&rv) = 0;
2287 return sign ? -dval(&rv) : dval(&rv);
2288
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002289}
2290
2291static char *
2292rv_alloc(int i)
2293{
2294 int j, k, *r;
2295
2296 j = sizeof(ULong);
2297 for(k = 0;
2298 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2299 j <<= 1)
2300 k++;
2301 r = (int*)Balloc(k);
2302 if (r == NULL)
2303 return NULL;
2304 *r = k;
2305 return (char *)(r+1);
2306}
2307
2308static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002309nrv_alloc(const char *s, char **rve, int n)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002310{
2311 char *rv, *t;
2312
2313 rv = rv_alloc(n);
2314 if (rv == NULL)
2315 return NULL;
2316 t = rv;
2317 while((*t = *s++)) t++;
2318 if (rve)
2319 *rve = t;
2320 return rv;
2321}
2322
2323/* freedtoa(s) must be used to free values s returned by dtoa
2324 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2325 * but for consistency with earlier versions of dtoa, it is optional
2326 * when MULTIPLE_THREADS is not defined.
2327 */
2328
2329void
2330_Py_dg_freedtoa(char *s)
2331{
2332 Bigint *b = (Bigint *)((int *)s - 1);
2333 b->maxwds = 1 << (b->k = *(int*)b);
2334 Bfree(b);
2335}
2336
2337/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2338 *
2339 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2340 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2341 *
2342 * Modifications:
2343 * 1. Rather than iterating, we use a simple numeric overestimate
2344 * to determine k = floor(log10(d)). We scale relevant
2345 * quantities using O(log2(k)) rather than O(k) multiplications.
2346 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2347 * try to generate digits strictly left to right. Instead, we
2348 * compute with fewer bits and propagate the carry if necessary
2349 * when rounding the final digit up. This is often faster.
2350 * 3. Under the assumption that input will be rounded nearest,
2351 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2352 * That is, we allow equality in stopping tests when the
2353 * round-nearest rule will give the same floating-point value
2354 * as would satisfaction of the stopping test with strict
2355 * inequality.
2356 * 4. We remove common factors of powers of 2 from relevant
2357 * quantities.
2358 * 5. When converting floating-point integers less than 1e16,
2359 * we use floating-point arithmetic rather than resorting
2360 * to multiple-precision integers.
2361 * 6. When asked to produce fewer than 15 digits, we first try
2362 * to get by with floating-point arithmetic; we resort to
2363 * multiple-precision integer arithmetic only if we cannot
2364 * guarantee that the floating-point calculation has given
2365 * the correctly rounded result. For k requested digits and
2366 * "uniformly" distributed input, the probability is
2367 * something like 10^(k-15) that we must resort to the Long
2368 * calculation.
2369 */
2370
2371/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2372 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2373 call to _Py_dg_freedtoa. */
2374
2375char *
2376_Py_dg_dtoa(double dd, int mode, int ndigits,
2377 int *decpt, int *sign, char **rve)
2378{
2379 /* Arguments ndigits, decpt, sign are similar to those
2380 of ecvt and fcvt; trailing zeros are suppressed from
2381 the returned string. If not null, *rve is set to point
2382 to the end of the return value. If d is +-Infinity or NaN,
2383 then *decpt is set to 9999.
2384
2385 mode:
2386 0 ==> shortest string that yields d when read in
2387 and rounded to nearest.
2388 1 ==> like 0, but with Steele & White stopping rule;
2389 e.g. with IEEE P754 arithmetic , mode 0 gives
2390 1e23 whereas mode 1 gives 9.999999999999999e22.
2391 2 ==> max(1,ndigits) significant digits. This gives a
2392 return value similar to that of ecvt, except
2393 that trailing zeros are suppressed.
2394 3 ==> through ndigits past the decimal point. This
2395 gives a return value similar to that from fcvt,
2396 except that trailing zeros are suppressed, and
2397 ndigits can be negative.
2398 4,5 ==> similar to 2 and 3, respectively, but (in
2399 round-nearest mode) with the tests of mode 0 to
2400 possibly return a shorter string that rounds to d.
2401 With IEEE arithmetic and compilation with
2402 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2403 as modes 2 and 3 when FLT_ROUNDS != 1.
2404 6-9 ==> Debugging modes similar to mode - 4: don't try
2405 fast floating-point estimate (if applicable).
2406
2407 Values of mode other than 0-9 are treated as mode 0.
2408
2409 Sufficient space is allocated to the return value
2410 to hold the suppressed trailing zeros.
2411 */
2412
2413 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2414 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2415 spec_case, try_quick;
2416 Long L;
2417 int denorm;
2418 ULong x;
2419 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2420 U d2, eps, u;
2421 double ds;
2422 char *s, *s0;
2423
2424 /* set pointers to NULL, to silence gcc compiler warnings and make
2425 cleanup easier on error */
Mark Dickinsond3697262010-05-13 11:52:22 +00002426 mlo = mhi = S = 0;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002427 s0 = 0;
2428
2429 u.d = dd;
2430 if (word0(&u) & Sign_bit) {
2431 /* set sign for everything, including 0's and NaNs */
2432 *sign = 1;
2433 word0(&u) &= ~Sign_bit; /* clear sign bit */
2434 }
2435 else
2436 *sign = 0;
2437
2438 /* quick return for Infinities, NaNs and zeros */
2439 if ((word0(&u) & Exp_mask) == Exp_mask)
2440 {
2441 /* Infinity or NaN */
2442 *decpt = 9999;
2443 if (!word1(&u) && !(word0(&u) & 0xfffff))
2444 return nrv_alloc("Infinity", rve, 8);
2445 return nrv_alloc("NaN", rve, 3);
2446 }
2447 if (!dval(&u)) {
2448 *decpt = 1;
2449 return nrv_alloc("0", rve, 1);
2450 }
2451
2452 /* compute k = floor(log10(d)). The computation may leave k
2453 one too large, but should never leave k too small. */
2454 b = d2b(&u, &be, &bbits);
2455 if (b == NULL)
2456 goto failed_malloc;
2457 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2458 dval(&d2) = dval(&u);
2459 word0(&d2) &= Frac_mask1;
2460 word0(&d2) |= Exp_11;
2461
2462 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2463 * log10(x) = log(x) / log(10)
2464 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2465 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2466 *
2467 * This suggests computing an approximation k to log10(d) by
2468 *
2469 * k = (i - Bias)*0.301029995663981
2470 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2471 *
2472 * We want k to be too large rather than too small.
2473 * The error in the first-order Taylor series approximation
2474 * is in our favor, so we just round up the constant enough
2475 * to compensate for any error in the multiplication of
2476 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2477 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2478 * adding 1e-13 to the constant term more than suffices.
2479 * Hence we adjust the constant term to 0.1760912590558.
2480 * (We could get a more accurate k by invoking log10,
2481 * but this is probably not worthwhile.)
2482 */
2483
2484 i -= Bias;
2485 denorm = 0;
2486 }
2487 else {
2488 /* d is denormalized */
2489
2490 i = bbits + be + (Bias + (P-1) - 1);
2491 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2492 : word1(&u) << (32 - i);
2493 dval(&d2) = x;
2494 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2495 i -= (Bias + (P-1) - 1) + 1;
2496 denorm = 1;
2497 }
2498 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2499 i*0.301029995663981;
2500 k = (int)ds;
2501 if (ds < 0. && ds != k)
2502 k--; /* want k = floor(ds) */
2503 k_check = 1;
2504 if (k >= 0 && k <= Ten_pmax) {
2505 if (dval(&u) < tens[k])
2506 k--;
2507 k_check = 0;
2508 }
2509 j = bbits - i - 1;
2510 if (j >= 0) {
2511 b2 = 0;
2512 s2 = j;
2513 }
2514 else {
2515 b2 = -j;
2516 s2 = 0;
2517 }
2518 if (k >= 0) {
2519 b5 = 0;
2520 s5 = k;
2521 s2 += k;
2522 }
2523 else {
2524 b2 -= k;
2525 b5 = -k;
2526 s5 = 0;
2527 }
2528 if (mode < 0 || mode > 9)
2529 mode = 0;
2530
2531 try_quick = 1;
2532
2533 if (mode > 5) {
2534 mode -= 4;
2535 try_quick = 0;
2536 }
2537 leftright = 1;
2538 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2539 /* silence erroneous "gcc -Wall" warning. */
2540 switch(mode) {
2541 case 0:
2542 case 1:
2543 i = 18;
2544 ndigits = 0;
2545 break;
2546 case 2:
2547 leftright = 0;
2548 /* no break */
2549 case 4:
2550 if (ndigits <= 0)
2551 ndigits = 1;
2552 ilim = ilim1 = i = ndigits;
2553 break;
2554 case 3:
2555 leftright = 0;
2556 /* no break */
2557 case 5:
2558 i = ndigits + k + 1;
2559 ilim = i;
2560 ilim1 = i - 1;
2561 if (i <= 0)
2562 i = 1;
2563 }
2564 s0 = rv_alloc(i);
2565 if (s0 == NULL)
2566 goto failed_malloc;
2567 s = s0;
2568
2569
2570 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2571
2572 /* Try to get by with floating-point arithmetic. */
2573
2574 i = 0;
2575 dval(&d2) = dval(&u);
2576 k0 = k;
2577 ilim0 = ilim;
2578 ieps = 2; /* conservative */
2579 if (k > 0) {
2580 ds = tens[k&0xf];
2581 j = k >> 4;
2582 if (j & Bletch) {
2583 /* prevent overflows */
2584 j &= Bletch - 1;
2585 dval(&u) /= bigtens[n_bigtens-1];
2586 ieps++;
2587 }
2588 for(; j; j >>= 1, i++)
2589 if (j & 1) {
2590 ieps++;
2591 ds *= bigtens[i];
2592 }
2593 dval(&u) /= ds;
2594 }
2595 else if ((j1 = -k)) {
2596 dval(&u) *= tens[j1 & 0xf];
2597 for(j = j1 >> 4; j; j >>= 1, i++)
2598 if (j & 1) {
2599 ieps++;
2600 dval(&u) *= bigtens[i];
2601 }
2602 }
2603 if (k_check && dval(&u) < 1. && ilim > 0) {
2604 if (ilim1 <= 0)
2605 goto fast_failed;
2606 ilim = ilim1;
2607 k--;
2608 dval(&u) *= 10.;
2609 ieps++;
2610 }
2611 dval(&eps) = ieps*dval(&u) + 7.;
2612 word0(&eps) -= (P-1)*Exp_msk1;
2613 if (ilim == 0) {
2614 S = mhi = 0;
2615 dval(&u) -= 5.;
2616 if (dval(&u) > dval(&eps))
2617 goto one_digit;
2618 if (dval(&u) < -dval(&eps))
2619 goto no_digits;
2620 goto fast_failed;
2621 }
2622 if (leftright) {
2623 /* Use Steele & White method of only
2624 * generating digits needed.
2625 */
2626 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2627 for(i = 0;;) {
2628 L = (Long)dval(&u);
2629 dval(&u) -= L;
2630 *s++ = '0' + (int)L;
2631 if (dval(&u) < dval(&eps))
2632 goto ret1;
2633 if (1. - dval(&u) < dval(&eps))
2634 goto bump_up;
2635 if (++i >= ilim)
2636 break;
2637 dval(&eps) *= 10.;
2638 dval(&u) *= 10.;
2639 }
2640 }
2641 else {
2642 /* Generate ilim digits, then fix them up. */
2643 dval(&eps) *= tens[ilim-1];
2644 for(i = 1;; i++, dval(&u) *= 10.) {
2645 L = (Long)(dval(&u));
2646 if (!(dval(&u) -= L))
2647 ilim = i;
2648 *s++ = '0' + (int)L;
2649 if (i == ilim) {
2650 if (dval(&u) > 0.5 + dval(&eps))
2651 goto bump_up;
2652 else if (dval(&u) < 0.5 - dval(&eps)) {
2653 while(*--s == '0');
2654 s++;
2655 goto ret1;
2656 }
2657 break;
2658 }
2659 }
2660 }
2661 fast_failed:
2662 s = s0;
2663 dval(&u) = dval(&d2);
2664 k = k0;
2665 ilim = ilim0;
2666 }
2667
2668 /* Do we have a "small" integer? */
2669
2670 if (be >= 0 && k <= Int_max) {
2671 /* Yes. */
2672 ds = tens[k];
2673 if (ndigits < 0 && ilim <= 0) {
2674 S = mhi = 0;
2675 if (ilim < 0 || dval(&u) <= 5*ds)
2676 goto no_digits;
2677 goto one_digit;
2678 }
2679 for(i = 1;; i++, dval(&u) *= 10.) {
2680 L = (Long)(dval(&u) / ds);
2681 dval(&u) -= L*ds;
2682 *s++ = '0' + (int)L;
2683 if (!dval(&u)) {
2684 break;
2685 }
2686 if (i == ilim) {
2687 dval(&u) += dval(&u);
2688 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2689 bump_up:
2690 while(*--s == '9')
2691 if (s == s0) {
2692 k++;
2693 *s = '0';
2694 break;
2695 }
2696 ++*s++;
2697 }
2698 break;
2699 }
2700 }
2701 goto ret1;
2702 }
2703
2704 m2 = b2;
2705 m5 = b5;
2706 if (leftright) {
2707 i =
2708 denorm ? be + (Bias + (P-1) - 1 + 1) :
2709 1 + P - bbits;
2710 b2 += i;
2711 s2 += i;
2712 mhi = i2b(1);
2713 if (mhi == NULL)
2714 goto failed_malloc;
2715 }
2716 if (m2 > 0 && s2 > 0) {
2717 i = m2 < s2 ? m2 : s2;
2718 b2 -= i;
2719 m2 -= i;
2720 s2 -= i;
2721 }
2722 if (b5 > 0) {
2723 if (leftright) {
2724 if (m5 > 0) {
2725 mhi = pow5mult(mhi, m5);
2726 if (mhi == NULL)
2727 goto failed_malloc;
2728 b1 = mult(mhi, b);
2729 Bfree(b);
2730 b = b1;
2731 if (b == NULL)
2732 goto failed_malloc;
2733 }
2734 if ((j = b5 - m5)) {
2735 b = pow5mult(b, j);
2736 if (b == NULL)
2737 goto failed_malloc;
2738 }
2739 }
2740 else {
2741 b = pow5mult(b, b5);
2742 if (b == NULL)
2743 goto failed_malloc;
2744 }
2745 }
2746 S = i2b(1);
2747 if (S == NULL)
2748 goto failed_malloc;
2749 if (s5 > 0) {
2750 S = pow5mult(S, s5);
2751 if (S == NULL)
2752 goto failed_malloc;
2753 }
2754
2755 /* Check for special case that d is a normalized power of 2. */
2756
2757 spec_case = 0;
2758 if ((mode < 2 || leftright)
2759 ) {
2760 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2761 && word0(&u) & (Exp_mask & ~Exp_msk1)
2762 ) {
2763 /* The special case */
2764 b2 += Log2P;
2765 s2 += Log2P;
2766 spec_case = 1;
2767 }
2768 }
2769
2770 /* Arrange for convenient computation of quotients:
2771 * shift left if necessary so divisor has 4 leading 0 bits.
2772 *
2773 * Perhaps we should just compute leading 28 bits of S once
2774 * and for all and pass them and a shift to quorem, so it
2775 * can do shifts and ors to compute the numerator for q.
2776 */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002777#define iInc 28
2778 i = dshift(S, s2);
2779 b2 += i;
2780 m2 += i;
2781 s2 += i;
2782 if (b2 > 0) {
2783 b = lshift(b, b2);
2784 if (b == NULL)
2785 goto failed_malloc;
2786 }
2787 if (s2 > 0) {
2788 S = lshift(S, s2);
2789 if (S == NULL)
2790 goto failed_malloc;
2791 }
2792 if (k_check) {
2793 if (cmp(b,S) < 0) {
2794 k--;
2795 b = multadd(b, 10, 0); /* we botched the k estimate */
2796 if (b == NULL)
2797 goto failed_malloc;
2798 if (leftright) {
2799 mhi = multadd(mhi, 10, 0);
2800 if (mhi == NULL)
2801 goto failed_malloc;
2802 }
2803 ilim = ilim1;
2804 }
2805 }
2806 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2807 if (ilim < 0) {
2808 /* no digits, fcvt style */
2809 no_digits:
2810 k = -1 - ndigits;
2811 goto ret;
2812 }
2813 else {
2814 S = multadd(S, 5, 0);
2815 if (S == NULL)
2816 goto failed_malloc;
2817 if (cmp(b, S) <= 0)
2818 goto no_digits;
2819 }
2820 one_digit:
2821 *s++ = '1';
2822 k++;
2823 goto ret;
2824 }
2825 if (leftright) {
2826 if (m2 > 0) {
2827 mhi = lshift(mhi, m2);
2828 if (mhi == NULL)
2829 goto failed_malloc;
2830 }
2831
2832 /* Compute mlo -- check for special case
2833 * that d is a normalized power of 2.
2834 */
2835
2836 mlo = mhi;
2837 if (spec_case) {
2838 mhi = Balloc(mhi->k);
2839 if (mhi == NULL)
2840 goto failed_malloc;
2841 Bcopy(mhi, mlo);
2842 mhi = lshift(mhi, Log2P);
2843 if (mhi == NULL)
2844 goto failed_malloc;
2845 }
2846
2847 for(i = 1;;i++) {
2848 dig = quorem(b,S) + '0';
2849 /* Do we yet have the shortest decimal string
2850 * that will round to d?
2851 */
2852 j = cmp(b, mlo);
2853 delta = diff(S, mhi);
2854 if (delta == NULL)
2855 goto failed_malloc;
2856 j1 = delta->sign ? 1 : cmp(b, delta);
2857 Bfree(delta);
2858 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2859 ) {
2860 if (dig == '9')
2861 goto round_9_up;
2862 if (j > 0)
2863 dig++;
2864 *s++ = dig;
2865 goto ret;
2866 }
2867 if (j < 0 || (j == 0 && mode != 1
2868 && !(word1(&u) & 1)
2869 )) {
2870 if (!b->x[0] && b->wds <= 1) {
2871 goto accept_dig;
2872 }
2873 if (j1 > 0) {
2874 b = lshift(b, 1);
2875 if (b == NULL)
2876 goto failed_malloc;
2877 j1 = cmp(b, S);
2878 if ((j1 > 0 || (j1 == 0 && dig & 1))
2879 && dig++ == '9')
2880 goto round_9_up;
2881 }
2882 accept_dig:
2883 *s++ = dig;
2884 goto ret;
2885 }
2886 if (j1 > 0) {
2887 if (dig == '9') { /* possible if i == 1 */
2888 round_9_up:
2889 *s++ = '9';
2890 goto roundoff;
2891 }
2892 *s++ = dig + 1;
2893 goto ret;
2894 }
2895 *s++ = dig;
2896 if (i == ilim)
2897 break;
2898 b = multadd(b, 10, 0);
2899 if (b == NULL)
2900 goto failed_malloc;
2901 if (mlo == mhi) {
2902 mlo = mhi = multadd(mhi, 10, 0);
2903 if (mlo == NULL)
2904 goto failed_malloc;
2905 }
2906 else {
2907 mlo = multadd(mlo, 10, 0);
2908 if (mlo == NULL)
2909 goto failed_malloc;
2910 mhi = multadd(mhi, 10, 0);
2911 if (mhi == NULL)
2912 goto failed_malloc;
2913 }
2914 }
2915 }
2916 else
2917 for(i = 1;; i++) {
2918 *s++ = dig = quorem(b,S) + '0';
2919 if (!b->x[0] && b->wds <= 1) {
2920 goto ret;
2921 }
2922 if (i >= ilim)
2923 break;
2924 b = multadd(b, 10, 0);
2925 if (b == NULL)
2926 goto failed_malloc;
2927 }
2928
2929 /* Round off last digit */
2930
2931 b = lshift(b, 1);
2932 if (b == NULL)
2933 goto failed_malloc;
2934 j = cmp(b, S);
2935 if (j > 0 || (j == 0 && dig & 1)) {
2936 roundoff:
2937 while(*--s == '9')
2938 if (s == s0) {
2939 k++;
2940 *s++ = '1';
2941 goto ret;
2942 }
2943 ++*s++;
2944 }
2945 else {
2946 while(*--s == '0');
2947 s++;
2948 }
2949 ret:
2950 Bfree(S);
2951 if (mhi) {
2952 if (mlo && mlo != mhi)
2953 Bfree(mlo);
2954 Bfree(mhi);
2955 }
2956 ret1:
2957 Bfree(b);
2958 *s = 0;
2959 *decpt = k + 1;
2960 if (rve)
2961 *rve = s;
2962 return s0;
2963 failed_malloc:
2964 if (S)
2965 Bfree(S);
2966 if (mlo && mlo != mhi)
2967 Bfree(mlo);
2968 if (mhi)
2969 Bfree(mhi);
2970 if (b)
2971 Bfree(b);
2972 if (s0)
2973 _Py_dg_freedtoa(s0);
2974 return NULL;
2975}
2976#ifdef __cplusplus
2977}
2978#endif
2979
2980#endif /* PY_NO_SHORT_FLOAT_REPR */