blob: 822adc612962a90f6bf1919aa8458c0072a1caea [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"
Victor Stinnere9e7d282020-02-12 22:54:42 +0100118#include "pycore_dtoa.h"
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000119
120/* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
121 the following code */
122#ifndef PY_NO_SHORT_FLOAT_REPR
123
124#include "float.h"
125
126#define MALLOC PyMem_Malloc
127#define FREE PyMem_Free
128
129/* This code should also work for ARM mixed-endian format on little-endian
130 machines, where doubles have byte order 45670123 (in increasing address
131 order, 0 being the least significant byte). */
132#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
133# define IEEE_8087
134#endif
135#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
136 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
137# define IEEE_MC68k
138#endif
139#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
140#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
141#endif
142
143/* The code below assumes that the endianness of integers matches the
144 endianness of the two 32-bit words of a double. Check this. */
145#if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
146 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
147#error "doubles and ints have incompatible endianness"
148#endif
149
150#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
151#error "doubles and ints have incompatible endianness"
152#endif
153
154
Benjamin Peterson4fe55102016-09-06 11:58:01 -0700155typedef uint32_t ULong;
156typedef int32_t Long;
157typedef uint64_t ULLong;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000158
159#undef DEBUG
160#ifdef Py_DEBUG
161#define DEBUG
162#endif
163
164/* End Python #define linking */
165
166#ifdef DEBUG
167#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
168#endif
169
170#ifndef PRIVATE_MEM
171#define PRIVATE_MEM 2304
172#endif
173#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
174static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
175
176#ifdef __cplusplus
177extern "C" {
178#endif
179
180typedef union { double d; ULong L[2]; } U;
181
182#ifdef IEEE_8087
183#define word0(x) (x)->L[1]
184#define word1(x) (x)->L[0]
185#else
186#define word0(x) (x)->L[0]
187#define word1(x) (x)->L[1]
188#endif
189#define dval(x) (x)->d
190
191#ifndef STRTOD_DIGLIM
192#define STRTOD_DIGLIM 40
193#endif
194
Mark Dickinson81612e82010-01-12 23:04:19 +0000195/* maximum permitted exponent value for strtod; exponents larger than
196 MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
197 should fit into an int. */
198#ifndef MAX_ABS_EXP
Mark Dickinsonf45bbb62013-11-26 16:19:13 +0000199#define MAX_ABS_EXP 1100000000U
200#endif
201/* Bound on length of pieces of input strings in _Py_dg_strtod; specifically,
202 this is used to bound the total number of digits ignoring leading zeros and
203 the number of digits that follow the decimal point. Ideally, MAX_DIGITS
204 should satisfy MAX_DIGITS + 400 < MAX_ABS_EXP; that ensures that the
205 exponent clipping in _Py_dg_strtod can't affect the value of the output. */
206#ifndef MAX_DIGITS
207#define MAX_DIGITS 1000000000U
208#endif
209
210/* Guard against trying to use the above values on unusual platforms with ints
211 * of width less than 32 bits. */
212#if MAX_ABS_EXP > INT_MAX
213#error "MAX_ABS_EXP should fit in an int"
214#endif
215#if MAX_DIGITS > INT_MAX
216#error "MAX_DIGITS should fit in an int"
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000217#endif
218
219/* The following definition of Storeinc is appropriate for MIPS processors.
220 * An alternative that might be better on some machines is
221 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
222 */
223#if defined(IEEE_8087)
224#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
225 ((unsigned short *)a)[0] = (unsigned short)c, a++)
226#else
227#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
228 ((unsigned short *)a)[1] = (unsigned short)c, a++)
229#endif
230
231/* #define P DBL_MANT_DIG */
232/* Ten_pmax = floor(P*log(2)/log(5)) */
233/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
234/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
235/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
236
237#define Exp_shift 20
238#define Exp_shift1 20
239#define Exp_msk1 0x100000
240#define Exp_msk11 0x100000
241#define Exp_mask 0x7ff00000
242#define P 53
243#define Nbits 53
244#define Bias 1023
245#define Emax 1023
246#define Emin (-1022)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000247#define Etiny (-1074) /* smallest denormal is 2**Etiny */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000248#define Exp_1 0x3ff00000
249#define Exp_11 0x3ff00000
250#define Ebits 11
251#define Frac_mask 0xfffff
252#define Frac_mask1 0xfffff
253#define Ten_pmax 22
254#define Bletch 0x10
255#define Bndry_mask 0xfffff
256#define Bndry_mask1 0xfffff
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000257#define Sign_bit 0x80000000
258#define Log2P 1
259#define Tiny0 0
260#define Tiny1 1
261#define Quick_max 14
262#define Int_max 14
263
264#ifndef Flt_Rounds
265#ifdef FLT_ROUNDS
266#define Flt_Rounds FLT_ROUNDS
267#else
268#define Flt_Rounds 1
269#endif
270#endif /*Flt_Rounds*/
271
272#define Rounding Flt_Rounds
273
274#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
275#define Big1 0xffffffff
276
Mark Dickinsone383e822012-04-29 15:31:56 +0100277/* Standard NaN used by _Py_dg_stdnan. */
278
279#define NAN_WORD0 0x7ff80000
280#define NAN_WORD1 0
281
282/* Bits of the representation of positive infinity. */
283
284#define POSINF_WORD0 0x7ff00000
285#define POSINF_WORD1 0
286
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000287/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
288
289typedef struct BCinfo BCinfo;
290struct
291BCinfo {
Mark Dickinsonadd28232010-01-21 19:51:08 +0000292 int e0, nd, nd0, scale;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000293};
294
295#define FFFFFFFF 0xffffffffUL
296
297#define Kmax 7
298
299/* struct Bigint is used to represent arbitrary-precision integers. These
300 integers are stored in sign-magnitude format, with the magnitude stored as
301 an array of base 2**32 digits. Bigints are always normalized: if x is a
302 Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
303
304 The Bigint fields are as follows:
305
306 - next is a header used by Balloc and Bfree to keep track of lists
307 of freed Bigints; it's also used for the linked list of
308 powers of 5 of the form 5**2**i used by pow5mult.
309 - k indicates which pool this Bigint was allocated from
310 - maxwds is the maximum number of words space was allocated for
311 (usually maxwds == 2**k)
312 - sign is 1 for negative Bigints, 0 for positive. The sign is unused
313 (ignored on inputs, set to 0 on outputs) in almost all operations
314 involving Bigints: a notable exception is the diff function, which
315 ignores signs on inputs but sets the sign of the output correctly.
316 - wds is the actual number of significant words
317 - x contains the vector of words (digits) for this Bigint, from least
318 significant (x[0]) to most significant (x[wds-1]).
319*/
320
321struct
322Bigint {
323 struct Bigint *next;
324 int k, maxwds, sign, wds;
325 ULong x[1];
326};
327
328typedef struct Bigint Bigint;
329
Mark Dickinsonde508002010-01-17 21:02:55 +0000330#ifndef Py_USING_MEMORY_DEBUGGER
331
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000332/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
333 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
334 1 << k. These pools are maintained as linked lists, with freelist[k]
335 pointing to the head of the list for pool k.
336
337 On allocation, if there's no free slot in the appropriate pool, MALLOC is
338 called to get more memory. This memory is not returned to the system until
339 Python quits. There's also a private memory pool that's allocated from
340 in preference to using MALLOC.
341
342 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
343 decimal digits), memory is directly allocated using MALLOC, and freed using
344 FREE.
345
346 XXX: it would be easy to bypass this memory-management system and
347 translate each call to Balloc into a call to PyMem_Malloc, and each
348 Bfree to PyMem_Free. Investigate whether this has any significant
349 performance on impact. */
350
351static Bigint *freelist[Kmax+1];
352
353/* Allocate space for a Bigint with up to 1<<k digits */
354
355static Bigint *
356Balloc(int k)
357{
358 int x;
359 Bigint *rv;
360 unsigned int len;
361
362 if (k <= Kmax && (rv = freelist[k]))
363 freelist[k] = rv->next;
364 else {
365 x = 1 << k;
366 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
367 /sizeof(double);
Victor Stinner938b0b92015-03-18 15:01:44 +0100368 if (k <= Kmax && pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000369 rv = (Bigint*)pmem_next;
370 pmem_next += len;
371 }
372 else {
373 rv = (Bigint*)MALLOC(len*sizeof(double));
374 if (rv == NULL)
375 return NULL;
376 }
377 rv->k = k;
378 rv->maxwds = x;
379 }
380 rv->sign = rv->wds = 0;
381 return rv;
382}
383
384/* Free a Bigint allocated with Balloc */
385
386static void
387Bfree(Bigint *v)
388{
389 if (v) {
390 if (v->k > Kmax)
391 FREE((void*)v);
392 else {
393 v->next = freelist[v->k];
394 freelist[v->k] = v;
395 }
396 }
397}
398
Mark Dickinsonde508002010-01-17 21:02:55 +0000399#else
400
401/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
402 PyMem_Free directly in place of the custom memory allocation scheme above.
403 These are provided for the benefit of memory debugging tools like
404 Valgrind. */
405
406/* Allocate space for a Bigint with up to 1<<k digits */
407
408static Bigint *
409Balloc(int k)
410{
411 int x;
412 Bigint *rv;
413 unsigned int len;
414
415 x = 1 << k;
416 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
417 /sizeof(double);
418
419 rv = (Bigint*)MALLOC(len*sizeof(double));
420 if (rv == NULL)
421 return NULL;
422
423 rv->k = k;
424 rv->maxwds = x;
425 rv->sign = rv->wds = 0;
426 return rv;
427}
428
429/* Free a Bigint allocated with Balloc */
430
431static void
432Bfree(Bigint *v)
433{
434 if (v) {
435 FREE((void*)v);
436 }
437}
438
439#endif /* Py_USING_MEMORY_DEBUGGER */
440
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000441#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
442 y->wds*sizeof(Long) + 2*sizeof(int))
443
444/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
445 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
446 On failure, return NULL. In this case, b will have been already freed. */
447
448static Bigint *
449multadd(Bigint *b, int m, int a) /* multiply by m and add a */
450{
451 int i, wds;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000452 ULong *x;
453 ULLong carry, y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000454 Bigint *b1;
455
456 wds = b->wds;
457 x = b->x;
458 i = 0;
459 carry = a;
460 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000461 y = *x * (ULLong)m + carry;
462 carry = y >> 32;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000463 *x++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000464 }
465 while(++i < wds);
466 if (carry) {
467 if (wds >= b->maxwds) {
468 b1 = Balloc(b->k+1);
469 if (b1 == NULL){
470 Bfree(b);
471 return NULL;
472 }
473 Bcopy(b1, b);
474 Bfree(b);
475 b = b1;
476 }
477 b->x[wds++] = (ULong)carry;
478 b->wds = wds;
479 }
480 return b;
481}
482
483/* convert a string s containing nd decimal digits (possibly containing a
484 decimal separator at position nd0, which is ignored) to a Bigint. This
485 function carries on where the parsing code in _Py_dg_strtod leaves off: on
486 entry, y9 contains the result of converting the first 9 digits. Returns
487 NULL on failure. */
488
489static Bigint *
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000490s2b(const char *s, int nd0, int nd, ULong y9)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000491{
492 Bigint *b;
493 int i, k;
494 Long x, y;
495
496 x = (nd + 8) / 9;
497 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
498 b = Balloc(k);
499 if (b == NULL)
500 return NULL;
501 b->x[0] = y9;
502 b->wds = 1;
503
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000504 if (nd <= 9)
505 return b;
506
507 s += 9;
508 for (i = 9; i < nd0; i++) {
509 b = multadd(b, 10, *s++ - '0');
510 if (b == NULL)
511 return NULL;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000512 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +0000513 s++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000514 for(; i < nd; i++) {
515 b = multadd(b, 10, *s++ - '0');
516 if (b == NULL)
517 return NULL;
518 }
519 return b;
520}
521
522/* count leading 0 bits in the 32-bit integer x. */
523
524static int
525hi0bits(ULong x)
526{
527 int k = 0;
528
529 if (!(x & 0xffff0000)) {
530 k = 16;
531 x <<= 16;
532 }
533 if (!(x & 0xff000000)) {
534 k += 8;
535 x <<= 8;
536 }
537 if (!(x & 0xf0000000)) {
538 k += 4;
539 x <<= 4;
540 }
541 if (!(x & 0xc0000000)) {
542 k += 2;
543 x <<= 2;
544 }
545 if (!(x & 0x80000000)) {
546 k++;
547 if (!(x & 0x40000000))
548 return 32;
549 }
550 return k;
551}
552
553/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
554 number of bits. */
555
556static int
557lo0bits(ULong *y)
558{
559 int k;
560 ULong x = *y;
561
562 if (x & 7) {
563 if (x & 1)
564 return 0;
565 if (x & 2) {
566 *y = x >> 1;
567 return 1;
568 }
569 *y = x >> 2;
570 return 2;
571 }
572 k = 0;
573 if (!(x & 0xffff)) {
574 k = 16;
575 x >>= 16;
576 }
577 if (!(x & 0xff)) {
578 k += 8;
579 x >>= 8;
580 }
581 if (!(x & 0xf)) {
582 k += 4;
583 x >>= 4;
584 }
585 if (!(x & 0x3)) {
586 k += 2;
587 x >>= 2;
588 }
589 if (!(x & 1)) {
590 k++;
591 x >>= 1;
592 if (!x)
593 return 32;
594 }
595 *y = x;
596 return k;
597}
598
599/* convert a small nonnegative integer to a Bigint */
600
601static Bigint *
602i2b(int i)
603{
604 Bigint *b;
605
606 b = Balloc(1);
607 if (b == NULL)
608 return NULL;
609 b->x[0] = i;
610 b->wds = 1;
611 return b;
612}
613
614/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
615 the signs of a and b. */
616
617static Bigint *
618mult(Bigint *a, Bigint *b)
619{
620 Bigint *c;
621 int k, wa, wb, wc;
622 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
623 ULong y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000624 ULLong carry, z;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000625
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000626 if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
627 c = Balloc(0);
628 if (c == NULL)
629 return NULL;
630 c->wds = 1;
631 c->x[0] = 0;
632 return c;
633 }
634
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000635 if (a->wds < b->wds) {
636 c = a;
637 a = b;
638 b = c;
639 }
640 k = a->k;
641 wa = a->wds;
642 wb = b->wds;
643 wc = wa + wb;
644 if (wc > a->maxwds)
645 k++;
646 c = Balloc(k);
647 if (c == NULL)
648 return NULL;
649 for(x = c->x, xa = x + wc; x < xa; x++)
650 *x = 0;
651 xa = a->x;
652 xae = xa + wa;
653 xb = b->x;
654 xbe = xb + wb;
655 xc0 = c->x;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000656 for(; xb < xbe; xc0++) {
657 if ((y = *xb++)) {
658 x = xa;
659 xc = xc0;
660 carry = 0;
661 do {
662 z = *x++ * (ULLong)y + *xc + carry;
663 carry = z >> 32;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000664 *xc++ = (ULong)(z & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000665 }
666 while(x < xae);
667 *xc = (ULong)carry;
668 }
669 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000670 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
671 c->wds = wc;
672 return c;
673}
674
Mark Dickinsonde508002010-01-17 21:02:55 +0000675#ifndef Py_USING_MEMORY_DEBUGGER
676
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000677/* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
678
679static Bigint *p5s;
680
681/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
682 failure; if the returned pointer is distinct from b then the original
683 Bigint b will have been Bfree'd. Ignores the sign of b. */
684
685static Bigint *
686pow5mult(Bigint *b, int k)
687{
688 Bigint *b1, *p5, *p51;
689 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200690 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000691
692 if ((i = k & 3)) {
693 b = multadd(b, p05[i-1], 0);
694 if (b == NULL)
695 return NULL;
696 }
697
698 if (!(k >>= 2))
699 return b;
700 p5 = p5s;
701 if (!p5) {
702 /* first time */
703 p5 = i2b(625);
704 if (p5 == NULL) {
705 Bfree(b);
706 return NULL;
707 }
708 p5s = p5;
709 p5->next = 0;
710 }
711 for(;;) {
712 if (k & 1) {
713 b1 = mult(b, p5);
714 Bfree(b);
715 b = b1;
716 if (b == NULL)
717 return NULL;
718 }
719 if (!(k >>= 1))
720 break;
721 p51 = p5->next;
722 if (!p51) {
723 p51 = mult(p5,p5);
724 if (p51 == NULL) {
725 Bfree(b);
726 return NULL;
727 }
728 p51->next = 0;
729 p5->next = p51;
730 }
731 p5 = p51;
732 }
733 return b;
734}
735
Mark Dickinsonde508002010-01-17 21:02:55 +0000736#else
737
738/* Version of pow5mult that doesn't cache powers of 5. Provided for
739 the benefit of memory debugging tools like Valgrind. */
740
741static Bigint *
742pow5mult(Bigint *b, int k)
743{
744 Bigint *b1, *p5, *p51;
745 int i;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200746 static const int p05[3] = { 5, 25, 125 };
Mark Dickinsonde508002010-01-17 21:02:55 +0000747
748 if ((i = k & 3)) {
749 b = multadd(b, p05[i-1], 0);
750 if (b == NULL)
751 return NULL;
752 }
753
754 if (!(k >>= 2))
755 return b;
756 p5 = i2b(625);
757 if (p5 == NULL) {
758 Bfree(b);
759 return NULL;
760 }
761
762 for(;;) {
763 if (k & 1) {
764 b1 = mult(b, p5);
765 Bfree(b);
766 b = b1;
767 if (b == NULL) {
768 Bfree(p5);
769 return NULL;
770 }
771 }
772 if (!(k >>= 1))
773 break;
774 p51 = mult(p5, p5);
775 Bfree(p5);
776 p5 = p51;
777 if (p5 == NULL) {
778 Bfree(b);
779 return NULL;
780 }
781 }
782 Bfree(p5);
783 return b;
784}
785
786#endif /* Py_USING_MEMORY_DEBUGGER */
787
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000788/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
789 or NULL on failure. If the returned pointer is distinct from b then the
790 original b will have been Bfree'd. Ignores the sign of b. */
791
792static Bigint *
793lshift(Bigint *b, int k)
794{
795 int i, k1, n, n1;
796 Bigint *b1;
797 ULong *x, *x1, *xe, z;
798
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000799 if (!k || (!b->x[0] && b->wds == 1))
800 return b;
801
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000802 n = k >> 5;
803 k1 = b->k;
804 n1 = n + b->wds + 1;
805 for(i = b->maxwds; n1 > i; i <<= 1)
806 k1++;
807 b1 = Balloc(k1);
808 if (b1 == NULL) {
809 Bfree(b);
810 return NULL;
811 }
812 x1 = b1->x;
813 for(i = 0; i < n; i++)
814 *x1++ = 0;
815 x = b->x;
816 xe = x + b->wds;
817 if (k &= 0x1f) {
818 k1 = 32 - k;
819 z = 0;
820 do {
821 *x1++ = *x << k | z;
822 z = *x++ >> k1;
823 }
824 while(x < xe);
825 if ((*x1 = z))
826 ++n1;
827 }
828 else do
829 *x1++ = *x++;
830 while(x < xe);
831 b1->wds = n1 - 1;
832 Bfree(b);
833 return b1;
834}
835
836/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
837 1 if a > b. Ignores signs of a and b. */
838
839static int
840cmp(Bigint *a, Bigint *b)
841{
842 ULong *xa, *xa0, *xb, *xb0;
843 int i, j;
844
845 i = a->wds;
846 j = b->wds;
847#ifdef DEBUG
848 if (i > 1 && !a->x[i-1])
849 Bug("cmp called with a->x[a->wds-1] == 0");
850 if (j > 1 && !b->x[j-1])
851 Bug("cmp called with b->x[b->wds-1] == 0");
852#endif
853 if (i -= j)
854 return i;
855 xa0 = a->x;
856 xa = xa0 + j;
857 xb0 = b->x;
858 xb = xb0 + j;
859 for(;;) {
860 if (*--xa != *--xb)
861 return *xa < *xb ? -1 : 1;
862 if (xa <= xa0)
863 break;
864 }
865 return 0;
866}
867
868/* Take the difference of Bigints a and b, returning a new Bigint. Returns
869 NULL on failure. The signs of a and b are ignored, but the sign of the
870 result is set appropriately. */
871
872static Bigint *
873diff(Bigint *a, Bigint *b)
874{
875 Bigint *c;
876 int i, wa, wb;
877 ULong *xa, *xae, *xb, *xbe, *xc;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000878 ULLong borrow, y;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000879
880 i = cmp(a,b);
881 if (!i) {
882 c = Balloc(0);
883 if (c == NULL)
884 return NULL;
885 c->wds = 1;
886 c->x[0] = 0;
887 return c;
888 }
889 if (i < 0) {
890 c = a;
891 a = b;
892 b = c;
893 i = 1;
894 }
895 else
896 i = 0;
897 c = Balloc(a->k);
898 if (c == NULL)
899 return NULL;
900 c->sign = i;
901 wa = a->wds;
902 xa = a->x;
903 xae = xa + wa;
904 wb = b->wds;
905 xb = b->x;
906 xbe = xb + wb;
907 xc = c->x;
908 borrow = 0;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000909 do {
910 y = (ULLong)*xa++ - *xb++ - borrow;
911 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000912 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000913 }
914 while(xb < xbe);
915 while(xa < xae) {
916 y = *xa++ - borrow;
917 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +0000918 *xc++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000919 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000920 while(!*--xc)
921 wa--;
922 c->wds = wa;
923 return c;
924}
925
Mark Dickinsonadd28232010-01-21 19:51:08 +0000926/* Given a positive normal double x, return the difference between x and the
927 next double up. Doesn't give correct results for subnormals. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +0000928
929static double
930ulp(U *x)
931{
932 Long L;
933 U u;
934
935 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
936 word0(&u) = L;
937 word1(&u) = 0;
938 return dval(&u);
939}
940
941/* Convert a Bigint to a double plus an exponent */
942
943static double
944b2d(Bigint *a, int *e)
945{
946 ULong *xa, *xa0, w, y, z;
947 int k;
948 U d;
949
950 xa0 = a->x;
951 xa = xa0 + a->wds;
952 y = *--xa;
953#ifdef DEBUG
954 if (!y) Bug("zero y in b2d");
955#endif
956 k = hi0bits(y);
957 *e = 32 - k;
958 if (k < Ebits) {
959 word0(&d) = Exp_1 | y >> (Ebits - k);
960 w = xa > xa0 ? *--xa : 0;
961 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
962 goto ret_d;
963 }
964 z = xa > xa0 ? *--xa : 0;
965 if (k -= Ebits) {
966 word0(&d) = Exp_1 | y << k | z >> (32 - k);
967 y = xa > xa0 ? *--xa : 0;
968 word1(&d) = z << k | y >> (32 - k);
969 }
970 else {
971 word0(&d) = Exp_1 | y;
972 word1(&d) = z;
973 }
974 ret_d:
975 return dval(&d);
976}
977
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000978/* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
979 except that it accepts the scale parameter used in _Py_dg_strtod (which
980 should be either 0 or 2*P), and the normalization for the return value is
981 different (see below). On input, d should be finite and nonnegative, and d
982 / 2**scale should be exactly representable as an IEEE 754 double.
983
984 Returns a Bigint b and an integer e such that
985
986 dval(d) / 2**scale = b * 2**e.
987
988 Unlike d2b, b is not necessarily odd: b and e are normalized so
989 that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
990 and e == Etiny. This applies equally to an input of 0.0: in that
991 case the return values are b = 0 and e = Etiny.
992
993 The above normalization ensures that for all possible inputs d,
994 2**e gives ulp(d/2**scale).
995
996 Returns NULL on failure.
997*/
998
999static Bigint *
1000sd2b(U *d, int scale, int *e)
1001{
1002 Bigint *b;
1003
1004 b = Balloc(1);
1005 if (b == NULL)
1006 return NULL;
Victor Stinner938b0b92015-03-18 15:01:44 +01001007
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001008 /* First construct b and e assuming that scale == 0. */
1009 b->wds = 2;
1010 b->x[0] = word1(d);
1011 b->x[1] = word0(d) & Frac_mask;
1012 *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
1013 if (*e < Etiny)
1014 *e = Etiny;
1015 else
1016 b->x[1] |= Exp_msk1;
1017
1018 /* Now adjust for scale, provided that b != 0. */
1019 if (scale && (b->x[0] || b->x[1])) {
1020 *e -= scale;
1021 if (*e < Etiny) {
1022 scale = Etiny - *e;
1023 *e = Etiny;
1024 /* We can't shift more than P-1 bits without shifting out a 1. */
1025 assert(0 < scale && scale <= P - 1);
1026 if (scale >= 32) {
1027 /* The bits shifted out should all be zero. */
1028 assert(b->x[0] == 0);
1029 b->x[0] = b->x[1];
1030 b->x[1] = 0;
1031 scale -= 32;
1032 }
1033 if (scale) {
1034 /* The bits shifted out should all be zero. */
1035 assert(b->x[0] << (32 - scale) == 0);
1036 b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1037 b->x[1] >>= scale;
1038 }
1039 }
1040 }
1041 /* Ensure b is normalized. */
1042 if (!b->x[1])
1043 b->wds = 1;
1044
1045 return b;
1046}
1047
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001048/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
1049
1050 Given a finite nonzero double d, return an odd Bigint b and exponent *e
1051 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
Mark Dickinson180e4cd2010-01-04 21:33:31 +00001052 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001053
1054 If d is zero, then b == 0, *e == -1010, *bbits = 0.
1055 */
1056
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001057static Bigint *
1058d2b(U *d, int *e, int *bits)
1059{
1060 Bigint *b;
1061 int de, k;
1062 ULong *x, y, z;
1063 int i;
1064
1065 b = Balloc(1);
1066 if (b == NULL)
1067 return NULL;
1068 x = b->x;
1069
1070 z = word0(d) & Frac_mask;
1071 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
1072 if ((de = (int)(word0(d) >> Exp_shift)))
1073 z |= Exp_msk1;
1074 if ((y = word1(d))) {
1075 if ((k = lo0bits(&y))) {
1076 x[0] = y | z << (32 - k);
1077 z >>= k;
1078 }
1079 else
1080 x[0] = y;
1081 i =
1082 b->wds = (x[1] = z) ? 2 : 1;
1083 }
1084 else {
1085 k = lo0bits(&z);
1086 x[0] = z;
1087 i =
1088 b->wds = 1;
1089 k += 32;
1090 }
1091 if (de) {
1092 *e = de - Bias - (P-1) + k;
1093 *bits = P - k;
1094 }
1095 else {
1096 *e = de - Bias - (P-1) + 1 + k;
1097 *bits = 32*i - hi0bits(x[i-1]);
1098 }
1099 return b;
1100}
1101
1102/* Compute the ratio of two Bigints, as a double. The result may have an
1103 error of up to 2.5 ulps. */
1104
1105static double
1106ratio(Bigint *a, Bigint *b)
1107{
1108 U da, db;
1109 int k, ka, kb;
1110
1111 dval(&da) = b2d(a, &ka);
1112 dval(&db) = b2d(b, &kb);
1113 k = ka - kb + 32*(a->wds - b->wds);
1114 if (k > 0)
1115 word0(&da) += k*Exp_msk1;
1116 else {
1117 k = -k;
1118 word0(&db) += k*Exp_msk1;
1119 }
1120 return dval(&da) / dval(&db);
1121}
1122
1123static const double
1124tens[] = {
1125 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1126 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1127 1e20, 1e21, 1e22
1128};
1129
1130static const double
1131bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1132static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1133 9007199254740992.*9007199254740992.e-256
1134 /* = 2^106 * 1e-256 */
1135};
1136/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1137/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1138#define Scale_Bit 0x10
1139#define n_bigtens 5
1140
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001141#define ULbits 32
1142#define kshift 5
1143#define kmask 31
1144
1145
1146static int
1147dshift(Bigint *b, int p2)
1148{
1149 int rv = hi0bits(b->x[b->wds-1]) - 4;
1150 if (p2 > 0)
1151 rv -= p2;
1152 return rv & kmask;
1153}
1154
1155/* special case of Bigint division. The quotient is always in the range 0 <=
1156 quotient < 10, and on entry the divisor S is normalized so that its top 4
1157 bits (28--31) are zero and bit 27 is set. */
1158
1159static int
1160quorem(Bigint *b, Bigint *S)
1161{
1162 int n;
1163 ULong *bx, *bxe, q, *sx, *sxe;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001164 ULLong borrow, carry, y, ys;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001165
1166 n = S->wds;
1167#ifdef DEBUG
1168 /*debug*/ if (b->wds > n)
1169 /*debug*/ Bug("oversize b in quorem");
1170#endif
1171 if (b->wds < n)
1172 return 0;
1173 sx = S->x;
1174 sxe = sx + --n;
1175 bx = b->x;
1176 bxe = bx + n;
1177 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1178#ifdef DEBUG
1179 /*debug*/ if (q > 9)
1180 /*debug*/ Bug("oversized quotient in quorem");
1181#endif
1182 if (q) {
1183 borrow = 0;
1184 carry = 0;
1185 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001186 ys = *sx++ * (ULLong)q + carry;
1187 carry = ys >> 32;
1188 y = *bx - (ys & FFFFFFFF) - borrow;
1189 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001190 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001191 }
1192 while(sx <= sxe);
1193 if (!*bxe) {
1194 bx = b->x;
1195 while(--bxe > bx && !*bxe)
1196 --n;
1197 b->wds = n;
1198 }
1199 }
1200 if (cmp(b, S) >= 0) {
1201 q++;
1202 borrow = 0;
1203 carry = 0;
1204 bx = b->x;
1205 sx = S->x;
1206 do {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001207 ys = *sx++ + carry;
1208 carry = ys >> 32;
1209 y = *bx - (ys & FFFFFFFF) - borrow;
1210 borrow = y >> 32 & (ULong)1;
Mark Dickinsonfd2ad8b2009-04-17 19:29:46 +00001211 *bx++ = (ULong)(y & FFFFFFFF);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001212 }
1213 while(sx <= sxe);
1214 bx = b->x;
1215 bxe = bx + n;
1216 if (!*bxe) {
1217 while(--bxe > bx && !*bxe)
1218 --n;
1219 b->wds = n;
1220 }
1221 }
1222 return q;
1223}
1224
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001225/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
Mark Dickinson81612e82010-01-12 23:04:19 +00001226
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001227 Assuming that x is finite and nonnegative (positive zero is fine
1228 here) and x / 2^bc.scale is exactly representable as a double,
1229 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
Mark Dickinson81612e82010-01-12 23:04:19 +00001230
1231static double
1232sulp(U *x, BCinfo *bc)
1233{
1234 U u;
1235
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001236 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00001237 /* rv/2^bc->scale is subnormal */
1238 word0(&u) = (P+2)*Exp_msk1;
1239 word1(&u) = 0;
1240 return u.d;
1241 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001242 else {
1243 assert(word0(x) || word1(x)); /* x != 0.0 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001244 return ulp(x);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001245 }
Mark Dickinson81612e82010-01-12 23:04:19 +00001246}
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001247
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001248/* The bigcomp function handles some hard cases for strtod, for inputs
1249 with more than STRTOD_DIGLIM digits. It's called once an initial
1250 estimate for the double corresponding to the input string has
1251 already been obtained by the code in _Py_dg_strtod.
1252
1253 The bigcomp function is only called after _Py_dg_strtod has found a
1254 double value rv such that either rv or rv + 1ulp represents the
1255 correctly rounded value corresponding to the original string. It
1256 determines which of these two values is the correct one by
1257 computing the decimal digits of rv + 0.5ulp and comparing them with
1258 the corresponding digits of s0.
1259
1260 In the following, write dv for the absolute value of the number represented
1261 by the input string.
1262
1263 Inputs:
1264
1265 s0 points to the first significant digit of the input string.
1266
1267 rv is a (possibly scaled) estimate for the closest double value to the
1268 value represented by the original input to _Py_dg_strtod. If
1269 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1270 the input value.
1271
1272 bc is a struct containing information gathered during the parsing and
1273 estimation steps of _Py_dg_strtod. Description of fields follows:
1274
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001275 bc->e0 gives the exponent of the input value, such that dv = (integer
1276 given by the bd->nd digits of s0) * 10**e0
1277
1278 bc->nd gives the total number of significant digits of s0. It will
1279 be at least 1.
1280
1281 bc->nd0 gives the number of significant digits of s0 before the
1282 decimal separator. If there's no decimal separator, bc->nd0 ==
1283 bc->nd.
1284
1285 bc->scale is the value used to scale rv to avoid doing arithmetic with
1286 subnormal values. It's either 0 or 2*P (=106).
1287
1288 Outputs:
1289
1290 On successful exit, rv/2^(bc->scale) is the closest double to dv.
1291
1292 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001293
1294static int
1295bigcomp(U *rv, const char *s0, BCinfo *bc)
1296{
1297 Bigint *b, *d;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001298 int b2, d2, dd, i, nd, nd0, odd, p2, p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001299
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001300 nd = bc->nd;
1301 nd0 = bc->nd0;
Mark Dickinson81612e82010-01-12 23:04:19 +00001302 p5 = nd + bc->e0;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001303 b = sd2b(rv, bc->scale, &p2);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001304 if (b == NULL)
1305 return -1;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001306
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001307 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1308 case, this is used for round to even. */
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001309 odd = b->x[0] & 1;
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001310
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001311 /* left shift b by 1 bit and or a 1 into the least significant bit;
1312 this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1313 b = lshift(b, 1);
1314 if (b == NULL)
1315 return -1;
1316 b->x[0] |= 1;
1317 p2--;
1318
1319 p2 -= p5;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001320 d = i2b(1);
1321 if (d == NULL) {
1322 Bfree(b);
1323 return -1;
1324 }
1325 /* Arrange for convenient computation of quotients:
1326 * shift left if necessary so divisor has 4 leading 0 bits.
1327 */
1328 if (p5 > 0) {
1329 d = pow5mult(d, p5);
1330 if (d == NULL) {
1331 Bfree(b);
1332 return -1;
1333 }
1334 }
1335 else if (p5 < 0) {
1336 b = pow5mult(b, -p5);
1337 if (b == NULL) {
1338 Bfree(d);
1339 return -1;
1340 }
1341 }
1342 if (p2 > 0) {
1343 b2 = p2;
1344 d2 = 0;
1345 }
1346 else {
1347 b2 = 0;
1348 d2 = -p2;
1349 }
1350 i = dshift(d, d2);
1351 if ((b2 += i) > 0) {
1352 b = lshift(b, b2);
1353 if (b == NULL) {
1354 Bfree(d);
1355 return -1;
1356 }
1357 }
1358 if ((d2 += i) > 0) {
1359 d = lshift(d, d2);
1360 if (d == NULL) {
1361 Bfree(b);
1362 return -1;
1363 }
1364 }
1365
Mark Dickinsonadd28232010-01-21 19:51:08 +00001366 /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1367 * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
1368 * a number in the range [0.1, 1). */
1369 if (cmp(b, d) >= 0)
1370 /* b/d >= 1 */
Mark Dickinson81612e82010-01-12 23:04:19 +00001371 dd = -1;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001372 else {
1373 i = 0;
1374 for(;;) {
1375 b = multadd(b, 10, 0);
1376 if (b == NULL) {
1377 Bfree(d);
1378 return -1;
1379 }
1380 dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1381 i++;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001382
Mark Dickinsonadd28232010-01-21 19:51:08 +00001383 if (dd)
1384 break;
1385 if (!b->x[0] && b->wds == 1) {
1386 /* b/d == 0 */
1387 dd = i < nd;
1388 break;
1389 }
1390 if (!(i < nd)) {
1391 /* b/d != 0, but digits of s0 exhausted */
1392 dd = -1;
1393 break;
1394 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001395 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001396 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001397 Bfree(b);
1398 Bfree(d);
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001399 if (dd > 0 || (dd == 0 && odd))
1400 dval(rv) += sulp(rv, bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001401 return 0;
1402}
1403
Mark Dickinsone383e822012-04-29 15:31:56 +01001404/* Return a 'standard' NaN value.
1405
1406 There are exactly two quiet NaNs that don't arise by 'quieting' signaling
1407 NaNs (see IEEE 754-2008, section 6.2.1). If sign == 0, return the one whose
1408 sign bit is cleared. Otherwise, return the one whose sign bit is set.
1409*/
1410
1411double
1412_Py_dg_stdnan(int sign)
1413{
1414 U rv;
1415 word0(&rv) = NAN_WORD0;
1416 word1(&rv) = NAN_WORD1;
1417 if (sign)
1418 word0(&rv) |= Sign_bit;
1419 return dval(&rv);
1420}
1421
1422/* Return positive or negative infinity, according to the given sign (0 for
1423 * positive infinity, 1 for negative infinity). */
1424
1425double
1426_Py_dg_infinity(int sign)
1427{
1428 U rv;
1429 word0(&rv) = POSINF_WORD0;
1430 word1(&rv) = POSINF_WORD1;
1431 return sign ? -dval(&rv) : dval(&rv);
1432}
1433
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001434double
1435_Py_dg_strtod(const char *s00, char **se)
1436{
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001437 int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1438 int esign, i, j, k, lz, nd, nd0, odd, sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001439 const char *s, *s0, *s1;
1440 double aadj, aadj1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001441 U aadj2, adj, rv, rv0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001442 ULong y, z, abs_exp;
Mark Dickinson45b63652010-01-16 18:10:25 +00001443 Long L;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001444 BCinfo bc;
Victor Stinner9776b062019-03-13 17:55:01 +01001445 Bigint *bb = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001446 size_t ndigits, fraclen;
Victor Stinner9776b062019-03-13 17:55:01 +01001447 double result;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001448
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001449 dval(&rv) = 0.;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001450
1451 /* Start parsing. */
1452 c = *(s = s00);
1453
1454 /* Parse optional sign, if present. */
1455 sign = 0;
1456 switch (c) {
1457 case '-':
1458 sign = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001459 /* fall through */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001460 case '+':
1461 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001462 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001463
1464 /* Skip leading zeros: lz is true iff there were leading zeros. */
1465 s1 = s;
1466 while (c == '0')
1467 c = *++s;
1468 lz = s != s1;
1469
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001470 /* Point s0 at the first nonzero digit (if any). fraclen will be the
1471 number of digits between the decimal point and the end of the
1472 digit string. ndigits will be the total number of digits ignoring
1473 leading zeros. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001474 s0 = s1 = s;
1475 while ('0' <= c && c <= '9')
1476 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001477 ndigits = s - s1;
1478 fraclen = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001479
1480 /* Parse decimal point and following digits. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001481 if (c == '.') {
1482 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001483 if (!ndigits) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001484 s1 = s;
1485 while (c == '0')
1486 c = *++s;
1487 lz = lz || s != s1;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001488 fraclen += (s - s1);
Mark Dickinsonadd28232010-01-21 19:51:08 +00001489 s0 = s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001490 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001491 s1 = s;
1492 while ('0' <= c && c <= '9')
1493 c = *++s;
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001494 ndigits += s - s1;
1495 fraclen += s - s1;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001496 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001497
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001498 /* Now lz is true if and only if there were leading zero digits, and
1499 ndigits gives the total number of digits ignoring leading zeros. A
1500 valid input must have at least one digit. */
1501 if (!ndigits && !lz) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001502 if (se)
1503 *se = (char *)s00;
1504 goto parse_error;
1505 }
1506
Mark Dickinsonf45bbb62013-11-26 16:19:13 +00001507 /* Range check ndigits and fraclen to make sure that they, and values
1508 computed with them, can safely fit in an int. */
1509 if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) {
1510 if (se)
1511 *se = (char *)s00;
1512 goto parse_error;
1513 }
1514 nd = (int)ndigits;
1515 nd0 = (int)ndigits - (int)fraclen;
1516
Mark Dickinsonadd28232010-01-21 19:51:08 +00001517 /* Parse exponent. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001518 e = 0;
1519 if (c == 'e' || c == 'E') {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001520 s00 = s;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001521 c = *++s;
1522
1523 /* Exponent sign. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001524 esign = 0;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001525 switch (c) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001526 case '-':
1527 esign = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001528 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001529 case '+':
1530 c = *++s;
1531 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001532
1533 /* Skip zeros. lz is true iff there are leading zeros. */
1534 s1 = s;
1535 while (c == '0')
1536 c = *++s;
1537 lz = s != s1;
1538
1539 /* Get absolute value of the exponent. */
1540 s1 = s;
1541 abs_exp = 0;
1542 while ('0' <= c && c <= '9') {
1543 abs_exp = 10*abs_exp + (c - '0');
1544 c = *++s;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001545 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001546
1547 /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
1548 there are at most 9 significant exponent digits then overflow is
1549 impossible. */
1550 if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1551 e = (int)MAX_ABS_EXP;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001552 else
Mark Dickinsonadd28232010-01-21 19:51:08 +00001553 e = (int)abs_exp;
1554 if (esign)
1555 e = -e;
1556
1557 /* A valid exponent must have at least one digit. */
1558 if (s == s1 && !lz)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001559 s = s00;
1560 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001561
1562 /* Adjust exponent to take into account position of the point. */
1563 e -= nd - nd0;
1564 if (nd0 <= 0)
Mark Dickinson45b63652010-01-16 18:10:25 +00001565 nd0 = nd;
1566
Mark Dickinsonadd28232010-01-21 19:51:08 +00001567 /* Finished parsing. Set se to indicate how far we parsed */
1568 if (se)
1569 *se = (char *)s;
1570
1571 /* If all digits were zero, exit with return value +-0.0. Otherwise,
1572 strip trailing zeros: scan back until we hit a nonzero digit. */
1573 if (!nd)
1574 goto ret;
Mark Dickinson45b63652010-01-16 18:10:25 +00001575 for (i = nd; i > 0; ) {
Mark Dickinson45b63652010-01-16 18:10:25 +00001576 --i;
1577 if (s0[i < nd0 ? i : i+1] != '0') {
1578 ++i;
1579 break;
1580 }
1581 }
1582 e += nd - i;
1583 nd = i;
1584 if (nd0 > nd)
1585 nd0 = nd;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001586
Mark Dickinsonadd28232010-01-21 19:51:08 +00001587 /* Summary of parsing results. After parsing, and dealing with zero
1588 * inputs, we have values s0, nd0, nd, e, sign, where:
Mark Dickinson45b63652010-01-16 18:10:25 +00001589 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001590 * - s0 points to the first significant digit of the input string
Mark Dickinson45b63652010-01-16 18:10:25 +00001591 *
1592 * - nd is the total number of significant digits (here, and
1593 * below, 'significant digits' means the set of digits of the
1594 * significand of the input that remain after ignoring leading
Mark Dickinsonadd28232010-01-21 19:51:08 +00001595 * and trailing zeros).
Mark Dickinson45b63652010-01-16 18:10:25 +00001596 *
Mark Dickinsonadd28232010-01-21 19:51:08 +00001597 * - nd0 indicates the position of the decimal point, if present; it
1598 * satisfies 1 <= nd0 <= nd. The nd significant digits are in
1599 * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1600 * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
1601 * nd0 == nd, then s0[nd0] could be any non-digit character.)
Mark Dickinson45b63652010-01-16 18:10:25 +00001602 *
1603 * - e is the adjusted exponent: the absolute value of the number
1604 * represented by the original input string is n * 10**e, where
1605 * n is the integer represented by the concatenation of
1606 * s0[0:nd0] and s0[nd0+1:nd+1]
1607 *
1608 * - sign gives the sign of the input: 1 for negative, 0 for positive
1609 *
1610 * - the first and last significant digits are nonzero
1611 */
1612
1613 /* put first DBL_DIG+1 digits into integer y and z.
1614 *
1615 * - y contains the value represented by the first min(9, nd)
1616 * significant digits
1617 *
1618 * - if nd > 9, z contains the value represented by significant digits
1619 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1620 * gives the value represented by the first min(16, nd) sig. digits.
1621 */
1622
Mark Dickinsonadd28232010-01-21 19:51:08 +00001623 bc.e0 = e1 = e;
Mark Dickinson45b63652010-01-16 18:10:25 +00001624 y = z = 0;
1625 for (i = 0; i < nd; i++) {
1626 if (i < 9)
1627 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1628 else if (i < DBL_DIG+1)
1629 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1630 else
1631 break;
1632 }
1633
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001634 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1635 dval(&rv) = y;
1636 if (k > 9) {
1637 dval(&rv) = tens[k - 9] * dval(&rv) + z;
1638 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001639 if (nd <= DBL_DIG
1640 && Flt_Rounds == 1
1641 ) {
1642 if (!e)
1643 goto ret;
1644 if (e > 0) {
1645 if (e <= Ten_pmax) {
1646 dval(&rv) *= tens[e];
1647 goto ret;
1648 }
1649 i = DBL_DIG - nd;
1650 if (e <= Ten_pmax + i) {
1651 /* A fancier test would sometimes let us do
1652 * this for larger i values.
1653 */
1654 e -= i;
1655 dval(&rv) *= tens[i];
1656 dval(&rv) *= tens[e];
1657 goto ret;
1658 }
1659 }
1660 else if (e >= -Ten_pmax) {
1661 dval(&rv) /= tens[-e];
1662 goto ret;
1663 }
1664 }
1665 e1 += nd - k;
1666
1667 bc.scale = 0;
1668
1669 /* Get starting approximation = rv * 10**e1 */
1670
1671 if (e1 > 0) {
1672 if ((i = e1 & 15))
1673 dval(&rv) *= tens[i];
1674 if (e1 &= ~15) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001675 if (e1 > DBL_MAX_10_EXP)
1676 goto ovfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001677 e1 >>= 4;
1678 for(j = 0; e1 > 1; j++, e1 >>= 1)
1679 if (e1 & 1)
1680 dval(&rv) *= bigtens[j];
1681 /* The last multiplication could overflow. */
1682 word0(&rv) -= P*Exp_msk1;
1683 dval(&rv) *= bigtens[j];
1684 if ((z = word0(&rv) & Exp_mask)
1685 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1686 goto ovfl;
1687 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1688 /* set to largest number */
1689 /* (Can't trust DBL_MAX) */
1690 word0(&rv) = Big0;
1691 word1(&rv) = Big1;
1692 }
1693 else
1694 word0(&rv) += P*Exp_msk1;
1695 }
1696 }
1697 else if (e1 < 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001698 /* The input decimal value lies in [10**e1, 10**(e1+16)).
1699
1700 If e1 <= -512, underflow immediately.
1701 If e1 <= -256, set bc.scale to 2*P.
1702
1703 So for input value < 1e-256, bc.scale is always set;
1704 for input value >= 1e-240, bc.scale is never set.
1705 For input values in [1e-256, 1e-240), bc.scale may or may
1706 not be set. */
1707
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001708 e1 = -e1;
1709 if ((i = e1 & 15))
1710 dval(&rv) /= tens[i];
1711 if (e1 >>= 4) {
1712 if (e1 >= 1 << n_bigtens)
1713 goto undfl;
1714 if (e1 & Scale_Bit)
1715 bc.scale = 2*P;
1716 for(j = 0; e1 > 0; j++, e1 >>= 1)
1717 if (e1 & 1)
1718 dval(&rv) *= tinytens[j];
1719 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1720 >> Exp_shift)) > 0) {
1721 /* scaled rv is denormal; clear j low bits */
1722 if (j >= 32) {
1723 word1(&rv) = 0;
1724 if (j >= 53)
1725 word0(&rv) = (P+2)*Exp_msk1;
1726 else
1727 word0(&rv) &= 0xffffffff << (j-32);
1728 }
1729 else
1730 word1(&rv) &= 0xffffffff << j;
1731 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001732 if (!dval(&rv))
1733 goto undfl;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001734 }
1735 }
1736
1737 /* Now the hard part -- adjusting rv to the correct value.*/
1738
1739 /* Put digits into bd: true value = bd * 10^e */
1740
1741 bc.nd = nd;
Mark Dickinson81612e82010-01-12 23:04:19 +00001742 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001743 /* to silence an erroneous warning about bc.nd0 */
1744 /* possibly not being initialized. */
Mark Dickinson81612e82010-01-12 23:04:19 +00001745 if (nd > STRTOD_DIGLIM) {
1746 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001747 /* minimum number of decimal digits to distinguish double values */
1748 /* in IEEE arithmetic. */
Mark Dickinson45b63652010-01-16 18:10:25 +00001749
1750 /* Truncate input to 18 significant digits, then discard any trailing
1751 zeros on the result by updating nd, nd0, e and y suitably. (There's
1752 no need to update z; it's not reused beyond this point.) */
1753 for (i = 18; i > 0; ) {
1754 /* scan back until we hit a nonzero digit. significant digit 'i'
1755 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001756 --i;
Mark Dickinson45b63652010-01-16 18:10:25 +00001757 if (s0[i < nd0 ? i : i+1] != '0') {
1758 ++i;
1759 break;
1760 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001761 }
1762 e += nd - i;
1763 nd = i;
1764 if (nd0 > nd)
1765 nd0 = nd;
1766 if (nd < 9) { /* must recompute y */
1767 y = 0;
1768 for(i = 0; i < nd0; ++i)
1769 y = 10*y + s0[i] - '0';
Mark Dickinson45b63652010-01-16 18:10:25 +00001770 for(; i < nd; ++i)
1771 y = 10*y + s0[i+1] - '0';
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001772 }
1773 }
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001774 bd0 = s2b(s0, nd0, nd, y);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001775 if (bd0 == NULL)
1776 goto failed_malloc;
1777
Mark Dickinsonadd28232010-01-21 19:51:08 +00001778 /* Notation for the comments below. Write:
1779
1780 - dv for the absolute value of the number represented by the original
1781 decimal input string.
1782
1783 - if we've truncated dv, write tdv for the truncated value.
1784 Otherwise, set tdv == dv.
1785
1786 - srv for the quantity rv/2^bc.scale; so srv is the current binary
1787 approximation to tdv (and dv). It should be exactly representable
1788 in an IEEE 754 double.
1789 */
1790
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001791 for(;;) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001792
1793 /* This is the main correction loop for _Py_dg_strtod.
1794
1795 We've got a decimal value tdv, and a floating-point approximation
1796 srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
1797 close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1798 approximation if not.
1799
1800 To determine whether srv is close enough to tdv, compute integers
1801 bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1802 respectively, and then use integer arithmetic to determine whether
1803 |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1804 */
1805
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001806 bd = Balloc(bd0->k);
1807 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001808 goto failed_malloc;
1809 }
1810 Bcopy(bd, bd0);
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001811 bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001812 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001813 goto failed_malloc;
1814 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001815 /* Record whether lsb of bb is odd, in case we need this
1816 for the round-to-even step later. */
1817 odd = bb->x[0] & 1;
1818
1819 /* tdv = bd * 10**e; srv = bb * 2**bbe */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001820 bs = i2b(1);
1821 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001822 goto failed_malloc;
1823 }
1824
1825 if (e >= 0) {
1826 bb2 = bb5 = 0;
1827 bd2 = bd5 = e;
1828 }
1829 else {
1830 bb2 = bb5 = -e;
1831 bd2 = bd5 = 0;
1832 }
1833 if (bbe >= 0)
1834 bb2 += bbe;
1835 else
1836 bd2 -= bbe;
1837 bs2 = bb2;
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001838 bb2++;
1839 bd2++;
Mark Dickinsonadd28232010-01-21 19:51:08 +00001840
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001841 /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
Mark Dickinsone383e822012-04-29 15:31:56 +01001842 and bs == 1, so:
Mark Dickinsonadd28232010-01-21 19:51:08 +00001843
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001844 tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1845 srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
Mark Dickinsone383e822012-04-29 15:31:56 +01001846 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001847
Mark Dickinsone383e822012-04-29 15:31:56 +01001848 It follows that:
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001849
1850 M * tdv = bd * 2**bd2 * 5**bd5
1851 M * srv = bb * 2**bb2 * 5**bb5
1852 M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1853
Mark Dickinsone383e822012-04-29 15:31:56 +01001854 for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1855 this fact is not needed below.)
Mark Dickinsonadd28232010-01-21 19:51:08 +00001856 */
1857
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00001858 /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001859 i = bb2 < bd2 ? bb2 : bd2;
1860 if (i > bs2)
1861 i = bs2;
1862 if (i > 0) {
1863 bb2 -= i;
1864 bd2 -= i;
1865 bs2 -= i;
1866 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001867
1868 /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001869 if (bb5 > 0) {
1870 bs = pow5mult(bs, bb5);
1871 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001872 goto failed_malloc;
1873 }
Victor Stinner9776b062019-03-13 17:55:01 +01001874 Bigint *bb1 = mult(bs, bb);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001875 Bfree(bb);
1876 bb = bb1;
1877 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001878 goto failed_malloc;
1879 }
1880 }
1881 if (bb2 > 0) {
1882 bb = lshift(bb, bb2);
1883 if (bb == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001884 goto failed_malloc;
1885 }
1886 }
1887 if (bd5 > 0) {
1888 bd = pow5mult(bd, bd5);
1889 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001890 goto failed_malloc;
1891 }
1892 }
1893 if (bd2 > 0) {
1894 bd = lshift(bd, bd2);
1895 if (bd == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001896 goto failed_malloc;
1897 }
1898 }
1899 if (bs2 > 0) {
1900 bs = lshift(bs, bs2);
1901 if (bs == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001902 goto failed_malloc;
1903 }
1904 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001905
1906 /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
1907 respectively. Compute the difference |tdv - srv|, and compare
1908 with 0.5 ulp(srv). */
1909
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001910 delta = diff(bb, bd);
1911 if (delta == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001912 goto failed_malloc;
1913 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00001914 dsign = delta->sign;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001915 delta->sign = 0;
1916 i = cmp(delta, bs);
1917 if (bc.nd > nd && i <= 0) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00001918 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001919 break; /* Must use bigcomp(). */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001920
1921 /* Here rv overestimates the truncated decimal value by at most
1922 0.5 ulp(rv). Hence rv either overestimates the true decimal
1923 value by <= 0.5 ulp(rv), or underestimates it by some small
1924 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1925 the true decimal value, so it's possible to exit.
1926
1927 Exception: if scaled rv is a normal exact power of 2, but not
1928 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1929 next double, so the correctly rounded result is either rv - 0.5
1930 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1931
1932 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1933 /* rv can't be 0, since it's an overestimate for some
1934 nonzero value. So rv is a normal power of 2. */
1935 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1936 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1937 rv / 2^bc.scale >= 2^-1021. */
1938 if (j - bc.scale >= 2) {
1939 dval(&rv) -= 0.5 * sulp(&rv, &bc);
Mark Dickinsonadd28232010-01-21 19:51:08 +00001940 break; /* Use bigcomp. */
Mark Dickinson853c3bb2010-01-14 15:37:49 +00001941 }
1942 }
1943
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001944 {
1945 bc.nd = nd;
1946 i = -1; /* Discarded digits make delta smaller. */
1947 }
1948 }
1949
1950 if (i < 0) {
1951 /* Error is less than half an ulp -- check for
1952 * special case of mantissa a power of two.
1953 */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001954 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001955 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
1956 ) {
1957 break;
1958 }
1959 if (!delta->x[0] && delta->wds <= 1) {
1960 /* exact result */
1961 break;
1962 }
1963 delta = lshift(delta,Log2P);
1964 if (delta == NULL) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001965 goto failed_malloc;
1966 }
1967 if (cmp(delta, bs) > 0)
1968 goto drop_down;
1969 break;
1970 }
1971 if (i == 0) {
1972 /* exactly half-way between */
Mark Dickinsonadd28232010-01-21 19:51:08 +00001973 if (dsign) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001974 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1975 && word1(&rv) == (
1976 (bc.scale &&
1977 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
1978 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1979 0xffffffff)) {
1980 /*boundary case -- increment exponent*/
1981 word0(&rv) = (word0(&rv) & Exp_mask)
1982 + Exp_msk1
1983 ;
1984 word1(&rv) = 0;
Brett Cannonb94767f2011-02-22 20:15:44 +00001985 /* dsign = 0; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00001986 break;
1987 }
1988 }
1989 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1990 drop_down:
1991 /* boundary case -- decrement exponent */
1992 if (bc.scale) {
1993 L = word0(&rv) & Exp_mask;
1994 if (L <= (2*P+1)*Exp_msk1) {
1995 if (L > (P+2)*Exp_msk1)
1996 /* round even ==> */
1997 /* accept rv */
1998 break;
1999 /* rv = smallest denormal */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002000 if (bc.nd > nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002001 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002002 goto undfl;
2003 }
2004 }
2005 L = (word0(&rv) & Exp_mask) - Exp_msk1;
2006 word0(&rv) = L | Bndry_mask1;
2007 word1(&rv) = 0xffffffff;
2008 break;
2009 }
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002010 if (!odd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002011 break;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002012 if (dsign)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002013 dval(&rv) += sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002014 else {
Mark Dickinsonf41d29a2010-01-24 10:16:29 +00002015 dval(&rv) -= sulp(&rv, &bc);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002016 if (!dval(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002017 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002018 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002019 goto undfl;
2020 }
2021 }
Brett Cannonb94767f2011-02-22 20:15:44 +00002022 /* dsign = 1 - dsign; */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002023 break;
2024 }
2025 if ((aadj = ratio(delta, bs)) <= 2.) {
Mark Dickinsonadd28232010-01-21 19:51:08 +00002026 if (dsign)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002027 aadj = aadj1 = 1.;
2028 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
2029 if (word1(&rv) == Tiny1 && !word0(&rv)) {
Mark Dickinson81612e82010-01-12 23:04:19 +00002030 if (bc.nd >nd)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002031 break;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002032 goto undfl;
2033 }
2034 aadj = 1.;
2035 aadj1 = -1.;
2036 }
2037 else {
2038 /* special case -- power of FLT_RADIX to be */
2039 /* rounded down... */
2040
2041 if (aadj < 2./FLT_RADIX)
2042 aadj = 1./FLT_RADIX;
2043 else
2044 aadj *= 0.5;
2045 aadj1 = -aadj;
2046 }
2047 }
2048 else {
2049 aadj *= 0.5;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002050 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002051 if (Flt_Rounds == 0)
2052 aadj1 += 0.5;
2053 }
2054 y = word0(&rv) & Exp_mask;
2055
2056 /* Check for overflow */
2057
2058 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2059 dval(&rv0) = dval(&rv);
2060 word0(&rv) -= P*Exp_msk1;
2061 adj.d = aadj1 * ulp(&rv);
2062 dval(&rv) += adj.d;
2063 if ((word0(&rv) & Exp_mask) >=
2064 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002065 if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002066 goto ovfl;
Mark Dickinsonc4f18682010-01-17 14:39:12 +00002067 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002068 word0(&rv) = Big0;
2069 word1(&rv) = Big1;
2070 goto cont;
2071 }
2072 else
2073 word0(&rv) += P*Exp_msk1;
2074 }
2075 else {
2076 if (bc.scale && y <= 2*P*Exp_msk1) {
2077 if (aadj <= 0x7fffffff) {
2078 if ((z = (ULong)aadj) <= 0)
2079 z = 1;
2080 aadj = z;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002081 aadj1 = dsign ? aadj : -aadj;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002082 }
2083 dval(&aadj2) = aadj1;
2084 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2085 aadj1 = dval(&aadj2);
2086 }
2087 adj.d = aadj1 * ulp(&rv);
2088 dval(&rv) += adj.d;
2089 }
2090 z = word0(&rv) & Exp_mask;
2091 if (bc.nd == nd) {
2092 if (!bc.scale)
2093 if (y == z) {
2094 /* Can we stop now? */
2095 L = (Long)aadj;
2096 aadj -= L;
2097 /* The tolerances below are conservative. */
Mark Dickinsonadd28232010-01-21 19:51:08 +00002098 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002099 if (aadj < .4999999 || aadj > .5000001)
2100 break;
2101 }
2102 else if (aadj < .4999999/FLT_RADIX)
2103 break;
2104 }
2105 }
2106 cont:
Victor Stinner9776b062019-03-13 17:55:01 +01002107 Bfree(bb); bb = NULL;
2108 Bfree(bd); bd = NULL;
2109 Bfree(bs); bs = NULL;
2110 Bfree(delta); delta = NULL;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002111 }
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002112 if (bc.nd > nd) {
2113 error = bigcomp(&rv, s0, &bc);
2114 if (error)
2115 goto failed_malloc;
2116 }
2117
2118 if (bc.scale) {
2119 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2120 word1(&rv0) = 0;
2121 dval(&rv) *= dval(&rv0);
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002122 }
Mark Dickinsonadd28232010-01-21 19:51:08 +00002123
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002124 ret:
Victor Stinner9776b062019-03-13 17:55:01 +01002125 result = sign ? -dval(&rv) : dval(&rv);
2126 goto done;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002127
Mark Dickinsonadd28232010-01-21 19:51:08 +00002128 parse_error:
Victor Stinner9776b062019-03-13 17:55:01 +01002129 result = 0.0;
2130 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002131
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002132 failed_malloc:
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002133 errno = ENOMEM;
Victor Stinner9776b062019-03-13 17:55:01 +01002134 result = -1.0;
2135 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002136
2137 undfl:
Victor Stinner9776b062019-03-13 17:55:01 +01002138 result = sign ? -0.0 : 0.0;
2139 goto done;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002140
2141 ovfl:
2142 errno = ERANGE;
2143 /* Can't trust HUGE_VAL */
2144 word0(&rv) = Exp_mask;
2145 word1(&rv) = 0;
Victor Stinner9776b062019-03-13 17:55:01 +01002146 result = sign ? -dval(&rv) : dval(&rv);
2147 goto done;
2148
2149 done:
2150 Bfree(bb);
2151 Bfree(bd);
2152 Bfree(bs);
2153 Bfree(bd0);
2154 Bfree(delta);
2155 return result;
Mark Dickinsonadd28232010-01-21 19:51:08 +00002156
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002157}
2158
2159static char *
2160rv_alloc(int i)
2161{
2162 int j, k, *r;
2163
2164 j = sizeof(ULong);
2165 for(k = 0;
2166 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2167 j <<= 1)
2168 k++;
2169 r = (int*)Balloc(k);
2170 if (r == NULL)
2171 return NULL;
2172 *r = k;
2173 return (char *)(r+1);
2174}
2175
2176static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002177nrv_alloc(const char *s, char **rve, int n)
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002178{
2179 char *rv, *t;
2180
2181 rv = rv_alloc(n);
2182 if (rv == NULL)
2183 return NULL;
2184 t = rv;
2185 while((*t = *s++)) t++;
2186 if (rve)
2187 *rve = t;
2188 return rv;
2189}
2190
2191/* freedtoa(s) must be used to free values s returned by dtoa
2192 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2193 * but for consistency with earlier versions of dtoa, it is optional
2194 * when MULTIPLE_THREADS is not defined.
2195 */
2196
2197void
2198_Py_dg_freedtoa(char *s)
2199{
2200 Bigint *b = (Bigint *)((int *)s - 1);
2201 b->maxwds = 1 << (b->k = *(int*)b);
2202 Bfree(b);
2203}
2204
2205/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2206 *
2207 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2208 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2209 *
2210 * Modifications:
2211 * 1. Rather than iterating, we use a simple numeric overestimate
2212 * to determine k = floor(log10(d)). We scale relevant
2213 * quantities using O(log2(k)) rather than O(k) multiplications.
2214 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2215 * try to generate digits strictly left to right. Instead, we
2216 * compute with fewer bits and propagate the carry if necessary
2217 * when rounding the final digit up. This is often faster.
2218 * 3. Under the assumption that input will be rounded nearest,
2219 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2220 * That is, we allow equality in stopping tests when the
2221 * round-nearest rule will give the same floating-point value
2222 * as would satisfaction of the stopping test with strict
2223 * inequality.
2224 * 4. We remove common factors of powers of 2 from relevant
2225 * quantities.
2226 * 5. When converting floating-point integers less than 1e16,
2227 * we use floating-point arithmetic rather than resorting
2228 * to multiple-precision integers.
2229 * 6. When asked to produce fewer than 15 digits, we first try
2230 * to get by with floating-point arithmetic; we resort to
2231 * multiple-precision integer arithmetic only if we cannot
2232 * guarantee that the floating-point calculation has given
2233 * the correctly rounded result. For k requested digits and
2234 * "uniformly" distributed input, the probability is
2235 * something like 10^(k-15) that we must resort to the Long
2236 * calculation.
2237 */
2238
2239/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2240 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2241 call to _Py_dg_freedtoa. */
2242
2243char *
2244_Py_dg_dtoa(double dd, int mode, int ndigits,
2245 int *decpt, int *sign, char **rve)
2246{
2247 /* Arguments ndigits, decpt, sign are similar to those
2248 of ecvt and fcvt; trailing zeros are suppressed from
2249 the returned string. If not null, *rve is set to point
2250 to the end of the return value. If d is +-Infinity or NaN,
2251 then *decpt is set to 9999.
2252
2253 mode:
2254 0 ==> shortest string that yields d when read in
2255 and rounded to nearest.
2256 1 ==> like 0, but with Steele & White stopping rule;
2257 e.g. with IEEE P754 arithmetic , mode 0 gives
2258 1e23 whereas mode 1 gives 9.999999999999999e22.
2259 2 ==> max(1,ndigits) significant digits. This gives a
2260 return value similar to that of ecvt, except
2261 that trailing zeros are suppressed.
2262 3 ==> through ndigits past the decimal point. This
2263 gives a return value similar to that from fcvt,
2264 except that trailing zeros are suppressed, and
2265 ndigits can be negative.
2266 4,5 ==> similar to 2 and 3, respectively, but (in
2267 round-nearest mode) with the tests of mode 0 to
2268 possibly return a shorter string that rounds to d.
2269 With IEEE arithmetic and compilation with
2270 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2271 as modes 2 and 3 when FLT_ROUNDS != 1.
2272 6-9 ==> Debugging modes similar to mode - 4: don't try
2273 fast floating-point estimate (if applicable).
2274
2275 Values of mode other than 0-9 are treated as mode 0.
2276
2277 Sufficient space is allocated to the return value
2278 to hold the suppressed trailing zeros.
2279 */
2280
2281 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2282 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2283 spec_case, try_quick;
2284 Long L;
2285 int denorm;
2286 ULong x;
2287 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2288 U d2, eps, u;
2289 double ds;
2290 char *s, *s0;
2291
2292 /* set pointers to NULL, to silence gcc compiler warnings and make
2293 cleanup easier on error */
Mark Dickinsond3697262010-05-13 11:52:22 +00002294 mlo = mhi = S = 0;
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002295 s0 = 0;
2296
2297 u.d = dd;
2298 if (word0(&u) & Sign_bit) {
2299 /* set sign for everything, including 0's and NaNs */
2300 *sign = 1;
2301 word0(&u) &= ~Sign_bit; /* clear sign bit */
2302 }
2303 else
2304 *sign = 0;
2305
2306 /* quick return for Infinities, NaNs and zeros */
2307 if ((word0(&u) & Exp_mask) == Exp_mask)
2308 {
2309 /* Infinity or NaN */
2310 *decpt = 9999;
2311 if (!word1(&u) && !(word0(&u) & 0xfffff))
2312 return nrv_alloc("Infinity", rve, 8);
2313 return nrv_alloc("NaN", rve, 3);
2314 }
2315 if (!dval(&u)) {
2316 *decpt = 1;
2317 return nrv_alloc("0", rve, 1);
2318 }
2319
2320 /* compute k = floor(log10(d)). The computation may leave k
2321 one too large, but should never leave k too small. */
2322 b = d2b(&u, &be, &bbits);
2323 if (b == NULL)
2324 goto failed_malloc;
2325 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2326 dval(&d2) = dval(&u);
2327 word0(&d2) &= Frac_mask1;
2328 word0(&d2) |= Exp_11;
2329
2330 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2331 * log10(x) = log(x) / log(10)
2332 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2333 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2334 *
2335 * This suggests computing an approximation k to log10(d) by
2336 *
2337 * k = (i - Bias)*0.301029995663981
2338 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2339 *
2340 * We want k to be too large rather than too small.
2341 * The error in the first-order Taylor series approximation
2342 * is in our favor, so we just round up the constant enough
2343 * to compensate for any error in the multiplication of
2344 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2345 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2346 * adding 1e-13 to the constant term more than suffices.
2347 * Hence we adjust the constant term to 0.1760912590558.
2348 * (We could get a more accurate k by invoking log10,
2349 * but this is probably not worthwhile.)
2350 */
2351
2352 i -= Bias;
2353 denorm = 0;
2354 }
2355 else {
2356 /* d is denormalized */
2357
2358 i = bbits + be + (Bias + (P-1) - 1);
2359 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2360 : word1(&u) << (32 - i);
2361 dval(&d2) = x;
2362 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2363 i -= (Bias + (P-1) - 1) + 1;
2364 denorm = 1;
2365 }
2366 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2367 i*0.301029995663981;
2368 k = (int)ds;
2369 if (ds < 0. && ds != k)
2370 k--; /* want k = floor(ds) */
2371 k_check = 1;
2372 if (k >= 0 && k <= Ten_pmax) {
2373 if (dval(&u) < tens[k])
2374 k--;
2375 k_check = 0;
2376 }
2377 j = bbits - i - 1;
2378 if (j >= 0) {
2379 b2 = 0;
2380 s2 = j;
2381 }
2382 else {
2383 b2 = -j;
2384 s2 = 0;
2385 }
2386 if (k >= 0) {
2387 b5 = 0;
2388 s5 = k;
2389 s2 += k;
2390 }
2391 else {
2392 b2 -= k;
2393 b5 = -k;
2394 s5 = 0;
2395 }
2396 if (mode < 0 || mode > 9)
2397 mode = 0;
2398
2399 try_quick = 1;
2400
2401 if (mode > 5) {
2402 mode -= 4;
2403 try_quick = 0;
2404 }
2405 leftright = 1;
2406 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2407 /* silence erroneous "gcc -Wall" warning. */
2408 switch(mode) {
2409 case 0:
2410 case 1:
2411 i = 18;
2412 ndigits = 0;
2413 break;
2414 case 2:
2415 leftright = 0;
Stefan Krahf432a322017-08-21 13:09:59 +02002416 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002417 case 4:
2418 if (ndigits <= 0)
2419 ndigits = 1;
2420 ilim = ilim1 = i = ndigits;
2421 break;
2422 case 3:
2423 leftright = 0;
Stefan Krahf432a322017-08-21 13:09:59 +02002424 /* fall through */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002425 case 5:
2426 i = ndigits + k + 1;
2427 ilim = i;
2428 ilim1 = i - 1;
2429 if (i <= 0)
2430 i = 1;
2431 }
2432 s0 = rv_alloc(i);
2433 if (s0 == NULL)
2434 goto failed_malloc;
2435 s = s0;
2436
2437
2438 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2439
2440 /* Try to get by with floating-point arithmetic. */
2441
2442 i = 0;
2443 dval(&d2) = dval(&u);
2444 k0 = k;
2445 ilim0 = ilim;
2446 ieps = 2; /* conservative */
2447 if (k > 0) {
2448 ds = tens[k&0xf];
2449 j = k >> 4;
2450 if (j & Bletch) {
2451 /* prevent overflows */
2452 j &= Bletch - 1;
2453 dval(&u) /= bigtens[n_bigtens-1];
2454 ieps++;
2455 }
2456 for(; j; j >>= 1, i++)
2457 if (j & 1) {
2458 ieps++;
2459 ds *= bigtens[i];
2460 }
2461 dval(&u) /= ds;
2462 }
2463 else if ((j1 = -k)) {
2464 dval(&u) *= tens[j1 & 0xf];
2465 for(j = j1 >> 4; j; j >>= 1, i++)
2466 if (j & 1) {
2467 ieps++;
2468 dval(&u) *= bigtens[i];
2469 }
2470 }
2471 if (k_check && dval(&u) < 1. && ilim > 0) {
2472 if (ilim1 <= 0)
2473 goto fast_failed;
2474 ilim = ilim1;
2475 k--;
2476 dval(&u) *= 10.;
2477 ieps++;
2478 }
2479 dval(&eps) = ieps*dval(&u) + 7.;
2480 word0(&eps) -= (P-1)*Exp_msk1;
2481 if (ilim == 0) {
2482 S = mhi = 0;
2483 dval(&u) -= 5.;
2484 if (dval(&u) > dval(&eps))
2485 goto one_digit;
2486 if (dval(&u) < -dval(&eps))
2487 goto no_digits;
2488 goto fast_failed;
2489 }
2490 if (leftright) {
2491 /* Use Steele & White method of only
2492 * generating digits needed.
2493 */
2494 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2495 for(i = 0;;) {
2496 L = (Long)dval(&u);
2497 dval(&u) -= L;
2498 *s++ = '0' + (int)L;
2499 if (dval(&u) < dval(&eps))
2500 goto ret1;
2501 if (1. - dval(&u) < dval(&eps))
2502 goto bump_up;
2503 if (++i >= ilim)
2504 break;
2505 dval(&eps) *= 10.;
2506 dval(&u) *= 10.;
2507 }
2508 }
2509 else {
2510 /* Generate ilim digits, then fix them up. */
2511 dval(&eps) *= tens[ilim-1];
2512 for(i = 1;; i++, dval(&u) *= 10.) {
2513 L = (Long)(dval(&u));
2514 if (!(dval(&u) -= L))
2515 ilim = i;
2516 *s++ = '0' + (int)L;
2517 if (i == ilim) {
2518 if (dval(&u) > 0.5 + dval(&eps))
2519 goto bump_up;
2520 else if (dval(&u) < 0.5 - dval(&eps)) {
2521 while(*--s == '0');
2522 s++;
2523 goto ret1;
2524 }
2525 break;
2526 }
2527 }
2528 }
2529 fast_failed:
2530 s = s0;
2531 dval(&u) = dval(&d2);
2532 k = k0;
2533 ilim = ilim0;
2534 }
2535
2536 /* Do we have a "small" integer? */
2537
2538 if (be >= 0 && k <= Int_max) {
2539 /* Yes. */
2540 ds = tens[k];
2541 if (ndigits < 0 && ilim <= 0) {
2542 S = mhi = 0;
2543 if (ilim < 0 || dval(&u) <= 5*ds)
2544 goto no_digits;
2545 goto one_digit;
2546 }
2547 for(i = 1;; i++, dval(&u) *= 10.) {
2548 L = (Long)(dval(&u) / ds);
2549 dval(&u) -= L*ds;
2550 *s++ = '0' + (int)L;
2551 if (!dval(&u)) {
2552 break;
2553 }
2554 if (i == ilim) {
2555 dval(&u) += dval(&u);
2556 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2557 bump_up:
2558 while(*--s == '9')
2559 if (s == s0) {
2560 k++;
2561 *s = '0';
2562 break;
2563 }
2564 ++*s++;
2565 }
2566 break;
2567 }
2568 }
2569 goto ret1;
2570 }
2571
2572 m2 = b2;
2573 m5 = b5;
2574 if (leftright) {
2575 i =
2576 denorm ? be + (Bias + (P-1) - 1 + 1) :
2577 1 + P - bbits;
2578 b2 += i;
2579 s2 += i;
2580 mhi = i2b(1);
2581 if (mhi == NULL)
2582 goto failed_malloc;
2583 }
2584 if (m2 > 0 && s2 > 0) {
2585 i = m2 < s2 ? m2 : s2;
2586 b2 -= i;
2587 m2 -= i;
2588 s2 -= i;
2589 }
2590 if (b5 > 0) {
2591 if (leftright) {
2592 if (m5 > 0) {
2593 mhi = pow5mult(mhi, m5);
2594 if (mhi == NULL)
2595 goto failed_malloc;
2596 b1 = mult(mhi, b);
2597 Bfree(b);
2598 b = b1;
2599 if (b == NULL)
2600 goto failed_malloc;
2601 }
2602 if ((j = b5 - m5)) {
2603 b = pow5mult(b, j);
2604 if (b == NULL)
2605 goto failed_malloc;
2606 }
2607 }
2608 else {
2609 b = pow5mult(b, b5);
2610 if (b == NULL)
2611 goto failed_malloc;
2612 }
2613 }
2614 S = i2b(1);
2615 if (S == NULL)
2616 goto failed_malloc;
2617 if (s5 > 0) {
2618 S = pow5mult(S, s5);
2619 if (S == NULL)
2620 goto failed_malloc;
2621 }
2622
2623 /* Check for special case that d is a normalized power of 2. */
2624
2625 spec_case = 0;
2626 if ((mode < 2 || leftright)
2627 ) {
2628 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2629 && word0(&u) & (Exp_mask & ~Exp_msk1)
2630 ) {
2631 /* The special case */
2632 b2 += Log2P;
2633 s2 += Log2P;
2634 spec_case = 1;
2635 }
2636 }
2637
2638 /* Arrange for convenient computation of quotients:
2639 * shift left if necessary so divisor has 4 leading 0 bits.
2640 *
2641 * Perhaps we should just compute leading 28 bits of S once
2642 * and for all and pass them and a shift to quorem, so it
2643 * can do shifts and ors to compute the numerator for q.
2644 */
Mark Dickinsonb08a53a2009-04-16 19:52:09 +00002645#define iInc 28
2646 i = dshift(S, s2);
2647 b2 += i;
2648 m2 += i;
2649 s2 += i;
2650 if (b2 > 0) {
2651 b = lshift(b, b2);
2652 if (b == NULL)
2653 goto failed_malloc;
2654 }
2655 if (s2 > 0) {
2656 S = lshift(S, s2);
2657 if (S == NULL)
2658 goto failed_malloc;
2659 }
2660 if (k_check) {
2661 if (cmp(b,S) < 0) {
2662 k--;
2663 b = multadd(b, 10, 0); /* we botched the k estimate */
2664 if (b == NULL)
2665 goto failed_malloc;
2666 if (leftright) {
2667 mhi = multadd(mhi, 10, 0);
2668 if (mhi == NULL)
2669 goto failed_malloc;
2670 }
2671 ilim = ilim1;
2672 }
2673 }
2674 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2675 if (ilim < 0) {
2676 /* no digits, fcvt style */
2677 no_digits:
2678 k = -1 - ndigits;
2679 goto ret;
2680 }
2681 else {
2682 S = multadd(S, 5, 0);
2683 if (S == NULL)
2684 goto failed_malloc;
2685 if (cmp(b, S) <= 0)
2686 goto no_digits;
2687 }
2688 one_digit:
2689 *s++ = '1';
2690 k++;
2691 goto ret;
2692 }
2693 if (leftright) {
2694 if (m2 > 0) {
2695 mhi = lshift(mhi, m2);
2696 if (mhi == NULL)
2697 goto failed_malloc;
2698 }
2699
2700 /* Compute mlo -- check for special case
2701 * that d is a normalized power of 2.
2702 */
2703
2704 mlo = mhi;
2705 if (spec_case) {
2706 mhi = Balloc(mhi->k);
2707 if (mhi == NULL)
2708 goto failed_malloc;
2709 Bcopy(mhi, mlo);
2710 mhi = lshift(mhi, Log2P);
2711 if (mhi == NULL)
2712 goto failed_malloc;
2713 }
2714
2715 for(i = 1;;i++) {
2716 dig = quorem(b,S) + '0';
2717 /* Do we yet have the shortest decimal string
2718 * that will round to d?
2719 */
2720 j = cmp(b, mlo);
2721 delta = diff(S, mhi);
2722 if (delta == NULL)
2723 goto failed_malloc;
2724 j1 = delta->sign ? 1 : cmp(b, delta);
2725 Bfree(delta);
2726 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2727 ) {
2728 if (dig == '9')
2729 goto round_9_up;
2730 if (j > 0)
2731 dig++;
2732 *s++ = dig;
2733 goto ret;
2734 }
2735 if (j < 0 || (j == 0 && mode != 1
2736 && !(word1(&u) & 1)
2737 )) {
2738 if (!b->x[0] && b->wds <= 1) {
2739 goto accept_dig;
2740 }
2741 if (j1 > 0) {
2742 b = lshift(b, 1);
2743 if (b == NULL)
2744 goto failed_malloc;
2745 j1 = cmp(b, S);
2746 if ((j1 > 0 || (j1 == 0 && dig & 1))
2747 && dig++ == '9')
2748 goto round_9_up;
2749 }
2750 accept_dig:
2751 *s++ = dig;
2752 goto ret;
2753 }
2754 if (j1 > 0) {
2755 if (dig == '9') { /* possible if i == 1 */
2756 round_9_up:
2757 *s++ = '9';
2758 goto roundoff;
2759 }
2760 *s++ = dig + 1;
2761 goto ret;
2762 }
2763 *s++ = dig;
2764 if (i == ilim)
2765 break;
2766 b = multadd(b, 10, 0);
2767 if (b == NULL)
2768 goto failed_malloc;
2769 if (mlo == mhi) {
2770 mlo = mhi = multadd(mhi, 10, 0);
2771 if (mlo == NULL)
2772 goto failed_malloc;
2773 }
2774 else {
2775 mlo = multadd(mlo, 10, 0);
2776 if (mlo == NULL)
2777 goto failed_malloc;
2778 mhi = multadd(mhi, 10, 0);
2779 if (mhi == NULL)
2780 goto failed_malloc;
2781 }
2782 }
2783 }
2784 else
2785 for(i = 1;; i++) {
2786 *s++ = dig = quorem(b,S) + '0';
2787 if (!b->x[0] && b->wds <= 1) {
2788 goto ret;
2789 }
2790 if (i >= ilim)
2791 break;
2792 b = multadd(b, 10, 0);
2793 if (b == NULL)
2794 goto failed_malloc;
2795 }
2796
2797 /* Round off last digit */
2798
2799 b = lshift(b, 1);
2800 if (b == NULL)
2801 goto failed_malloc;
2802 j = cmp(b, S);
2803 if (j > 0 || (j == 0 && dig & 1)) {
2804 roundoff:
2805 while(*--s == '9')
2806 if (s == s0) {
2807 k++;
2808 *s++ = '1';
2809 goto ret;
2810 }
2811 ++*s++;
2812 }
2813 else {
2814 while(*--s == '0');
2815 s++;
2816 }
2817 ret:
2818 Bfree(S);
2819 if (mhi) {
2820 if (mlo && mlo != mhi)
2821 Bfree(mlo);
2822 Bfree(mhi);
2823 }
2824 ret1:
2825 Bfree(b);
2826 *s = 0;
2827 *decpt = k + 1;
2828 if (rve)
2829 *rve = s;
2830 return s0;
2831 failed_malloc:
2832 if (S)
2833 Bfree(S);
2834 if (mlo && mlo != mhi)
2835 Bfree(mlo);
2836 if (mhi)
2837 Bfree(mhi);
2838 if (b)
2839 Bfree(b);
2840 if (s0)
2841 _Py_dg_freedtoa(s0);
2842 return NULL;
2843}
2844#ifdef __cplusplus
2845}
2846#endif
2847
2848#endif /* PY_NO_SHORT_FLOAT_REPR */