blob: c7002e5d86d51b1a1d22951236d1959b88b57d3e [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 Dickinson895c9c12020-05-29 14:23:57 +010067 * 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
68 * fixed. (bugs.python.org/issue40780)
69 *
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000070 ***************************************************************/
71
72/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
73 * at acm dot org, with " at " changed at "@" and " dot " changed to ".").
74 * Please report bugs for this modified version using the Python issue tracker
75 * (http://bugs.python.org). */
76
77/* On a machine with IEEE extended-precision registers, it is
78 * necessary to specify double-precision (53-bit) rounding precision
79 * before invoking strtod or dtoa. If the machine uses (the equivalent
80 * of) Intel 80x87 arithmetic, the call
81 * _control87(PC_53, MCW_PC);
82 * does this with many compilers. Whether this or another call is
83 * appropriate depends on the compiler; for this to work, it may be
84 * necessary to #include "float.h" or another system-dependent header
85 * file.
86 */
87
88/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
89 *
90 * This strtod returns a nearest machine number to the input decimal
91 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
92 * broken by the IEEE round-even rule. Otherwise ties are broken by
93 * biased rounding (add half and chop).
94 *
95 * Inspired loosely by William D. Clinger's paper "How to Read Floating
96 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
97 *
98 * Modifications:
99 *
100 * 1. We only require IEEE, IBM, or VAX double-precision
101 * arithmetic (not IEEE double-extended).
102 * 2. We get by with floating-point arithmetic in a case that
103 * Clinger missed -- when we're computing d * 10^n
104 * for a small integer d and the integer n is not too
105 * much larger than 22 (the maximum integer k for which
106 * we can represent 10^k exactly), we may be able to
107 * compute (d*10^k) * 10^(e-k) with just one roundoff.
108 * 3. Rather than a bit-at-a-time adjustment of the binary
109 * result in the hard case, we use floating-point
110 * arithmetic to determine the adjustment to within
111 * one bit; only in really hard cases do we need to
112 * compute a second residual.
113 * 4. Because of 3., we don't need a large table of powers of 10
114 * for ten-to-e (just some small tables, e.g. of 10^k
115 * for 0 <= k <= 22).
116 */
117
118/* Linking of Python's #defines to Gay's #defines starts here. */
119
120#include "Python.h"
Victor Stinnere9e7d282020-02-12 22:54:42 +0100121#include "pycore_dtoa.h"
junyixie5bd10592021-03-13 21:25:14 +0800122#include "pycore_interp.h"
123#include "pycore_pystate.h"
124
125#define ULong _PyDtoa_ULong
126#define Long _PyDtoa_Long
127#define ULLong _PyDtoa_ULLong
128#define Kmax _PyDtoa_Kmax
129
130typedef struct _PyDtoa_Bigint Bigint;
131
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000132
133/* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
134 the following code */
135#ifndef PY_NO_SHORT_FLOAT_REPR
136
137#include "float.h"
138
139#define MALLOC PyMem_Malloc
140#define FREE PyMem_Free
141
142/* This code should also work for ARM mixed-endian format on little-endian
143 machines, where doubles have byte order 45670123 (in increasing address
144 order, 0 being the least significant byte). */
145#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
146# define IEEE_8087
147#endif
148#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
149 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
150# define IEEE_MC68k
151#endif
152#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
153#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
154#endif
155
156/* The code below assumes that the endianness of integers matches the
157 endianness of the two 32-bit words of a double. Check this. */
158#if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
159 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
160#error "doubles and ints have incompatible endianness"
161#endif
162
163#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
164#error "doubles and ints have incompatible endianness"
165#endif
166
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000167#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 Dickinson81612e82010-01-12 23:04:19 +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
Mark Dickinsonf45bbb62013-11-26 16:19:13 +0000207#define MAX_ABS_EXP 1100000000U
208#endif
209/* Bound on length of pieces of input strings in _Py_dg_strtod; specifically,
210 this is used to bound the total number of digits ignoring leading zeros and
211 the number of digits that follow the decimal point. Ideally, MAX_DIGITS
212 should satisfy MAX_DIGITS + 400 < MAX_ABS_EXP; that ensures that the
213 exponent clipping in _Py_dg_strtod can't affect the value of the output. */
214#ifndef MAX_DIGITS
215#define MAX_DIGITS 1000000000U
216#endif
217
218/* Guard against trying to use the above values on unusual platforms with ints
219 * of width less than 32 bits. */
220#if MAX_ABS_EXP > INT_MAX
221#error "MAX_ABS_EXP should fit in an int"
222#endif
223#if MAX_DIGITS > INT_MAX
224#error "MAX_DIGITS should fit in an int"
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000225#endif
226
227/* The following definition of Storeinc is appropriate for MIPS processors.
228 * An alternative that might be better on some machines is
229 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
230 */
231#if defined(IEEE_8087)
232#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
233 ((unsigned short *)a)[0] = (unsigned short)c, a++)
234#else
235#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
236 ((unsigned short *)a)[1] = (unsigned short)c, a++)
237#endif
238
239/* #define P DBL_MANT_DIG */
240/* Ten_pmax = floor(P*log(2)/log(5)) */
241/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
242/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
243/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
244
245#define Exp_shift 20
246#define Exp_shift1 20
247#define Exp_msk1 0x100000
248#define Exp_msk11 0x100000
249#define Exp_mask 0x7ff00000
250#define P 53
251#define Nbits 53
252#define Bias 1023
253#define Emax 1023
254#define Emin (-1022)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000255#define Etiny (-1074) /* smallest denormal is 2**Etiny */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000256#define Exp_1 0x3ff00000
257#define Exp_11 0x3ff00000
258#define Ebits 11
259#define Frac_mask 0xfffff
260#define Frac_mask1 0xfffff
261#define Ten_pmax 22
262#define Bletch 0x10
263#define Bndry_mask 0xfffff
264#define Bndry_mask1 0xfffff
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000265#define Sign_bit 0x80000000
266#define Log2P 1
267#define Tiny0 0
268#define Tiny1 1
269#define Quick_max 14
270#define Int_max 14
271
272#ifndef Flt_Rounds
273#ifdef FLT_ROUNDS
274#define Flt_Rounds FLT_ROUNDS
275#else
276#define Flt_Rounds 1
277#endif
278#endif /*Flt_Rounds*/
279
280#define Rounding Flt_Rounds
281
282#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
283#define Big1 0xffffffff
284
Mark Dickinsone383e822012-04-29 15:31:56 +0100285/* Standard NaN used by _Py_dg_stdnan. */
286
287#define NAN_WORD0 0x7ff80000
288#define NAN_WORD1 0
289
290/* Bits of the representation of positive infinity. */
291
292#define POSINF_WORD0 0x7ff00000
293#define POSINF_WORD1 0
294
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000295/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
296
297typedef struct BCinfo BCinfo;
298struct
299BCinfo {
Mark Dickinsonadd28232010-01-21 19:51:08 +0000300 int e0, nd, nd0, scale;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000301};
302
303#define FFFFFFFF 0xffffffffUL
304
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000305/* struct Bigint is used to represent arbitrary-precision integers. These
306 integers are stored in sign-magnitude format, with the magnitude stored as
307 an array of base 2**32 digits. Bigints are always normalized: if x is a
308 Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
309
310 The Bigint fields are as follows:
311
312 - next is a header used by Balloc and Bfree to keep track of lists
313 of freed Bigints; it's also used for the linked list of
314 powers of 5 of the form 5**2**i used by pow5mult.
315 - k indicates which pool this Bigint was allocated from
316 - maxwds is the maximum number of words space was allocated for
317 (usually maxwds == 2**k)
318 - sign is 1 for negative Bigints, 0 for positive. The sign is unused
319 (ignored on inputs, set to 0 on outputs) in almost all operations
320 involving Bigints: a notable exception is the diff function, which
321 ignores signs on inputs but sets the sign of the output correctly.
322 - wds is the actual number of significant words
323 - x contains the vector of words (digits) for this Bigint, from least
324 significant (x[0]) to most significant (x[wds-1]).
325*/
326
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000327
Mark Dickinsonde508002010-01-17 21:02:55 +0000328#ifndef Py_USING_MEMORY_DEBUGGER
329
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000330/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
331 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
332 1 << k. These pools are maintained as linked lists, with freelist[k]
333 pointing to the head of the list for pool k.
334
335 On allocation, if there's no free slot in the appropriate pool, MALLOC is
336 called to get more memory. This memory is not returned to the system until
337 Python quits. There's also a private memory pool that's allocated from
338 in preference to using MALLOC.
339
340 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
341 decimal digits), memory is directly allocated using MALLOC, and freed using
342 FREE.
343
344 XXX: it would be easy to bypass this memory-management system and
345 translate each call to Balloc into a call to PyMem_Malloc, and each
346 Bfree to PyMem_Free. Investigate whether this has any significant
347 performance on impact. */
348
junyixie5bd10592021-03-13 21:25:14 +0800349
350/* Get Bigint freelist from interpreter */
351static Bigint **
352get_freelist(void) {
353 PyInterpreterState *interp = _PyInterpreterState_GET();
354 return interp->dtoa_freelist;
355}
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000356
357/* Allocate space for a Bigint with up to 1<<k digits */
358
359static Bigint *
360Balloc(int k)
361{
362 int x;
363 Bigint *rv;
364 unsigned int len;
junyixie5bd10592021-03-13 21:25:14 +0800365 Bigint **freelist = get_freelist();
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000366 if (k <= Kmax && (rv = freelist[k]))
367 freelist[k] = rv->next;
368 else {
369 x = 1 << k;
370 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
371 /sizeof(double);
Victor Stinner938b0b92015-03-18 15:01:44 +0100372 if (k <= Kmax && pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000373 rv = (Bigint*)pmem_next;
374 pmem_next += len;
375 }
376 else {
377 rv = (Bigint*)MALLOC(len*sizeof(double));
378 if (rv == NULL)
379 return NULL;
380 }
381 rv->k = k;
382 rv->maxwds = x;
383 }
384 rv->sign = rv->wds = 0;
385 return rv;
386}
387
388/* Free a Bigint allocated with Balloc */
389
390static void
391Bfree(Bigint *v)
392{
393 if (v) {
394 if (v->k > Kmax)
395 FREE((void*)v);
396 else {
junyixie5bd10592021-03-13 21:25:14 +0800397 Bigint **freelist = get_freelist();
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000398 v->next = freelist[v->k];
399 freelist[v->k] = v;
400 }
401 }
402}
403
Mark Dickinsonde508002010-01-17 21:02:55 +0000404#else
405
406/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
407 PyMem_Free directly in place of the custom memory allocation scheme above.
408 These are provided for the benefit of memory debugging tools like
409 Valgrind. */
410
411/* Allocate space for a Bigint with up to 1<<k digits */
412
413static Bigint *
414Balloc(int k)
415{
416 int x;
417 Bigint *rv;
418 unsigned int len;
419
420 x = 1 << k;
421 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
422 /sizeof(double);
423
424 rv = (Bigint*)MALLOC(len*sizeof(double));
425 if (rv == NULL)
426 return NULL;
427
428 rv->k = k;
429 rv->maxwds = x;
430 rv->sign = rv->wds = 0;
431 return rv;
432}
433
434/* Free a Bigint allocated with Balloc */
435
436static void
437Bfree(Bigint *v)
438{
439 if (v) {
440 FREE((void*)v);
441 }
442}
443
444#endif /* Py_USING_MEMORY_DEBUGGER */
445
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000446#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
447 y->wds*sizeof(Long) + 2*sizeof(int))
448
449/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
450 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
451 On failure, return NULL. In this case, b will have been already freed. */
452
453static Bigint *
454multadd(Bigint *b, int m, int a) /* multiply by m and add a */
455{
456 int i, wds;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000457 ULong *x;
458 ULLong carry, y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000459 Bigint *b1;
460
461 wds = b->wds;
462 x = b->x;
463 i = 0;
464 carry = a;
465 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000466 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 }
470 while(++i < wds);
471 if (carry) {
472 if (wds >= b->maxwds) {
473 b1 = Balloc(b->k+1);
474 if (b1 == NULL){
475 Bfree(b);
476 return NULL;
477 }
478 Bcopy(b1, b);
479 Bfree(b);
480 b = b1;
481 }
482 b->x[wds++] = (ULong)carry;
483 b->wds = wds;
484 }
485 return b;
486}
487
488/* convert a string s containing nd decimal digits (possibly containing a
489 decimal separator at position nd0, which is ignored) to a Bigint. This
490 function carries on where the parsing code in _Py_dg_strtod leaves off: on
491 entry, y9 contains the result of converting the first 9 digits. Returns
492 NULL on failure. */
493
494static Bigint *
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000495s2b(const char *s, int nd0, int nd, ULong y9)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000496{
497 Bigint *b;
498 int i, k;
499 Long x, y;
500
501 x = (nd + 8) / 9;
502 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
503 b = Balloc(k);
504 if (b == NULL)
505 return NULL;
506 b->x[0] = y9;
507 b->wds = 1;
508
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000509 if (nd <= 9)
510 return b;
511
512 s += 9;
513 for (i = 9; i < nd0; i++) {
514 b = multadd(b, 10, *s++ - '0');
515 if (b == NULL)
516 return NULL;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000517 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000518 s++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000519 for(; i < nd; i++) {
520 b = multadd(b, 10, *s++ - '0');
521 if (b == NULL)
522 return NULL;
523 }
524 return b;
525}
526
527/* count leading 0 bits in the 32-bit integer x. */
528
529static int
530hi0bits(ULong x)
531{
532 int k = 0;
533
534 if (!(x & 0xffff0000)) {
535 k = 16;
536 x <<= 16;
537 }
538 if (!(x & 0xff000000)) {
539 k += 8;
540 x <<= 8;
541 }
542 if (!(x & 0xf0000000)) {
543 k += 4;
544 x <<= 4;
545 }
546 if (!(x & 0xc0000000)) {
547 k += 2;
548 x <<= 2;
549 }
550 if (!(x & 0x80000000)) {
551 k++;
552 if (!(x & 0x40000000))
553 return 32;
554 }
555 return k;
556}
557
558/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
559 number of bits. */
560
561static int
562lo0bits(ULong *y)
563{
564 int k;
565 ULong x = *y;
566
567 if (x & 7) {
568 if (x & 1)
569 return 0;
570 if (x & 2) {
571 *y = x >> 1;
572 return 1;
573 }
574 *y = x >> 2;
575 return 2;
576 }
577 k = 0;
578 if (!(x & 0xffff)) {
579 k = 16;
580 x >>= 16;
581 }
582 if (!(x & 0xff)) {
583 k += 8;
584 x >>= 8;
585 }
586 if (!(x & 0xf)) {
587 k += 4;
588 x >>= 4;
589 }
590 if (!(x & 0x3)) {
591 k += 2;
592 x >>= 2;
593 }
594 if (!(x & 1)) {
595 k++;
596 x >>= 1;
597 if (!x)
598 return 32;
599 }
600 *y = x;
601 return k;
602}
603
604/* convert a small nonnegative integer to a Bigint */
605
606static Bigint *
607i2b(int i)
608{
609 Bigint *b;
610
611 b = Balloc(1);
612 if (b == NULL)
613 return NULL;
614 b->x[0] = i;
615 b->wds = 1;
616 return b;
617}
618
619/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
620 the signs of a and b. */
621
622static Bigint *
623mult(Bigint *a, Bigint *b)
624{
625 Bigint *c;
626 int k, wa, wb, wc;
627 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
628 ULong y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000629 ULLong carry, z;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000630
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000631 if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
632 c = Balloc(0);
633 if (c == NULL)
634 return NULL;
635 c->wds = 1;
636 c->x[0] = 0;
637 return c;
638 }
639
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000640 if (a->wds < b->wds) {
641 c = a;
642 a = b;
643 b = c;
644 }
645 k = a->k;
646 wa = a->wds;
647 wb = b->wds;
648 wc = wa + wb;
649 if (wc > a->maxwds)
650 k++;
651 c = Balloc(k);
652 if (c == NULL)
653 return NULL;
654 for(x = c->x, xa = x + wc; x < xa; x++)
655 *x = 0;
656 xa = a->x;
657 xae = xa + wa;
658 xb = b->x;
659 xbe = xb + wb;
660 xc0 = c->x;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000661 for(; xb < xbe; xc0++) {
662 if ((y = *xb++)) {
663 x = xa;
664 xc = xc0;
665 carry = 0;
666 do {
667 z = *x++ * (ULLong)y + *xc + carry;
668 carry = z >> 32;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000669 *xc++ = (ULong)(z & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000670 }
671 while(x < xae);
672 *xc = (ULong)carry;
673 }
674 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000675 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
676 c->wds = wc;
677 return c;
678}
679
Mark Dickinsonde508002010-01-17 21:02:55 +0000680#ifndef Py_USING_MEMORY_DEBUGGER
681
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000682/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
683
684static Bigint *p5s;
685
686/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
687 failure; if the returned pointer is distinct from b then the original
688 Bigint b will have been Bfree'd. Ignores the sign of b. */
689
690static Bigint *
691pow5mult(Bigint *b, int k)
692{
693 Bigint *b1, *p5, *p51;
694 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200695 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000696
697 if ((i = k & 3)) {
698 b = multadd(b, p05[i-1], 0);
699 if (b == NULL)
700 return NULL;
701 }
702
703 if (!(k >>= 2))
704 return b;
705 p5 = p5s;
706 if (!p5) {
707 /* first time */
708 p5 = i2b(625);
709 if (p5 == NULL) {
710 Bfree(b);
711 return NULL;
712 }
713 p5s = p5;
714 p5->next = 0;
715 }
716 for(;;) {
717 if (k & 1) {
718 b1 = mult(b, p5);
719 Bfree(b);
720 b = b1;
721 if (b == NULL)
722 return NULL;
723 }
724 if (!(k >>= 1))
725 break;
726 p51 = p5->next;
727 if (!p51) {
728 p51 = mult(p5,p5);
729 if (p51 == NULL) {
730 Bfree(b);
731 return NULL;
732 }
733 p51->next = 0;
734 p5->next = p51;
735 }
736 p5 = p51;
737 }
738 return b;
739}
740
Mark Dickinsonde508002010-01-17 21:02:55 +0000741#else
742
743/* Version of pow5mult that doesn't cache powers of 5. Provided for
744 the benefit of memory debugging tools like Valgrind. */
745
746static Bigint *
747pow5mult(Bigint *b, int k)
748{
749 Bigint *b1, *p5, *p51;
750 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200751 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonde508002010-01-17 21:02:55 +0000752
753 if ((i = k & 3)) {
754 b = multadd(b, p05[i-1], 0);
755 if (b == NULL)
756 return NULL;
757 }
758
759 if (!(k >>= 2))
760 return b;
761 p5 = i2b(625);
762 if (p5 == NULL) {
763 Bfree(b);
764 return NULL;
765 }
766
767 for(;;) {
768 if (k & 1) {
769 b1 = mult(b, p5);
770 Bfree(b);
771 b = b1;
772 if (b == NULL) {
773 Bfree(p5);
774 return NULL;
775 }
776 }
777 if (!(k >>= 1))
778 break;
779 p51 = mult(p5, p5);
780 Bfree(p5);
781 p5 = p51;
782 if (p5 == NULL) {
783 Bfree(b);
784 return NULL;
785 }
786 }
787 Bfree(p5);
788 return b;
789}
790
791#endif /* Py_USING_MEMORY_DEBUGGER */
792
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000793/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
794 or NULL on failure. If the returned pointer is distinct from b then the
795 original b will have been Bfree'd. Ignores the sign of b. */
796
797static Bigint *
798lshift(Bigint *b, int k)
799{
800 int i, k1, n, n1;
801 Bigint *b1;
802 ULong *x, *x1, *xe, z;
803
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000804 if (!k || (!b->x[0] && b->wds == 1))
805 return b;
806
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000807 n = k >> 5;
808 k1 = b->k;
809 n1 = n + b->wds + 1;
810 for(i = b->maxwds; n1 > i; i <<= 1)
811 k1++;
812 b1 = Balloc(k1);
813 if (b1 == NULL) {
814 Bfree(b);
815 return NULL;
816 }
817 x1 = b1->x;
818 for(i = 0; i < n; i++)
819 *x1++ = 0;
820 x = b->x;
821 xe = x + b->wds;
822 if (k &= 0x1f) {
823 k1 = 32 - k;
824 z = 0;
825 do {
826 *x1++ = *x << k | z;
827 z = *x++ >> k1;
828 }
829 while(x < xe);
830 if ((*x1 = z))
831 ++n1;
832 }
833 else do
834 *x1++ = *x++;
835 while(x < xe);
836 b1->wds = n1 - 1;
837 Bfree(b);
838 return b1;
839}
840
841/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
842 1 if a > b. Ignores signs of a and b. */
843
844static int
845cmp(Bigint *a, Bigint *b)
846{
847 ULong *xa, *xa0, *xb, *xb0;
848 int i, j;
849
850 i = a->wds;
851 j = b->wds;
852#ifdef DEBUG
853 if (i > 1 && !a->x[i-1])
854 Bug("cmp called with a->x[a->wds-1] == 0");
855 if (j > 1 && !b->x[j-1])
856 Bug("cmp called with b->x[b->wds-1] == 0");
857#endif
858 if (i -= j)
859 return i;
860 xa0 = a->x;
861 xa = xa0 + j;
862 xb0 = b->x;
863 xb = xb0 + j;
864 for(;;) {
865 if (*--xa != *--xb)
866 return *xa < *xb ? -1 : 1;
867 if (xa <= xa0)
868 break;
869 }
870 return 0;
871}
872
873/* Take the difference of Bigints a and b, returning a new Bigint. Returns
874 NULL on failure. The signs of a and b are ignored, but the sign of the
875 result is set appropriately. */
876
877static Bigint *
878diff(Bigint *a, Bigint *b)
879{
880 Bigint *c;
881 int i, wa, wb;
882 ULong *xa, *xae, *xb, *xbe, *xc;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000883 ULLong borrow, y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000884
885 i = cmp(a,b);
886 if (!i) {
887 c = Balloc(0);
888 if (c == NULL)
889 return NULL;
890 c->wds = 1;
891 c->x[0] = 0;
892 return c;
893 }
894 if (i < 0) {
895 c = a;
896 a = b;
897 b = c;
898 i = 1;
899 }
900 else
901 i = 0;
902 c = Balloc(a->k);
903 if (c == NULL)
904 return NULL;
905 c->sign = i;
906 wa = a->wds;
907 xa = a->x;
908 xae = xa + wa;
909 wb = b->wds;
910 xb = b->x;
911 xbe = xb + wb;
912 xc = c->x;
913 borrow = 0;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000914 do {
915 y = (ULLong)*xa++ - *xb++ - borrow;
916 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000917 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000918 }
919 while(xb < xbe);
920 while(xa < xae) {
921 y = *xa++ - borrow;
922 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000923 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000924 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000925 while(!*--xc)
926 wa--;
927 c->wds = wa;
928 return c;
929}
930
Mark Dickinsonadd28232010-01-21 19:51:08 +0000931/* Given a positive normal double x, return the difference between x and the
932 next double up. Doesn't give correct results for subnormals. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000933
934static double
935ulp(U *x)
936{
937 Long L;
938 U u;
939
940 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
941 word0(&u) = L;
942 word1(&u) = 0;
943 return dval(&u);
944}
945
946/* Convert a Bigint to a double plus an exponent */
947
948static double
949b2d(Bigint *a, int *e)
950{
951 ULong *xa, *xa0, w, y, z;
952 int k;
953 U d;
954
955 xa0 = a->x;
956 xa = xa0 + a->wds;
957 y = *--xa;
958#ifdef DEBUG
959 if (!y) Bug("zero y in b2d");
960#endif
961 k = hi0bits(y);
962 *e = 32 - k;
963 if (k < Ebits) {
964 word0(&d) = Exp_1 | y >> (Ebits - k);
965 w = xa > xa0 ? *--xa : 0;
966 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
967 goto ret_d;
968 }
969 z = xa > xa0 ? *--xa : 0;
970 if (k -= Ebits) {
971 word0(&d) = Exp_1 | y << k | z >> (32 - k);
972 y = xa > xa0 ? *--xa : 0;
973 word1(&d) = z << k | y >> (32 - k);
974 }
975 else {
976 word0(&d) = Exp_1 | y;
977 word1(&d) = z;
978 }
979 ret_d:
980 return dval(&d);
981}
982
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000983/* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
984 except that it accepts the scale parameter used in _Py_dg_strtod (which
985 should be either 0 or 2*P), and the normalization for the return value is
986 different (see below). On input, d should be finite and nonnegative, and d
987 / 2**scale should be exactly representable as an IEEE 754 double.
988
989 Returns a Bigint b and an integer e such that
990
991 dval(d) / 2**scale = b * 2**e.
992
993 Unlike d2b, b is not necessarily odd: b and e are normalized so
994 that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
995 and e == Etiny. This applies equally to an input of 0.0: in that
996 case the return values are b = 0 and e = Etiny.
997
998 The above normalization ensures that for all possible inputs d,
999 2**e gives ulp(d/2**scale).
1000
1001 Returns NULL on failure.
1002*/
1003
1004static Bigint *
1005sd2b(U *d, int scale, int *e)
1006{
1007 Bigint *b;
1008
1009 b = Balloc(1);
1010 if (b == NULL)
1011 return NULL;
Victor Stinner938b0b92015-03-18 15:01:44 +01001012
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001013 /* First construct b and e assuming that scale == 0. */
1014 b->wds = 2;
1015 b->x[0] = word1(d);
1016 b->x[1] = word0(d) & Frac_mask;
1017 *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
1018 if (*e < Etiny)
1019 *e = Etiny;
1020 else
1021 b->x[1] |= Exp_msk1;
1022
1023 /* Now adjust for scale, provided that b != 0. */
1024 if (scale && (b->x[0] || b->x[1])) {
1025 *e -= scale;
1026 if (*e < Etiny) {
1027 scale = Etiny - *e;
1028 *e = Etiny;
1029 /* We can't shift more than P-1 bits without shifting out a 1. */
1030 assert(0 < scale && scale <= P - 1);
1031 if (scale >= 32) {
1032 /* The bits shifted out should all be zero. */
1033 assert(b->x[0] == 0);
1034 b->x[0] = b->x[1];
1035 b->x[1] = 0;
1036 scale -= 32;
1037 }
1038 if (scale) {
1039 /* The bits shifted out should all be zero. */
1040 assert(b->x[0] << (32 - scale) == 0);
1041 b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1042 b->x[1] >>= scale;
1043 }
1044 }
1045 }
1046 /* Ensure b is normalized. */
1047 if (!b->x[1])
1048 b->wds = 1;
1049
1050 return b;
1051}
1052
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001053/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
1054
1055 Given a finite nonzero double d, return an odd Bigint b and exponent *e
1056 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
Mark Dickinson180e4cd2010-01-04 21:33:31 +00001057 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001058
1059 If d is zero, then b == 0, *e == -1010, *bbits = 0.
1060 */
1061
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001062static Bigint *
1063d2b(U *d, int *e, int *bits)
1064{
1065 Bigint *b;
1066 int de, k;
1067 ULong *x, y, z;
1068 int i;
1069
1070 b = Balloc(1);
1071 if (b == NULL)
1072 return NULL;
1073 x = b->x;
1074
1075 z = word0(d) & Frac_mask;
1076 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
1077 if ((de = (int)(word0(d) >> Exp_shift)))
1078 z |= Exp_msk1;
1079 if ((y = word1(d))) {
1080 if ((k = lo0bits(&y))) {
1081 x[0] = y | z << (32 - k);
1082 z >>= k;
1083 }
1084 else
1085 x[0] = y;
1086 i =
1087 b->wds = (x[1] = z) ? 2 : 1;
1088 }
1089 else {
1090 k = lo0bits(&z);
1091 x[0] = z;
1092 i =
1093 b->wds = 1;
1094 k += 32;
1095 }
1096 if (de) {
1097 *e = de - Bias - (P-1) + k;
1098 *bits = P - k;
1099 }
1100 else {
1101 *e = de - Bias - (P-1) + 1 + k;
1102 *bits = 32*i - hi0bits(x[i-1]);
1103 }
1104 return b;
1105}
1106
1107/* Compute the ratio of two Bigints, as a double. The result may have an
1108 error of up to 2.5 ulps. */
1109
1110static double
1111ratio(Bigint *a, Bigint *b)
1112{
1113 U da, db;
1114 int k, ka, kb;
1115
1116 dval(&da) = b2d(a, &ka);
1117 dval(&db) = b2d(b, &kb);
1118 k = ka - kb + 32*(a->wds - b->wds);
1119 if (k > 0)
1120 word0(&da) += k*Exp_msk1;
1121 else {
1122 k = -k;
1123 word0(&db) += k*Exp_msk1;
1124 }
1125 return dval(&da) / dval(&db);
1126}
1127
1128static const double
1129tens[] = {
1130 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1131 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1132 1e20, 1e21, 1e22
1133};
1134
1135static const double
1136bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1137static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1138 9007199254740992.*9007199254740992.e-256
1139 /* = 2^106 * 1e-256 */
1140};
1141/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1142/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1143#define Scale_Bit 0x10
1144#define n_bigtens 5
1145
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001146#define ULbits 32
1147#define kshift 5
1148#define kmask 31
1149
1150
1151static int
1152dshift(Bigint *b, int p2)
1153{
1154 int rv = hi0bits(b->x[b->wds-1]) - 4;
1155 if (p2 > 0)
1156 rv -= p2;
1157 return rv & kmask;
1158}
1159
1160/* special case of Bigint division. The quotient is always in the range 0 <=
1161 quotient < 10, and on entry the divisor S is normalized so that its top 4
1162 bits (28--31) are zero and bit 27 is set. */
1163
1164static int
1165quorem(Bigint *b, Bigint *S)
1166{
1167 int n;
1168 ULong *bx, *bxe, q, *sx, *sxe;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001169 ULLong borrow, carry, y, ys;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001170
1171 n = S->wds;
1172#ifdef DEBUG
1173 /*debug*/ if (b->wds > n)
1174 /*debug*/ Bug("oversize b in quorem");
1175#endif
1176 if (b->wds < n)
1177 return 0;
1178 sx = S->x;
1179 sxe = sx + --n;
1180 bx = b->x;
1181 bxe = bx + n;
1182 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1183#ifdef DEBUG
1184 /*debug*/ if (q > 9)
1185 /*debug*/ Bug("oversized quotient in quorem");
1186#endif
1187 if (q) {
1188 borrow = 0;
1189 carry = 0;
1190 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001191 ys = *sx++ * (ULLong)q + carry;
1192 carry = ys >> 32;
1193 y = *bx - (ys & FFFFFFFF) - borrow;
1194 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001195 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001196 }
1197 while(sx <= sxe);
1198 if (!*bxe) {
1199 bx = b->x;
1200 while(--bxe > bx && !*bxe)
1201 --n;
1202 b->wds = n;
1203 }
1204 }
1205 if (cmp(b, S) >= 0) {
1206 q++;
1207 borrow = 0;
1208 carry = 0;
1209 bx = b->x;
1210 sx = S->x;
1211 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001212 ys = *sx++ + carry;
1213 carry = ys >> 32;
1214 y = *bx - (ys & FFFFFFFF) - borrow;
1215 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001216 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001217 }
1218 while(sx <= sxe);
1219 bx = b->x;
1220 bxe = bx + n;
1221 if (!*bxe) {
1222 while(--bxe > bx && !*bxe)
1223 --n;
1224 b->wds = n;
1225 }
1226 }
1227 return q;
1228}
1229
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001230/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
Mark Dickinson81612e82010-01-12 23:04:19 +00001231
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001232 Assuming that x is finite and nonnegative (positive zero is fine
1233 here) and x / 2^bc.scale is exactly representable as a double,
1234 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
Mark Dickinson81612e82010-01-12 23:04:19 +00001235
1236static double
1237sulp(U *x, BCinfo *bc)
1238{
1239 U u;
1240
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001241 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00001242 /* rv/2^bc->scale is subnormal */
1243 word0(&u) = (P+2)*Exp_msk1;
1244 word1(&u) = 0;
1245 return u.d;
1246 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001247 else {
1248 assert(word0(x) || word1(x)); /* x != 0.0 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001249 return ulp(x);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001250 }
Mark Dickinson81612e82010-01-12 23:04:19 +00001251}
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001252
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001253/* The bigcomp function handles some hard cases for strtod, for inputs
1254 with more than STRTOD_DIGLIM digits. It's called once an initial
1255 estimate for the double corresponding to the input string has
1256 already been obtained by the code in _Py_dg_strtod.
1257
1258 The bigcomp function is only called after _Py_dg_strtod has found a
1259 double value rv such that either rv or rv + 1ulp represents the
1260 correctly rounded value corresponding to the original string. It
1261 determines which of these two values is the correct one by
1262 computing the decimal digits of rv + 0.5ulp and comparing them with
1263 the corresponding digits of s0.
1264
1265 In the following, write dv for the absolute value of the number represented
1266 by the input string.
1267
1268 Inputs:
1269
1270 s0 points to the first significant digit of the input string.
1271
1272 rv is a (possibly scaled) estimate for the closest double value to the
1273 value represented by the original input to _Py_dg_strtod. If
1274 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1275 the input value.
1276
1277 bc is a struct containing information gathered during the parsing and
1278 estimation steps of _Py_dg_strtod. Description of fields follows:
1279
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001280 bc->e0 gives the exponent of the input value, such that dv = (integer
1281 given by the bd->nd digits of s0) * 10**e0
1282
1283 bc->nd gives the total number of significant digits of s0. It will
1284 be at least 1.
1285
1286 bc->nd0 gives the number of significant digits of s0 before the
1287 decimal separator. If there's no decimal separator, bc->nd0 ==
1288 bc->nd.
1289
1290 bc->scale is the value used to scale rv to avoid doing arithmetic with
1291 subnormal values. It's either 0 or 2*P (=106).
1292
1293 Outputs:
1294
1295 On successful exit, rv/2^(bc->scale) is the closest double to dv.
1296
1297 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001298
1299static int
1300bigcomp(U *rv, const char *s0, BCinfo *bc)
1301{
1302 Bigint *b, *d;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001303 int b2, d2, dd, i, nd, nd0, odd, p2, p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001304
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001305 nd = bc->nd;
1306 nd0 = bc->nd0;
Mark Dickinson81612e82010-01-12 23:04:19 +00001307 p5 = nd + bc->e0;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001308 b = sd2b(rv, bc->scale, &p2);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001309 if (b == NULL)
1310 return -1;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001311
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001312 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1313 case, this is used for round to even. */
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001314 odd = b->x[0] & 1;
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001315
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001316 /* left shift b by 1 bit and or a 1 into the least significant bit;
1317 this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1318 b = lshift(b, 1);
1319 if (b == NULL)
1320 return -1;
1321 b->x[0] |= 1;
1322 p2--;
1323
1324 p2 -= p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001325 d = i2b(1);
1326 if (d == NULL) {
1327 Bfree(b);
1328 return -1;
1329 }
1330 /* Arrange for convenient computation of quotients:
1331 * shift left if necessary so divisor has 4 leading 0 bits.
1332 */
1333 if (p5 > 0) {
1334 d = pow5mult(d, p5);
1335 if (d == NULL) {
1336 Bfree(b);
1337 return -1;
1338 }
1339 }
1340 else if (p5 < 0) {
1341 b = pow5mult(b, -p5);
1342 if (b == NULL) {
1343 Bfree(d);
1344 return -1;
1345 }
1346 }
1347 if (p2 > 0) {
1348 b2 = p2;
1349 d2 = 0;
1350 }
1351 else {
1352 b2 = 0;
1353 d2 = -p2;
1354 }
1355 i = dshift(d, d2);
1356 if ((b2 += i) > 0) {
1357 b = lshift(b, b2);
1358 if (b == NULL) {
1359 Bfree(d);
1360 return -1;
1361 }
1362 }
1363 if ((d2 += i) > 0) {
1364 d = lshift(d, d2);
1365 if (d == NULL) {
1366 Bfree(b);
1367 return -1;
1368 }
1369 }
1370
Mark Dickinsonadd28232010-01-21 19:51:08 +00001371 /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1372 * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
1373 * a number in the range [0.1, 1). */
1374 if (cmp(b, d) >= 0)
1375 /* b/d >= 1 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001376 dd = -1;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001377 else {
1378 i = 0;
1379 for(;;) {
1380 b = multadd(b, 10, 0);
1381 if (b == NULL) {
1382 Bfree(d);
1383 return -1;
1384 }
1385 dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1386 i++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001387
Mark Dickinsonadd28232010-01-21 19:51:08 +00001388 if (dd)
1389 break;
1390 if (!b->x[0] && b->wds == 1) {
1391 /* b/d == 0 */
1392 dd = i < nd;
1393 break;
1394 }
1395 if (!(i < nd)) {
1396 /* b/d != 0, but digits of s0 exhausted */
1397 dd = -1;
1398 break;
1399 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001400 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001401 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001402 Bfree(b);
1403 Bfree(d);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001404 if (dd > 0 || (dd == 0 && odd))
1405 dval(rv) += sulp(rv, bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001406 return 0;
1407}
1408
Mark Dickinsone383e822012-04-29 15:31:56 +01001409/* Return a 'standard' NaN value.
1410
1411 There are exactly two quiet NaNs that don't arise by 'quieting' signaling
1412 NaNs (see IEEE 754-2008, section 6.2.1). If sign == 0, return the one whose
1413 sign bit is cleared. Otherwise, return the one whose sign bit is set.
1414*/
1415
1416double
1417_Py_dg_stdnan(int sign)
1418{
1419 U rv;
1420 word0(&rv) = NAN_WORD0;
1421 word1(&rv) = NAN_WORD1;
1422 if (sign)
1423 word0(&rv) |= Sign_bit;
1424 return dval(&rv);
1425}
1426
1427/* Return positive or negative infinity, according to the given sign (0 for
1428 * positive infinity, 1 for negative infinity). */
1429
1430double
1431_Py_dg_infinity(int sign)
1432{
1433 U rv;
1434 word0(&rv) = POSINF_WORD0;
1435 word1(&rv) = POSINF_WORD1;
1436 return sign ? -dval(&rv) : dval(&rv);
1437}
1438
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001439double
1440_Py_dg_strtod(const char *s00, char **se)
1441{
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001442 int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1443 int esign, i, j, k, lz, nd, nd0, odd, sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001444 const char *s, *s0, *s1;
1445 double aadj, aadj1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001446 U aadj2, adj, rv, rv0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001447 ULong y, z, abs_exp;
Mark Dickinson45b63652010-01-16 18:10:25 +00001448 Long L;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001449 BCinfo bc;
Victor Stinner9776b062019-03-13 17:55:01 +01001450 Bigint *bb = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001451 size_t ndigits, fraclen;
Victor Stinner9776b062019-03-13 17:55:01 +01001452 double result;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001453
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001454 dval(&rv) = 0.;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001455
1456 /* Start parsing. */
1457 c = *(s = s00);
1458
1459 /* Parse optional sign, if present. */
1460 sign = 0;
1461 switch (c) {
1462 case '-':
1463 sign = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001464 /* fall through */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001465 case '+':
1466 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001467 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001468
1469 /* Skip leading zeros: lz is true iff there were leading zeros. */
1470 s1 = s;
1471 while (c == '0')
1472 c = *++s;
1473 lz = s != s1;
1474
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001475 /* Point s0 at the first nonzero digit (if any). fraclen will be the
1476 number of digits between the decimal point and the end of the
1477 digit string. ndigits will be the total number of digits ignoring
1478 leading zeros. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001479 s0 = s1 = s;
1480 while ('0' <= c && c <= '9')
1481 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001482 ndigits = s - s1;
1483 fraclen = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001484
1485 /* Parse decimal point and following digits. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001486 if (c == '.') {
1487 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001488 if (!ndigits) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001489 s1 = s;
1490 while (c == '0')
1491 c = *++s;
1492 lz = lz || s != s1;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001493 fraclen += (s - s1);
Mark Dickinsonadd28232010-01-21 19:51:08 +00001494 s0 = s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001495 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001496 s1 = s;
1497 while ('0' <= c && c <= '9')
1498 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001499 ndigits += s - s1;
1500 fraclen += s - s1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001501 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001502
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001503 /* Now lz is true if and only if there were leading zero digits, and
1504 ndigits gives the total number of digits ignoring leading zeros. A
1505 valid input must have at least one digit. */
1506 if (!ndigits && !lz) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001507 if (se)
1508 *se = (char *)s00;
1509 goto parse_error;
1510 }
1511
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001512 /* Range check ndigits and fraclen to make sure that they, and values
1513 computed with them, can safely fit in an int. */
1514 if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) {
1515 if (se)
1516 *se = (char *)s00;
1517 goto parse_error;
1518 }
1519 nd = (int)ndigits;
1520 nd0 = (int)ndigits - (int)fraclen;
1521
Mark Dickinsonadd28232010-01-21 19:51:08 +00001522 /* Parse exponent. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001523 e = 0;
1524 if (c == 'e' || c == 'E') {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001525 s00 = s;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001526 c = *++s;
1527
1528 /* Exponent sign. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001529 esign = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001530 switch (c) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001531 case '-':
1532 esign = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001533 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001534 case '+':
1535 c = *++s;
1536 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001537
1538 /* Skip zeros. lz is true iff there are leading zeros. */
1539 s1 = s;
1540 while (c == '0')
1541 c = *++s;
1542 lz = s != s1;
1543
1544 /* Get absolute value of the exponent. */
1545 s1 = s;
1546 abs_exp = 0;
1547 while ('0' <= c && c <= '9') {
1548 abs_exp = 10*abs_exp + (c - '0');
1549 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001550 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001551
1552 /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
1553 there are at most 9 significant exponent digits then overflow is
1554 impossible. */
1555 if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1556 e = (int)MAX_ABS_EXP;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001557 else
Mark Dickinsonadd28232010-01-21 19:51:08 +00001558 e = (int)abs_exp;
1559 if (esign)
1560 e = -e;
1561
1562 /* A valid exponent must have at least one digit. */
1563 if (s == s1 && !lz)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001564 s = s00;
1565 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001566
1567 /* Adjust exponent to take into account position of the point. */
1568 e -= nd - nd0;
1569 if (nd0 <= 0)
Mark Dickinson45b63652010-01-16 18:10:25 +00001570 nd0 = nd;
1571
Mark Dickinsonadd28232010-01-21 19:51:08 +00001572 /* Finished parsing. Set se to indicate how far we parsed */
1573 if (se)
1574 *se = (char *)s;
1575
1576 /* If all digits were zero, exit with return value +-0.0. Otherwise,
1577 strip trailing zeros: scan back until we hit a nonzero digit. */
1578 if (!nd)
1579 goto ret;
Mark Dickinson45b63652010-01-16 18:10:25 +00001580 for (i = nd; i > 0; ) {
Mark Dickinson45b63652010-01-16 18:10:25 +00001581 --i;
1582 if (s0[i < nd0 ? i : i+1] != '0') {
1583 ++i;
1584 break;
1585 }
1586 }
1587 e += nd - i;
1588 nd = i;
1589 if (nd0 > nd)
1590 nd0 = nd;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001591
Mark Dickinsonadd28232010-01-21 19:51:08 +00001592 /* Summary of parsing results. After parsing, and dealing with zero
1593 * inputs, we have values s0, nd0, nd, e, sign, where:
Mark Dickinson45b63652010-01-16 18:10:25 +00001594 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001595 * - s0 points to the first significant digit of the input string
Mark Dickinson45b63652010-01-16 18:10:25 +00001596 *
1597 * - nd is the total number of significant digits (here, and
1598 * below, 'significant digits' means the set of digits of the
1599 * significand of the input that remain after ignoring leading
Mark Dickinsonadd28232010-01-21 19:51:08 +00001600 * and trailing zeros).
Mark Dickinson45b63652010-01-16 18:10:25 +00001601 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001602 * - nd0 indicates the position of the decimal point, if present; it
1603 * satisfies 1 <= nd0 <= nd. The nd significant digits are in
1604 * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1605 * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
1606 * nd0 == nd, then s0[nd0] could be any non-digit character.)
Mark Dickinson45b63652010-01-16 18:10:25 +00001607 *
1608 * - e is the adjusted exponent: the absolute value of the number
1609 * represented by the original input string is n * 10**e, where
1610 * n is the integer represented by the concatenation of
1611 * s0[0:nd0] and s0[nd0+1:nd+1]
1612 *
1613 * - sign gives the sign of the input: 1 for negative, 0 for positive
1614 *
1615 * - the first and last significant digits are nonzero
1616 */
1617
1618 /* put first DBL_DIG+1 digits into integer y and z.
1619 *
1620 * - y contains the value represented by the first min(9, nd)
1621 * significant digits
1622 *
1623 * - if nd > 9, z contains the value represented by significant digits
1624 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1625 * gives the value represented by the first min(16, nd) sig. digits.
1626 */
1627
Mark Dickinsonadd28232010-01-21 19:51:08 +00001628 bc.e0 = e1 = e;
Mark Dickinson45b63652010-01-16 18:10:25 +00001629 y = z = 0;
1630 for (i = 0; i < nd; i++) {
1631 if (i < 9)
1632 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1633 else if (i < DBL_DIG+1)
1634 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1635 else
1636 break;
1637 }
1638
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001639 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1640 dval(&rv) = y;
1641 if (k > 9) {
1642 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1643 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001644 if (nd <= DBL_DIG
1645 && Flt_Rounds == 1
1646 ) {
1647 if (!e)
1648 goto ret;
1649 if (e > 0) {
1650 if (e <= Ten_pmax) {
1651 dval(&rv) *= tens[e];
1652 goto ret;
1653 }
1654 i = DBL_DIG - nd;
1655 if (e <= Ten_pmax + i) {
1656 /* A fancier test would sometimes let us do
1657 * this for larger i values.
1658 */
1659 e -= i;
1660 dval(&rv) *= tens[i];
1661 dval(&rv) *= tens[e];
1662 goto ret;
1663 }
1664 }
1665 else if (e >= -Ten_pmax) {
1666 dval(&rv) /= tens[-e];
1667 goto ret;
1668 }
1669 }
1670 e1 += nd - k;
1671
1672 bc.scale = 0;
1673
1674 /* Get starting approximation = rv * 10**e1 */
1675
1676 if (e1 > 0) {
1677 if ((i = e1 & 15))
1678 dval(&rv) *= tens[i];
1679 if (e1 &= ~15) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001680 if (e1 > DBL_MAX_10_EXP)
1681 goto ovfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001682 e1 >>= 4;
1683 for(j = 0; e1 > 1; j++, e1 >>= 1)
1684 if (e1 & 1)
1685 dval(&rv) *= bigtens[j];
1686 /* The last multiplication could overflow. */
1687 word0(&rv) -= P*Exp_msk1;
1688 dval(&rv) *= bigtens[j];
1689 if ((z = word0(&rv) & Exp_mask)
1690 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1691 goto ovfl;
1692 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1693 /* set to largest number */
1694 /* (Can't trust DBL_MAX) */
1695 word0(&rv) = Big0;
1696 word1(&rv) = Big1;
1697 }
1698 else
1699 word0(&rv) += P*Exp_msk1;
1700 }
1701 }
1702 else if (e1 < 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001703 /* The input decimal value lies in [10**e1, 10**(e1+16)).
1704
1705 If e1 <= -512, underflow immediately.
1706 If e1 <= -256, set bc.scale to 2*P.
1707
1708 So for input value < 1e-256, bc.scale is always set;
1709 for input value >= 1e-240, bc.scale is never set.
1710 For input values in [1e-256, 1e-240), bc.scale may or may
1711 not be set. */
1712
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001713 e1 = -e1;
1714 if ((i = e1 & 15))
1715 dval(&rv) /= tens[i];
1716 if (e1 >>= 4) {
1717 if (e1 >= 1 << n_bigtens)
1718 goto undfl;
1719 if (e1 & Scale_Bit)
1720 bc.scale = 2*P;
1721 for(j = 0; e1 > 0; j++, e1 >>= 1)
1722 if (e1 & 1)
1723 dval(&rv) *= tinytens[j];
1724 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1725 >> Exp_shift)) > 0) {
1726 /* scaled rv is denormal; clear j low bits */
1727 if (j >= 32) {
1728 word1(&rv) = 0;
1729 if (j >= 53)
1730 word0(&rv) = (P+2)*Exp_msk1;
1731 else
1732 word0(&rv) &= 0xffffffff << (j-32);
1733 }
1734 else
1735 word1(&rv) &= 0xffffffff << j;
1736 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001737 if (!dval(&rv))
1738 goto undfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001739 }
1740 }
1741
1742 /* Now the hard part -- adjusting rv to the correct value.*/
1743
1744 /* Put digits into bd: true value = bd * 10^e */
1745
1746 bc.nd = nd;
Mark Dickinson81612e82010-01-12 23:04:19 +00001747 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001748 /* to silence an erroneous warning about bc.nd0 */
1749 /* possibly not being initialized. */
Mark Dickinson81612e82010-01-12 23:04:19 +00001750 if (nd > STRTOD_DIGLIM) {
1751 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001752 /* minimum number of decimal digits to distinguish double values */
1753 /* in IEEE arithmetic. */
Mark Dickinson45b63652010-01-16 18:10:25 +00001754
1755 /* Truncate input to 18 significant digits, then discard any trailing
1756 zeros on the result by updating nd, nd0, e and y suitably. (There's
1757 no need to update z; it's not reused beyond this point.) */
1758 for (i = 18; i > 0; ) {
1759 /* scan back until we hit a nonzero digit. significant digit 'i'
1760 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001761 --i;
Mark Dickinson45b63652010-01-16 18:10:25 +00001762 if (s0[i < nd0 ? i : i+1] != '0') {
1763 ++i;
1764 break;
1765 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001766 }
1767 e += nd - i;
1768 nd = i;
1769 if (nd0 > nd)
1770 nd0 = nd;
1771 if (nd < 9) { /* must recompute y */
1772 y = 0;
1773 for(i = 0; i < nd0; ++i)
1774 y = 10*y + s0[i] - '0';
Mark Dickinson45b63652010-01-16 18:10:25 +00001775 for(; i < nd; ++i)
1776 y = 10*y + s0[i+1] - '0';
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001777 }
1778 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001779 bd0 = s2b(s0, nd0, nd, y);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001780 if (bd0 == NULL)
1781 goto failed_malloc;
1782
Mark Dickinsonadd28232010-01-21 19:51:08 +00001783 /* Notation for the comments below. Write:
1784
1785 - dv for the absolute value of the number represented by the original
1786 decimal input string.
1787
1788 - if we've truncated dv, write tdv for the truncated value.
1789 Otherwise, set tdv == dv.
1790
1791 - srv for the quantity rv/2^bc.scale; so srv is the current binary
1792 approximation to tdv (and dv). It should be exactly representable
1793 in an IEEE 754 double.
1794 */
1795
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001796 for(;;) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001797
1798 /* This is the main correction loop for _Py_dg_strtod.
1799
1800 We've got a decimal value tdv, and a floating-point approximation
1801 srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
1802 close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1803 approximation if not.
1804
1805 To determine whether srv is close enough to tdv, compute integers
1806 bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1807 respectively, and then use integer arithmetic to determine whether
1808 |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1809 */
1810
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001811 bd = Balloc(bd0->k);
1812 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001813 goto failed_malloc;
1814 }
1815 Bcopy(bd, bd0);
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001816 bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001817 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001818 goto failed_malloc;
1819 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001820 /* Record whether lsb of bb is odd, in case we need this
1821 for the round-to-even step later. */
1822 odd = bb->x[0] & 1;
1823
1824 /* tdv = bd * 10**e; srv = bb * 2**bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001825 bs = i2b(1);
1826 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001827 goto failed_malloc;
1828 }
1829
1830 if (e >= 0) {
1831 bb2 = bb5 = 0;
1832 bd2 = bd5 = e;
1833 }
1834 else {
1835 bb2 = bb5 = -e;
1836 bd2 = bd5 = 0;
1837 }
1838 if (bbe >= 0)
1839 bb2 += bbe;
1840 else
1841 bd2 -= bbe;
1842 bs2 = bb2;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001843 bb2++;
1844 bd2++;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001845
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001846 /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
Mark Dickinsone383e822012-04-29 15:31:56 +01001847 and bs == 1, so:
Mark Dickinsonadd28232010-01-21 19:51:08 +00001848
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001849 tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1850 srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
Mark Dickinsone383e822012-04-29 15:31:56 +01001851 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001852
Mark Dickinsone383e822012-04-29 15:31:56 +01001853 It follows that:
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001854
1855 M * tdv = bd * 2**bd2 * 5**bd5
1856 M * srv = bb * 2**bb2 * 5**bb5
1857 M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1858
Mark Dickinsone383e822012-04-29 15:31:56 +01001859 for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1860 this fact is not needed below.)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001861 */
1862
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001863 /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001864 i = bb2 < bd2 ? bb2 : bd2;
1865 if (i > bs2)
1866 i = bs2;
1867 if (i > 0) {
1868 bb2 -= i;
1869 bd2 -= i;
1870 bs2 -= i;
1871 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001872
1873 /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001874 if (bb5 > 0) {
1875 bs = pow5mult(bs, bb5);
1876 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001877 goto failed_malloc;
1878 }
Victor Stinner9776b062019-03-13 17:55:01 +01001879 Bigint *bb1 = mult(bs, bb);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001880 Bfree(bb);
1881 bb = bb1;
1882 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001883 goto failed_malloc;
1884 }
1885 }
1886 if (bb2 > 0) {
1887 bb = lshift(bb, bb2);
1888 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001889 goto failed_malloc;
1890 }
1891 }
1892 if (bd5 > 0) {
1893 bd = pow5mult(bd, bd5);
1894 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001895 goto failed_malloc;
1896 }
1897 }
1898 if (bd2 > 0) {
1899 bd = lshift(bd, bd2);
1900 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001901 goto failed_malloc;
1902 }
1903 }
1904 if (bs2 > 0) {
1905 bs = lshift(bs, bs2);
1906 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001907 goto failed_malloc;
1908 }
1909 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001910
1911 /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
1912 respectively. Compute the difference |tdv - srv|, and compare
1913 with 0.5 ulp(srv). */
1914
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001915 delta = diff(bb, bd);
1916 if (delta == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001917 goto failed_malloc;
1918 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001919 dsign = delta->sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001920 delta->sign = 0;
1921 i = cmp(delta, bs);
1922 if (bc.nd > nd && i <= 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001923 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001924 break; /* Must use bigcomp(). */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001925
1926 /* Here rv overestimates the truncated decimal value by at most
1927 0.5 ulp(rv). Hence rv either overestimates the true decimal
1928 value by <= 0.5 ulp(rv), or underestimates it by some small
1929 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1930 the true decimal value, so it's possible to exit.
1931
1932 Exception: if scaled rv is a normal exact power of 2, but not
1933 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1934 next double, so the correctly rounded result is either rv - 0.5
1935 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1936
1937 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1938 /* rv can't be 0, since it's an overestimate for some
1939 nonzero value. So rv is a normal power of 2. */
1940 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1941 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1942 rv / 2^bc.scale >= 2^-1021. */
1943 if (j - bc.scale >= 2) {
1944 dval(&rv) -= 0.5 * sulp(&rv, &bc);
Mark Dickinsonadd28232010-01-21 19:51:08 +00001945 break; /* Use bigcomp. */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001946 }
1947 }
1948
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001949 {
1950 bc.nd = nd;
1951 i = -1; /* Discarded digits make delta smaller. */
1952 }
1953 }
1954
1955 if (i < 0) {
1956 /* Error is less than half an ulp -- check for
1957 * special case of mantissa a power of two.
1958 */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001959 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001960 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
1961 ) {
1962 break;
1963 }
1964 if (!delta->x[0] && delta->wds <= 1) {
1965 /* exact result */
1966 break;
1967 }
1968 delta = lshift(delta,Log2P);
1969 if (delta == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001970 goto failed_malloc;
1971 }
1972 if (cmp(delta, bs) > 0)
1973 goto drop_down;
1974 break;
1975 }
1976 if (i == 0) {
1977 /* exactly half-way between */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001978 if (dsign) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001979 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1980 && word1(&rv) == (
1981 (bc.scale &&
1982 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
1983 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1984 0xffffffff)) {
1985 /*boundary case -- increment exponent*/
1986 word0(&rv) = (word0(&rv) & Exp_mask)
1987 + Exp_msk1
1988 ;
1989 word1(&rv) = 0;
Brett Cannonb94767f2011-02-22 20:15:44 +00001990 /* dsign = 0; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001991 break;
1992 }
1993 }
1994 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1995 drop_down:
1996 /* boundary case -- decrement exponent */
1997 if (bc.scale) {
1998 L = word0(&rv) & Exp_mask;
1999 if (L <= (2*P+1)*Exp_msk1) {
2000 if (L > (P+2)*Exp_msk1)
2001 /* round even ==> */
2002 /* accept rv */
2003 break;
2004 /* rv = smallest denormal */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002005 if (bc.nd > nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002006 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002007 goto undfl;
2008 }
2009 }
2010 L = (word0(&rv) & Exp_mask) - Exp_msk1;
2011 word0(&rv) = L | Bndry_mask1;
2012 word1(&rv) = 0xffffffff;
2013 break;
2014 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002015 if (!odd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002016 break;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002017 if (dsign)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002018 dval(&rv) += sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002019 else {
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002020 dval(&rv) -= sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002021 if (!dval(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002022 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002023 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002024 goto undfl;
2025 }
2026 }
Brett Cannonb94767f2011-02-22 20:15:44 +00002027 /* dsign = 1 - dsign; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002028 break;
2029 }
2030 if ((aadj = ratio(delta, bs)) <= 2.) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00002031 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002032 aadj = aadj1 = 1.;
2033 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
2034 if (word1(&rv) == Tiny1 && !word0(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002035 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002036 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002037 goto undfl;
2038 }
2039 aadj = 1.;
2040 aadj1 = -1.;
2041 }
2042 else {
2043 /* special case -- power of FLT_RADIX to be */
2044 /* rounded down... */
2045
2046 if (aadj < 2./FLT_RADIX)
2047 aadj = 1./FLT_RADIX;
2048 else
2049 aadj *= 0.5;
2050 aadj1 = -aadj;
2051 }
2052 }
2053 else {
2054 aadj *= 0.5;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002055 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002056 if (Flt_Rounds == 0)
2057 aadj1 += 0.5;
2058 }
2059 y = word0(&rv) & Exp_mask;
2060
2061 /* Check for overflow */
2062
2063 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2064 dval(&rv0) = dval(&rv);
2065 word0(&rv) -= P*Exp_msk1;
2066 adj.d = aadj1 * ulp(&rv);
2067 dval(&rv) += adj.d;
2068 if ((word0(&rv) & Exp_mask) >=
2069 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002070 if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002071 goto ovfl;
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002072 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002073 word0(&rv) = Big0;
2074 word1(&rv) = Big1;
2075 goto cont;
2076 }
2077 else
2078 word0(&rv) += P*Exp_msk1;
2079 }
2080 else {
2081 if (bc.scale && y <= 2*P*Exp_msk1) {
2082 if (aadj <= 0x7fffffff) {
2083 if ((z = (ULong)aadj) <= 0)
2084 z = 1;
2085 aadj = z;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002086 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002087 }
2088 dval(&aadj2) = aadj1;
2089 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2090 aadj1 = dval(&aadj2);
2091 }
2092 adj.d = aadj1 * ulp(&rv);
2093 dval(&rv) += adj.d;
2094 }
2095 z = word0(&rv) & Exp_mask;
2096 if (bc.nd == nd) {
2097 if (!bc.scale)
2098 if (y == z) {
2099 /* Can we stop now? */
2100 L = (Long)aadj;
2101 aadj -= L;
2102 /* The tolerances below are conservative. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002103 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002104 if (aadj < .4999999 || aadj > .5000001)
2105 break;
2106 }
2107 else if (aadj < .4999999/FLT_RADIX)
2108 break;
2109 }
2110 }
2111 cont:
Victor Stinner9776b062019-03-13 17:55:01 +01002112 Bfree(bb); bb = NULL;
2113 Bfree(bd); bd = NULL;
2114 Bfree(bs); bs = NULL;
2115 Bfree(delta); delta = NULL;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002116 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002117 if (bc.nd > nd) {
2118 error = bigcomp(&rv, s0, &bc);
2119 if (error)
2120 goto failed_malloc;
2121 }
2122
2123 if (bc.scale) {
2124 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2125 word1(&rv0) = 0;
2126 dval(&rv) *= dval(&rv0);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002127 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00002128
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002129 ret:
Victor Stinner9776b062019-03-13 17:55:01 +01002130 result = sign ? -dval(&rv) : dval(&rv);
2131 goto done;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002132
Mark Dickinsonadd28232010-01-21 19:51:08 +00002133 parse_error:
Victor Stinner9776b062019-03-13 17:55:01 +01002134 result = 0.0;
2135 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002136
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002137 failed_malloc:
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002138 errno = ENOMEM;
Victor Stinner9776b062019-03-13 17:55:01 +01002139 result = -1.0;
2140 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002141
2142 undfl:
Victor Stinner9776b062019-03-13 17:55:01 +01002143 result = sign ? -0.0 : 0.0;
2144 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002145
2146 ovfl:
2147 errno = ERANGE;
2148 /* Can't trust HUGE_VAL */
2149 word0(&rv) = Exp_mask;
2150 word1(&rv) = 0;
Victor Stinner9776b062019-03-13 17:55:01 +01002151 result = sign ? -dval(&rv) : dval(&rv);
2152 goto done;
2153
2154 done:
2155 Bfree(bb);
2156 Bfree(bd);
2157 Bfree(bs);
2158 Bfree(bd0);
2159 Bfree(delta);
2160 return result;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002161
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002162}
2163
2164static char *
2165rv_alloc(int i)
2166{
2167 int j, k, *r;
2168
2169 j = sizeof(ULong);
2170 for(k = 0;
2171 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2172 j <<= 1)
2173 k++;
2174 r = (int*)Balloc(k);
2175 if (r == NULL)
2176 return NULL;
2177 *r = k;
2178 return (char *)(r+1);
2179}
2180
2181static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002182nrv_alloc(const char *s, char **rve, int n)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002183{
2184 char *rv, *t;
2185
2186 rv = rv_alloc(n);
2187 if (rv == NULL)
2188 return NULL;
2189 t = rv;
2190 while((*t = *s++)) t++;
2191 if (rve)
2192 *rve = t;
2193 return rv;
2194}
2195
2196/* freedtoa(s) must be used to free values s returned by dtoa
2197 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2198 * but for consistency with earlier versions of dtoa, it is optional
2199 * when MULTIPLE_THREADS is not defined.
2200 */
2201
2202void
2203_Py_dg_freedtoa(char *s)
2204{
2205 Bigint *b = (Bigint *)((int *)s - 1);
2206 b->maxwds = 1 << (b->k = *(int*)b);
2207 Bfree(b);
2208}
2209
2210/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2211 *
2212 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2213 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2214 *
2215 * Modifications:
2216 * 1. Rather than iterating, we use a simple numeric overestimate
2217 * to determine k = floor(log10(d)). We scale relevant
2218 * quantities using O(log2(k)) rather than O(k) multiplications.
2219 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2220 * try to generate digits strictly left to right. Instead, we
2221 * compute with fewer bits and propagate the carry if necessary
2222 * when rounding the final digit up. This is often faster.
2223 * 3. Under the assumption that input will be rounded nearest,
2224 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2225 * That is, we allow equality in stopping tests when the
2226 * round-nearest rule will give the same floating-point value
2227 * as would satisfaction of the stopping test with strict
2228 * inequality.
2229 * 4. We remove common factors of powers of 2 from relevant
2230 * quantities.
2231 * 5. When converting floating-point integers less than 1e16,
2232 * we use floating-point arithmetic rather than resorting
2233 * to multiple-precision integers.
2234 * 6. When asked to produce fewer than 15 digits, we first try
2235 * to get by with floating-point arithmetic; we resort to
2236 * multiple-precision integer arithmetic only if we cannot
2237 * guarantee that the floating-point calculation has given
2238 * the correctly rounded result. For k requested digits and
2239 * "uniformly" distributed input, the probability is
2240 * something like 10^(k-15) that we must resort to the Long
2241 * calculation.
2242 */
2243
2244/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2245 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2246 call to _Py_dg_freedtoa. */
2247
2248char *
2249_Py_dg_dtoa(double dd, int mode, int ndigits,
2250 int *decpt, int *sign, char **rve)
2251{
2252 /* Arguments ndigits, decpt, sign are similar to those
2253 of ecvt and fcvt; trailing zeros are suppressed from
2254 the returned string. If not null, *rve is set to point
2255 to the end of the return value. If d is +-Infinity or NaN,
2256 then *decpt is set to 9999.
2257
2258 mode:
2259 0 ==> shortest string that yields d when read in
2260 and rounded to nearest.
2261 1 ==> like 0, but with Steele & White stopping rule;
2262 e.g. with IEEE P754 arithmetic , mode 0 gives
2263 1e23 whereas mode 1 gives 9.999999999999999e22.
2264 2 ==> max(1,ndigits) significant digits. This gives a
2265 return value similar to that of ecvt, except
2266 that trailing zeros are suppressed.
2267 3 ==> through ndigits past the decimal point. This
2268 gives a return value similar to that from fcvt,
2269 except that trailing zeros are suppressed, and
2270 ndigits can be negative.
2271 4,5 ==> similar to 2 and 3, respectively, but (in
2272 round-nearest mode) with the tests of mode 0 to
2273 possibly return a shorter string that rounds to d.
2274 With IEEE arithmetic and compilation with
2275 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2276 as modes 2 and 3 when FLT_ROUNDS != 1.
2277 6-9 ==> Debugging modes similar to mode - 4: don't try
2278 fast floating-point estimate (if applicable).
2279
2280 Values of mode other than 0-9 are treated as mode 0.
2281
2282 Sufficient space is allocated to the return value
2283 to hold the suppressed trailing zeros.
2284 */
2285
2286 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2287 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2288 spec_case, try_quick;
2289 Long L;
2290 int denorm;
2291 ULong x;
2292 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2293 U d2, eps, u;
2294 double ds;
2295 char *s, *s0;
2296
2297 /* set pointers to NULL, to silence gcc compiler warnings and make
2298 cleanup easier on error */
Mark Dickinsond3697262010-05-13 11:52:22 +00002299 mlo = mhi = S = 0;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002300 s0 = 0;
2301
2302 u.d = dd;
2303 if (word0(&u) & Sign_bit) {
2304 /* set sign for everything, including 0's and NaNs */
2305 *sign = 1;
2306 word0(&u) &= ~Sign_bit; /* clear sign bit */
2307 }
2308 else
2309 *sign = 0;
2310
2311 /* quick return for Infinities, NaNs and zeros */
2312 if ((word0(&u) & Exp_mask) == Exp_mask)
2313 {
2314 /* Infinity or NaN */
2315 *decpt = 9999;
2316 if (!word1(&u) && !(word0(&u) & 0xfffff))
2317 return nrv_alloc("Infinity", rve, 8);
2318 return nrv_alloc("NaN", rve, 3);
2319 }
2320 if (!dval(&u)) {
2321 *decpt = 1;
2322 return nrv_alloc("0", rve, 1);
2323 }
2324
2325 /* compute k = floor(log10(d)). The computation may leave k
2326 one too large, but should never leave k too small. */
2327 b = d2b(&u, &be, &bbits);
2328 if (b == NULL)
2329 goto failed_malloc;
2330 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2331 dval(&d2) = dval(&u);
2332 word0(&d2) &= Frac_mask1;
2333 word0(&d2) |= Exp_11;
2334
2335 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2336 * log10(x) = log(x) / log(10)
2337 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2338 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2339 *
2340 * This suggests computing an approximation k to log10(d) by
2341 *
2342 * k = (i - Bias)*0.301029995663981
2343 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2344 *
2345 * We want k to be too large rather than too small.
2346 * The error in the first-order Taylor series approximation
2347 * is in our favor, so we just round up the constant enough
2348 * to compensate for any error in the multiplication of
2349 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2350 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2351 * adding 1e-13 to the constant term more than suffices.
2352 * Hence we adjust the constant term to 0.1760912590558.
2353 * (We could get a more accurate k by invoking log10,
2354 * but this is probably not worthwhile.)
2355 */
2356
2357 i -= Bias;
2358 denorm = 0;
2359 }
2360 else {
2361 /* d is denormalized */
2362
2363 i = bbits + be + (Bias + (P-1) - 1);
2364 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2365 : word1(&u) << (32 - i);
2366 dval(&d2) = x;
2367 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2368 i -= (Bias + (P-1) - 1) + 1;
2369 denorm = 1;
2370 }
2371 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2372 i*0.301029995663981;
2373 k = (int)ds;
2374 if (ds < 0. && ds != k)
2375 k--; /* want k = floor(ds) */
2376 k_check = 1;
2377 if (k >= 0 && k <= Ten_pmax) {
2378 if (dval(&u) < tens[k])
2379 k--;
2380 k_check = 0;
2381 }
2382 j = bbits - i - 1;
2383 if (j >= 0) {
2384 b2 = 0;
2385 s2 = j;
2386 }
2387 else {
2388 b2 = -j;
2389 s2 = 0;
2390 }
2391 if (k >= 0) {
2392 b5 = 0;
2393 s5 = k;
2394 s2 += k;
2395 }
2396 else {
2397 b2 -= k;
2398 b5 = -k;
2399 s5 = 0;
2400 }
2401 if (mode < 0 || mode > 9)
2402 mode = 0;
2403
2404 try_quick = 1;
2405
2406 if (mode > 5) {
2407 mode -= 4;
2408 try_quick = 0;
2409 }
2410 leftright = 1;
2411 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2412 /* silence erroneous "gcc -Wall" warning. */
2413 switch(mode) {
2414 case 0:
2415 case 1:
2416 i = 18;
2417 ndigits = 0;
2418 break;
2419 case 2:
2420 leftright = 0;
Stefan Krahf432a322017-08-21 13:09:59 +02002421 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002422 case 4:
2423 if (ndigits <= 0)
2424 ndigits = 1;
2425 ilim = ilim1 = i = ndigits;
2426 break;
2427 case 3:
2428 leftright = 0;
Stefan Krahf432a322017-08-21 13:09:59 +02002429 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002430 case 5:
2431 i = ndigits + k + 1;
2432 ilim = i;
2433 ilim1 = i - 1;
2434 if (i <= 0)
2435 i = 1;
2436 }
2437 s0 = rv_alloc(i);
2438 if (s0 == NULL)
2439 goto failed_malloc;
2440 s = s0;
2441
2442
2443 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2444
2445 /* Try to get by with floating-point arithmetic. */
2446
2447 i = 0;
2448 dval(&d2) = dval(&u);
2449 k0 = k;
2450 ilim0 = ilim;
2451 ieps = 2; /* conservative */
2452 if (k > 0) {
2453 ds = tens[k&0xf];
2454 j = k >> 4;
2455 if (j & Bletch) {
2456 /* prevent overflows */
2457 j &= Bletch - 1;
2458 dval(&u) /= bigtens[n_bigtens-1];
2459 ieps++;
2460 }
2461 for(; j; j >>= 1, i++)
2462 if (j & 1) {
2463 ieps++;
2464 ds *= bigtens[i];
2465 }
2466 dval(&u) /= ds;
2467 }
2468 else if ((j1 = -k)) {
2469 dval(&u) *= tens[j1 & 0xf];
2470 for(j = j1 >> 4; j; j >>= 1, i++)
2471 if (j & 1) {
2472 ieps++;
2473 dval(&u) *= bigtens[i];
2474 }
2475 }
2476 if (k_check && dval(&u) < 1. && ilim > 0) {
2477 if (ilim1 <= 0)
2478 goto fast_failed;
2479 ilim = ilim1;
2480 k--;
2481 dval(&u) *= 10.;
2482 ieps++;
2483 }
2484 dval(&eps) = ieps*dval(&u) + 7.;
2485 word0(&eps) -= (P-1)*Exp_msk1;
2486 if (ilim == 0) {
2487 S = mhi = 0;
2488 dval(&u) -= 5.;
2489 if (dval(&u) > dval(&eps))
2490 goto one_digit;
2491 if (dval(&u) < -dval(&eps))
2492 goto no_digits;
2493 goto fast_failed;
2494 }
2495 if (leftright) {
2496 /* Use Steele & White method of only
2497 * generating digits needed.
2498 */
2499 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2500 for(i = 0;;) {
2501 L = (Long)dval(&u);
2502 dval(&u) -= L;
2503 *s++ = '0' + (int)L;
2504 if (dval(&u) < dval(&eps))
2505 goto ret1;
2506 if (1. - dval(&u) < dval(&eps))
2507 goto bump_up;
2508 if (++i >= ilim)
2509 break;
2510 dval(&eps) *= 10.;
2511 dval(&u) *= 10.;
2512 }
2513 }
2514 else {
2515 /* Generate ilim digits, then fix them up. */
2516 dval(&eps) *= tens[ilim-1];
2517 for(i = 1;; i++, dval(&u) *= 10.) {
2518 L = (Long)(dval(&u));
2519 if (!(dval(&u) -= L))
2520 ilim = i;
2521 *s++ = '0' + (int)L;
2522 if (i == ilim) {
2523 if (dval(&u) > 0.5 + dval(&eps))
2524 goto bump_up;
2525 else if (dval(&u) < 0.5 - dval(&eps)) {
2526 while(*--s == '0');
2527 s++;
2528 goto ret1;
2529 }
2530 break;
2531 }
2532 }
2533 }
2534 fast_failed:
2535 s = s0;
2536 dval(&u) = dval(&d2);
2537 k = k0;
2538 ilim = ilim0;
2539 }
2540
2541 /* Do we have a "small" integer? */
2542
2543 if (be >= 0 && k <= Int_max) {
2544 /* Yes. */
2545 ds = tens[k];
2546 if (ndigits < 0 && ilim <= 0) {
2547 S = mhi = 0;
2548 if (ilim < 0 || dval(&u) <= 5*ds)
2549 goto no_digits;
2550 goto one_digit;
2551 }
2552 for(i = 1;; i++, dval(&u) *= 10.) {
2553 L = (Long)(dval(&u) / ds);
2554 dval(&u) -= L*ds;
2555 *s++ = '0' + (int)L;
2556 if (!dval(&u)) {
2557 break;
2558 }
2559 if (i == ilim) {
2560 dval(&u) += dval(&u);
2561 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2562 bump_up:
2563 while(*--s == '9')
2564 if (s == s0) {
2565 k++;
2566 *s = '0';
2567 break;
2568 }
2569 ++*s++;
2570 }
Mark Dickinson895c9c12020-05-29 14:23:57 +01002571 else {
2572 /* Strip trailing zeros. This branch was missing from the
2573 original dtoa.c, leading to surplus trailing zeros in
2574 some cases. See bugs.python.org/issue40780. */
2575 while (s > s0 && s[-1] == '0') {
2576 --s;
2577 }
2578 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002579 break;
2580 }
2581 }
2582 goto ret1;
2583 }
2584
2585 m2 = b2;
2586 m5 = b5;
2587 if (leftright) {
2588 i =
2589 denorm ? be + (Bias + (P-1) - 1 + 1) :
2590 1 + P - bbits;
2591 b2 += i;
2592 s2 += i;
2593 mhi = i2b(1);
2594 if (mhi == NULL)
2595 goto failed_malloc;
2596 }
2597 if (m2 > 0 && s2 > 0) {
2598 i = m2 < s2 ? m2 : s2;
2599 b2 -= i;
2600 m2 -= i;
2601 s2 -= i;
2602 }
2603 if (b5 > 0) {
2604 if (leftright) {
2605 if (m5 > 0) {
2606 mhi = pow5mult(mhi, m5);
2607 if (mhi == NULL)
2608 goto failed_malloc;
2609 b1 = mult(mhi, b);
2610 Bfree(b);
2611 b = b1;
2612 if (b == NULL)
2613 goto failed_malloc;
2614 }
2615 if ((j = b5 - m5)) {
2616 b = pow5mult(b, j);
2617 if (b == NULL)
2618 goto failed_malloc;
2619 }
2620 }
2621 else {
2622 b = pow5mult(b, b5);
2623 if (b == NULL)
2624 goto failed_malloc;
2625 }
2626 }
2627 S = i2b(1);
2628 if (S == NULL)
2629 goto failed_malloc;
2630 if (s5 > 0) {
2631 S = pow5mult(S, s5);
2632 if (S == NULL)
2633 goto failed_malloc;
2634 }
2635
2636 /* Check for special case that d is a normalized power of 2. */
2637
2638 spec_case = 0;
2639 if ((mode < 2 || leftright)
2640 ) {
2641 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2642 && word0(&u) & (Exp_mask & ~Exp_msk1)
2643 ) {
2644 /* The special case */
2645 b2 += Log2P;
2646 s2 += Log2P;
2647 spec_case = 1;
2648 }
2649 }
2650
2651 /* Arrange for convenient computation of quotients:
2652 * shift left if necessary so divisor has 4 leading 0 bits.
2653 *
2654 * Perhaps we should just compute leading 28 bits of S once
2655 * and for all and pass them and a shift to quorem, so it
2656 * can do shifts and ors to compute the numerator for q.
2657 */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002658#define iInc 28
2659 i = dshift(S, s2);
2660 b2 += i;
2661 m2 += i;
2662 s2 += i;
2663 if (b2 > 0) {
2664 b = lshift(b, b2);
2665 if (b == NULL)
2666 goto failed_malloc;
2667 }
2668 if (s2 > 0) {
2669 S = lshift(S, s2);
2670 if (S == NULL)
2671 goto failed_malloc;
2672 }
2673 if (k_check) {
2674 if (cmp(b,S) < 0) {
2675 k--;
2676 b = multadd(b, 10, 0); /* we botched the k estimate */
2677 if (b == NULL)
2678 goto failed_malloc;
2679 if (leftright) {
2680 mhi = multadd(mhi, 10, 0);
2681 if (mhi == NULL)
2682 goto failed_malloc;
2683 }
2684 ilim = ilim1;
2685 }
2686 }
2687 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2688 if (ilim < 0) {
2689 /* no digits, fcvt style */
2690 no_digits:
2691 k = -1 - ndigits;
2692 goto ret;
2693 }
2694 else {
2695 S = multadd(S, 5, 0);
2696 if (S == NULL)
2697 goto failed_malloc;
2698 if (cmp(b, S) <= 0)
2699 goto no_digits;
2700 }
2701 one_digit:
2702 *s++ = '1';
2703 k++;
2704 goto ret;
2705 }
2706 if (leftright) {
2707 if (m2 > 0) {
2708 mhi = lshift(mhi, m2);
2709 if (mhi == NULL)
2710 goto failed_malloc;
2711 }
2712
2713 /* Compute mlo -- check for special case
2714 * that d is a normalized power of 2.
2715 */
2716
2717 mlo = mhi;
2718 if (spec_case) {
2719 mhi = Balloc(mhi->k);
2720 if (mhi == NULL)
2721 goto failed_malloc;
2722 Bcopy(mhi, mlo);
2723 mhi = lshift(mhi, Log2P);
2724 if (mhi == NULL)
2725 goto failed_malloc;
2726 }
2727
2728 for(i = 1;;i++) {
2729 dig = quorem(b,S) + '0';
2730 /* Do we yet have the shortest decimal string
2731 * that will round to d?
2732 */
2733 j = cmp(b, mlo);
2734 delta = diff(S, mhi);
2735 if (delta == NULL)
2736 goto failed_malloc;
2737 j1 = delta->sign ? 1 : cmp(b, delta);
2738 Bfree(delta);
2739 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2740 ) {
2741 if (dig == '9')
2742 goto round_9_up;
2743 if (j > 0)
2744 dig++;
2745 *s++ = dig;
2746 goto ret;
2747 }
2748 if (j < 0 || (j == 0 && mode != 1
2749 && !(word1(&u) & 1)
2750 )) {
2751 if (!b->x[0] && b->wds <= 1) {
2752 goto accept_dig;
2753 }
2754 if (j1 > 0) {
2755 b = lshift(b, 1);
2756 if (b == NULL)
2757 goto failed_malloc;
2758 j1 = cmp(b, S);
2759 if ((j1 > 0 || (j1 == 0 && dig & 1))
2760 && dig++ == '9')
2761 goto round_9_up;
2762 }
2763 accept_dig:
2764 *s++ = dig;
2765 goto ret;
2766 }
2767 if (j1 > 0) {
2768 if (dig == '9') { /* possible if i == 1 */
2769 round_9_up:
2770 *s++ = '9';
2771 goto roundoff;
2772 }
2773 *s++ = dig + 1;
2774 goto ret;
2775 }
2776 *s++ = dig;
2777 if (i == ilim)
2778 break;
2779 b = multadd(b, 10, 0);
2780 if (b == NULL)
2781 goto failed_malloc;
2782 if (mlo == mhi) {
2783 mlo = mhi = multadd(mhi, 10, 0);
2784 if (mlo == NULL)
2785 goto failed_malloc;
2786 }
2787 else {
2788 mlo = multadd(mlo, 10, 0);
2789 if (mlo == NULL)
2790 goto failed_malloc;
2791 mhi = multadd(mhi, 10, 0);
2792 if (mhi == NULL)
2793 goto failed_malloc;
2794 }
2795 }
2796 }
2797 else
2798 for(i = 1;; i++) {
2799 *s++ = dig = quorem(b,S) + '0';
2800 if (!b->x[0] && b->wds <= 1) {
2801 goto ret;
2802 }
2803 if (i >= ilim)
2804 break;
2805 b = multadd(b, 10, 0);
2806 if (b == NULL)
2807 goto failed_malloc;
2808 }
2809
2810 /* Round off last digit */
2811
2812 b = lshift(b, 1);
2813 if (b == NULL)
2814 goto failed_malloc;
2815 j = cmp(b, S);
2816 if (j > 0 || (j == 0 && dig & 1)) {
2817 roundoff:
2818 while(*--s == '9')
2819 if (s == s0) {
2820 k++;
2821 *s++ = '1';
2822 goto ret;
2823 }
2824 ++*s++;
2825 }
2826 else {
2827 while(*--s == '0');
2828 s++;
2829 }
2830 ret:
2831 Bfree(S);
2832 if (mhi) {
2833 if (mlo && mlo != mhi)
2834 Bfree(mlo);
2835 Bfree(mhi);
2836 }
2837 ret1:
2838 Bfree(b);
2839 *s = 0;
2840 *decpt = k + 1;
2841 if (rve)
2842 *rve = s;
2843 return s0;
2844 failed_malloc:
2845 if (S)
2846 Bfree(S);
2847 if (mlo && mlo != mhi)
2848 Bfree(mlo);
2849 if (mhi)
2850 Bfree(mhi);
2851 if (b)
2852 Bfree(b);
2853 if (s0)
2854 _Py_dg_freedtoa(s0);
2855 return NULL;
2856}
2857#ifdef __cplusplus
2858}
2859#endif
2860
2861#endif /* PY_NO_SHORT_FLOAT_REPR */