Coverage Report

Created: 2026-06-09 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/dtoa.c
Line
Count
Source
1
/****************************************************************
2
 *
3
 * The author of this software is David M. Gay.
4
 *
5
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose without fee is hereby granted, provided that this entire notice
9
 * is included in all copies of any software which is or includes a copy
10
 * or modification of this software and in all copies of the supporting
11
 * documentation for such software.
12
 *
13
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17
 *
18
 ***************************************************************/
19
20
/****************************************************************
21
 * This is dtoa.c by David M. Gay, downloaded from
22
 * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
23
 * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
24
 *
25
 * Please remember to check http://www.netlib.org/fp regularly (and especially
26
 * before any Python release) for bugfixes and updates.
27
 *
28
 * The major modifications from Gay's original code are as follows:
29
 *
30
 *  0. The original code has been specialized to Python's needs by removing
31
 *     many of the #ifdef'd sections.  In particular, code to support VAX and
32
 *     IBM floating-point formats, hex NaNs, hex floats, locale-aware
33
 *     treatment of the decimal point, and setting of the inexact flag have
34
 *     been removed.
35
 *
36
 *  1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
37
 *
38
 *  2. The public functions strtod, dtoa and freedtoa all now have
39
 *     a _Py_dg_ prefix.
40
 *
41
 *  3. Instead of assuming that PyMem_Malloc always succeeds, we thread
42
 *     PyMem_Malloc failures through the code.  The functions
43
 *
44
 *       Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
45
 *
46
 *     of return type *Bigint all return NULL to indicate a malloc failure.
47
 *     Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
48
 *     failure.  bigcomp now has return type int (it used to be void) and
49
 *     returns -1 on failure and 0 otherwise.  _Py_dg_dtoa returns NULL
50
 *     on failure.  _Py_dg_strtod indicates failure due to malloc failure
51
 *     by returning -1.0, setting errno=ENOMEM and *se to s00.
52
 *
53
 *  4. The static variable dtoa_result has been removed.  Callers of
54
 *     _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free
55
 *     the memory allocated by _Py_dg_dtoa.
56
 *
57
 *  5. The code has been reformatted to better fit with Python's
58
 *     C style guide (PEP 7).
59
 *
60
 *  6. A bug in the memory allocation has been fixed: to avoid FREEing memory
61
 *     that hasn't been MALLOC'ed, private_mem should only be used when k <=
62
 *     Kmax.
63
 *
64
 *  7. _Py_dg_strtod has been modified so that it doesn't accept strings with
65
 *     leading whitespace.
66
 *
67
 *  8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
68
 *     fixed. (bugs.python.org/issue40780)
69
 *
70
 ***************************************************************/
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
 * as detailed at (https://devguide.python.org/triage/issue-tracker/). */
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"
121
#include "pycore_dtoa.h"          // _PY_SHORT_FLOAT_REPR
122
#include "pycore_interp_structs.h"// struct Bigint
123
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
124
#include <stdlib.h>               // exit()
125
126
127
/* if _PY_SHORT_FLOAT_REPR == 0, then don't even try to compile
128
   the following code */
129
#if _PY_SHORT_FLOAT_REPR == 1
130
131
#include "float.h"
132
133
59
#define MALLOC PyMem_Malloc
134
0
#define FREE PyMem_Free
135
136
/* This code should also work for ARM mixed-endian format on little-endian
137
   machines, where doubles have byte order 45670123 (in increasing address
138
   order, 0 being the least significant byte). */
139
#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
140
#  define IEEE_8087
141
#endif
142
#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
143
#  define IEEE_MC68k
144
#endif
145
#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
146
#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
147
#endif
148
149
/* The code below assumes that the endianness of integers matches the
150
   endianness of the two 32-bit words of a double.  Check this. */
151
#if defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754)
152
#error "doubles and ints have incompatible endianness"
153
#endif
154
155
#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
156
#error "doubles and ints have incompatible endianness"
157
#endif
158
159
160
typedef uint32_t ULong;
161
typedef int32_t Long;
162
typedef uint64_t ULLong;
163
164
#undef DEBUG
165
#ifdef Py_DEBUG
166
#define DEBUG
167
#endif
168
169
/* End Python #define linking */
170
171
#ifdef DEBUG
172
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
173
#endif
174
175
typedef union { double d; ULong L[2]; } U;
176
177
#ifdef IEEE_8087
178
5.70M
#define word0(x) (x)->L[1]
179
3.95M
#define word1(x) (x)->L[0]
180
#else
181
#define word0(x) (x)->L[0]
182
#define word1(x) (x)->L[1]
183
#endif
184
10.2M
#define dval(x) (x)->d
185
186
#ifndef STRTOD_DIGLIM
187
454k
#define STRTOD_DIGLIM 40
188
#endif
189
190
/* maximum permitted exponent value for strtod; exponents larger than
191
   MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP.  MAX_ABS_EXP
192
   should fit into an int. */
193
#ifndef MAX_ABS_EXP
194
1.21M
#define MAX_ABS_EXP 1100000000U
195
#endif
196
/* Bound on length of pieces of input strings in _Py_dg_strtod; specifically,
197
   this is used to bound the total number of digits ignoring leading zeros and
198
   the number of digits that follow the decimal point.  Ideally, MAX_DIGITS
199
   should satisfy MAX_DIGITS + 400 < MAX_ABS_EXP; that ensures that the
200
   exponent clipping in _Py_dg_strtod can't affect the value of the output. */
201
#ifndef MAX_DIGITS
202
3.90M
#define MAX_DIGITS 1000000000U
203
#endif
204
205
/* Guard against trying to use the above values on unusual platforms with ints
206
 * of width less than 32 bits. */
207
#if MAX_ABS_EXP > INT_MAX
208
#error "MAX_ABS_EXP should fit in an int"
209
#endif
210
#if MAX_DIGITS > INT_MAX
211
#error "MAX_DIGITS should fit in an int"
212
#endif
213
214
/* The following definition of Storeinc is appropriate for MIPS processors.
215
 * An alternative that might be better on some machines is
216
 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
217
 */
218
#if defined(IEEE_8087)
219
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b,  \
220
                         ((unsigned short *)a)[0] = (unsigned short)c, a++)
221
#else
222
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b,  \
223
                         ((unsigned short *)a)[1] = (unsigned short)c, a++)
224
#endif
225
226
/* #define P DBL_MANT_DIG */
227
/* Ten_pmax = floor(P*log(2)/log(5)) */
228
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
229
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
230
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
231
232
604k
#define Exp_shift  20
233
102k
#define Exp_shift1 20
234
3.14M
#define Exp_msk1    0x100000
235
#define Exp_msk11   0x100000
236
2.78M
#define Exp_mask  0x7ff00000
237
2.15M
#define P 53
238
#define Nbits 53
239
1.20M
#define Bias 1023
240
#define Emax 1023
241
#define Emin (-1022)
242
1.16M
#define Etiny (-1074)  /* smallest denormal is 2**Etiny */
243
643k
#define Exp_1  0x3ff00000
244
45.9k
#define Exp_11 0x3ff00000
245
1.96M
#define Ebits 11
246
575k
#define Frac_mask  0xfffff
247
47.6k
#define Frac_mask1 0xfffff
248
2.33M
#define Ten_pmax 22
249
85
#define Bletch 0x10
250
186k
#define Bndry_mask  0xfffff
251
8.22k
#define Bndry_mask1 0xfffff
252
78.7k
#define Sign_bit 0x80000000
253
7.87k
#define Log2P 1
254
#define Tiny0 0
255
490k
#define Tiny1 1
256
51.1k
#define Quick_max 14
257
32.3k
#define Int_max 14
258
259
#ifndef Flt_Rounds
260
#ifdef FLT_ROUNDS
261
1.25M
#define Flt_Rounds FLT_ROUNDS
262
#else
263
#define Flt_Rounds 1
264
#endif
265
#endif /*Flt_Rounds*/
266
267
#define Rounding Flt_Rounds
268
269
2.74k
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
270
1.71k
#define Big1 0xffffffff
271
272
/* Bits of the representation of positive infinity. */
273
274
#define POSINF_WORD0 0x7ff00000
275
#define POSINF_WORD1 0
276
277
/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
278
279
typedef struct BCinfo BCinfo;
280
struct
281
BCinfo {
282
    int e0, nd, nd0, scale;
283
};
284
285
35.0M
#define FFFFFFFF 0xffffffffUL
286
287
/* struct Bigint is used to represent arbitrary-precision integers.  These
288
   integers are stored in sign-magnitude format, with the magnitude stored as
289
   an array of base 2**32 digits.  Bigints are always normalized: if x is a
290
   Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
291
292
   The Bigint fields are as follows:
293
294
     - next is a header used by Balloc and Bfree to keep track of lists
295
         of freed Bigints;  it's also used for the linked list of
296
         powers of 5 of the form 5**2**i used by pow5mult.
297
     - k indicates which pool this Bigint was allocated from
298
     - maxwds is the maximum number of words space was allocated for
299
       (usually maxwds == 2**k)
300
     - sign is 1 for negative Bigints, 0 for positive.  The sign is unused
301
       (ignored on inputs, set to 0 on outputs) in almost all operations
302
       involving Bigints: a notable exception is the diff function, which
303
       ignores signs on inputs but sets the sign of the output correctly.
304
     - wds is the actual number of significant words
305
     - x contains the vector of words (digits) for this Bigint, from least
306
       significant (x[0]) to most significant (x[wds-1]).
307
*/
308
309
// struct Bigint is defined in pycore_dtoa.h.
310
typedef struct Bigint Bigint;
311
312
#if !defined(Py_GIL_DISABLED) && !defined(Py_USING_MEMORY_DEBUGGER)
313
314
/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
315
   of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
316
   1 << k.  These pools are maintained as linked lists, with freelist[k]
317
   pointing to the head of the list for pool k.
318
319
   On allocation, if there's no free slot in the appropriate pool, MALLOC is
320
   called to get more memory.  This memory is not returned to the system until
321
   Python quits.  There's also a private memory pool that's allocated from
322
   in preference to using MALLOC.
323
324
   For Bigints with more than (1 << Kmax) digits (which implies at least 1233
325
   decimal digits), memory is directly allocated using MALLOC, and freed using
326
   FREE.
327
328
   XXX: it would be easy to bypass this memory-management system and
329
   translate each call to Balloc into a call to PyMem_Malloc, and each
330
   Bfree to PyMem_Free.  Investigate whether this has any significant
331
   performance on impact. */
332
333
22.8M
#define freelist interp->dtoa.freelist
334
440
#define private_mem interp->dtoa.preallocated
335
1.20k
#define pmem_next interp->dtoa.preallocated_next
336
337
/* Allocate space for a Bigint with up to 1<<k digits */
338
339
static Bigint *
340
Balloc(int k)
341
5.71M
{
342
5.71M
    int x;
343
5.71M
    Bigint *rv;
344
5.71M
    unsigned int len;
345
5.71M
    PyInterpreterState *interp = _PyInterpreterState_GET();
346
347
5.71M
    if (k <= Bigint_Kmax && (rv = freelist[k]))
348
5.71M
        freelist[k] = rv->next;
349
440
    else {
350
440
        x = 1 << k;
351
440
        len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
352
440
            /sizeof(double);
353
440
        if (k <= Bigint_Kmax &&
354
440
            pmem_next - private_mem + len <= (Py_ssize_t)Bigint_PREALLOC_SIZE
355
440
        ) {
356
381
            rv = (Bigint*)pmem_next;
357
381
            pmem_next += len;
358
381
        }
359
59
        else {
360
59
            rv = (Bigint*)MALLOC(len*sizeof(double));
361
59
            if (rv == NULL)
362
0
                return NULL;
363
59
        }
364
440
        rv->k = k;
365
440
        rv->maxwds = x;
366
440
    }
367
5.71M
    rv->sign = rv->wds = 0;
368
5.71M
    return rv;
369
5.71M
}
370
371
/* Free a Bigint allocated with Balloc */
372
373
static void
374
Bfree(Bigint *v)
375
10.2M
{
376
10.2M
    if (v) {
377
5.71M
        if (v->k > Bigint_Kmax)
378
0
            FREE((void*)v);
379
5.71M
        else {
380
5.71M
            PyInterpreterState *interp = _PyInterpreterState_GET();
381
5.71M
            v->next = freelist[v->k];
382
5.71M
            freelist[v->k] = v;
383
5.71M
        }
384
5.71M
    }
385
10.2M
}
386
387
#undef pmem_next
388
#undef private_mem
389
#undef freelist
390
391
#else
392
393
/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
394
   PyMem_Free directly in place of the custom memory allocation scheme above.
395
   These are provided for the benefit of memory debugging tools like
396
   Valgrind. */
397
398
/* Allocate space for a Bigint with up to 1<<k digits */
399
400
static Bigint *
401
Balloc(int k)
402
{
403
    int x;
404
    Bigint *rv;
405
    unsigned int len;
406
407
    x = 1 << k;
408
    len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
409
        /sizeof(double);
410
411
    rv = (Bigint*)MALLOC(len*sizeof(double));
412
    if (rv == NULL)
413
        return NULL;
414
415
    rv->k = k;
416
    rv->maxwds = x;
417
    rv->sign = rv->wds = 0;
418
    return rv;
419
}
420
421
/* Free a Bigint allocated with Balloc */
422
423
static void
424
Bfree(Bigint *v)
425
{
426
    if (v) {
427
        FREE((void*)v);
428
    }
429
}
430
431
#endif /* !defined(Py_GIL_DISABLED) && !defined(Py_USING_MEMORY_DEBUGGER) */
432
433
504k
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign,   \
434
504k
                          y->wds*sizeof(Long) + 2*sizeof(int))
435
436
/* Multiply a Bigint b by m and add a.  Either modifies b in place and returns
437
   a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
438
   On failure, return NULL.  In this case, b will have been already freed. */
439
440
static Bigint *
441
multadd(Bigint *b, int m, int a)       /* multiply by m and add a */
442
1.31M
{
443
1.31M
    int i, wds;
444
1.31M
    ULong *x;
445
1.31M
    ULLong carry, y;
446
1.31M
    Bigint *b1;
447
448
1.31M
    wds = b->wds;
449
1.31M
    x = b->x;
450
1.31M
    i = 0;
451
1.31M
    carry = a;
452
4.49M
    do {
453
4.49M
        y = *x * (ULLong)m + carry;
454
4.49M
        carry = y >> 32;
455
4.49M
        *x++ = (ULong)(y & FFFFFFFF);
456
4.49M
    }
457
4.49M
    while(++i < wds);
458
1.31M
    if (carry) {
459
81.9k
        if (wds >= b->maxwds) {
460
4.38k
            b1 = Balloc(b->k+1);
461
4.38k
            if (b1 == NULL){
462
0
                Bfree(b);
463
0
                return NULL;
464
0
            }
465
4.38k
            Bcopy(b1, b);
466
4.38k
            Bfree(b);
467
4.38k
            b = b1;
468
4.38k
        }
469
81.9k
        b->x[wds++] = (ULong)carry;
470
81.9k
        b->wds = wds;
471
81.9k
    }
472
1.31M
    return b;
473
1.31M
}
474
475
/* convert a string s containing nd decimal digits (possibly containing a
476
   decimal separator at position nd0, which is ignored) to a Bigint.  This
477
   function carries on where the parsing code in _Py_dg_strtod leaves off: on
478
   entry, y9 contains the result of converting the first 9 digits.  Returns
479
   NULL on failure. */
480
481
static Bigint *
482
s2b(const char *s, int nd0, int nd, ULong y9)
483
454k
{
484
454k
    Bigint *b;
485
454k
    int i, k;
486
454k
    Long x, y;
487
488
454k
    x = (nd + 8) / 9;
489
518k
    for(k = 0, y = 1; x > y; y <<= 1, k++) ;
490
454k
    b = Balloc(k);
491
454k
    if (b == NULL)
492
0
        return NULL;
493
454k
    b->x[0] = y9;
494
454k
    b->wds = 1;
495
496
454k
    if (nd <= 9)
497
404k
      return b;
498
499
50.0k
    s += 9;
500
391k
    for (i = 9; i < nd0; i++) {
501
341k
        b = multadd(b, 10, *s++ - '0');
502
341k
        if (b == NULL)
503
0
            return NULL;
504
341k
    }
505
50.0k
    s++;
506
167k
    for(; i < nd; i++) {
507
117k
        b = multadd(b, 10, *s++ - '0');
508
117k
        if (b == NULL)
509
0
            return NULL;
510
117k
    }
511
50.0k
    return b;
512
50.0k
}
513
514
/* count leading 0 bits in the 32-bit integer x. */
515
516
static int
517
hi0bits(ULong x)
518
686k
{
519
686k
    int k = 0;
520
521
686k
    if (!(x & 0xffff0000)) {
522
263k
        k = 16;
523
263k
        x <<= 16;
524
263k
    }
525
686k
    if (!(x & 0xff000000)) {
526
272k
        k += 8;
527
272k
        x <<= 8;
528
272k
    }
529
686k
    if (!(x & 0xf0000000)) {
530
480k
        k += 4;
531
480k
        x <<= 4;
532
480k
    }
533
686k
    if (!(x & 0xc0000000)) {
534
326k
        k += 2;
535
326k
        x <<= 2;
536
326k
    }
537
686k
    if (!(x & 0x80000000)) {
538
174k
        k++;
539
174k
        if (!(x & 0x40000000))
540
0
            return 32;
541
174k
    }
542
686k
    return k;
543
686k
}
544
545
/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
546
   number of bits. */
547
548
static int
549
lo0bits(ULong *y)
550
51.0k
{
551
51.0k
    int k;
552
51.0k
    ULong x = *y;
553
554
51.0k
    if (x & 7) {
555
30.8k
        if (x & 1)
556
14.6k
            return 0;
557
16.2k
        if (x & 2) {
558
6.71k
            *y = x >> 1;
559
6.71k
            return 1;
560
6.71k
        }
561
9.49k
        *y = x >> 2;
562
9.49k
        return 2;
563
16.2k
    }
564
20.1k
    k = 0;
565
20.1k
    if (!(x & 0xffff)) {
566
7.91k
        k = 16;
567
7.91k
        x >>= 16;
568
7.91k
    }
569
20.1k
    if (!(x & 0xff)) {
570
3.56k
        k += 8;
571
3.56k
        x >>= 8;
572
3.56k
    }
573
20.1k
    if (!(x & 0xf)) {
574
9.88k
        k += 4;
575
9.88k
        x >>= 4;
576
9.88k
    }
577
20.1k
    if (!(x & 0x3)) {
578
11.3k
        k += 2;
579
11.3k
        x >>= 2;
580
11.3k
    }
581
20.1k
    if (!(x & 1)) {
582
14.2k
        k++;
583
14.2k
        x >>= 1;
584
14.2k
        if (!x)
585
0
            return 32;
586
14.2k
    }
587
20.1k
    *y = x;
588
20.1k
    return k;
589
20.1k
}
590
591
/* convert a small nonnegative integer to a Bigint */
592
593
static Bigint *
594
i2b(int i)
595
601k
{
596
601k
    Bigint *b;
597
598
601k
    b = Balloc(1);
599
601k
    if (b == NULL)
600
0
        return NULL;
601
601k
    b->x[0] = i;
602
601k
    b->wds = 1;
603
601k
    return b;
604
601k
}
605
606
/* multiply two Bigints.  Returns a new Bigint, or NULL on failure.  Ignores
607
   the signs of a and b. */
608
609
static Bigint *
610
mult(Bigint *a, Bigint *b)
611
1.71M
{
612
1.71M
    Bigint *c;
613
1.71M
    int k, wa, wb, wc;
614
1.71M
    ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
615
1.71M
    ULong y;
616
1.71M
    ULLong carry, z;
617
618
1.71M
    if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
619
7.62k
        c = Balloc(0);
620
7.62k
        if (c == NULL)
621
0
            return NULL;
622
7.62k
        c->wds = 1;
623
7.62k
        c->x[0] = 0;
624
7.62k
        return c;
625
7.62k
    }
626
627
1.70M
    if (a->wds < b->wds) {
628
917k
        c = a;
629
917k
        a = b;
630
917k
        b = c;
631
917k
    }
632
1.70M
    k = a->k;
633
1.70M
    wa = a->wds;
634
1.70M
    wb = b->wds;
635
1.70M
    wc = wa + wb;
636
1.70M
    if (wc > a->maxwds)
637
1.08M
        k++;
638
1.70M
    c = Balloc(k);
639
1.70M
    if (c == NULL)
640
0
        return NULL;
641
11.5M
    for(x = c->x, xa = x + wc; x < xa; x++)
642
9.88M
        *x = 0;
643
1.70M
    xa = a->x;
644
1.70M
    xae = xa + wa;
645
1.70M
    xb = b->x;
646
1.70M
    xbe = xb + wb;
647
1.70M
    xc0 = c->x;
648
5.08M
    for(; xb < xbe; xc0++) {
649
3.38M
        if ((y = *xb++)) {
650
3.37M
            x = xa;
651
3.37M
            xc = xc0;
652
3.37M
            carry = 0;
653
20.7M
            do {
654
20.7M
                z = *x++ * (ULLong)y + *xc + carry;
655
20.7M
                carry = z >> 32;
656
20.7M
                *xc++ = (ULong)(z & FFFFFFFF);
657
20.7M
            }
658
20.7M
            while(x < xae);
659
3.37M
            *xc = (ULong)carry;
660
3.37M
        }
661
3.38M
    }
662
2.85M
    for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
663
1.70M
    c->wds = wc;
664
1.70M
    return c;
665
1.70M
}
666
667
#ifndef Py_USING_MEMORY_DEBUGGER
668
669
/* multiply the Bigint b by 5**k.  Returns a pointer to the result, or NULL on
670
   failure; if the returned pointer is distinct from b then the original
671
   Bigint b will have been Bfree'd.   Ignores the sign of b. */
672
673
static Bigint *
674
pow5mult(Bigint *b, int k)
675
549k
{
676
549k
    Bigint *b1, *p5, **p5s;
677
549k
    int i;
678
549k
    static const int p05[3] = { 5, 25, 125 };
679
680
    // For double-to-string conversion, the maximum value of k is limited by
681
    // DBL_MAX_10_EXP (308), the maximum decimal base-10 exponent for binary64.
682
    // For string-to-double conversion, the extreme case is constrained by our
683
    // hardcoded exponent limit before we underflow of -512, adjusted by
684
    // STRTOD_DIGLIM-DBL_DIG-1, giving a maximum of k=535.
685
549k
    assert(0 <= k && k < 1024);
686
687
549k
    if ((i = k & 3)) {
688
194k
        b = multadd(b, p05[i-1], 0);
689
194k
        if (b == NULL)
690
0
            return NULL;
691
194k
    }
692
693
549k
    if (!(k >>= 2))
694
14.3k
        return b;
695
535k
    PyInterpreterState *interp = _PyInterpreterState_GET();
696
535k
    p5s = interp->dtoa.p5s;
697
2.50M
    for(;;) {
698
2.50M
        assert(p5s != interp->dtoa.p5s + Bigint_Pow5size);
699
2.50M
        p5 = *p5s;
700
2.50M
        p5s++;
701
2.50M
        if (k & 1) {
702
1.62M
            b1 = mult(b, p5);
703
1.62M
            Bfree(b);
704
1.62M
            b = b1;
705
1.62M
            if (b == NULL)
706
0
                return NULL;
707
1.62M
        }
708
2.50M
        if (!(k >>= 1))
709
535k
            break;
710
2.50M
    }
711
535k
    return b;
712
535k
}
713
714
#else
715
716
/* Version of pow5mult that doesn't cache powers of 5. Provided for
717
   the benefit of memory debugging tools like Valgrind. */
718
719
static Bigint *
720
pow5mult(Bigint *b, int k)
721
{
722
    Bigint *b1, *p5, *p51;
723
    int i;
724
    static const int p05[3] = { 5, 25, 125 };
725
726
    if ((i = k & 3)) {
727
        b = multadd(b, p05[i-1], 0);
728
        if (b == NULL)
729
            return NULL;
730
    }
731
732
    if (!(k >>= 2))
733
        return b;
734
    p5 = i2b(625);
735
    if (p5 == NULL) {
736
        Bfree(b);
737
        return NULL;
738
    }
739
740
    for(;;) {
741
        if (k & 1) {
742
            b1 = mult(b, p5);
743
            Bfree(b);
744
            b = b1;
745
            if (b == NULL) {
746
                Bfree(p5);
747
                return NULL;
748
            }
749
        }
750
        if (!(k >>= 1))
751
            break;
752
        p51 = mult(p5, p5);
753
        Bfree(p5);
754
        p5 = p51;
755
        if (p5 == NULL) {
756
            Bfree(b);
757
            return NULL;
758
        }
759
    }
760
    Bfree(p5);
761
    return b;
762
}
763
764
#endif /* Py_USING_MEMORY_DEBUGGER */
765
766
/* shift a Bigint b left by k bits.  Return a pointer to the shifted result,
767
   or NULL on failure.  If the returned pointer is distinct from b then the
768
   original b will have been Bfree'd.   Ignores the sign of b. */
769
770
static Bigint *
771
lshift(Bigint *b, int k)
772
1.19M
{
773
1.19M
    int i, k1, n, n1;
774
1.19M
    Bigint *b1;
775
1.19M
    ULong *x, *x1, *xe, z;
776
777
1.19M
    if (!k || (!b->x[0] && b->wds == 1))
778
8.17k
        return b;
779
780
1.18M
    n = k >> 5;
781
1.18M
    k1 = b->k;
782
1.18M
    n1 = n + b->wds + 1;
783
3.21M
    for(i = b->maxwds; n1 > i; i <<= 1)
784
2.03M
        k1++;
785
1.18M
    b1 = Balloc(k1);
786
1.18M
    if (b1 == NULL) {
787
0
        Bfree(b);
788
0
        return NULL;
789
0
    }
790
1.18M
    x1 = b1->x;
791
6.42M
    for(i = 0; i < n; i++)
792
5.24M
        *x1++ = 0;
793
1.18M
    x = b->x;
794
1.18M
    xe = x + b->wds;
795
1.18M
    if (k &= 0x1f) {
796
1.15M
        k1 = 32 - k;
797
1.15M
        z = 0;
798
3.29M
        do {
799
3.29M
            *x1++ = *x << k | z;
800
3.29M
            z = *x++ >> k1;
801
3.29M
        }
802
3.29M
        while(x < xe);
803
1.15M
        if ((*x1 = z))
804
304k
            ++n1;
805
1.15M
    }
806
22.2k
    else do
807
41.3k
             *x1++ = *x++;
808
41.3k
        while(x < xe);
809
1.18M
    b1->wds = n1 - 1;
810
1.18M
    Bfree(b);
811
1.18M
    return b1;
812
1.18M
}
813
814
/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
815
   1 if a > b.  Ignores signs of a and b. */
816
817
static int
818
cmp(Bigint *a, Bigint *b)
819
2.02M
{
820
2.02M
    ULong *xa, *xa0, *xb, *xb0;
821
2.02M
    int i, j;
822
823
2.02M
    i = a->wds;
824
2.02M
    j = b->wds;
825
#ifdef DEBUG
826
    if (i > 1 && !a->x[i-1])
827
        Bug("cmp called with a->x[a->wds-1] == 0");
828
    if (j > 1 && !b->x[j-1])
829
        Bug("cmp called with b->x[b->wds-1] == 0");
830
#endif
831
2.02M
    if (i -= j)
832
203k
        return i;
833
1.82M
    xa0 = a->x;
834
1.82M
    xa = xa0 + j;
835
1.82M
    xb0 = b->x;
836
1.82M
    xb = xb0 + j;
837
2.67M
    for(;;) {
838
2.67M
        if (*--xa != *--xb)
839
1.80M
            return *xa < *xb ? -1 : 1;
840
875k
        if (xa <= xa0)
841
19.9k
            break;
842
875k
    }
843
19.9k
    return 0;
844
1.82M
}
845
846
/* Take the difference of Bigints a and b, returning a new Bigint.  Returns
847
   NULL on failure.  The signs of a and b are ignored, but the sign of the
848
   result is set appropriately. */
849
850
static Bigint *
851
diff(Bigint *a, Bigint *b)
852
629k
{
853
629k
    Bigint *c;
854
629k
    int i, wa, wb;
855
629k
    ULong *xa, *xae, *xb, *xbe, *xc;
856
629k
    ULLong borrow, y;
857
858
629k
    i = cmp(a,b);
859
629k
    if (!i) {
860
3.55k
        c = Balloc(0);
861
3.55k
        if (c == NULL)
862
0
            return NULL;
863
3.55k
        c->wds = 1;
864
3.55k
        c->x[0] = 0;
865
3.55k
        return c;
866
3.55k
    }
867
625k
    if (i < 0) {
868
95.2k
        c = a;
869
95.2k
        a = b;
870
95.2k
        b = c;
871
95.2k
        i = 1;
872
95.2k
    }
873
530k
    else
874
530k
        i = 0;
875
625k
    c = Balloc(a->k);
876
625k
    if (c == NULL)
877
0
        return NULL;
878
625k
    c->sign = i;
879
625k
    wa = a->wds;
880
625k
    xa = a->x;
881
625k
    xae = xa + wa;
882
625k
    wb = b->wds;
883
625k
    xb = b->x;
884
625k
    xbe = xb + wb;
885
625k
    xc = c->x;
886
625k
    borrow = 0;
887
4.68M
    do {
888
4.68M
        y = (ULLong)*xa++ - *xb++ - borrow;
889
4.68M
        borrow = y >> 32 & (ULong)1;
890
4.68M
        *xc++ = (ULong)(y & FFFFFFFF);
891
4.68M
    }
892
4.68M
    while(xb < xbe);
893
937k
    while(xa < xae) {
894
312k
        y = *xa++ - borrow;
895
312k
        borrow = y >> 32 & (ULong)1;
896
312k
        *xc++ = (ULong)(y & FFFFFFFF);
897
312k
    }
898
1.41M
    while(!*--xc)
899
792k
        wa--;
900
625k
    c->wds = wa;
901
625k
    return c;
902
625k
}
903
904
/* Given a positive normal double x, return the difference between x and the
905
   next double up.  Doesn't give correct results for subnormals. */
906
907
static double
908
ulp(U *x)
909
314k
{
910
314k
    Long L;
911
314k
    U u;
912
913
314k
    L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
914
314k
    word0(&u) = L;
915
314k
    word1(&u) = 0;
916
314k
    return dval(&u);
917
314k
}
918
919
/* Convert a Bigint to a double plus an exponent */
920
921
static double
922
b2d(Bigint *a, int *e)
923
617k
{
924
617k
    ULong *xa, *xa0, w, y, z;
925
617k
    int k;
926
617k
    U d;
927
928
617k
    xa0 = a->x;
929
617k
    xa = xa0 + a->wds;
930
617k
    y = *--xa;
931
#ifdef DEBUG
932
    if (!y) Bug("zero y in b2d");
933
#endif
934
617k
    k = hi0bits(y);
935
617k
    *e = 32 - k;
936
617k
    if (k < Ebits) {
937
362k
        word0(&d) = Exp_1 | y >> (Ebits - k);
938
362k
        w = xa > xa0 ? *--xa : 0;
939
362k
        word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
940
362k
        goto ret_d;
941
362k
    }
942
255k
    z = xa > xa0 ? *--xa : 0;
943
255k
    if (k -= Ebits) {
944
249k
        word0(&d) = Exp_1 | y << k | z >> (32 - k);
945
249k
        y = xa > xa0 ? *--xa : 0;
946
249k
        word1(&d) = z << k | y >> (32 - k);
947
249k
    }
948
6.01k
    else {
949
6.01k
        word0(&d) = Exp_1 | y;
950
6.01k
        word1(&d) = z;
951
6.01k
    }
952
617k
  ret_d:
953
617k
    return dval(&d);
954
255k
}
955
956
/* Convert a scaled double to a Bigint plus an exponent.  Similar to d2b,
957
   except that it accepts the scale parameter used in _Py_dg_strtod (which
958
   should be either 0 or 2*P), and the normalization for the return value is
959
   different (see below).  On input, d should be finite and nonnegative, and d
960
   / 2**scale should be exactly representable as an IEEE 754 double.
961
962
   Returns a Bigint b and an integer e such that
963
964
     dval(d) / 2**scale = b * 2**e.
965
966
   Unlike d2b, b is not necessarily odd: b and e are normalized so
967
   that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
968
   and e == Etiny.  This applies equally to an input of 0.0: in that
969
   case the return values are b = 0 and e = Etiny.
970
971
   The above normalization ensures that for all possible inputs d,
972
   2**e gives ulp(d/2**scale).
973
974
   Returns NULL on failure.
975
*/
976
977
static Bigint *
978
sd2b(U *d, int scale, int *e)
979
524k
{
980
524k
    Bigint *b;
981
982
524k
    b = Balloc(1);
983
524k
    if (b == NULL)
984
0
        return NULL;
985
986
    /* First construct b and e assuming that scale == 0. */
987
524k
    b->wds = 2;
988
524k
    b->x[0] = word1(d);
989
524k
    b->x[1] = word0(d) & Frac_mask;
990
524k
    *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
991
524k
    if (*e < Etiny)
992
8.17k
        *e = Etiny;
993
516k
    else
994
516k
        b->x[1] |= Exp_msk1;
995
996
    /* Now adjust for scale, provided that b != 0. */
997
524k
    if (scale && (b->x[0] || b->x[1])) {
998
40.3k
        *e -= scale;
999
40.3k
        if (*e < Etiny) {
1000
34.7k
            scale = Etiny - *e;
1001
34.7k
            *e = Etiny;
1002
            /* We can't shift more than P-1 bits without shifting out a 1. */
1003
34.7k
            assert(0 < scale && scale <= P - 1);
1004
34.7k
            if (scale >= 32) {
1005
                /* The bits shifted out should all be zero. */
1006
20.0k
                assert(b->x[0] == 0);
1007
20.0k
                b->x[0] = b->x[1];
1008
20.0k
                b->x[1] = 0;
1009
20.0k
                scale -= 32;
1010
20.0k
            }
1011
34.7k
            if (scale) {
1012
                /* The bits shifted out should all be zero. */
1013
29.2k
                assert(b->x[0] << (32 - scale) == 0);
1014
29.2k
                b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1015
29.2k
                b->x[1] >>= scale;
1016
29.2k
            }
1017
34.7k
        }
1018
40.3k
    }
1019
    /* Ensure b is normalized. */
1020
524k
    if (!b->x[1])
1021
32.7k
        b->wds = 1;
1022
1023
524k
    return b;
1024
524k
}
1025
1026
/* Convert a double to a Bigint plus an exponent.  Return NULL on failure.
1027
1028
   Given a finite nonzero double d, return an odd Bigint b and exponent *e
1029
   such that fabs(d) = b * 2**e.  On return, *bbits gives the number of
1030
   significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
1031
1032
   If d is zero, then b == 0, *e == -1010, *bbits = 0.
1033
 */
1034
1035
static Bigint *
1036
d2b(U *d, int *e, int *bits)
1037
51.0k
{
1038
51.0k
    Bigint *b;
1039
51.0k
    int de, k;
1040
51.0k
    ULong *x, y, z;
1041
51.0k
    int i;
1042
1043
51.0k
    b = Balloc(1);
1044
51.0k
    if (b == NULL)
1045
0
        return NULL;
1046
51.0k
    x = b->x;
1047
1048
51.0k
    z = word0(d) & Frac_mask;
1049
51.0k
    word0(d) &= 0x7fffffff;   /* clear sign bit, which we ignore */
1050
51.0k
    if ((de = (int)(word0(d) >> Exp_shift)))
1051
45.9k
        z |= Exp_msk1;
1052
51.0k
    if ((y = word1(d))) {
1053
37.9k
        if ((k = lo0bits(&y))) {
1054
24.0k
            x[0] = y | z << (32 - k);
1055
24.0k
            z >>= k;
1056
24.0k
        }
1057
13.8k
        else
1058
13.8k
            x[0] = y;
1059
37.9k
        i =
1060
37.9k
            b->wds = (x[1] = z) ? 2 : 1;
1061
37.9k
    }
1062
13.1k
    else {
1063
13.1k
        k = lo0bits(&z);
1064
13.1k
        x[0] = z;
1065
13.1k
        i =
1066
13.1k
            b->wds = 1;
1067
13.1k
        k += 32;
1068
13.1k
    }
1069
51.0k
    if (de) {
1070
45.9k
        *e = de - Bias - (P-1) + k;
1071
45.9k
        *bits = P - k;
1072
45.9k
    }
1073
5.12k
    else {
1074
5.12k
        *e = de - Bias - (P-1) + 1 + k;
1075
5.12k
        *bits = 32*i - hi0bits(x[i-1]);
1076
5.12k
    }
1077
51.0k
    return b;
1078
51.0k
}
1079
1080
/* Compute the ratio of two Bigints, as a double.  The result may have an
1081
   error of up to 2.5 ulps. */
1082
1083
static double
1084
ratio(Bigint *a, Bigint *b)
1085
308k
{
1086
308k
    U da, db;
1087
308k
    int k, ka, kb;
1088
1089
308k
    dval(&da) = b2d(a, &ka);
1090
308k
    dval(&db) = b2d(b, &kb);
1091
308k
    k = ka - kb + 32*(a->wds - b->wds);
1092
308k
    if (k > 0)
1093
49.6k
        word0(&da) += k*Exp_msk1;
1094
259k
    else {
1095
259k
        k = -k;
1096
259k
        word0(&db) += k*Exp_msk1;
1097
259k
    }
1098
308k
    return dval(&da) / dval(&db);
1099
308k
}
1100
1101
static const double
1102
tens[] = {
1103
    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1104
    1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1105
    1e20, 1e21, 1e22
1106
};
1107
1108
static const double
1109
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1110
static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1111
                                   9007199254740992.*9007199254740992.e-256
1112
                                   /* = 2^106 * 1e-256 */
1113
};
1114
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1115
/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
1116
40.5k
#define Scale_Bit 0x10
1117
46.2k
#define n_bigtens 5
1118
1119
#define ULbits 32
1120
#define kshift 5
1121
64.1k
#define kmask 31
1122
1123
1124
static int
1125
dshift(Bigint *b, int p2)
1126
64.1k
{
1127
64.1k
    int rv = hi0bits(b->x[b->wds-1]) - 4;
1128
64.1k
    if (p2 > 0)
1129
27.1k
        rv -= p2;
1130
64.1k
    return rv & kmask;
1131
64.1k
}
1132
1133
/* special case of Bigint division.  The quotient is always in the range 0 <=
1134
   quotient < 10, and on entry the divisor S is normalized so that its top 4
1135
   bits (28--31) are zero and bit 27 is set. */
1136
1137
static int
1138
quorem(Bigint *b, Bigint *S)
1139
590k
{
1140
590k
    int n;
1141
590k
    ULong *bx, *bxe, q, *sx, *sxe;
1142
590k
    ULLong borrow, carry, y, ys;
1143
1144
590k
    n = S->wds;
1145
#ifdef DEBUG
1146
    /*debug*/ if (b->wds > n)
1147
        /*debug*/       Bug("oversize b in quorem");
1148
#endif
1149
590k
    if (b->wds < n)
1150
13.7k
        return 0;
1151
576k
    sx = S->x;
1152
576k
    sxe = sx + --n;
1153
576k
    bx = b->x;
1154
576k
    bxe = bx + n;
1155
576k
    q = *bxe / (*sxe + 1);      /* ensure q <= true quotient */
1156
#ifdef DEBUG
1157
    /*debug*/ if (q > 9)
1158
        /*debug*/       Bug("oversized quotient in quorem");
1159
#endif
1160
576k
    if (q) {
1161
402k
        borrow = 0;
1162
402k
        carry = 0;
1163
2.23M
        do {
1164
2.23M
            ys = *sx++ * (ULLong)q + carry;
1165
2.23M
            carry = ys >> 32;
1166
2.23M
            y = *bx - (ys & FFFFFFFF) - borrow;
1167
2.23M
            borrow = y >> 32 & (ULong)1;
1168
2.23M
            *bx++ = (ULong)(y & FFFFFFFF);
1169
2.23M
        }
1170
2.23M
        while(sx <= sxe);
1171
402k
        if (!*bxe) {
1172
2.67k
            bx = b->x;
1173
2.67k
            while(--bxe > bx && !*bxe)
1174
0
                --n;
1175
2.67k
            b->wds = n;
1176
2.67k
        }
1177
402k
    }
1178
576k
    if (cmp(b, S) >= 0) {
1179
31.7k
        q++;
1180
31.7k
        borrow = 0;
1181
31.7k
        carry = 0;
1182
31.7k
        bx = b->x;
1183
31.7k
        sx = S->x;
1184
186k
        do {
1185
186k
            ys = *sx++ + carry;
1186
186k
            carry = ys >> 32;
1187
186k
            y = *bx - (ys & FFFFFFFF) - borrow;
1188
186k
            borrow = y >> 32 & (ULong)1;
1189
186k
            *bx++ = (ULong)(y & FFFFFFFF);
1190
186k
        }
1191
186k
        while(sx <= sxe);
1192
31.7k
        bx = b->x;
1193
31.7k
        bxe = bx + n;
1194
31.7k
        if (!*bxe) {
1195
22.7k
            while(--bxe > bx && !*bxe)
1196
977
                --n;
1197
21.7k
            b->wds = n;
1198
21.7k
        }
1199
31.7k
    }
1200
576k
    return q;
1201
590k
}
1202
1203
/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
1204
1205
   Assuming that x is finite and nonnegative (positive zero is fine
1206
   here) and x / 2^bc.scale is exactly representable as a double,
1207
   sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
1208
1209
static double
1210
sulp(U *x, BCinfo *bc)
1211
5.47k
{
1212
5.47k
    U u;
1213
1214
5.47k
    if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
1215
        /* rv/2^bc->scale is subnormal */
1216
281
        word0(&u) = (P+2)*Exp_msk1;
1217
281
        word1(&u) = 0;
1218
281
        return u.d;
1219
281
    }
1220
5.19k
    else {
1221
5.19k
        assert(word0(x) || word1(x)); /* x != 0.0 */
1222
5.19k
        return ulp(x);
1223
5.19k
    }
1224
5.47k
}
1225
1226
/* The bigcomp function handles some hard cases for strtod, for inputs
1227
   with more than STRTOD_DIGLIM digits.  It's called once an initial
1228
   estimate for the double corresponding to the input string has
1229
   already been obtained by the code in _Py_dg_strtod.
1230
1231
   The bigcomp function is only called after _Py_dg_strtod has found a
1232
   double value rv such that either rv or rv + 1ulp represents the
1233
   correctly rounded value corresponding to the original string.  It
1234
   determines which of these two values is the correct one by
1235
   computing the decimal digits of rv + 0.5ulp and comparing them with
1236
   the corresponding digits of s0.
1237
1238
   In the following, write dv for the absolute value of the number represented
1239
   by the input string.
1240
1241
   Inputs:
1242
1243
     s0 points to the first significant digit of the input string.
1244
1245
     rv is a (possibly scaled) estimate for the closest double value to the
1246
        value represented by the original input to _Py_dg_strtod.  If
1247
        bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1248
        the input value.
1249
1250
     bc is a struct containing information gathered during the parsing and
1251
        estimation steps of _Py_dg_strtod.  Description of fields follows:
1252
1253
        bc->e0 gives the exponent of the input value, such that dv = (integer
1254
           given by the bd->nd digits of s0) * 10**e0
1255
1256
        bc->nd gives the total number of significant digits of s0.  It will
1257
           be at least 1.
1258
1259
        bc->nd0 gives the number of significant digits of s0 before the
1260
           decimal separator.  If there's no decimal separator, bc->nd0 ==
1261
           bc->nd.
1262
1263
        bc->scale is the value used to scale rv to avoid doing arithmetic with
1264
           subnormal values.  It's either 0 or 2*P (=106).
1265
1266
   Outputs:
1267
1268
     On successful exit, rv/2^(bc->scale) is the closest double to dv.
1269
1270
     Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
1271
1272
static int
1273
bigcomp(U *rv, const char *s0, BCinfo *bc)
1274
25.7k
{
1275
25.7k
    Bigint *b, *d;
1276
25.7k
    int b2, d2, dd, i, nd, nd0, odd, p2, p5;
1277
1278
25.7k
    nd = bc->nd;
1279
25.7k
    nd0 = bc->nd0;
1280
25.7k
    p5 = nd + bc->e0;
1281
25.7k
    b = sd2b(rv, bc->scale, &p2);
1282
25.7k
    if (b == NULL)
1283
0
        return -1;
1284
1285
    /* record whether the lsb of rv/2^(bc->scale) is odd:  in the exact halfway
1286
       case, this is used for round to even. */
1287
25.7k
    odd = b->x[0] & 1;
1288
1289
    /* left shift b by 1 bit and or a 1 into the least significant bit;
1290
       this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1291
25.7k
    b = lshift(b, 1);
1292
25.7k
    if (b == NULL)
1293
0
        return -1;
1294
25.7k
    b->x[0] |= 1;
1295
25.7k
    p2--;
1296
1297
25.7k
    p2 -= p5;
1298
25.7k
    d = i2b(1);
1299
25.7k
    if (d == NULL) {
1300
0
        Bfree(b);
1301
0
        return -1;
1302
0
    }
1303
    /* Arrange for convenient computation of quotients:
1304
     * shift left if necessary so divisor has 4 leading 0 bits.
1305
     */
1306
25.7k
    if (p5 > 0) {
1307
23.0k
        d = pow5mult(d, p5);
1308
23.0k
        if (d == NULL) {
1309
0
            Bfree(b);
1310
0
            return -1;
1311
0
        }
1312
23.0k
    }
1313
2.74k
    else if (p5 < 0) {
1314
1.79k
        b = pow5mult(b, -p5);
1315
1.79k
        if (b == NULL) {
1316
0
            Bfree(d);
1317
0
            return -1;
1318
0
        }
1319
1.79k
    }
1320
25.7k
    if (p2 > 0) {
1321
20.1k
        b2 = p2;
1322
20.1k
        d2 = 0;
1323
20.1k
    }
1324
5.61k
    else {
1325
5.61k
        b2 = 0;
1326
5.61k
        d2 = -p2;
1327
5.61k
    }
1328
25.7k
    i = dshift(d, d2);
1329
25.7k
    if ((b2 += i) > 0) {
1330
25.4k
        b = lshift(b, b2);
1331
25.4k
        if (b == NULL) {
1332
0
            Bfree(d);
1333
0
            return -1;
1334
0
        }
1335
25.4k
    }
1336
25.7k
    if ((d2 += i) > 0) {
1337
24.5k
        d = lshift(d, d2);
1338
24.5k
        if (d == NULL) {
1339
0
            Bfree(b);
1340
0
            return -1;
1341
0
        }
1342
24.5k
    }
1343
1344
    /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1345
     * b/d, or s0 > b/d.  Here the digits of s0 are thought of as representing
1346
     * a number in the range [0.1, 1). */
1347
25.7k
    if (cmp(b, d) >= 0)
1348
        /* b/d >= 1 */
1349
880
        dd = -1;
1350
24.9k
    else {
1351
24.9k
        i = 0;
1352
459k
        for(;;) {
1353
459k
            b = multadd(b, 10, 0);
1354
459k
            if (b == NULL) {
1355
0
                Bfree(d);
1356
0
                return -1;
1357
0
            }
1358
459k
            dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1359
459k
            i++;
1360
1361
459k
            if (dd)
1362
21.9k
                break;
1363
438k
            if (!b->x[0] && b->wds == 1) {
1364
                /* b/d == 0 */
1365
1.46k
                dd = i < nd;
1366
1.46k
                break;
1367
1.46k
            }
1368
436k
            if (!(i < nd)) {
1369
                /* b/d != 0, but digits of s0 exhausted */
1370
1.49k
                dd = -1;
1371
1.49k
                break;
1372
1.49k
            }
1373
436k
        }
1374
24.9k
    }
1375
25.7k
    Bfree(b);
1376
25.7k
    Bfree(d);
1377
25.7k
    if (dd > 0 || (dd == 0 && odd))
1378
3.30k
        dval(rv) += sulp(rv, bc);
1379
25.7k
    return 0;
1380
25.7k
}
1381
1382
1383
double
1384
_Py_dg_strtod(const char *s00, char **se)
1385
1.35M
{
1386
1.35M
    int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1387
1.35M
    int esign, i, j, k, lz, nd, nd0, odd, sign;
1388
1.35M
    const char *s, *s0, *s1;
1389
1.35M
    double aadj, aadj1;
1390
1.35M
    U aadj2, adj, rv, rv0;
1391
1.35M
    ULong y, z, abs_exp;
1392
1.35M
    Long L;
1393
1.35M
    BCinfo bc;
1394
1.35M
    Bigint *bb = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
1395
1.35M
    size_t ndigits, fraclen;
1396
1.35M
    double result;
1397
1398
1.35M
    dval(&rv) = 0.;
1399
1400
    /* Start parsing. */
1401
1.35M
    c = *(s = s00);
1402
1403
    /* Parse optional sign, if present. */
1404
1.35M
    sign = 0;
1405
1.35M
    switch (c) {
1406
715k
    case '-':
1407
715k
        sign = 1;
1408
715k
        _Py_FALLTHROUGH;
1409
715k
    case '+':
1410
715k
        c = *++s;
1411
1.35M
    }
1412
1413
    /* Skip leading zeros: lz is true iff there were leading zeros. */
1414
1.35M
    s1 = s;
1415
1.38M
    while (c == '0')
1416
27.5k
        c = *++s;
1417
1.35M
    lz = s != s1;
1418
1419
    /* Point s0 at the first nonzero digit (if any).  fraclen will be the
1420
       number of digits between the decimal point and the end of the
1421
       digit string.  ndigits will be the total number of digits ignoring
1422
       leading zeros. */
1423
1.35M
    s0 = s1 = s;
1424
7.91M
    while ('0' <= c && c <= '9')
1425
6.55M
        c = *++s;
1426
1.35M
    ndigits = s - s1;
1427
1.35M
    fraclen = 0;
1428
1429
    /* Parse decimal point and following digits. */
1430
1.35M
    if (c == '.') {
1431
87.3k
        c = *++s;
1432
87.3k
        if (!ndigits) {
1433
29.7k
            s1 = s;
1434
1.18M
            while (c == '0')
1435
1.15M
                c = *++s;
1436
29.7k
            lz = lz || s != s1;
1437
29.7k
            fraclen += (s - s1);
1438
29.7k
            s0 = s;
1439
29.7k
        }
1440
87.3k
        s1 = s;
1441
32.2M
        while ('0' <= c && c <= '9')
1442
32.1M
            c = *++s;
1443
87.3k
        ndigits += s - s1;
1444
87.3k
        fraclen += s - s1;
1445
87.3k
    }
1446
1447
    /* Now lz is true if and only if there were leading zero digits, and
1448
       ndigits gives the total number of digits ignoring leading zeros.  A
1449
       valid input must have at least one digit. */
1450
1.35M
    if (!ndigits && !lz) {
1451
56.5k
        if (se)
1452
56.5k
            *se = (char *)s00;
1453
56.5k
        goto parse_error;
1454
56.5k
    }
1455
1456
    /* Range check ndigits and fraclen to make sure that they, and values
1457
       computed with them, can safely fit in an int. */
1458
1.30M
    if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) {
1459
0
        if (se)
1460
0
            *se = (char *)s00;
1461
0
        goto parse_error;
1462
0
    }
1463
1.30M
    nd = (int)ndigits;
1464
1.30M
    nd0 = (int)ndigits - (int)fraclen;
1465
1466
    /* Parse exponent. */
1467
1.30M
    e = 0;
1468
1.30M
    if (c == 'e' || c == 'E') {
1469
1.21M
        s00 = s;
1470
1.21M
        c = *++s;
1471
1472
        /* Exponent sign. */
1473
1.21M
        esign = 0;
1474
1.21M
        switch (c) {
1475
48.6k
        case '-':
1476
48.6k
            esign = 1;
1477
48.6k
            _Py_FALLTHROUGH;
1478
67.8k
        case '+':
1479
67.8k
            c = *++s;
1480
1.21M
        }
1481
1482
        /* Skip zeros.  lz is true iff there are leading zeros. */
1483
1.21M
        s1 = s;
1484
1.45M
        while (c == '0')
1485
239k
            c = *++s;
1486
1.21M
        lz = s != s1;
1487
1488
        /* Get absolute value of the exponent. */
1489
1.21M
        s1 = s;
1490
1.21M
        abs_exp = 0;
1491
15.1M
        while ('0' <= c && c <= '9') {
1492
13.9M
            abs_exp = 10*abs_exp + (c - '0');
1493
13.9M
            c = *++s;
1494
13.9M
        }
1495
1496
        /* abs_exp will be correct modulo 2**32.  But 10**9 < 2**32, so if
1497
           there are at most 9 significant exponent digits then overflow is
1498
           impossible. */
1499
1.21M
        if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1500
11.4k
            e = (int)MAX_ABS_EXP;
1501
1.20M
        else
1502
1.20M
            e = (int)abs_exp;
1503
1.21M
        if (esign)
1504
48.6k
            e = -e;
1505
1506
        /* A valid exponent must have at least one digit. */
1507
1.21M
        if (s == s1 && !lz)
1508
0
            s = s00;
1509
1.21M
    }
1510
1511
    /* Adjust exponent to take into account position of the point. */
1512
1.30M
    e -= nd - nd0;
1513
1.30M
    if (nd0 <= 0)
1514
35.0k
        nd0 = nd;
1515
1516
    /* Finished parsing.  Set se to indicate how far we parsed */
1517
1.30M
    if (se)
1518
1.30M
        *se = (char *)s;
1519
1520
    /* If all digits were zero, exit with return value +-0.0.  Otherwise,
1521
       strip trailing zeros: scan back until we hit a nonzero digit. */
1522
1.30M
    if (!nd)
1523
14.6k
        goto ret;
1524
10.1M
    for (i = nd; i > 0; ) {
1525
10.1M
        --i;
1526
10.1M
        if (s0[i < nd0 ? i : i+1] != '0') {
1527
1.28M
            ++i;
1528
1.28M
            break;
1529
1.28M
        }
1530
10.1M
    }
1531
1.28M
    e += nd - i;
1532
1.28M
    nd = i;
1533
1.28M
    if (nd0 > nd)
1534
23.1k
        nd0 = nd;
1535
1536
    /* Summary of parsing results.  After parsing, and dealing with zero
1537
     * inputs, we have values s0, nd0, nd, e, sign, where:
1538
     *
1539
     *  - s0 points to the first significant digit of the input string
1540
     *
1541
     *  - nd is the total number of significant digits (here, and
1542
     *    below, 'significant digits' means the set of digits of the
1543
     *    significand of the input that remain after ignoring leading
1544
     *    and trailing zeros).
1545
     *
1546
     *  - nd0 indicates the position of the decimal point, if present; it
1547
     *    satisfies 1 <= nd0 <= nd.  The nd significant digits are in
1548
     *    s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1549
     *    notation.  (If nd0 < nd, then s0[nd0] contains a '.'  character; if
1550
     *    nd0 == nd, then s0[nd0] could be any non-digit character.)
1551
     *
1552
     *  - e is the adjusted exponent: the absolute value of the number
1553
     *    represented by the original input string is n * 10**e, where
1554
     *    n is the integer represented by the concatenation of
1555
     *    s0[0:nd0] and s0[nd0+1:nd+1]
1556
     *
1557
     *  - sign gives the sign of the input:  1 for negative, 0 for positive
1558
     *
1559
     *  - the first and last significant digits are nonzero
1560
     */
1561
1562
    /* put first DBL_DIG+1 digits into integer y and z.
1563
     *
1564
     *  - y contains the value represented by the first min(9, nd)
1565
     *    significant digits
1566
     *
1567
     *  - if nd > 9, z contains the value represented by significant digits
1568
     *    with indices in [9, min(16, nd)).  So y * 10**(min(16, nd) - 9) + z
1569
     *    gives the value represented by the first min(16, nd) sig. digits.
1570
     */
1571
1572
1.28M
    bc.e0 = e1 = e;
1573
1.28M
    y = z = 0;
1574
4.97M
    for (i = 0; i < nd; i++) {
1575
3.74M
        if (i < 9)
1576
3.20M
            y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1577
540k
        else if (i < DBL_DIG+1)
1578
485k
            z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1579
54.8k
        else
1580
54.8k
            break;
1581
3.74M
    }
1582
1583
1.28M
    k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1584
1.28M
    dval(&rv) = y;
1585
1.28M
    if (k > 9) {
1586
86.8k
        dval(&rv) = tens[k - 9] * dval(&rv) + z;
1587
86.8k
    }
1588
1.28M
    if (nd <= DBL_DIG
1589
1.22M
        && Flt_Rounds == 1
1590
1.28M
        ) {
1591
1.22M
        if (!e)
1592
17.6k
            goto ret;
1593
1.20M
        if (e > 0) {
1594
1.13M
            if (e <= Ten_pmax) {
1595
38.6k
                dval(&rv) *= tens[e];
1596
38.6k
                goto ret;
1597
38.6k
            }
1598
1.09M
            i = DBL_DIG - nd;
1599
1.09M
            if (e <= Ten_pmax + i) {
1600
                /* A fancier test would sometimes let us do
1601
                 * this for larger i values.
1602
                 */
1603
3.35k
                e -= i;
1604
3.35k
                dval(&rv) *= tens[i];
1605
3.35k
                dval(&rv) *= tens[e];
1606
3.35k
                goto ret;
1607
3.35k
            }
1608
1.09M
        }
1609
70.5k
        else if (e >= -Ten_pmax) {
1610
32.1k
            dval(&rv) /= tens[-e];
1611
32.1k
            goto ret;
1612
32.1k
        }
1613
1.20M
    }
1614
1.19M
    e1 += nd - k;
1615
1616
1.19M
    bc.scale = 0;
1617
1618
    /* Get starting approximation = rv * 10**e1 */
1619
1620
1.19M
    if (e1 > 0) {
1621
1.13M
        if ((i = e1 & 15))
1622
1.00M
            dval(&rv) *= tens[i];
1623
1.13M
        if (e1 &= ~15) {
1624
1.12M
            if (e1 > DBL_MAX_10_EXP)
1625
731k
                goto ovfl;
1626
389k
            e1 >>= 4;
1627
1.00M
            for(j = 0; e1 > 1; j++, e1 >>= 1)
1628
616k
                if (e1 & 1)
1629
337k
                    dval(&rv) *= bigtens[j];
1630
            /* The last multiplication could overflow. */
1631
389k
            word0(&rv) -= P*Exp_msk1;
1632
389k
            dval(&rv) *= bigtens[j];
1633
389k
            if ((z = word0(&rv) & Exp_mask)
1634
389k
                > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1635
2.26k
                goto ovfl;
1636
387k
            if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1637
                /* set to largest number */
1638
                /* (Can't trust DBL_MAX) */
1639
342
                word0(&rv) = Big0;
1640
342
                word1(&rv) = Big1;
1641
342
            }
1642
387k
            else
1643
387k
                word0(&rv) += P*Exp_msk1;
1644
387k
        }
1645
1.13M
    }
1646
60.6k
    else if (e1 < 0) {
1647
        /* The input decimal value lies in [10**e1, 10**(e1+16)).
1648
1649
           If e1 <= -512, underflow immediately.
1650
           If e1 <= -256, set bc.scale to 2*P.
1651
1652
           So for input value < 1e-256, bc.scale is always set;
1653
           for input value >= 1e-240, bc.scale is never set.
1654
           For input values in [1e-256, 1e-240), bc.scale may or may
1655
           not be set. */
1656
1657
56.5k
        e1 = -e1;
1658
56.5k
        if ((i = e1 & 15))
1659
46.1k
            dval(&rv) /= tens[i];
1660
56.5k
        if (e1 >>= 4) {
1661
46.2k
            if (e1 >= 1 << n_bigtens)
1662
5.71k
                goto undfl;
1663
40.5k
            if (e1 & Scale_Bit)
1664
25.7k
                bc.scale = 2*P;
1665
200k
            for(j = 0; e1 > 0; j++, e1 >>= 1)
1666
159k
                if (e1 & 1)
1667
96.9k
                    dval(&rv) *= tinytens[j];
1668
40.5k
            if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1669
25.7k
                                            >> Exp_shift)) > 0) {
1670
                /* scaled rv is denormal; clear j low bits */
1671
23.1k
                if (j >= 32) {
1672
14.8k
                    word1(&rv) = 0;
1673
14.8k
                    if (j >= 53)
1674
8.09k
                        word0(&rv) = (P+2)*Exp_msk1;
1675
6.75k
                    else
1676
6.75k
                        word0(&rv) &= 0xffffffff << (j-32);
1677
14.8k
                }
1678
8.25k
                else
1679
8.25k
                    word1(&rv) &= 0xffffffff << j;
1680
23.1k
            }
1681
40.5k
            if (!dval(&rv))
1682
0
                goto undfl;
1683
40.5k
        }
1684
56.5k
    }
1685
1686
    /* Now the hard part -- adjusting rv to the correct value.*/
1687
1688
    /* Put digits into bd: true value = bd * 10^e */
1689
1690
454k
    bc.nd = nd;
1691
454k
    bc.nd0 = nd0;       /* Only needed if nd > STRTOD_DIGLIM, but done here */
1692
                        /* to silence an erroneous warning about bc.nd0 */
1693
                        /* possibly not being initialized. */
1694
454k
    if (nd > STRTOD_DIGLIM) {
1695
        /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
1696
        /* minimum number of decimal digits to distinguish double values */
1697
        /* in IEEE arithmetic. */
1698
1699
        /* Truncate input to 18 significant digits, then discard any trailing
1700
           zeros on the result by updating nd, nd0, e and y suitably. (There's
1701
           no need to update z; it's not reused beyond this point.) */
1702
249k
        for (i = 18; i > 0; ) {
1703
            /* scan back until we hit a nonzero digit.  significant digit 'i'
1704
            is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
1705
249k
            --i;
1706
249k
            if (s0[i < nd0 ? i : i+1] != '0') {
1707
32.8k
                ++i;
1708
32.8k
                break;
1709
32.8k
            }
1710
249k
        }
1711
32.8k
        e += nd - i;
1712
32.8k
        nd = i;
1713
32.8k
        if (nd0 > nd)
1714
28.0k
            nd0 = nd;
1715
32.8k
        if (nd < 9) { /* must recompute y */
1716
19.0k
            y = 0;
1717
147k
            for(i = 0; i < nd0; ++i)
1718
128k
                y = 10*y + s0[i] - '0';
1719
26.5k
            for(; i < nd; ++i)
1720
7.45k
                y = 10*y + s0[i+1] - '0';
1721
19.0k
        }
1722
32.8k
    }
1723
454k
    bd0 = s2b(s0, nd0, nd, y);
1724
454k
    if (bd0 == NULL)
1725
0
        goto failed_malloc;
1726
1727
    /* Notation for the comments below.  Write:
1728
1729
         - dv for the absolute value of the number represented by the original
1730
           decimal input string.
1731
1732
         - if we've truncated dv, write tdv for the truncated value.
1733
           Otherwise, set tdv == dv.
1734
1735
         - srv for the quantity rv/2^bc.scale; so srv is the current binary
1736
           approximation to tdv (and dv).  It should be exactly representable
1737
           in an IEEE 754 double.
1738
    */
1739
1740
498k
    for(;;) {
1741
1742
        /* This is the main correction loop for _Py_dg_strtod.
1743
1744
           We've got a decimal value tdv, and a floating-point approximation
1745
           srv=rv/2^bc.scale to tdv.  The aim is to determine whether srv is
1746
           close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1747
           approximation if not.
1748
1749
           To determine whether srv is close enough to tdv, compute integers
1750
           bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1751
           respectively, and then use integer arithmetic to determine whether
1752
           |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1753
        */
1754
1755
498k
        bd = Balloc(bd0->k);
1756
498k
        if (bd == NULL) {
1757
0
            goto failed_malloc;
1758
0
        }
1759
498k
        Bcopy(bd, bd0);
1760
498k
        bb = sd2b(&rv, bc.scale, &bbe);   /* srv = bb * 2^bbe */
1761
498k
        if (bb == NULL) {
1762
0
            goto failed_malloc;
1763
0
        }
1764
        /* Record whether lsb of bb is odd, in case we need this
1765
           for the round-to-even step later. */
1766
498k
        odd = bb->x[0] & 1;
1767
1768
        /* tdv = bd * 10**e;  srv = bb * 2**bbe */
1769
498k
        bs = i2b(1);
1770
498k
        if (bs == NULL) {
1771
0
            goto failed_malloc;
1772
0
        }
1773
1774
498k
        if (e >= 0) {
1775
420k
            bb2 = bb5 = 0;
1776
420k
            bd2 = bd5 = e;
1777
420k
        }
1778
78.0k
        else {
1779
78.0k
            bb2 = bb5 = -e;
1780
78.0k
            bd2 = bd5 = 0;
1781
78.0k
        }
1782
498k
        if (bbe >= 0)
1783
423k
            bb2 += bbe;
1784
75.3k
        else
1785
75.3k
            bd2 -= bbe;
1786
498k
        bs2 = bb2;
1787
498k
        bb2++;
1788
498k
        bd2++;
1789
1790
        /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
1791
           and bs == 1, so:
1792
1793
              tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1794
              srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
1795
              0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
1796
1797
           It follows that:
1798
1799
              M * tdv = bd * 2**bd2 * 5**bd5
1800
              M * srv = bb * 2**bb2 * 5**bb5
1801
              M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1802
1803
           for some constant M.  (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1804
           this fact is not needed below.)
1805
        */
1806
1807
        /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
1808
498k
        i = bb2 < bd2 ? bb2 : bd2;
1809
498k
        if (i > bs2)
1810
74.2k
            i = bs2;
1811
498k
        if (i > 0) {
1812
497k
            bb2 -= i;
1813
497k
            bd2 -= i;
1814
497k
            bs2 -= i;
1815
497k
        }
1816
1817
        /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
1818
498k
        if (bb5 > 0) {
1819
78.0k
            bs = pow5mult(bs, bb5);
1820
78.0k
            if (bs == NULL) {
1821
0
                goto failed_malloc;
1822
0
            }
1823
78.0k
            Bigint *bb1 = mult(bs, bb);
1824
78.0k
            Bfree(bb);
1825
78.0k
            bb = bb1;
1826
78.0k
            if (bb == NULL) {
1827
0
                goto failed_malloc;
1828
0
            }
1829
78.0k
        }
1830
498k
        if (bb2 > 0) {
1831
498k
            bb = lshift(bb, bb2);
1832
498k
            if (bb == NULL) {
1833
0
                goto failed_malloc;
1834
0
            }
1835
498k
        }
1836
498k
        if (bd5 > 0) {
1837
410k
            bd = pow5mult(bd, bd5);
1838
410k
            if (bd == NULL) {
1839
0
                goto failed_malloc;
1840
0
            }
1841
410k
        }
1842
498k
        if (bd2 > 0) {
1843
74.2k
            bd = lshift(bd, bd2);
1844
74.2k
            if (bd == NULL) {
1845
0
                goto failed_malloc;
1846
0
            }
1847
74.2k
        }
1848
498k
        if (bs2 > 0) {
1849
419k
            bs = lshift(bs, bs2);
1850
419k
            if (bs == NULL) {
1851
0
                goto failed_malloc;
1852
0
            }
1853
419k
        }
1854
1855
        /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
1856
           respectively.  Compute the difference |tdv - srv|, and compare
1857
           with 0.5 ulp(srv). */
1858
1859
498k
        delta = diff(bb, bd);
1860
498k
        if (delta == NULL) {
1861
0
            goto failed_malloc;
1862
0
        }
1863
498k
        dsign = delta->sign;
1864
498k
        delta->sign = 0;
1865
498k
        i = cmp(delta, bs);
1866
498k
        if (bc.nd > nd && i <= 0) {
1867
32.8k
            if (dsign)
1868
24.4k
                break;  /* Must use bigcomp(). */
1869
1870
            /* Here rv overestimates the truncated decimal value by at most
1871
               0.5 ulp(rv).  Hence rv either overestimates the true decimal
1872
               value by <= 0.5 ulp(rv), or underestimates it by some small
1873
               amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1874
               the true decimal value, so it's possible to exit.
1875
1876
               Exception: if scaled rv is a normal exact power of 2, but not
1877
               DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1878
               next double, so the correctly rounded result is either rv - 0.5
1879
               ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1880
1881
8.41k
            if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1882
                /* rv can't be 0, since it's an overestimate for some
1883
                   nonzero value.  So rv is a normal power of 2. */
1884
1.59k
                j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1885
                /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1886
                   rv / 2^bc.scale >= 2^-1021. */
1887
1.59k
                if (j - bc.scale >= 2) {
1888
1.32k
                    dval(&rv) -= 0.5 * sulp(&rv, &bc);
1889
1.32k
                    break; /* Use bigcomp. */
1890
1.32k
                }
1891
1.59k
            }
1892
1893
7.08k
            {
1894
7.08k
                bc.nd = nd;
1895
7.08k
                i = -1; /* Discarded digits make delta smaller. */
1896
7.08k
            }
1897
7.08k
        }
1898
1899
472k
        if (i < 0) {
1900
            /* Error is less than half an ulp -- check for
1901
             * special case of mantissa a power of two.
1902
             */
1903
158k
            if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
1904
6.40k
                || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
1905
158k
                ) {
1906
153k
                break;
1907
153k
            }
1908
4.85k
            if (!delta->x[0] && delta->wds <= 1) {
1909
                /* exact result */
1910
585
                break;
1911
585
            }
1912
4.26k
            delta = lshift(delta,Log2P);
1913
4.26k
            if (delta == NULL) {
1914
0
                goto failed_malloc;
1915
0
            }
1916
4.26k
            if (cmp(delta, bs) > 0)
1917
1.14k
                goto drop_down;
1918
3.11k
            break;
1919
4.26k
        }
1920
314k
        if (i == 0) {
1921
            /* exactly half-way between */
1922
5.43k
            if (dsign) {
1923
2.36k
                if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1924
860
                    &&  word1(&rv) == (
1925
860
                        (bc.scale &&
1926
0
                         (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
1927
0
                        (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1928
860
                        0xffffffff)) {
1929
                    /*boundary case -- increment exponent*/
1930
519
                    word0(&rv) = (word0(&rv) & Exp_mask)
1931
519
                        + Exp_msk1
1932
519
                        ;
1933
519
                    word1(&rv) = 0;
1934
                    /* dsign = 0; */
1935
519
                    break;
1936
519
                }
1937
2.36k
            }
1938
3.07k
            else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1939
1.14k
              drop_down:
1940
                /* boundary case -- decrement exponent */
1941
1.14k
                if (bc.scale) {
1942
0
                    L = word0(&rv) & Exp_mask;
1943
0
                    if (L <= (2*P+1)*Exp_msk1) {
1944
0
                        if (L > (P+2)*Exp_msk1)
1945
                            /* round even ==> */
1946
                            /* accept rv */
1947
0
                            break;
1948
                        /* rv = smallest denormal */
1949
0
                        if (bc.nd > nd)
1950
0
                            break;
1951
0
                        goto undfl;
1952
0
                    }
1953
0
                }
1954
1.14k
                L = (word0(&rv) & Exp_mask) - Exp_msk1;
1955
1.14k
                word0(&rv) = L | Bndry_mask1;
1956
1.14k
                word1(&rv) = 0xffffffff;
1957
1.14k
                break;
1958
1.14k
            }
1959
4.91k
            if (!odd)
1960
4.07k
                break;
1961
837
            if (dsign)
1962
574
                dval(&rv) += sulp(&rv, &bc);
1963
263
            else {
1964
263
                dval(&rv) -= sulp(&rv, &bc);
1965
263
                if (!dval(&rv)) {
1966
0
                    if (bc.nd >nd)
1967
0
                        break;
1968
0
                    goto undfl;
1969
0
                }
1970
263
            }
1971
            /* dsign = 1 - dsign; */
1972
837
            break;
1973
837
        }
1974
308k
        if ((aadj = ratio(delta, bs)) <= 2.) {
1975
277k
            if (dsign)
1976
24.2k
                aadj = aadj1 = 1.;
1977
252k
            else if (word1(&rv) || word0(&rv) & Bndry_mask) {
1978
245k
                if (word1(&rv) == Tiny1 && !word0(&rv)) {
1979
0
                    if (bc.nd >nd)
1980
0
                        break;
1981
0
                    goto undfl;
1982
0
                }
1983
245k
                aadj = 1.;
1984
245k
                aadj1 = -1.;
1985
245k
            }
1986
7.62k
            else {
1987
                /* special case -- power of FLT_RADIX to be */
1988
                /* rounded down... */
1989
1990
7.62k
                if (aadj < 2./FLT_RADIX)
1991
0
                    aadj = 1./FLT_RADIX;
1992
7.62k
                else
1993
7.62k
                    aadj *= 0.5;
1994
7.62k
                aadj1 = -aadj;
1995
7.62k
            }
1996
277k
        }
1997
31.5k
        else {
1998
31.5k
            aadj *= 0.5;
1999
31.5k
            aadj1 = dsign ? aadj : -aadj;
2000
31.5k
            if (Flt_Rounds == 0)
2001
0
                aadj1 += 0.5;
2002
31.5k
        }
2003
308k
        y = word0(&rv) & Exp_mask;
2004
2005
        /* Check for overflow */
2006
2007
308k
        if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2008
2.41k
            dval(&rv0) = dval(&rv);
2009
2.41k
            word0(&rv) -= P*Exp_msk1;
2010
2.41k
            adj.d = aadj1 * ulp(&rv);
2011
2.41k
            dval(&rv) += adj.d;
2012
2.41k
            if ((word0(&rv) & Exp_mask) >=
2013
2.41k
                Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2014
1.03k
                if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
2015
686
                    goto ovfl;
2016
686
                }
2017
344
                word0(&rv) = Big0;
2018
344
                word1(&rv) = Big1;
2019
344
                goto cont;
2020
1.03k
            }
2021
1.38k
            else
2022
1.38k
                word0(&rv) += P*Exp_msk1;
2023
2.41k
        }
2024
306k
        else {
2025
306k
            if (bc.scale && y <= 2*P*Exp_msk1) {
2026
19.5k
                if (aadj <= 0x7fffffff) {
2027
19.5k
                    if ((z = (ULong)aadj) <= 0)
2028
1.83k
                        z = 1;
2029
19.5k
                    aadj = z;
2030
19.5k
                    aadj1 = dsign ? aadj : -aadj;
2031
19.5k
                }
2032
19.5k
                dval(&aadj2) = aadj1;
2033
19.5k
                word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2034
19.5k
                aadj1 = dval(&aadj2);
2035
19.5k
            }
2036
306k
            adj.d = aadj1 * ulp(&rv);
2037
306k
            dval(&rv) += adj.d;
2038
306k
        }
2039
307k
        z = word0(&rv) & Exp_mask;
2040
307k
        if (bc.nd == nd) {
2041
287k
            if (!bc.scale)
2042
267k
                if (y == z) {
2043
                    /* Can we stop now? */
2044
266k
                    L = (Long)aadj;
2045
266k
                    aadj -= L;
2046
                    /* The tolerances below are conservative. */
2047
266k
                    if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
2048
266k
                        if (aadj < .4999999 || aadj > .5000001)
2049
264k
                            break;
2050
266k
                    }
2051
23
                    else if (aadj < .4999999/FLT_RADIX)
2052
23
                        break;
2053
266k
                }
2054
287k
        }
2055
43.9k
      cont:
2056
43.9k
        Bfree(bb); bb = NULL;
2057
43.9k
        Bfree(bd); bd = NULL;
2058
43.9k
        Bfree(bs); bs = NULL;
2059
43.9k
        Bfree(delta); delta = NULL;
2060
43.9k
    }
2061
454k
    if (bc.nd > nd) {
2062
25.7k
        error = bigcomp(&rv, s0, &bc);
2063
25.7k
        if (error)
2064
0
            goto failed_malloc;
2065
25.7k
    }
2066
2067
454k
    if (bc.scale) {
2068
25.7k
        word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2069
25.7k
        word1(&rv0) = 0;
2070
25.7k
        dval(&rv) *= dval(&rv0);
2071
25.7k
    }
2072
2073
560k
  ret:
2074
560k
    result = sign ? -dval(&rv) : dval(&rv);
2075
560k
    goto done;
2076
2077
56.5k
  parse_error:
2078
56.5k
    result = 0.0;
2079
56.5k
    goto done;
2080
2081
0
  failed_malloc:
2082
0
    errno = ENOMEM;
2083
0
    result = -1.0;
2084
0
    goto done;
2085
2086
5.71k
  undfl:
2087
5.71k
    result = sign ? -0.0 : 0.0;
2088
5.71k
    goto done;
2089
2090
734k
  ovfl:
2091
734k
    errno = ERANGE;
2092
    /* Can't trust HUGE_VAL */
2093
734k
    word0(&rv) = Exp_mask;
2094
734k
    word1(&rv) = 0;
2095
734k
    result = sign ? -dval(&rv) : dval(&rv);
2096
734k
    goto done;
2097
2098
1.35M
  done:
2099
1.35M
    Bfree(bb);
2100
1.35M
    Bfree(bd);
2101
1.35M
    Bfree(bs);
2102
1.35M
    Bfree(bd0);
2103
1.35M
    Bfree(delta);
2104
1.35M
    return result;
2105
2106
454k
}
2107
2108
static char *
2109
rv_alloc(int i)
2110
57.6k
{
2111
57.6k
    int j, k, *r;
2112
2113
57.6k
    j = sizeof(ULong);
2114
57.6k
    for(k = 0;
2115
57.6k
        sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2116
57.6k
        j <<= 1)
2117
0
        k++;
2118
57.6k
    r = (int*)Balloc(k);
2119
57.6k
    if (r == NULL)
2120
0
        return NULL;
2121
57.6k
    *r = k;
2122
57.6k
    return (char *)(r+1);
2123
57.6k
}
2124
2125
static char *
2126
nrv_alloc(const char *s, char **rve, int n)
2127
6.59k
{
2128
6.59k
    char *rv, *t;
2129
2130
6.59k
    rv = rv_alloc(n);
2131
6.59k
    if (rv == NULL)
2132
0
        return NULL;
2133
6.59k
    t = rv;
2134
19.6k
    while((*t = *s++)) t++;
2135
6.59k
    if (rve)
2136
6.59k
        *rve = t;
2137
6.59k
    return rv;
2138
6.59k
}
2139
2140
/* freedtoa(s) must be used to free values s returned by dtoa
2141
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
2142
 * but for consistency with earlier versions of dtoa, it is optional
2143
 * when MULTIPLE_THREADS is not defined.
2144
 */
2145
2146
void
2147
_Py_dg_freedtoa(char *s)
2148
57.6k
{
2149
57.6k
    Bigint *b = (Bigint *)((int *)s - 1);
2150
57.6k
    b->maxwds = 1 << (b->k = *(int*)b);
2151
57.6k
    Bfree(b);
2152
57.6k
}
2153
2154
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2155
 *
2156
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2157
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2158
 *
2159
 * Modifications:
2160
 *      1. Rather than iterating, we use a simple numeric overestimate
2161
 *         to determine k = floor(log10(d)).  We scale relevant
2162
 *         quantities using O(log2(k)) rather than O(k) multiplications.
2163
 *      2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2164
 *         try to generate digits strictly left to right.  Instead, we
2165
 *         compute with fewer bits and propagate the carry if necessary
2166
 *         when rounding the final digit up.  This is often faster.
2167
 *      3. Under the assumption that input will be rounded nearest,
2168
 *         mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2169
 *         That is, we allow equality in stopping tests when the
2170
 *         round-nearest rule will give the same floating-point value
2171
 *         as would satisfaction of the stopping test with strict
2172
 *         inequality.
2173
 *      4. We remove common factors of powers of 2 from relevant
2174
 *         quantities.
2175
 *      5. When converting floating-point integers less than 1e16,
2176
 *         we use floating-point arithmetic rather than resorting
2177
 *         to multiple-precision integers.
2178
 *      6. When asked to produce fewer than 15 digits, we first try
2179
 *         to get by with floating-point arithmetic; we resort to
2180
 *         multiple-precision integer arithmetic only if we cannot
2181
 *         guarantee that the floating-point calculation has given
2182
 *         the correctly rounded result.  For k requested digits and
2183
 *         "uniformly" distributed input, the probability is
2184
 *         something like 10^(k-15) that we must resort to the Long
2185
 *         calculation.
2186
 */
2187
2188
/* Additional notes (METD): (1) returns NULL on failure.  (2) to avoid memory
2189
   leakage, a successful call to _Py_dg_dtoa should always be matched by a
2190
   call to _Py_dg_freedtoa. */
2191
2192
char *
2193
_Py_dg_dtoa(double dd, int mode, int ndigits,
2194
            int *decpt, int *sign, char **rve)
2195
57.6k
{
2196
    /*  Arguments ndigits, decpt, sign are similar to those
2197
        of ecvt and fcvt; trailing zeros are suppressed from
2198
        the returned string.  If not null, *rve is set to point
2199
        to the end of the return value.  If d is +-Infinity or NaN,
2200
        then *decpt is set to 9999.
2201
2202
        mode:
2203
        0 ==> shortest string that yields d when read in
2204
        and rounded to nearest.
2205
        1 ==> like 0, but with Steele & White stopping rule;
2206
        e.g. with IEEE P754 arithmetic , mode 0 gives
2207
        1e23 whereas mode 1 gives 9.999999999999999e22.
2208
        2 ==> max(1,ndigits) significant digits.  This gives a
2209
        return value similar to that of ecvt, except
2210
        that trailing zeros are suppressed.
2211
        3 ==> through ndigits past the decimal point.  This
2212
        gives a return value similar to that from fcvt,
2213
        except that trailing zeros are suppressed, and
2214
        ndigits can be negative.
2215
        4,5 ==> similar to 2 and 3, respectively, but (in
2216
        round-nearest mode) with the tests of mode 0 to
2217
        possibly return a shorter string that rounds to d.
2218
        With IEEE arithmetic and compilation with
2219
        -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2220
        as modes 2 and 3 when FLT_ROUNDS != 1.
2221
        6-9 ==> Debugging modes similar to mode - 4:  don't try
2222
        fast floating-point estimate (if applicable).
2223
2224
        Values of mode other than 0-9 are treated as mode 0.
2225
2226
        Sufficient space is allocated to the return value
2227
        to hold the suppressed trailing zeros.
2228
    */
2229
2230
57.6k
    int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2231
57.6k
        j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2232
57.6k
        spec_case, try_quick;
2233
57.6k
    Long L;
2234
57.6k
    int denorm;
2235
57.6k
    ULong x;
2236
57.6k
    Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2237
57.6k
    U d2, eps, u;
2238
57.6k
    double ds;
2239
57.6k
    char *s, *s0;
2240
2241
    /* set pointers to NULL, to silence gcc compiler warnings and make
2242
       cleanup easier on error */
2243
57.6k
    mlo = mhi = S = 0;
2244
57.6k
    s0 = 0;
2245
2246
57.6k
    u.d = dd;
2247
57.6k
    if (word0(&u) & Sign_bit) {
2248
        /* set sign for everything, including 0's and NaNs */
2249
21.1k
        *sign = 1;
2250
21.1k
        word0(&u) &= ~Sign_bit; /* clear sign bit */
2251
21.1k
    }
2252
36.5k
    else
2253
36.5k
        *sign = 0;
2254
2255
    /* quick return for Infinities, NaNs and zeros */
2256
57.6k
    if ((word0(&u) & Exp_mask) == Exp_mask)
2257
915
    {
2258
        /* Infinity or NaN */
2259
915
        *decpt = 9999;
2260
915
        if (!word1(&u) && !(word0(&u) & 0xfffff))
2261
915
            return nrv_alloc("Infinity", rve, 8);
2262
0
        return nrv_alloc("NaN", rve, 3);
2263
915
    }
2264
56.7k
    if (!dval(&u)) {
2265
5.68k
        *decpt = 1;
2266
5.68k
        return nrv_alloc("0", rve, 1);
2267
5.68k
    }
2268
2269
    /* compute k = floor(log10(d)).  The computation may leave k
2270
       one too large, but should never leave k too small. */
2271
51.0k
    b = d2b(&u, &be, &bbits);
2272
51.0k
    if (b == NULL)
2273
0
        goto failed_malloc;
2274
51.0k
    if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2275
45.9k
        dval(&d2) = dval(&u);
2276
45.9k
        word0(&d2) &= Frac_mask1;
2277
45.9k
        word0(&d2) |= Exp_11;
2278
2279
        /* log(x)       ~=~ log(1.5) + (x-1.5)/1.5
2280
         * log10(x)      =  log(x) / log(10)
2281
         *              ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2282
         * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2283
         *
2284
         * This suggests computing an approximation k to log10(d) by
2285
         *
2286
         * k = (i - Bias)*0.301029995663981
2287
         *      + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2288
         *
2289
         * We want k to be too large rather than too small.
2290
         * The error in the first-order Taylor series approximation
2291
         * is in our favor, so we just round up the constant enough
2292
         * to compensate for any error in the multiplication of
2293
         * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2294
         * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2295
         * adding 1e-13 to the constant term more than suffices.
2296
         * Hence we adjust the constant term to 0.1760912590558.
2297
         * (We could get a more accurate k by invoking log10,
2298
         *  but this is probably not worthwhile.)
2299
         */
2300
2301
45.9k
        i -= Bias;
2302
45.9k
        denorm = 0;
2303
45.9k
    }
2304
5.12k
    else {
2305
        /* d is denormalized */
2306
2307
5.12k
        i = bbits + be + (Bias + (P-1) - 1);
2308
5.12k
        x = i > 32  ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2309
5.12k
            : word1(&u) << (32 - i);
2310
5.12k
        dval(&d2) = x;
2311
5.12k
        word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2312
5.12k
        i -= (Bias + (P-1) - 1) + 1;
2313
5.12k
        denorm = 1;
2314
5.12k
    }
2315
51.0k
    ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2316
51.0k
        i*0.301029995663981;
2317
51.0k
    k = (int)ds;
2318
51.0k
    if (ds < 0. && ds != k)
2319
14.6k
        k--;    /* want k = floor(ds) */
2320
51.0k
    k_check = 1;
2321
51.0k
    if (k >= 0 && k <= Ten_pmax) {
2322
19.8k
        if (dval(&u) < tens[k])
2323
1.75k
            k--;
2324
19.8k
        k_check = 0;
2325
19.8k
    }
2326
51.0k
    j = bbits - i - 1;
2327
51.0k
    if (j >= 0) {
2328
20.7k
        b2 = 0;
2329
20.7k
        s2 = j;
2330
20.7k
    }
2331
30.3k
    else {
2332
30.3k
        b2 = -j;
2333
30.3k
        s2 = 0;
2334
30.3k
    }
2335
51.0k
    if (k >= 0) {
2336
35.9k
        b5 = 0;
2337
35.9k
        s5 = k;
2338
35.9k
        s2 += k;
2339
35.9k
    }
2340
15.1k
    else {
2341
15.1k
        b2 -= k;
2342
15.1k
        b5 = -k;
2343
15.1k
        s5 = 0;
2344
15.1k
    }
2345
51.0k
    if (mode < 0 || mode > 9)
2346
0
        mode = 0;
2347
2348
51.0k
    try_quick = 1;
2349
2350
51.0k
    if (mode > 5) {
2351
0
        mode -= 4;
2352
0
        try_quick = 0;
2353
0
    }
2354
51.0k
    leftright = 1;
2355
51.0k
    ilim = ilim1 = -1;  /* Values for cases 0 and 1; done here to */
2356
    /* silence erroneous "gcc -Wall" warning. */
2357
51.0k
    switch(mode) {
2358
50.9k
    case 0:
2359
50.9k
    case 1:
2360
50.9k
        i = 18;
2361
50.9k
        ndigits = 0;
2362
50.9k
        break;
2363
0
    case 2:
2364
0
        leftright = 0;
2365
0
        _Py_FALLTHROUGH;
2366
0
    case 4:
2367
0
        if (ndigits <= 0)
2368
0
            ndigits = 1;
2369
0
        ilim = ilim1 = i = ndigits;
2370
0
        break;
2371
106
    case 3:
2372
106
        leftright = 0;
2373
106
        _Py_FALLTHROUGH;
2374
106
    case 5:
2375
106
        i = ndigits + k + 1;
2376
106
        ilim = i;
2377
106
        ilim1 = i - 1;
2378
106
        if (i <= 0)
2379
0
            i = 1;
2380
51.0k
    }
2381
51.0k
    s0 = rv_alloc(i);
2382
51.0k
    if (s0 == NULL)
2383
0
        goto failed_malloc;
2384
51.0k
    s = s0;
2385
2386
2387
51.0k
    if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2388
2389
        /* Try to get by with floating-point arithmetic. */
2390
2391
106
        i = 0;
2392
106
        dval(&d2) = dval(&u);
2393
106
        k0 = k;
2394
106
        ilim0 = ilim;
2395
106
        ieps = 2; /* conservative */
2396
106
        if (k > 0) {
2397
85
            ds = tens[k&0xf];
2398
85
            j = k >> 4;
2399
85
            if (j & Bletch) {
2400
                /* prevent overflows */
2401
0
                j &= Bletch - 1;
2402
0
                dval(&u) /= bigtens[n_bigtens-1];
2403
0
                ieps++;
2404
0
            }
2405
85
            for(; j; j >>= 1, i++)
2406
0
                if (j & 1) {
2407
0
                    ieps++;
2408
0
                    ds *= bigtens[i];
2409
0
                }
2410
85
            dval(&u) /= ds;
2411
85
        }
2412
21
        else if ((j1 = -k)) {
2413
0
            dval(&u) *= tens[j1 & 0xf];
2414
0
            for(j = j1 >> 4; j; j >>= 1, i++)
2415
0
                if (j & 1) {
2416
0
                    ieps++;
2417
0
                    dval(&u) *= bigtens[i];
2418
0
                }
2419
0
        }
2420
106
        if (k_check && dval(&u) < 1. && ilim > 0) {
2421
0
            if (ilim1 <= 0)
2422
0
                goto fast_failed;
2423
0
            ilim = ilim1;
2424
0
            k--;
2425
0
            dval(&u) *= 10.;
2426
0
            ieps++;
2427
0
        }
2428
106
        dval(&eps) = ieps*dval(&u) + 7.;
2429
106
        word0(&eps) -= (P-1)*Exp_msk1;
2430
106
        if (ilim == 0) {
2431
0
            S = mhi = 0;
2432
0
            dval(&u) -= 5.;
2433
0
            if (dval(&u) > dval(&eps))
2434
0
                goto one_digit;
2435
0
            if (dval(&u) < -dval(&eps))
2436
0
                goto no_digits;
2437
0
            goto fast_failed;
2438
0
        }
2439
106
        if (leftright) {
2440
            /* Use Steele & White method of only
2441
             * generating digits needed.
2442
             */
2443
0
            dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2444
0
            for(i = 0;;) {
2445
0
                L = (Long)dval(&u);
2446
0
                dval(&u) -= L;
2447
0
                *s++ = '0' + (int)L;
2448
0
                if (dval(&u) < dval(&eps))
2449
0
                    goto ret1;
2450
0
                if (1. - dval(&u) < dval(&eps))
2451
0
                    goto bump_up;
2452
0
                if (++i >= ilim)
2453
0
                    break;
2454
0
                dval(&eps) *= 10.;
2455
0
                dval(&u) *= 10.;
2456
0
            }
2457
0
        }
2458
106
        else {
2459
            /* Generate ilim digits, then fix them up. */
2460
106
            dval(&eps) *= tens[ilim-1];
2461
291
            for(i = 1;; i++, dval(&u) *= 10.) {
2462
291
                L = (Long)(dval(&u));
2463
291
                if (!(dval(&u) -= L))
2464
12
                    ilim = i;
2465
291
                *s++ = '0' + (int)L;
2466
291
                if (i == ilim) {
2467
106
                    if (dval(&u) > 0.5 + dval(&eps))
2468
53
                        goto bump_up;
2469
53
                    else if (dval(&u) < 0.5 - dval(&eps)) {
2470
61
                        while(*--s == '0');
2471
53
                        s++;
2472
53
                        goto ret1;
2473
53
                    }
2474
0
                    break;
2475
106
                }
2476
291
            }
2477
106
        }
2478
0
      fast_failed:
2479
0
        s = s0;
2480
0
        dval(&u) = dval(&d2);
2481
0
        k = k0;
2482
0
        ilim = ilim0;
2483
0
    }
2484
2485
    /* Do we have a "small" integer? */
2486
2487
50.9k
    if (be >= 0 && k <= Int_max) {
2488
        /* Yes. */
2489
12.5k
        ds = tens[k];
2490
12.5k
        if (ndigits < 0 && ilim <= 0) {
2491
0
            S = mhi = 0;
2492
0
            if (ilim < 0 || dval(&u) <= 5*ds)
2493
0
                goto no_digits;
2494
0
            goto one_digit;
2495
0
        }
2496
16.9k
        for(i = 1;; i++, dval(&u) *= 10.) {
2497
16.9k
            L = (Long)(dval(&u) / ds);
2498
16.9k
            dval(&u) -= L*ds;
2499
16.9k
            *s++ = '0' + (int)L;
2500
16.9k
            if (!dval(&u)) {
2501
12.5k
                break;
2502
12.5k
            }
2503
4.35k
            if (i == ilim) {
2504
0
                dval(&u) += dval(&u);
2505
0
                if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2506
53
                  bump_up:
2507
58
                    while(*--s == '9')
2508
5
                        if (s == s0) {
2509
0
                            k++;
2510
0
                            *s = '0';
2511
0
                            break;
2512
0
                        }
2513
53
                    ++*s++;
2514
53
                }
2515
0
                else {
2516
                    /* Strip trailing zeros. This branch was missing from the
2517
                       original dtoa.c, leading to surplus trailing zeros in
2518
                       some cases. See bugs.python.org/issue40780. */
2519
0
                    while (s > s0 && s[-1] == '0') {
2520
0
                        --s;
2521
0
                    }
2522
0
                }
2523
53
                break;
2524
0
            }
2525
4.35k
        }
2526
12.6k
        goto ret1;
2527
12.5k
    }
2528
2529
38.3k
    m2 = b2;
2530
38.3k
    m5 = b5;
2531
38.3k
    if (leftright) {
2532
38.3k
        i =
2533
38.3k
            denorm ? be + (Bias + (P-1) - 1 + 1) :
2534
38.3k
            1 + P - bbits;
2535
38.3k
        b2 += i;
2536
38.3k
        s2 += i;
2537
38.3k
        mhi = i2b(1);
2538
38.3k
        if (mhi == NULL)
2539
0
            goto failed_malloc;
2540
38.3k
    }
2541
38.3k
    if (m2 > 0 && s2 > 0) {
2542
34.8k
        i = m2 < s2 ? m2 : s2;
2543
34.8k
        b2 -= i;
2544
34.8k
        m2 -= i;
2545
34.8k
        s2 -= i;
2546
34.8k
    }
2547
38.3k
    if (b5 > 0) {
2548
15.1k
        if (leftright) {
2549
15.1k
            if (m5 > 0) {
2550
15.1k
                mhi = pow5mult(mhi, m5);
2551
15.1k
                if (mhi == NULL)
2552
0
                    goto failed_malloc;
2553
15.1k
                b1 = mult(mhi, b);
2554
15.1k
                Bfree(b);
2555
15.1k
                b = b1;
2556
15.1k
                if (b == NULL)
2557
0
                    goto failed_malloc;
2558
15.1k
            }
2559
15.1k
            if ((j = b5 - m5)) {
2560
0
                b = pow5mult(b, j);
2561
0
                if (b == NULL)
2562
0
                    goto failed_malloc;
2563
0
            }
2564
15.1k
        }
2565
0
        else {
2566
0
            b = pow5mult(b, b5);
2567
0
            if (b == NULL)
2568
0
                goto failed_malloc;
2569
0
        }
2570
15.1k
    }
2571
38.3k
    S = i2b(1);
2572
38.3k
    if (S == NULL)
2573
0
        goto failed_malloc;
2574
38.3k
    if (s5 > 0) {
2575
21.3k
        S = pow5mult(S, s5);
2576
21.3k
        if (S == NULL)
2577
0
            goto failed_malloc;
2578
21.3k
    }
2579
2580
    /* Check for special case that d is a normalized power of 2. */
2581
2582
38.3k
    spec_case = 0;
2583
38.3k
    if ((mode < 2 || leftright)
2584
38.3k
        ) {
2585
38.3k
        if (!word1(&u) && !(word0(&u) & Bndry_mask)
2586
1.46k
            && word0(&u) & (Exp_mask & ~Exp_msk1)
2587
38.3k
            ) {
2588
            /* The special case */
2589
1.20k
            b2 += Log2P;
2590
1.20k
            s2 += Log2P;
2591
1.20k
            spec_case = 1;
2592
1.20k
        }
2593
38.3k
    }
2594
2595
    /* Arrange for convenient computation of quotients:
2596
     * shift left if necessary so divisor has 4 leading 0 bits.
2597
     *
2598
     * Perhaps we should just compute leading 28 bits of S once
2599
     * and for all and pass them and a shift to quorem, so it
2600
     * can do shifts and ors to compute the numerator for q.
2601
     */
2602
38.3k
#define iInc 28
2603
38.3k
    i = dshift(S, s2);
2604
38.3k
    b2 += i;
2605
38.3k
    m2 += i;
2606
38.3k
    s2 += i;
2607
38.3k
    if (b2 > 0) {
2608
38.3k
        b = lshift(b, b2);
2609
38.3k
        if (b == NULL)
2610
0
            goto failed_malloc;
2611
38.3k
    }
2612
38.3k
    if (s2 > 0) {
2613
37.8k
        S = lshift(S, s2);
2614
37.8k
        if (S == NULL)
2615
0
            goto failed_malloc;
2616
37.8k
    }
2617
38.3k
    if (k_check) {
2618
31.1k
        if (cmp(b,S) < 0) {
2619
5.55k
            k--;
2620
5.55k
            b = multadd(b, 10, 0);      /* we botched the k estimate */
2621
5.55k
            if (b == NULL)
2622
0
                goto failed_malloc;
2623
5.55k
            if (leftright) {
2624
5.55k
                mhi = multadd(mhi, 10, 0);
2625
5.55k
                if (mhi == NULL)
2626
0
                    goto failed_malloc;
2627
5.55k
            }
2628
5.55k
            ilim = ilim1;
2629
5.55k
        }
2630
31.1k
    }
2631
38.3k
    if (ilim <= 0 && (mode == 3 || mode == 5)) {
2632
0
        if (ilim < 0) {
2633
            /* no digits, fcvt style */
2634
0
          no_digits:
2635
0
            k = -1 - ndigits;
2636
0
            goto ret;
2637
0
        }
2638
0
        else {
2639
0
            S = multadd(S, 5, 0);
2640
0
            if (S == NULL)
2641
0
                goto failed_malloc;
2642
0
            if (cmp(b, S) <= 0)
2643
0
                goto no_digits;
2644
0
        }
2645
0
      one_digit:
2646
0
        *s++ = '1';
2647
0
        k++;
2648
0
        goto ret;
2649
0
    }
2650
38.3k
    if (leftright) {
2651
38.3k
        if (m2 > 0) {
2652
37.1k
            mhi = lshift(mhi, m2);
2653
37.1k
            if (mhi == NULL)
2654
0
                goto failed_malloc;
2655
37.1k
        }
2656
2657
        /* Compute mlo -- check for special case
2658
         * that d is a normalized power of 2.
2659
         */
2660
2661
38.3k
        mlo = mhi;
2662
38.3k
        if (spec_case) {
2663
1.20k
            mhi = Balloc(mhi->k);
2664
1.20k
            if (mhi == NULL)
2665
0
                goto failed_malloc;
2666
1.20k
            Bcopy(mhi, mlo);
2667
1.20k
            mhi = lshift(mhi, Log2P);
2668
1.20k
            if (mhi == NULL)
2669
0
                goto failed_malloc;
2670
1.20k
        }
2671
2672
130k
        for(i = 1;;i++) {
2673
130k
            dig = quorem(b,S) + '0';
2674
            /* Do we yet have the shortest decimal string
2675
             * that will round to d?
2676
             */
2677
130k
            j = cmp(b, mlo);
2678
130k
            delta = diff(S, mhi);
2679
130k
            if (delta == NULL)
2680
0
                goto failed_malloc;
2681
130k
            j1 = delta->sign ? 1 : cmp(b, delta);
2682
130k
            Bfree(delta);
2683
130k
            if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2684
130k
                ) {
2685
1.74k
                if (dig == '9')
2686
306
                    goto round_9_up;
2687
1.43k
                if (j > 0)
2688
692
                    dig++;
2689
1.43k
                *s++ = dig;
2690
1.43k
                goto ret;
2691
1.74k
            }
2692
128k
            if (j < 0 || (j == 0 && mode != 1
2693
1.89k
                          && !(word1(&u) & 1)
2694
110k
                    )) {
2695
19.3k
                if (!b->x[0] && b->wds <= 1) {
2696
2.33k
                    goto accept_dig;
2697
2.33k
                }
2698
17.0k
                if (j1 > 0) {
2699
2.88k
                    b = lshift(b, 1);
2700
2.88k
                    if (b == NULL)
2701
0
                        goto failed_malloc;
2702
2.88k
                    j1 = cmp(b, S);
2703
2.88k
                    if ((j1 > 0 || (j1 == 0 && dig & 1))
2704
1.79k
                        && dig++ == '9')
2705
415
                        goto round_9_up;
2706
2.88k
                }
2707
18.9k
              accept_dig:
2708
18.9k
                *s++ = dig;
2709
18.9k
                goto ret;
2710
17.0k
            }
2711
109k
            if (j1 > 0) {
2712
17.2k
                if (dig == '9') { /* possible if i == 1 */
2713
5.49k
                  round_9_up:
2714
5.49k
                    *s++ = '9';
2715
5.49k
                    goto roundoff;
2716
4.77k
                }
2717
12.4k
                *s++ = dig + 1;
2718
12.4k
                goto ret;
2719
17.2k
            }
2720
92.1k
            *s++ = dig;
2721
92.1k
            if (i == ilim)
2722
0
                break;
2723
92.1k
            b = multadd(b, 10, 0);
2724
92.1k
            if (b == NULL)
2725
0
                goto failed_malloc;
2726
92.1k
            if (mlo == mhi) {
2727
87.5k
                mlo = mhi = multadd(mhi, 10, 0);
2728
87.5k
                if (mlo == NULL)
2729
0
                    goto failed_malloc;
2730
87.5k
            }
2731
4.57k
            else {
2732
4.57k
                mlo = multadd(mlo, 10, 0);
2733
4.57k
                if (mlo == NULL)
2734
0
                    goto failed_malloc;
2735
4.57k
                mhi = multadd(mhi, 10, 0);
2736
4.57k
                if (mhi == NULL)
2737
0
                    goto failed_malloc;
2738
4.57k
            }
2739
92.1k
        }
2740
38.3k
    }
2741
0
    else
2742
0
        for(i = 1;; i++) {
2743
0
            *s++ = dig = quorem(b,S) + '0';
2744
0
            if (!b->x[0] && b->wds <= 1) {
2745
0
                goto ret;
2746
0
            }
2747
0
            if (i >= ilim)
2748
0
                break;
2749
0
            b = multadd(b, 10, 0);
2750
0
            if (b == NULL)
2751
0
                goto failed_malloc;
2752
0
        }
2753
2754
    /* Round off last digit */
2755
2756
0
    b = lshift(b, 1);
2757
0
    if (b == NULL)
2758
0
        goto failed_malloc;
2759
0
    j = cmp(b, S);
2760
0
    if (j > 0 || (j == 0 && dig & 1)) {
2761
5.49k
      roundoff:
2762
5.49k
        while(*--s == '9')
2763
5.49k
            if (s == s0) {
2764
5.49k
                k++;
2765
5.49k
                *s++ = '1';
2766
5.49k
                goto ret;
2767
5.49k
            }
2768
0
        ++*s++;
2769
0
    }
2770
0
    else {
2771
0
        while(*--s == '0');
2772
0
        s++;
2773
0
    }
2774
38.3k
  ret:
2775
38.3k
    Bfree(S);
2776
38.3k
    if (mhi) {
2777
38.3k
        if (mlo && mlo != mhi)
2778
1.20k
            Bfree(mlo);
2779
38.3k
        Bfree(mhi);
2780
38.3k
    }
2781
51.0k
  ret1:
2782
51.0k
    Bfree(b);
2783
51.0k
    *s = 0;
2784
51.0k
    *decpt = k + 1;
2785
51.0k
    if (rve)
2786
51.0k
        *rve = s;
2787
51.0k
    return s0;
2788
0
  failed_malloc:
2789
0
    if (S)
2790
0
        Bfree(S);
2791
0
    if (mlo && mlo != mhi)
2792
0
        Bfree(mlo);
2793
0
    if (mhi)
2794
0
        Bfree(mhi);
2795
0
    if (b)
2796
0
        Bfree(b);
2797
0
    if (s0)
2798
0
        _Py_dg_freedtoa(s0);
2799
0
    return NULL;
2800
38.3k
}
2801
2802
#endif  // _PY_SHORT_FLOAT_REPR == 1
2803
2804
PyStatus
2805
_PyDtoa_Init(PyInterpreterState *interp)
2806
36
{
2807
36
#if _PY_SHORT_FLOAT_REPR == 1 && !defined(Py_USING_MEMORY_DEBUGGER)
2808
36
    Bigint **p5s = interp->dtoa.p5s;
2809
2810
    // 5**4 = 625
2811
36
    Bigint *p5 = i2b(625);
2812
36
    if (p5 == NULL) {
2813
0
        return PyStatus_NoMemory();
2814
0
    }
2815
36
    p5s[0] = p5;
2816
2817
    // compute 5**8, 5**16, 5**32, ..., 5**512
2818
288
    for (Py_ssize_t i = 1; i < Bigint_Pow5size; i++) {
2819
252
        p5 = mult(p5, p5);
2820
252
        if (p5 == NULL) {
2821
0
            return PyStatus_NoMemory();
2822
0
        }
2823
252
        p5s[i] = p5;
2824
252
    }
2825
2826
36
#endif
2827
36
    return PyStatus_Ok();
2828
36
}
2829
2830
void
2831
_PyDtoa_Fini(PyInterpreterState *interp)
2832
0
{
2833
0
#if _PY_SHORT_FLOAT_REPR == 1 && !defined(Py_USING_MEMORY_DEBUGGER)
2834
0
    Bigint **p5s = interp->dtoa.p5s;
2835
0
    for (Py_ssize_t i = 0; i < Bigint_Pow5size; i++) {
2836
0
        Bigint *p5 = p5s[i];
2837
        p5s[i] = NULL;
2838
0
        Bfree(p5);
2839
0
    }
2840
0
#endif
2841
0
}