Coverage Report

Created: 2025-11-02 06:30

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