The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / third_party / dtoa / dtoa.c @ 40c0f755

History | View | Annotate | Download (67.7 KB)

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
/* Please send bug reports to David M. Gay (dmg at acm dot org,
21
 * with " at " changed at "@" and " dot " changed to ".").        */
22

    
23
/* On a machine with IEEE extended-precision registers, it is
24
 * necessary to specify double-precision (53-bit) rounding precision
25
 * before invoking strtod or dtoa.  If the machine uses (the equivalent
26
 * of) Intel 80x87 arithmetic, the call
27
 *        _control87(PC_53, MCW_PC);
28
 * does this with many compilers.  Whether this or another call is
29
 * appropriate depends on the compiler; for this to work, it may be
30
 * necessary to #include "float.h" or another system-dependent header
31
 * file.
32
 */
33

    
34
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
35
 *
36
 * This strtod returns a nearest machine number to the input decimal
37
 * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
38
 * broken by the IEEE round-even rule.  Otherwise ties are broken by
39
 * biased rounding (add half and chop).
40
 *
41
 * Inspired loosely by William D. Clinger's paper "How to Read Floating
42
 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
43
 *
44
 * Modifications:
45
 *
46
 *        1. We only require IEEE, IBM, or VAX double-precision
47
 *                arithmetic (not IEEE double-extended).
48
 *        2. We get by with floating-point arithmetic in a case that
49
 *                Clinger missed -- when we're computing d * 10^n
50
 *                for a small integer d and the integer n is not too
51
 *                much larger than 22 (the maximum integer k for which
52
 *                we can represent 10^k exactly), we may be able to
53
 *                compute (d*10^k) * 10^(e-k) with just one roundoff.
54
 *        3. Rather than a bit-at-a-time adjustment of the binary
55
 *                result in the hard case, we use floating-point
56
 *                arithmetic to determine the adjustment to within
57
 *                one bit; only in really hard cases do we need to
58
 *                compute a second residual.
59
 *        4. Because of 3., we don't need a large table of powers of 10
60
 *                for ten-to-e (just some small tables, e.g. of 10^k
61
 *                for 0 <= k <= 22).
62
 */
63

    
64
/*
65
 * #define IEEE_8087 for IEEE-arithmetic machines where the least
66
 *        significant byte has the lowest address.
67
 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
68
 *        significant byte has the lowest address.
69
 * #define Long int on machines with 32-bit ints and 64-bit longs.
70
 * #define IBM for IBM mainframe-style floating-point arithmetic.
71
 * #define VAX for VAX-style floating-point arithmetic (D_floating).
72
 * #define No_leftright to omit left-right logic in fast floating-point
73
 *        computation of dtoa.
74
 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
75
 *        and strtod and dtoa should round accordingly.
76
 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
77
 *        and Honor_FLT_ROUNDS is not #defined.
78
 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
79
 *        that use extended-precision instructions to compute rounded
80
 *        products and quotients) with IBM.
81
 * #define ROUND_BIASED for IEEE-format with biased rounding.
82
 * #define Inaccurate_Divide for IEEE-format with correctly rounded
83
 *        products but inaccurate quotients, e.g., for Intel i860.
84
 * #define NO_LONG_LONG on machines that do not have a "long long"
85
 *        integer type (of >= 64 bits).  On such machines, you can
86
 *        #define Just_16 to store 16 bits per 32-bit Long when doing
87
 *        high-precision integer arithmetic.  Whether this speeds things
88
 *        up or slows things down depends on the machine and the number
89
 *        being converted.  If long long is available and the name is
90
 *        something other than "long long", #define Llong to be the name,
91
 *        and if "unsigned Llong" does not work as an unsigned version of
92
 *        Llong, #define #ULLong to be the corresponding unsigned type.
93
 * #define KR_headers for old-style C function headers.
94
 * #define Bad_float_h if your system lacks a float.h or if it does not
95
 *        define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
96
 *        FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
97
 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
98
 *        if memory is available and otherwise does something you deem
99
 *        appropriate.  If MALLOC is undefined, malloc will be invoked
100
 *        directly -- and assumed always to succeed.
101
 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
102
 *        memory allocations from a private pool of memory when possible.
103
 *        When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
104
 *        unless #defined to be a different length.  This default length
105
 *        suffices to get rid of MALLOC calls except for unusual cases,
106
 *        such as decimal-to-binary conversion of a very long string of
107
 *        digits.  The longest string dtoa can return is about 751 bytes
108
 *        long.  For conversions by strtod of strings of 800 digits and
109
 *        all dtoa conversions in single-threaded executions with 8-byte
110
 *        pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
111
 *        pointers, PRIVATE_MEM >= 7112 appears adequate.
112
 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
113
 *        Infinity and NaN (case insensitively).  On some systems (e.g.,
114
 *        some HP systems), it may be necessary to #define NAN_WORD0
115
 *        appropriately -- to the most significant word of a quiet NaN.
116
 *        (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
117
 *        When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
118
 *        strtod also accepts (case insensitively) strings of the form
119
 *        NaN(x), where x is a string of hexadecimal digits and spaces;
120
 *        if there is only one string of hexadecimal digits, it is taken
121
 *        for the 52 fraction bits of the resulting NaN; if there are two
122
 *        or more strings of hex digits, the first is for the high 20 bits,
123
 *        the second and subsequent for the low 32 bits, with intervening
124
 *        white space ignored; but if this results in none of the 52
125
 *        fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
126
 *        and NAN_WORD1 are used instead.
127
 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
128
 *        multiple threads.  In this case, you must provide (or suitably
129
 *        #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
130
 *        by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
131
 *        in pow5mult, ensures lazy evaluation of only one copy of high
132
 *        powers of 5; omitting this lock would introduce a small
133
 *        probability of wasting memory, but would otherwise be harmless.)
134
 *        You must also invoke freedtoa(s) to free the value s returned by
135
 *        dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
136
 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
137
 *        avoids underflows on inputs whose result does not underflow.
138
 *        If you #define NO_IEEE_Scale on a machine that uses IEEE-format
139
 *        floating-point numbers and flushes underflows to zero rather
140
 *        than implementing gradual underflow, then you must also #define
141
 *        Sudden_Underflow.
142
 * #define YES_ALIAS to permit aliasing certain double values with
143
 *        arrays of ULongs.  This leads to slightly better code with
144
 *        some compilers and was always used prior to 19990916, but it
145
 *        is not strictly legal and can cause trouble with aggressively
146
 *        optimizing compilers (e.g., gcc 2.95.1 under -O2).
147
 * #define USE_LOCALE to use the current locale's decimal_point value.
148
 * #define SET_INEXACT if IEEE arithmetic is being used and extra
149
 *        computation should be done to set the inexact flag when the
150
 *        result is inexact and avoid setting inexact when the result
151
 *        is exact.  In this case, dtoa.c must be compiled in
152
 *        an environment, perhaps provided by #include "dtoa.c" in a
153
 *        suitable wrapper, that defines two functions,
154
 *                int get_inexact(void);
155
 *                void clear_inexact(void);
156
 *        such that get_inexact() returns a nonzero value if the
157
 *        inexact bit is already set, and clear_inexact() sets the
158
 *        inexact bit to 0.  When SET_INEXACT is #defined, strtod
159
 *        also does extra computations to set the underflow and overflow
160
 *        flags when appropriate (i.e., when the result is tiny and
161
 *        inexact or when it is a numeric value rounded to +-infinity).
162
 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
163
 *        the result overflows to +-Infinity or underflows to 0.
164
 */
165

    
166
#ifndef Long
167
#define Long long
168
#endif
169
#ifndef ULong
170
typedef unsigned Long ULong;
171
#endif
172

    
173
#ifdef DEBUG
174
#include "stdio.h"
175
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
176
#endif
177

    
178
#include "stdlib.h"
179
#include "string.h"
180

    
181
#ifdef USE_LOCALE
182
#include "locale.h"
183
#endif
184

    
185
#ifdef MALLOC
186
#ifdef KR_headers
187
extern char *MALLOC();
188
#else
189
extern void *MALLOC(size_t);
190
#endif
191
#else
192
#define MALLOC malloc
193
#endif
194

    
195
#ifndef Omit_Private_Memory
196
#ifndef PRIVATE_MEM
197
#define PRIVATE_MEM 2304
198
#endif
199
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
200
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
201
#endif
202

    
203
#undef IEEE_Arith
204
#undef Avoid_Underflow
205
#ifdef IEEE_MC68k
206
#define IEEE_Arith
207
#endif
208
#ifdef IEEE_8087
209
#define IEEE_Arith
210
#endif
211

    
212
#include "errno.h"
213

    
214
#ifdef Bad_float_h
215

    
216
#ifdef IEEE_Arith
217
#define DBL_DIG 15
218
#define DBL_MAX_10_EXP 308
219
#define DBL_MAX_EXP 1024
220
#define FLT_RADIX 2
221
#endif /*IEEE_Arith*/
222

    
223
#ifdef IBM
224
#define DBL_DIG 16
225
#define DBL_MAX_10_EXP 75
226
#define DBL_MAX_EXP 63
227
#define FLT_RADIX 16
228
#define DBL_MAX 7.2370055773322621e+75
229
#endif
230

    
231
#ifdef VAX
232
#define DBL_DIG 16
233
#define DBL_MAX_10_EXP 38
234
#define DBL_MAX_EXP 127
235
#define FLT_RADIX 2
236
#define DBL_MAX 1.7014118346046923e+38
237
#endif
238

    
239
#ifndef LONG_MAX
240
#define LONG_MAX 2147483647
241
#endif
242

    
243
#else /* ifndef Bad_float_h */
244
#include "float.h"
245
#endif /* Bad_float_h */
246

    
247
#ifndef __MATH_H__
248
#include "math.h"
249
#endif
250

    
251
#ifdef __cplusplus
252
extern "C" {
253
#endif
254

    
255
#ifndef CONST
256
#ifdef KR_headers
257
#define CONST /* blank */
258
#else
259
#define CONST const
260
#endif
261
#endif
262

    
263
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
264
Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
265
#endif
266

    
267
typedef union { double d; ULong L[2]; } U;
268

    
269
#ifdef YES_ALIAS
270
#define dval(x) x
271
#ifdef IEEE_8087
272
#define word0(x) ((ULong *)&x)[1]
273
#define word1(x) ((ULong *)&x)[0]
274
#else
275
#define word0(x) ((ULong *)&x)[0]
276
#define word1(x) ((ULong *)&x)[1]
277
#endif
278
#else
279
#ifdef IEEE_8087
280
#define word0(x) ((U*)&x)->L[1]
281
#define word1(x) ((U*)&x)->L[0]
282
#else
283
#define word0(x) ((U*)&x)->L[0]
284
#define word1(x) ((U*)&x)->L[1]
285
#endif
286
#define dval(x) ((U*)&x)->d
287
#endif
288

    
289
/* The following definition of Storeinc is appropriate for MIPS processors.
290
 * An alternative that might be better on some machines is
291
 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
292
 */
293
#if defined(IEEE_8087) + defined(VAX)
294
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
295
((unsigned short *)a)[0] = (unsigned short)c, a++)
296
#else
297
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
298
((unsigned short *)a)[1] = (unsigned short)c, a++)
299
#endif
300

    
301
/* #define P DBL_MANT_DIG */
302
/* Ten_pmax = floor(P*log(2)/log(5)) */
303
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
304
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
305
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
306

    
307
#ifdef IEEE_Arith
308
#define Exp_shift  20
309
#define Exp_shift1 20
310
#define Exp_msk1    0x100000
311
#define Exp_msk11   0x100000
312
#define Exp_mask  0x7ff00000
313
#define P 53
314
#define Bias 1023
315
#define Emin (-1022)
316
#define Exp_1  0x3ff00000
317
#define Exp_11 0x3ff00000
318
#define Ebits 11
319
#define Frac_mask  0xfffff
320
#define Frac_mask1 0xfffff
321
#define Ten_pmax 22
322
#define Bletch 0x10
323
#define Bndry_mask  0xfffff
324
#define Bndry_mask1 0xfffff
325
#define LSB 1
326
#define Sign_bit 0x80000000
327
#define Log2P 1
328
#define Tiny0 0
329
#define Tiny1 1
330
#define Quick_max 14
331
#define Int_max 14
332
#ifndef NO_IEEE_Scale
333
#define Avoid_Underflow
334
#ifdef Flush_Denorm        /* debugging option */
335
#undef Sudden_Underflow
336
#endif
337
#endif
338

    
339
#ifndef Flt_Rounds
340
#ifdef FLT_ROUNDS
341
#define Flt_Rounds FLT_ROUNDS
342
#else
343
#define Flt_Rounds 1
344
#endif
345
#endif /*Flt_Rounds*/
346

    
347
#ifdef Honor_FLT_ROUNDS
348
#define Rounding rounding
349
#undef Check_FLT_ROUNDS
350
#define Check_FLT_ROUNDS
351
#else
352
#define Rounding Flt_Rounds
353
#endif
354

    
355
#else /* ifndef IEEE_Arith */
356
#undef Check_FLT_ROUNDS
357
#undef Honor_FLT_ROUNDS
358
#undef SET_INEXACT
359
#undef  Sudden_Underflow
360
#define Sudden_Underflow
361
#ifdef IBM
362
#undef Flt_Rounds
363
#define Flt_Rounds 0
364
#define Exp_shift  24
365
#define Exp_shift1 24
366
#define Exp_msk1   0x1000000
367
#define Exp_msk11  0x1000000
368
#define Exp_mask  0x7f000000
369
#define P 14
370
#define Bias 65
371
#define Exp_1  0x41000000
372
#define Exp_11 0x41000000
373
#define Ebits 8        /* exponent has 7 bits, but 8 is the right value in b2d */
374
#define Frac_mask  0xffffff
375
#define Frac_mask1 0xffffff
376
#define Bletch 4
377
#define Ten_pmax 22
378
#define Bndry_mask  0xefffff
379
#define Bndry_mask1 0xffffff
380
#define LSB 1
381
#define Sign_bit 0x80000000
382
#define Log2P 4
383
#define Tiny0 0x100000
384
#define Tiny1 0
385
#define Quick_max 14
386
#define Int_max 15
387
#else /* VAX */
388
#undef Flt_Rounds
389
#define Flt_Rounds 1
390
#define Exp_shift  23
391
#define Exp_shift1 7
392
#define Exp_msk1    0x80
393
#define Exp_msk11   0x800000
394
#define Exp_mask  0x7f80
395
#define P 56
396
#define Bias 129
397
#define Exp_1  0x40800000
398
#define Exp_11 0x4080
399
#define Ebits 8
400
#define Frac_mask  0x7fffff
401
#define Frac_mask1 0xffff007f
402
#define Ten_pmax 24
403
#define Bletch 2
404
#define Bndry_mask  0xffff007f
405
#define Bndry_mask1 0xffff007f
406
#define LSB 0x10000
407
#define Sign_bit 0x8000
408
#define Log2P 1
409
#define Tiny0 0x80
410
#define Tiny1 0
411
#define Quick_max 15
412
#define Int_max 15
413
#endif /* IBM, VAX */
414
#endif /* IEEE_Arith */
415

    
416
#ifndef IEEE_Arith
417
#define ROUND_BIASED
418
#endif
419

    
420
#ifdef RND_PRODQUOT
421
#define rounded_product(a,b) a = rnd_prod(a, b)
422
#define rounded_quotient(a,b) a = rnd_quot(a, b)
423
#ifdef KR_headers
424
extern double rnd_prod(), rnd_quot();
425
#else
426
extern double rnd_prod(double, double), rnd_quot(double, double);
427
#endif
428
#else
429
#define rounded_product(a,b) a *= b
430
#define rounded_quotient(a,b) a /= b
431
#endif
432

    
433
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
434
#define Big1 0xffffffff
435

    
436
#ifndef Pack_32
437
#define Pack_32
438
#endif
439

    
440
#ifdef KR_headers
441
#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
442
#else
443
#define FFFFFFFF 0xffffffffUL
444
#endif
445

    
446
#ifdef NO_LONG_LONG
447
#undef ULLong
448
#ifdef Just_16
449
#undef Pack_32
450
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
451
 * This makes some inner loops simpler and sometimes saves work
452
 * during multiplications, but it often seems to make things slightly
453
 * slower.  Hence the default is now to store 32 bits per Long.
454
 */
455
#endif
456
#else        /* long long available */
457
#ifndef Llong
458
#define Llong long long
459
#endif
460
#ifndef ULLong
461
#define ULLong unsigned Llong
462
#endif
463
#endif /* NO_LONG_LONG */
464

    
465
#ifndef MULTIPLE_THREADS
466
#define ACQUIRE_DTOA_LOCK(n)        /*nothing*/
467
#define FREE_DTOA_LOCK(n)        /*nothing*/
468
#endif
469

    
470
#define Kmax 15
471

    
472
#ifdef __cplusplus
473
extern "C" double strtod(const char *s00, char **se);
474
extern "C" char *dtoa(double d, int mode, int ndigits,
475
                        int *decpt, int *sign, char **rve);
476
#endif
477

    
478
 struct
479
Bigint {
480
        struct Bigint *next;
481
        int k, maxwds, sign, wds;
482
        ULong x[1];
483
        };
484

    
485
 typedef struct Bigint Bigint;
486

    
487
 static Bigint *freelist[Kmax+1];
488

    
489
 static Bigint *
490
Balloc
491
#ifdef KR_headers
492
        (k) int k;
493
#else
494
        (int k)
495
#endif
496
{
497
        int x;
498
        Bigint *rv;
499
#ifndef Omit_Private_Memory
500
        unsigned int len;
501
#endif
502

    
503
        ACQUIRE_DTOA_LOCK(0);
504
        if ((rv = freelist[k])) {
505
                freelist[k] = rv->next;
506
                }
507
        else {
508
                x = 1 << k;
509
#ifdef Omit_Private_Memory
510
                rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
511
#else
512
                len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
513
                        /sizeof(double);
514
                if (pmem_next - private_mem + len <= PRIVATE_mem) {
515
                        rv = (Bigint*)pmem_next;
516
                        pmem_next += len;
517
                        }
518
                else
519
                        rv = (Bigint*)MALLOC(len*sizeof(double));
520
#endif
521
                rv->k = k;
522
                rv->maxwds = x;
523
                }
524
        FREE_DTOA_LOCK(0);
525
        rv->sign = rv->wds = 0;
526
        return rv;
527
        }
528

    
529
 static void
530
Bfree
531
#ifdef KR_headers
532
        (v) Bigint *v;
533
#else
534
        (Bigint *v)
535
#endif
536
{
537
        if (v) {
538
                ACQUIRE_DTOA_LOCK(0);
539
                v->next = freelist[v->k];
540
                freelist[v->k] = v;
541
                FREE_DTOA_LOCK(0);
542
                }
543
        }
544

    
545
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
546
y->wds*sizeof(Long) + 2*sizeof(int))
547

    
548
 static Bigint *
549
multadd
550
#ifdef KR_headers
551
        (b, m, a) Bigint *b; int m, a;
552
#else
553
        (Bigint *b, int m, int a)        /* multiply by m and add a */
554
#endif
555
{
556
        int i, wds;
557
#ifdef ULLong
558
        ULong *x;
559
        ULLong carry, y;
560
#else
561
        ULong carry, *x, y;
562
#ifdef Pack_32
563
        ULong xi, z;
564
#endif
565
#endif
566
        Bigint *b1;
567

    
568
        wds = b->wds;
569
        x = b->x;
570
        i = 0;
571
        carry = a;
572
        do {
573
#ifdef ULLong
574
                y = *x * (ULLong)m + carry;
575
                carry = y >> 32;
576
                *x++ = y & FFFFFFFF;
577
#else
578
#ifdef Pack_32
579
                xi = *x;
580
                y = (xi & 0xffff) * m + carry;
581
                z = (xi >> 16) * m + (y >> 16);
582
                carry = z >> 16;
583
                *x++ = (z << 16) + (y & 0xffff);
584
#else
585
                y = *x * m + carry;
586
                carry = y >> 16;
587
                *x++ = y & 0xffff;
588
#endif
589
#endif
590
                }
591
                while(++i < wds);
592
        if (carry) {
593
                if (wds >= b->maxwds) {
594
                        b1 = Balloc(b->k+1);
595
                        Bcopy(b1, b);
596
                        Bfree(b);
597
                        b = b1;
598
                        }
599
                b->x[wds++] = carry;
600
                b->wds = wds;
601
                }
602
        return b;
603
        }
604

    
605
 static Bigint *
606
s2b
607
#ifdef KR_headers
608
        (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
609
#else
610
        (CONST char *s, int nd0, int nd, ULong y9)
611
#endif
612
{
613
        Bigint *b;
614
        int i, k;
615
        Long x, y;
616

    
617
        x = (nd + 8) / 9;
618
        for(k = 0, y = 1; x > y; y <<= 1, k++) ;
619
#ifdef Pack_32
620
        b = Balloc(k);
621
        b->x[0] = y9;
622
        b->wds = 1;
623
#else
624
        b = Balloc(k+1);
625
        b->x[0] = y9 & 0xffff;
626
        b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
627
#endif
628

    
629
        i = 9;
630
        if (9 < nd0) {
631
                s += 9;
632
                do b = multadd(b, 10, *s++ - '0');
633
                        while(++i < nd0);
634
                s++;
635
                }
636
        else
637
                s += 10;
638
        for(; i < nd; i++)
639
                b = multadd(b, 10, *s++ - '0');
640
        return b;
641
        }
642

    
643
 static int
644
hi0bits
645
#ifdef KR_headers
646
        (x) register ULong x;
647
#else
648
        (register ULong x)
649
#endif
650
{
651
        register int k = 0;
652

    
653
        if (!(x & 0xffff0000)) {
654
                k = 16;
655
                x <<= 16;
656
                }
657
        if (!(x & 0xff000000)) {
658
                k += 8;
659
                x <<= 8;
660
                }
661
        if (!(x & 0xf0000000)) {
662
                k += 4;
663
                x <<= 4;
664
                }
665
        if (!(x & 0xc0000000)) {
666
                k += 2;
667
                x <<= 2;
668
                }
669
        if (!(x & 0x80000000)) {
670
                k++;
671
                if (!(x & 0x40000000))
672
                        return 32;
673
                }
674
        return k;
675
        }
676

    
677
 static int
678
lo0bits
679
#ifdef KR_headers
680
        (y) ULong *y;
681
#else
682
        (ULong *y)
683
#endif
684
{
685
        register int k;
686
        register ULong x = *y;
687

    
688
        if (x & 7) {
689
                if (x & 1)
690
                        return 0;
691
                if (x & 2) {
692
                        *y = x >> 1;
693
                        return 1;
694
                        }
695
                *y = x >> 2;
696
                return 2;
697
                }
698
        k = 0;
699
        if (!(x & 0xffff)) {
700
                k = 16;
701
                x >>= 16;
702
                }
703
        if (!(x & 0xff)) {
704
                k += 8;
705
                x >>= 8;
706
                }
707
        if (!(x & 0xf)) {
708
                k += 4;
709
                x >>= 4;
710
                }
711
        if (!(x & 0x3)) {
712
                k += 2;
713
                x >>= 2;
714
                }
715
        if (!(x & 1)) {
716
                k++;
717
                x >>= 1;
718
                if (!x)
719
                        return 32;
720
                }
721
        *y = x;
722
        return k;
723
        }
724

    
725
 static Bigint *
726
i2b
727
#ifdef KR_headers
728
        (i) int i;
729
#else
730
        (int i)
731
#endif
732
{
733
        Bigint *b;
734

    
735
        b = Balloc(1);
736
        b->x[0] = i;
737
        b->wds = 1;
738
        return b;
739
        }
740

    
741
 static Bigint *
742
mult
743
#ifdef KR_headers
744
        (a, b) Bigint *a, *b;
745
#else
746
        (Bigint *a, Bigint *b)
747
#endif
748
{
749
        Bigint *c;
750
        int k, wa, wb, wc;
751
        ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
752
        ULong y;
753
#ifdef ULLong
754
        ULLong carry, z;
755
#else
756
        ULong carry, z;
757
#ifdef Pack_32
758
        ULong z2;
759
#endif
760
#endif
761

    
762
        if (a->wds < b->wds) {
763
                c = a;
764
                a = b;
765
                b = c;
766
                }
767
        k = a->k;
768
        wa = a->wds;
769
        wb = b->wds;
770
        wc = wa + wb;
771
        if (wc > a->maxwds)
772
                k++;
773
        c = Balloc(k);
774
        for(x = c->x, xa = x + wc; x < xa; x++)
775
                *x = 0;
776
        xa = a->x;
777
        xae = xa + wa;
778
        xb = b->x;
779
        xbe = xb + wb;
780
        xc0 = c->x;
781
#ifdef ULLong
782
        for(; xb < xbe; xc0++) {
783
                if ((y = *xb++)) {
784
                        x = xa;
785
                        xc = xc0;
786
                        carry = 0;
787
                        do {
788
                                z = *x++ * (ULLong)y + *xc + carry;
789
                                carry = z >> 32;
790
                                *xc++ = z & FFFFFFFF;
791
                                }
792
                                while(x < xae);
793
                        *xc = carry;
794
                        }
795
                }
796
#else
797
#ifdef Pack_32
798
        for(; xb < xbe; xb++, xc0++) {
799
                if (y = *xb & 0xffff) {
800
                        x = xa;
801
                        xc = xc0;
802
                        carry = 0;
803
                        do {
804
                                z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
805
                                carry = z >> 16;
806
                                z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
807
                                carry = z2 >> 16;
808
                                Storeinc(xc, z2, z);
809
                                }
810
                                while(x < xae);
811
                        *xc = carry;
812
                        }
813
                if (y = *xb >> 16) {
814
                        x = xa;
815
                        xc = xc0;
816
                        carry = 0;
817
                        z2 = *xc;
818
                        do {
819
                                z = (*x & 0xffff) * y + (*xc >> 16) + carry;
820
                                carry = z >> 16;
821
                                Storeinc(xc, z, z2);
822
                                z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
823
                                carry = z2 >> 16;
824
                                }
825
                                while(x < xae);
826
                        *xc = z2;
827
                        }
828
                }
829
#else
830
        for(; xb < xbe; xc0++) {
831
                if (y = *xb++) {
832
                        x = xa;
833
                        xc = xc0;
834
                        carry = 0;
835
                        do {
836
                                z = *x++ * y + *xc + carry;
837
                                carry = z >> 16;
838
                                *xc++ = z & 0xffff;
839
                                }
840
                                while(x < xae);
841
                        *xc = carry;
842
                        }
843
                }
844
#endif
845
#endif
846
        for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
847
        c->wds = wc;
848
        return c;
849
        }
850

    
851
 static Bigint *p5s;
852

    
853
 static Bigint *
854
pow5mult
855
#ifdef KR_headers
856
        (b, k) Bigint *b; int k;
857
#else
858
        (Bigint *b, int k)
859
#endif
860
{
861
        Bigint *b1, *p5, *p51;
862
        int i;
863
        static int p05[3] = { 5, 25, 125 };
864

    
865
        if ((i = k & 3))
866
                b = multadd(b, p05[i-1], 0);
867

    
868
        if (!(k >>= 2))
869
                return b;
870
        if (!(p5 = p5s)) {
871
                /* first time */
872
#ifdef MULTIPLE_THREADS
873
                ACQUIRE_DTOA_LOCK(1);
874
                if (!(p5 = p5s)) {
875
                        p5 = p5s = i2b(625);
876
                        p5->next = 0;
877
                        }
878
                FREE_DTOA_LOCK(1);
879
#else
880
                p5 = p5s = i2b(625);
881
                p5->next = 0;
882
#endif
883
                }
884
        for(;;) {
885
                if (k & 1) {
886
                        b1 = mult(b, p5);
887
                        Bfree(b);
888
                        b = b1;
889
                        }
890
                if (!(k >>= 1))
891
                        break;
892
                if (!(p51 = p5->next)) {
893
#ifdef MULTIPLE_THREADS
894
                        ACQUIRE_DTOA_LOCK(1);
895
                        if (!(p51 = p5->next)) {
896
                                p51 = p5->next = mult(p5,p5);
897
                                p51->next = 0;
898
                                }
899
                        FREE_DTOA_LOCK(1);
900
#else
901
                        p51 = p5->next = mult(p5,p5);
902
                        p51->next = 0;
903
#endif
904
                        }
905
                p5 = p51;
906
                }
907
        return b;
908
        }
909

    
910
 static Bigint *
911
lshift
912
#ifdef KR_headers
913
        (b, k) Bigint *b; int k;
914
#else
915
        (Bigint *b, int k)
916
#endif
917
{
918
        int i, k1, n, n1;
919
        Bigint *b1;
920
        ULong *x, *x1, *xe, z;
921

    
922
#ifdef Pack_32
923
        n = k >> 5;
924
#else
925
        n = k >> 4;
926
#endif
927
        k1 = b->k;
928
        n1 = n + b->wds + 1;
929
        for(i = b->maxwds; n1 > i; i <<= 1)
930
                k1++;
931
        b1 = Balloc(k1);
932
        x1 = b1->x;
933
        for(i = 0; i < n; i++)
934
                *x1++ = 0;
935
        x = b->x;
936
        xe = x + b->wds;
937
#ifdef Pack_32
938
        if (k &= 0x1f) {
939
                k1 = 32 - k;
940
                z = 0;
941
                do {
942
                        *x1++ = *x << k | z;
943
                        z = *x++ >> k1;
944
                        }
945
                        while(x < xe);
946
                if ((*x1 = z))
947
                        ++n1;
948
                }
949
#else
950
        if (k &= 0xf) {
951
                k1 = 16 - k;
952
                z = 0;
953
                do {
954
                        *x1++ = *x << k  & 0xffff | z;
955
                        z = *x++ >> k1;
956
                        }
957
                        while(x < xe);
958
                if (*x1 = z)
959
                        ++n1;
960
                }
961
#endif
962
        else do
963
                *x1++ = *x++;
964
                while(x < xe);
965
        b1->wds = n1 - 1;
966
        Bfree(b);
967
        return b1;
968
        }
969

    
970
 static int
971
cmp
972
#ifdef KR_headers
973
        (a, b) Bigint *a, *b;
974
#else
975
        (Bigint *a, Bigint *b)
976
#endif
977
{
978
        ULong *xa, *xa0, *xb, *xb0;
979
        int i, j;
980

    
981
        i = a->wds;
982
        j = b->wds;
983
#ifdef DEBUG
984
        if (i > 1 && !a->x[i-1])
985
                Bug("cmp called with a->x[a->wds-1] == 0");
986
        if (j > 1 && !b->x[j-1])
987
                Bug("cmp called with b->x[b->wds-1] == 0");
988
#endif
989
        if (i -= j)
990
                return i;
991
        xa0 = a->x;
992
        xa = xa0 + j;
993
        xb0 = b->x;
994
        xb = xb0 + j;
995
        for(;;) {
996
                if (*--xa != *--xb)
997
                        return *xa < *xb ? -1 : 1;
998
                if (xa <= xa0)
999
                        break;
1000
                }
1001
        return 0;
1002
        }
1003

    
1004
 static Bigint *
1005
diff
1006
#ifdef KR_headers
1007
        (a, b) Bigint *a, *b;
1008
#else
1009
        (Bigint *a, Bigint *b)
1010
#endif
1011
{
1012
        Bigint *c;
1013
        int i, wa, wb;
1014
        ULong *xa, *xae, *xb, *xbe, *xc;
1015
#ifdef ULLong
1016
        ULLong borrow, y;
1017
#else
1018
        ULong borrow, y;
1019
#ifdef Pack_32
1020
        ULong z;
1021
#endif
1022
#endif
1023

    
1024
        i = cmp(a,b);
1025
        if (!i) {
1026
                c = Balloc(0);
1027
                c->wds = 1;
1028
                c->x[0] = 0;
1029
                return c;
1030
                }
1031
        if (i < 0) {
1032
                c = a;
1033
                a = b;
1034
                b = c;
1035
                i = 1;
1036
                }
1037
        else
1038
                i = 0;
1039
        c = Balloc(a->k);
1040
        c->sign = i;
1041
        wa = a->wds;
1042
        xa = a->x;
1043
        xae = xa + wa;
1044
        wb = b->wds;
1045
        xb = b->x;
1046
        xbe = xb + wb;
1047
        xc = c->x;
1048
        borrow = 0;
1049
#ifdef ULLong
1050
        do {
1051
                y = (ULLong)*xa++ - *xb++ - borrow;
1052
                borrow = y >> 32 & (ULong)1;
1053
                *xc++ = y & FFFFFFFF;
1054
                }
1055
                while(xb < xbe);
1056
        while(xa < xae) {
1057
                y = *xa++ - borrow;
1058
                borrow = y >> 32 & (ULong)1;
1059
                *xc++ = y & FFFFFFFF;
1060
                }
1061
#else
1062
#ifdef Pack_32
1063
        do {
1064
                y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1065
                borrow = (y & 0x10000) >> 16;
1066
                z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1067
                borrow = (z & 0x10000) >> 16;
1068
                Storeinc(xc, z, y);
1069
                }
1070
                while(xb < xbe);
1071
        while(xa < xae) {
1072
                y = (*xa & 0xffff) - borrow;
1073
                borrow = (y & 0x10000) >> 16;
1074
                z = (*xa++ >> 16) - borrow;
1075
                borrow = (z & 0x10000) >> 16;
1076
                Storeinc(xc, z, y);
1077
                }
1078
#else
1079
        do {
1080
                y = *xa++ - *xb++ - borrow;
1081
                borrow = (y & 0x10000) >> 16;
1082
                *xc++ = y & 0xffff;
1083
                }
1084
                while(xb < xbe);
1085
        while(xa < xae) {
1086
                y = *xa++ - borrow;
1087
                borrow = (y & 0x10000) >> 16;
1088
                *xc++ = y & 0xffff;
1089
                }
1090
#endif
1091
#endif
1092
        while(!*--xc)
1093
                wa--;
1094
        c->wds = wa;
1095
        return c;
1096
        }
1097

    
1098
 static double
1099
ulp
1100
#ifdef KR_headers
1101
        (x) double x;
1102
#else
1103
        (double x)
1104
#endif
1105
{
1106
        register Long L;
1107
        double a;
1108

    
1109
        L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1110
#ifndef Avoid_Underflow
1111
#ifndef Sudden_Underflow
1112
        if (L > 0) {
1113
#endif
1114
#endif
1115
#ifdef IBM
1116
                L |= Exp_msk1 >> 4;
1117
#endif
1118
                word0(a) = L;
1119
                word1(a) = 0;
1120
#ifndef Avoid_Underflow
1121
#ifndef Sudden_Underflow
1122
                }
1123
        else {
1124
                L = -L >> Exp_shift;
1125
                if (L < Exp_shift) {
1126
                        word0(a) = 0x80000 >> L;
1127
                        word1(a) = 0;
1128
                        }
1129
                else {
1130
                        word0(a) = 0;
1131
                        L -= Exp_shift;
1132
                        word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1133
                        }
1134
                }
1135
#endif
1136
#endif
1137
        return dval(a);
1138
        }
1139

    
1140
 static double
1141
b2d
1142
#ifdef KR_headers
1143
        (a, e) Bigint *a; int *e;
1144
#else
1145
        (Bigint *a, int *e)
1146
#endif
1147
{
1148
        ULong *xa, *xa0, w, y, z;
1149
        int k;
1150
        double d;
1151
#ifdef VAX
1152
        ULong d0, d1;
1153
#else
1154
#define d0 word0(d)
1155
#define d1 word1(d)
1156
#endif
1157

    
1158
        xa0 = a->x;
1159
        xa = xa0 + a->wds;
1160
        y = *--xa;
1161
#ifdef DEBUG
1162
        if (!y) Bug("zero y in b2d");
1163
#endif
1164
        k = hi0bits(y);
1165
        *e = 32 - k;
1166
#ifdef Pack_32
1167
        if (k < Ebits) {
1168
                d0 = Exp_1 | (y >> (Ebits - k));
1169
                w = xa > xa0 ? *--xa : 0;
1170
                d1 = (y << ((32-Ebits) + k)) | (w >> (Ebits - k));
1171
                goto ret_d;
1172
                }
1173
        z = xa > xa0 ? *--xa : 0;
1174
        if (k -= Ebits) {
1175
                d0 = Exp_1 | (y << k) | (z >> (32 - k));
1176
                y = xa > xa0 ? *--xa : 0;
1177
                d1 = (z << k) | (y >> (32 - k));
1178
                }
1179
        else {
1180
                d0 = Exp_1 | y;
1181
                d1 = z;
1182
                }
1183
#else
1184
        if (k < Ebits + 16) {
1185
                z = xa > xa0 ? *--xa : 0;
1186
                d0 = Exp_1 | (y << (k - Ebits)) | (z >> (Ebits + 16 - k));
1187
                w = xa > xa0 ? *--xa : 0;
1188
                y = xa > xa0 ? *--xa : 0;
1189
                d1 = (z << (k + 16 - Ebits)) | (w << (k - Ebits)) | (y >> (16 + Ebits - k));
1190
                goto ret_d;
1191
                }
1192
        z = xa > xa0 ? *--xa : 0;
1193
        w = xa > xa0 ? *--xa : 0;
1194
        k -= Ebits + 16;
1195
        d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1196
        y = xa > xa0 ? *--xa : 0;
1197
        d1 = w << k + 16 | y << k;
1198
#endif
1199
 ret_d:
1200
#ifdef VAX
1201
        word0(d) = d0 >> 16 | d0 << 16;
1202
        word1(d) = d1 >> 16 | d1 << 16;
1203
#else
1204
#undef d0
1205
#undef d1
1206
#endif
1207
        return dval(d);
1208
        }
1209

    
1210
 static Bigint *
1211
d2b
1212
#ifdef KR_headers
1213
        (d, e, bits) double d; int *e, *bits;
1214
#else
1215
        (double d, int *e, int *bits)
1216
#endif
1217
{
1218
        Bigint *b;
1219
        int de, k;
1220
        ULong *x, y, z;
1221
#ifndef Sudden_Underflow
1222
        int i;
1223
#endif
1224
#ifdef VAX
1225
        ULong d0, d1;
1226
        d0 = word0(d) >> 16 | word0(d) << 16;
1227
        d1 = word1(d) >> 16 | word1(d) << 16;
1228
#else
1229
#define d0 word0(d)
1230
#define d1 word1(d)
1231
#endif
1232

    
1233
#ifdef Pack_32
1234
        b = Balloc(1);
1235
#else
1236
        b = Balloc(2);
1237
#endif
1238
        x = b->x;
1239

    
1240
        z = d0 & Frac_mask;
1241
        d0 &= 0x7fffffff;        /* clear sign bit, which we ignore */
1242
#ifdef Sudden_Underflow
1243
        de = (int)(d0 >> Exp_shift);
1244
#ifndef IBM
1245
        z |= Exp_msk11;
1246
#endif
1247
#else
1248
        if ((de = (int)(d0 >> Exp_shift)))
1249
                z |= Exp_msk1;
1250
#endif
1251
#ifdef Pack_32
1252
        if ((y = d1)) {
1253
                if ((k = lo0bits(&y))) {
1254
                        x[0] = y | (z << (32 - k));
1255
                        z >>= k;
1256
                        }
1257
                else
1258
                        x[0] = y;
1259
#ifndef Sudden_Underflow
1260
                i =
1261
#endif
1262
                    b->wds = (x[1] = z) ? 2 : 1;
1263
                }
1264
        else {
1265
               /* This assertion fails for "1e-500" and other very 
1266
                * small numbers.  It provides the right result (0) 
1267
                * though. This assert has also been removed from KJS's
1268
                * version of dtoa.c.
1269
                *
1270
                * #ifdef DEBUG
1271
                *     if (!z) Bug("zero z in b2d");
1272
                * #endif
1273
                */
1274
                k = lo0bits(&z);
1275
                x[0] = z;
1276
#ifndef Sudden_Underflow
1277
                i =
1278
#endif
1279
                    b->wds = 1;
1280
                k += 32;
1281
                }
1282
#else
1283
        if (y = d1) {
1284
                if (k = lo0bits(&y))
1285
                        if (k >= 16) {
1286
                                x[0] = y | z << 32 - k & 0xffff;
1287
                                x[1] = z >> k - 16 & 0xffff;
1288
                                x[2] = z >> k;
1289
                                i = 2;
1290
                                }
1291
                        else {
1292
                                x[0] = y & 0xffff;
1293
                                x[1] = y >> 16 | z << 16 - k & 0xffff;
1294
                                x[2] = z >> k & 0xffff;
1295
                                x[3] = z >> k+16;
1296
                                i = 3;
1297
                                }
1298
                else {
1299
                        x[0] = y & 0xffff;
1300
                        x[1] = y >> 16;
1301
                        x[2] = z & 0xffff;
1302
                        x[3] = z >> 16;
1303
                        i = 3;
1304
                        }
1305
                }
1306
        else {
1307
#ifdef DEBUG
1308
                if (!z)
1309
                        Bug("Zero passed to d2b");
1310
#endif
1311
                k = lo0bits(&z);
1312
                if (k >= 16) {
1313
                        x[0] = z;
1314
                        i = 0;
1315
                        }
1316
                else {
1317
                        x[0] = z & 0xffff;
1318
                        x[1] = z >> 16;
1319
                        i = 1;
1320
                        }
1321
                k += 32;
1322
                }
1323
        while(!x[i])
1324
                --i;
1325
        b->wds = i + 1;
1326
#endif
1327
#ifndef Sudden_Underflow
1328
        if (de) {
1329
#endif
1330
#ifdef IBM
1331
                *e = (de - Bias - (P-1) << 2) + k;
1332
                *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1333
#else
1334
                *e = de - Bias - (P-1) + k;
1335
                *bits = P - k;
1336
#endif
1337
#ifndef Sudden_Underflow
1338
                }
1339
        else {
1340
                *e = de - Bias - (P-1) + 1 + k;
1341
#ifdef Pack_32
1342
                *bits = 32*i - hi0bits(x[i-1]);
1343
#else
1344
                *bits = (i+2)*16 - hi0bits(x[i]);
1345
#endif
1346
                }
1347
#endif
1348
        return b;
1349
        }
1350
#undef d0
1351
#undef d1
1352

    
1353
 static double
1354
ratio
1355
#ifdef KR_headers
1356
        (a, b) Bigint *a, *b;
1357
#else
1358
        (Bigint *a, Bigint *b)
1359
#endif
1360
{
1361
        double da, db;
1362
        int k, ka, kb;
1363

    
1364
        dval(da) = b2d(a, &ka);
1365
        dval(db) = b2d(b, &kb);
1366
#ifdef Pack_32
1367
        k = ka - kb + 32*(a->wds - b->wds);
1368
#else
1369
        k = ka - kb + 16*(a->wds - b->wds);
1370
#endif
1371
#ifdef IBM
1372
        if (k > 0) {
1373
                word0(da) += (k >> 2)*Exp_msk1;
1374
                if (k &= 3)
1375
                        dval(da) *= 1 << k;
1376
                }
1377
        else {
1378
                k = -k;
1379
                word0(db) += (k >> 2)*Exp_msk1;
1380
                if (k &= 3)
1381
                        dval(db) *= 1 << k;
1382
                }
1383
#else
1384
        if (k > 0)
1385
                word0(da) += k*Exp_msk1;
1386
        else {
1387
                k = -k;
1388
                word0(db) += k*Exp_msk1;
1389
                }
1390
#endif
1391
        return dval(da) / dval(db);
1392
        }
1393

    
1394
 static CONST double
1395
tens[] = {
1396
                1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1397
                1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1398
                1e20, 1e21, 1e22
1399
#ifdef VAX
1400
                , 1e23, 1e24
1401
#endif
1402
                };
1403

    
1404
 static CONST double
1405
#ifdef IEEE_Arith
1406
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1407
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1408
#ifdef Avoid_Underflow
1409
                9007199254740992.*9007199254740992.e-256
1410
                /* = 2^106 * 1e-53 */
1411
#else
1412
                1e-256
1413
#endif
1414
                };
1415
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1416
/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
1417
#define Scale_Bit 0x10
1418
#define n_bigtens 5
1419
#else
1420
#ifdef IBM
1421
bigtens[] = { 1e16, 1e32, 1e64 };
1422
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1423
#define n_bigtens 3
1424
#else
1425
bigtens[] = { 1e16, 1e32 };
1426
static CONST double tinytens[] = { 1e-16, 1e-32 };
1427
#define n_bigtens 2
1428
#endif
1429
#endif
1430

    
1431
#ifndef IEEE_Arith
1432
#undef INFNAN_CHECK
1433
#endif
1434

    
1435
#ifdef INFNAN_CHECK
1436

    
1437
#ifndef NAN_WORD0
1438
#define NAN_WORD0 0x7ff80000
1439
#endif
1440

    
1441
#ifndef NAN_WORD1
1442
#define NAN_WORD1 0
1443
#endif
1444

    
1445
 static int
1446
match
1447
#ifdef KR_headers
1448
        (sp, t) char **sp, *t;
1449
#else
1450
        (CONST char **sp, char *t)
1451
#endif
1452
{
1453
        int c, d;
1454
        CONST char *s = *sp;
1455

    
1456
        while(d = *t++) {
1457
                if ((c = *++s) >= 'A' && c <= 'Z')
1458
                        c += 'a' - 'A';
1459
                if (c != d)
1460
                        return 0;
1461
                }
1462
        *sp = s + 1;
1463
        return 1;
1464
        }
1465

    
1466
#ifndef No_Hex_NaN
1467
 static void
1468
hexnan
1469
#ifdef KR_headers
1470
        (rvp, sp) double *rvp; CONST char **sp;
1471
#else
1472
        (double *rvp, CONST char **sp)
1473
#endif
1474
{
1475
        ULong c, x[2];
1476
        CONST char *s;
1477
        int havedig, udx0, xshift;
1478

    
1479
        x[0] = x[1] = 0;
1480
        havedig = xshift = 0;
1481
        udx0 = 1;
1482
        s = *sp;
1483
        while(c = *(CONST unsigned char*)++s) {
1484
                if (c >= '0' && c <= '9')
1485
                        c -= '0';
1486
                else if (c >= 'a' && c <= 'f')
1487
                        c += 10 - 'a';
1488
                else if (c >= 'A' && c <= 'F')
1489
                        c += 10 - 'A';
1490
                else if (c <= ' ') {
1491
                        if (udx0 && havedig) {
1492
                                udx0 = 0;
1493
                                xshift = 1;
1494
                                }
1495
                        continue;
1496
                        }
1497
                else if (/*(*/ c == ')' && havedig) {
1498
                        *sp = s + 1;
1499
                        break;
1500
                        }
1501
                else
1502
                        return;        /* invalid form: don't change *sp */
1503
                havedig = 1;
1504
                if (xshift) {
1505
                        xshift = 0;
1506
                        x[0] = x[1];
1507
                        x[1] = 0;
1508
                        }
1509
                if (udx0)
1510
                        x[0] = (x[0] << 4) | (x[1] >> 28);
1511
                x[1] = (x[1] << 4) | c;
1512
                }
1513
        if ((x[0] &= 0xfffff) || x[1]) {
1514
                word0(*rvp) = Exp_mask | x[0];
1515
                word1(*rvp) = x[1];
1516
                }
1517
        }
1518
#endif /*No_Hex_NaN*/
1519
#endif /* INFNAN_CHECK */
1520

    
1521
 double
1522
strtod
1523
#ifdef KR_headers
1524
        (s00, se) CONST char *s00; char **se;
1525
#else
1526
        (CONST char *s00, char **se)
1527
#endif
1528
{
1529
#ifdef Avoid_Underflow
1530
        int scale;
1531
#endif
1532
        int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1533
                 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1534
        CONST char *s, *s0, *s1;
1535
        double aadj, aadj1, adj, rv, rv0;
1536
        Long L;
1537
        ULong y, z;
1538
        Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1539
#ifdef SET_INEXACT
1540
        int inexact, oldinexact;
1541
#endif
1542
#ifdef Honor_FLT_ROUNDS
1543
        int rounding;
1544
#endif
1545
#ifdef USE_LOCALE
1546
        CONST char *s2;
1547
#endif
1548

    
1549
        sign = nz0 = nz = 0;
1550
        dval(rv) = 0.;
1551
        for(s = s00;;s++) switch(*s) {
1552
                case '-':
1553
                        sign = 1;
1554
                        /* no break */
1555
                case '+':
1556
                        if (*++s)
1557
                                goto break2;
1558
                        /* no break */
1559
                case 0:
1560
                        goto ret0;
1561
                case '\t':
1562
                case '\n':
1563
                case '\v':
1564
                case '\f':
1565
                case '\r':
1566
                case ' ':
1567
                        continue;
1568
                default:
1569
                        goto break2;
1570
                }
1571
 break2:
1572
        if (*s == '0') {
1573
                nz0 = 1;
1574
                while(*++s == '0') ;
1575
                if (!*s)
1576
                        goto ret;
1577
                }
1578
        s0 = s;
1579
        y = z = 0;
1580
        for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1581
                if (nd < 9)
1582
                        y = 10*y + c - '0';
1583
                else if (nd < 16)
1584
                        z = 10*z + c - '0';
1585
        nd0 = nd;
1586
#ifdef USE_LOCALE
1587
        s1 = localeconv()->decimal_point;
1588
        if (c == *s1) {
1589
                c = '.';
1590
                if (*++s1) {
1591
                        s2 = s;
1592
                        for(;;) {
1593
                                if (*++s2 != *s1) {
1594
                                        c = 0;
1595
                                        break;
1596
                                        }
1597
                                if (!*++s1) {
1598
                                        s = s2;
1599
                                        break;
1600
                                        }
1601
                                }
1602
                        }
1603
                }
1604
#endif
1605
        if (c == '.') {
1606
                c = *++s;
1607
                if (!nd) {
1608
                        for(; c == '0'; c = *++s)
1609
                                nz++;
1610
                        if (c > '0' && c <= '9') {
1611
                                s0 = s;
1612
                                nf += nz;
1613
                                nz = 0;
1614
                                goto have_dig;
1615
                                }
1616
                        goto dig_done;
1617
                        }
1618
                for(; c >= '0' && c <= '9'; c = *++s) {
1619
 have_dig:
1620
                        nz++;
1621
                        if (c -= '0') {
1622
                                nf += nz;
1623
                                for(i = 1; i < nz; i++)
1624
                                        if (nd++ < 9)
1625
                                                y *= 10;
1626
                                        else if (nd <= DBL_DIG + 1)
1627
                                                z *= 10;
1628
                                if (nd++ < 9)
1629
                                        y = 10*y + c;
1630
                                else if (nd <= DBL_DIG + 1)
1631
                                        z = 10*z + c;
1632
                                nz = 0;
1633
                                }
1634
                        }
1635
                }
1636
 dig_done:
1637
        e = 0;
1638
        if (c == 'e' || c == 'E') {
1639
                if (!nd && !nz && !nz0) {
1640
                        goto ret0;
1641
                        }
1642
                s00 = s;
1643
                esign = 0;
1644
                switch(c = *++s) {
1645
                        case '-':
1646
                                esign = 1;
1647
                        case '+':
1648
                                c = *++s;
1649
                        }
1650
                if (c >= '0' && c <= '9') {
1651
                        while(c == '0')
1652
                                c = *++s;
1653
                        if (c > '0' && c <= '9') {
1654
                                L = c - '0';
1655
                                s1 = s;
1656
                                while((c = *++s) >= '0' && c <= '9')
1657
                                        L = 10*L + c - '0';
1658
                                if (s - s1 > 8 || L > 19999)
1659
                                        /* Avoid confusion from exponents
1660
                                         * so large that e might overflow.
1661
                                         */
1662
                                        e = 19999; /* safe for 16 bit ints */
1663
                                else
1664
                                        e = (int)L;
1665
                                if (esign)
1666
                                        e = -e;
1667
                                }
1668
                        else
1669
                                e = 0;
1670
                        }
1671
                else
1672
                        s = s00;
1673
                }
1674
        if (!nd) {
1675
                if (!nz && !nz0) {
1676
#ifdef INFNAN_CHECK
1677
                        /* Check for Nan and Infinity */
1678
                        switch(c) {
1679
                          case 'i':
1680
                          case 'I':
1681
                                if (match(&s,"nf")) {
1682
                                        --s;
1683
                                        if (!match(&s,"inity"))
1684
                                                ++s;
1685
                                        word0(rv) = 0x7ff00000;
1686
                                        word1(rv) = 0;
1687
                                        goto ret;
1688
                                        }
1689
                                break;
1690
                          case 'n':
1691
                          case 'N':
1692
                                if (match(&s, "an")) {
1693
                                        word0(rv) = NAN_WORD0;
1694
                                        word1(rv) = NAN_WORD1;
1695
#ifndef No_Hex_NaN
1696
                                        if (*s == '(') /*)*/
1697
                                                hexnan(&rv, &s);
1698
#endif
1699
                                        goto ret;
1700
                                        }
1701
                          }
1702
#endif /* INFNAN_CHECK */
1703
 ret0:
1704
                        s = s00;
1705
                        sign = 0;
1706
                        }
1707
                goto ret;
1708
                }
1709
        e1 = e -= nf;
1710

    
1711
        /* Now we have nd0 digits, starting at s0, followed by a
1712
         * decimal point, followed by nd-nd0 digits.  The number we're
1713
         * after is the integer represented by those digits times
1714
         * 10**e */
1715

    
1716
        if (!nd0)
1717
                nd0 = nd;
1718
        k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1719
        dval(rv) = y;
1720
        if (k > 9) {
1721
#ifdef SET_INEXACT
1722
                if (k > DBL_DIG)
1723
                        oldinexact = get_inexact();
1724
#endif
1725
                dval(rv) = tens[k - 9] * dval(rv) + z;
1726
                }
1727
        bd0 = 0;
1728
        if (nd <= DBL_DIG
1729
#ifndef RND_PRODQUOT
1730
#ifndef Honor_FLT_ROUNDS
1731
                && Flt_Rounds == 1
1732
#endif
1733
#endif
1734
                        ) {
1735
                if (!e)
1736
                        goto ret;
1737
                if (e > 0) {
1738
                        if (e <= Ten_pmax) {
1739
#ifdef VAX
1740
                                goto vax_ovfl_check;
1741
#else
1742
#ifdef Honor_FLT_ROUNDS
1743
                                /* round correctly FLT_ROUNDS = 2 or 3 */
1744
                                if (sign) {
1745
                                        rv = -rv;
1746
                                        sign = 0;
1747
                                        }
1748
#endif
1749
                                /* rv = */ rounded_product(dval(rv), tens[e]);
1750
                                goto ret;
1751
#endif
1752
                                }
1753
                        i = DBL_DIG - nd;
1754
                        if (e <= Ten_pmax + i) {
1755
                                /* A fancier test would sometimes let us do
1756
                                 * this for larger i values.
1757
                                 */
1758
#ifdef Honor_FLT_ROUNDS
1759
                                /* round correctly FLT_ROUNDS = 2 or 3 */
1760
                                if (sign) {
1761
                                        rv = -rv;
1762
                                        sign = 0;
1763
                                        }
1764
#endif
1765
                                e -= i;
1766
                                dval(rv) *= tens[i];
1767
#ifdef VAX
1768
                                /* VAX exponent range is so narrow we must
1769
                                 * worry about overflow here...
1770
                                 */
1771
 vax_ovfl_check:
1772
                                word0(rv) -= P*Exp_msk1;
1773
                                /* rv = */ rounded_product(dval(rv), tens[e]);
1774
                                if ((word0(rv) & Exp_mask)
1775
                                 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1776
                                        goto ovfl;
1777
                                word0(rv) += P*Exp_msk1;
1778
#else
1779
                                /* rv = */ rounded_product(dval(rv), tens[e]);
1780
#endif
1781
                                goto ret;
1782
                                }
1783
                        }
1784
#ifndef Inaccurate_Divide
1785
                else if (e >= -Ten_pmax) {
1786
#ifdef Honor_FLT_ROUNDS
1787
                        /* round correctly FLT_ROUNDS = 2 or 3 */
1788
                        if (sign) {
1789
                                rv = -rv;
1790
                                sign = 0;
1791
                                }
1792
#endif
1793
                        /* rv = */ rounded_quotient(dval(rv), tens[-e]);
1794
                        goto ret;
1795
                        }
1796
#endif
1797
                }
1798
        e1 += nd - k;
1799

    
1800
#ifdef IEEE_Arith
1801
#ifdef SET_INEXACT
1802
        inexact = 1;
1803
        if (k <= DBL_DIG)
1804
                oldinexact = get_inexact();
1805
#endif
1806
#ifdef Avoid_Underflow
1807
        scale = 0;
1808
#endif
1809
#ifdef Honor_FLT_ROUNDS
1810
        if ((rounding = Flt_Rounds) >= 2) {
1811
                if (sign)
1812
                        rounding = rounding == 2 ? 0 : 2;
1813
                else
1814
                        if (rounding != 2)
1815
                                rounding = 0;
1816
                }
1817
#endif
1818
#endif /*IEEE_Arith*/
1819

    
1820
        /* Get starting approximation = rv * 10**e1 */
1821

    
1822
        if (e1 > 0) {
1823
                if ((i = e1 & 15))
1824
                        dval(rv) *= tens[i];
1825
                if (e1 &= ~15) {
1826
                        if (e1 > DBL_MAX_10_EXP) {
1827
 ovfl:
1828
#ifndef NO_ERRNO
1829
                                errno = ERANGE;
1830
#endif
1831
                                /* Can't trust HUGE_VAL */
1832
#ifdef IEEE_Arith
1833
#ifdef Honor_FLT_ROUNDS
1834
                                switch(rounding) {
1835
                                  case 0: /* toward 0 */
1836
                                  case 3: /* toward -infinity */
1837
                                        word0(rv) = Big0;
1838
                                        word1(rv) = Big1;
1839
                                        break;
1840
                                  default:
1841
                                        word0(rv) = Exp_mask;
1842
                                        word1(rv) = 0;
1843
                                  }
1844
#else /*Honor_FLT_ROUNDS*/
1845
                                word0(rv) = Exp_mask;
1846
                                word1(rv) = 0;
1847
#endif /*Honor_FLT_ROUNDS*/
1848
#ifdef SET_INEXACT
1849
                                /* set overflow bit */
1850
                                dval(rv0) = 1e300;
1851
                                dval(rv0) *= dval(rv0);
1852
#endif
1853
#else /*IEEE_Arith*/
1854
                                word0(rv) = Big0;
1855
                                word1(rv) = Big1;
1856
#endif /*IEEE_Arith*/
1857
                                if (bd0)
1858
                                        goto retfree;
1859
                                goto ret;
1860
                                }
1861
                        e1 >>= 4;
1862
                        for(j = 0; e1 > 1; j++, e1 >>= 1)
1863
                                if (e1 & 1)
1864
                                        dval(rv) *= bigtens[j];
1865
                /* The last multiplication could overflow. */
1866
                        word0(rv) -= P*Exp_msk1;
1867
                        dval(rv) *= bigtens[j];
1868
                        if ((z = word0(rv) & Exp_mask)
1869
                         > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1870
                                goto ovfl;
1871
                        if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1872
                                /* set to largest number */
1873
                                /* (Can't trust DBL_MAX) */
1874
                                word0(rv) = Big0;
1875
                                word1(rv) = Big1;
1876
                                }
1877
                        else
1878
                                word0(rv) += P*Exp_msk1;
1879
                        }
1880
                }
1881
        else if (e1 < 0) {
1882
                e1 = -e1;
1883
                if ((i = e1 & 15))
1884
                        dval(rv) /= tens[i];
1885
                if (e1 >>= 4) {
1886
                        if (e1 >= 1 << n_bigtens)
1887
                                goto undfl;
1888
#ifdef Avoid_Underflow
1889
                        if (e1 & Scale_Bit)
1890
                                scale = 2*P;
1891
                        for(j = 0; e1 > 0; j++, e1 >>= 1)
1892
                                if (e1 & 1)
1893
                                        dval(rv) *= tinytens[j];
1894
                        if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1895
                                                >> Exp_shift)) > 0) {
1896
                                /* scaled rv is denormal; zap j low bits */
1897
                                if (j >= 32) {
1898
                                        word1(rv) = 0;
1899
                                        if (j >= 53)
1900
                                         word0(rv) = (P+2)*Exp_msk1;
1901
                                        else
1902
                                         word0(rv) &= 0xffffffff << (j-32);
1903
                                        }
1904
                                else
1905
                                        word1(rv) &= 0xffffffff << j;
1906
                                }
1907
#else
1908
                        for(j = 0; e1 > 1; j++, e1 >>= 1)
1909
                                if (e1 & 1)
1910
                                        dval(rv) *= tinytens[j];
1911
                        /* The last multiplication could underflow. */
1912
                        dval(rv0) = dval(rv);
1913
                        dval(rv) *= tinytens[j];
1914
                        if (!dval(rv)) {
1915
                                dval(rv) = 2.*dval(rv0);
1916
                                dval(rv) *= tinytens[j];
1917
#endif
1918
                                if (!dval(rv)) {
1919
 undfl:
1920
                                        dval(rv) = 0.;
1921
#ifndef NO_ERRNO
1922
                                        errno = ERANGE;
1923
#endif
1924
                                        if (bd0)
1925
                                                goto retfree;
1926
                                        goto ret;
1927
                                        }
1928
#ifndef Avoid_Underflow
1929
                                word0(rv) = Tiny0;
1930
                                word1(rv) = Tiny1;
1931
                                /* The refinement below will clean
1932
                                 * this approximation up.
1933
                                 */
1934
                                }
1935
#endif
1936
                        }
1937
                }
1938

    
1939
        /* Now the hard part -- adjusting rv to the correct value.*/
1940

    
1941
        /* Put digits into bd: true value = bd * 10^e */
1942

    
1943
        bd0 = s2b(s0, nd0, nd, y);
1944

    
1945
        for(;;) {
1946
                bd = Balloc(bd0->k);
1947
                Bcopy(bd, bd0);
1948
                bb = d2b(dval(rv), &bbe, &bbbits);        /* rv = bb * 2^bbe */
1949
                bs = i2b(1);
1950

    
1951
                if (e >= 0) {
1952
                        bb2 = bb5 = 0;
1953
                        bd2 = bd5 = e;
1954
                        }
1955
                else {
1956
                        bb2 = bb5 = -e;
1957
                        bd2 = bd5 = 0;
1958
                        }
1959
                if (bbe >= 0)
1960
                        bb2 += bbe;
1961
                else
1962
                        bd2 -= bbe;
1963
                bs2 = bb2;
1964
#ifdef Honor_FLT_ROUNDS
1965
                if (rounding != 1)
1966
                        bs2++;
1967
#endif
1968
#ifdef Avoid_Underflow
1969
                j = bbe - scale;
1970
                i = j + bbbits - 1;        /* logb(rv) */
1971
                if (i < Emin)        /* denormal */
1972
                        j += P - Emin;
1973
                else
1974
                        j = P + 1 - bbbits;
1975
#else /*Avoid_Underflow*/
1976
#ifdef Sudden_Underflow
1977
#ifdef IBM
1978
                j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1979
#else
1980
                j = P + 1 - bbbits;
1981
#endif
1982
#else /*Sudden_Underflow*/
1983
                j = bbe;
1984
                i = j + bbbits - 1;        /* logb(rv) */
1985
                if (i < Emin)        /* denormal */
1986
                        j += P - Emin;
1987
                else
1988
                        j = P + 1 - bbbits;
1989
#endif /*Sudden_Underflow*/
1990
#endif /*Avoid_Underflow*/
1991
                bb2 += j;
1992
                bd2 += j;
1993
#ifdef Avoid_Underflow
1994
                bd2 += scale;
1995
#endif
1996
                i = bb2 < bd2 ? bb2 : bd2;
1997
                if (i > bs2)
1998
                        i = bs2;
1999
                if (i > 0) {
2000
                        bb2 -= i;
2001
                        bd2 -= i;
2002
                        bs2 -= i;
2003
                        }
2004
                if (bb5 > 0) {
2005
                        bs = pow5mult(bs, bb5);
2006
                        bb1 = mult(bs, bb);
2007
                        Bfree(bb);
2008
                        bb = bb1;
2009
                        }
2010
                if (bb2 > 0)
2011
                        bb = lshift(bb, bb2);
2012
                if (bd5 > 0)
2013
                        bd = pow5mult(bd, bd5);
2014
                if (bd2 > 0)
2015
                        bd = lshift(bd, bd2);
2016
                if (bs2 > 0)
2017
                        bs = lshift(bs, bs2);
2018
                delta = diff(bb, bd);
2019
                dsign = delta->sign;
2020
                delta->sign = 0;
2021
                i = cmp(delta, bs);
2022
#ifdef Honor_FLT_ROUNDS
2023
                if (rounding != 1) {
2024
                        if (i < 0) {
2025
                                /* Error is less than an ulp */
2026
                                if (!delta->x[0] && delta->wds <= 1) {
2027
                                        /* exact */
2028
#ifdef SET_INEXACT
2029
                                        inexact = 0;
2030
#endif
2031
                                        break;
2032
                                        }
2033
                                if (rounding) {
2034
                                        if (dsign) {
2035
                                                adj = 1.;
2036
                                                goto apply_adj;
2037
                                                }
2038
                                        }
2039
                                else if (!dsign) {
2040
                                        adj = -1.;
2041
                                        if (!word1(rv)
2042
                                         && !(word0(rv) & Frac_mask)) {
2043
                                                y = word0(rv) & Exp_mask;
2044
#ifdef Avoid_Underflow
2045
                                                if (!scale || y > 2*P*Exp_msk1)
2046
#else
2047
                                                if (y)
2048
#endif
2049
                                                  {
2050
                                                  delta = lshift(delta,Log2P);
2051
                                                  if (cmp(delta, bs) <= 0)
2052
                                                        adj = -0.5;
2053
                                                  }
2054
                                                }
2055
 apply_adj:
2056
#ifdef Avoid_Underflow
2057
                                        if (scale && (y = word0(rv) & Exp_mask)
2058
                                                <= 2*P*Exp_msk1)
2059
                                          word0(adj) += (2*P+1)*Exp_msk1 - y;
2060
#else
2061
#ifdef Sudden_Underflow
2062
                                        if ((word0(rv) & Exp_mask) <=
2063
                                                        P*Exp_msk1) {
2064
                                                word0(rv) += P*Exp_msk1;
2065
                                                dval(rv) += adj*ulp(dval(rv));
2066
                                                word0(rv) -= P*Exp_msk1;
2067
                                                }
2068
                                        else
2069
#endif /*Sudden_Underflow*/
2070
#endif /*Avoid_Underflow*/
2071
                                        dval(rv) += adj*ulp(dval(rv));
2072
                                        }
2073
                                break;
2074
                                }
2075
                        adj = ratio(delta, bs);
2076
                        if (adj < 1.)
2077
                                adj = 1.;
2078
                        if (adj <= 0x7ffffffe) {
2079
                                /* adj = rounding ? ceil(adj) : floor(adj); */
2080
                                y = adj;
2081
                                if (y != adj) {
2082
                                        if (!((rounding>>1) ^ dsign))
2083
                                                y++;
2084
                                        adj = y;
2085
                                        }
2086
                                }
2087
#ifdef Avoid_Underflow
2088
                        if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2089
                                word0(adj) += (2*P+1)*Exp_msk1 - y;
2090
#else
2091
#ifdef Sudden_Underflow
2092
                        if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2093
                                word0(rv) += P*Exp_msk1;
2094
                                adj *= ulp(dval(rv));
2095
                                if (dsign)
2096
                                        dval(rv) += adj;
2097
                                else
2098
                                        dval(rv) -= adj;
2099
                                word0(rv) -= P*Exp_msk1;
2100
                                goto cont;
2101
                                }
2102
#endif /*Sudden_Underflow*/
2103
#endif /*Avoid_Underflow*/
2104
                        adj *= ulp(dval(rv));
2105
                        if (dsign)
2106
                                dval(rv) += adj;
2107
                        else
2108
                                dval(rv) -= adj;
2109
                        goto cont;
2110
                        }
2111
#endif /*Honor_FLT_ROUNDS*/
2112

    
2113
                if (i < 0) {
2114
                        /* Error is less than half an ulp -- check for
2115
                         * special case of mantissa a power of two.
2116
                         */
2117
                        if (dsign || word1(rv) || word0(rv) & Bndry_mask
2118
#ifdef IEEE_Arith
2119
#ifdef Avoid_Underflow
2120
                         || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2121
#else
2122
                         || (word0(rv) & Exp_mask) <= Exp_msk1
2123
#endif
2124
#endif
2125
                                ) {
2126
#ifdef SET_INEXACT
2127
                                if (!delta->x[0] && delta->wds <= 1)
2128
                                        inexact = 0;
2129
#endif
2130
                                break;
2131
                                }
2132
                        if (!delta->x[0] && delta->wds <= 1) {
2133
                                /* exact result */
2134
#ifdef SET_INEXACT
2135
                                inexact = 0;
2136
#endif
2137
                                break;
2138
                                }
2139
                        delta = lshift(delta,Log2P);
2140
                        if (cmp(delta, bs) > 0)
2141
                                goto drop_down;
2142
                        break;
2143
                        }
2144
                if (i == 0) {
2145
                        /* exactly half-way between */
2146
                        if (dsign) {
2147
                                if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2148
                                 &&  word1(rv) == (
2149
#ifdef Avoid_Underflow
2150
                        (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2151
                ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2152
#endif
2153
                                                   0xffffffff)) {
2154
                                        /*boundary case -- increment exponent*/
2155
                                        word0(rv) = (word0(rv) & Exp_mask)
2156
                                                + Exp_msk1
2157
#ifdef IBM
2158
                                                | Exp_msk1 >> 4
2159
#endif
2160
                                                ;
2161
                                        word1(rv) = 0;
2162
#ifdef Avoid_Underflow
2163
                                        dsign = 0;
2164
#endif
2165
                                        break;
2166
                                        }
2167
                                }
2168
                        else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2169
 drop_down:
2170
                                /* boundary case -- decrement exponent */
2171
#ifdef Sudden_Underflow /*{{*/
2172
                                L = word0(rv) & Exp_mask;
2173
#ifdef IBM
2174
                                if (L <  Exp_msk1)
2175
#else
2176
#ifdef Avoid_Underflow
2177
                                if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2178
#else
2179
                                if (L <= Exp_msk1)
2180
#endif /*Avoid_Underflow*/
2181
#endif /*IBM*/
2182
                                        goto undfl;
2183
                                L -= Exp_msk1;
2184
#else /*Sudden_Underflow}{*/
2185
#ifdef Avoid_Underflow
2186
                                if (scale) {
2187
                                        L = word0(rv) & Exp_mask;
2188
                                        if (L <= (2*P+1)*Exp_msk1) {
2189
                                                if (L > (P+2)*Exp_msk1)
2190
                                                        /* round even ==> */
2191
                                                        /* accept rv */
2192
                                                        break;
2193
                                                /* rv = smallest denormal */
2194
                                                goto undfl;
2195
                                                }
2196
                                        }
2197
#endif /*Avoid_Underflow*/
2198
                                L = (word0(rv) & Exp_mask) - Exp_msk1;
2199
#endif /*Sudden_Underflow}}*/
2200
                                word0(rv) = L | Bndry_mask1;
2201
                                word1(rv) = 0xffffffff;
2202
#ifdef IBM
2203
                                goto cont;
2204
#else
2205
                                break;
2206
#endif
2207
                                }
2208
#ifndef ROUND_BIASED
2209
                        if (!(word1(rv) & LSB))
2210
                                break;
2211
#endif
2212
                        if (dsign)
2213
                                dval(rv) += ulp(dval(rv));
2214
#ifndef ROUND_BIASED
2215
                        else {
2216
                                dval(rv) -= ulp(dval(rv));
2217
#ifndef Sudden_Underflow
2218
                                if (!dval(rv))
2219
                                        goto undfl;
2220
#endif
2221
                                }
2222
#ifdef Avoid_Underflow
2223
                        dsign = 1 - dsign;
2224
#endif
2225
#endif
2226
                        break;
2227
                        }
2228
                if ((aadj = ratio(delta, bs)) <= 2.) {
2229
                        if (dsign)
2230
                                aadj = aadj1 = 1.;
2231
                        else if (word1(rv) || word0(rv) & Bndry_mask) {
2232
#ifndef Sudden_Underflow
2233
                                if (word1(rv) == Tiny1 && !word0(rv))
2234
                                        goto undfl;
2235
#endif
2236
                                aadj = 1.;
2237
                                aadj1 = -1.;
2238
                                }
2239
                        else {
2240
                                /* special case -- power of FLT_RADIX to be */
2241
                                /* rounded down... */
2242

    
2243
                                if (aadj < 2./FLT_RADIX)
2244
                                        aadj = 1./FLT_RADIX;
2245
                                else
2246
                                        aadj *= 0.5;
2247
                                aadj1 = -aadj;
2248
                                }
2249
                        }
2250
                else {
2251
                        aadj *= 0.5;
2252
                        aadj1 = dsign ? aadj : -aadj;
2253
#ifdef Check_FLT_ROUNDS
2254
                        switch(Rounding) {
2255
                                case 2: /* towards +infinity */
2256
                                        aadj1 -= 0.5;
2257
                                        break;
2258
                                case 0: /* towards 0 */
2259
                                case 3: /* towards -infinity */
2260
                                        aadj1 += 0.5;
2261
                                }
2262
#else
2263
                        if (Flt_Rounds == 0)
2264
                                aadj1 += 0.5;
2265
#endif /*Check_FLT_ROUNDS*/
2266
                        }
2267
                y = word0(rv) & Exp_mask;
2268

    
2269
                /* Check for overflow */
2270

    
2271
                if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2272
                        dval(rv0) = dval(rv);
2273
                        word0(rv) -= P*Exp_msk1;
2274
                        adj = aadj1 * ulp(dval(rv));
2275
                        dval(rv) += adj;
2276
                        if ((word0(rv) & Exp_mask) >=
2277
                                        Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2278
                                if (word0(rv0) == Big0 && word1(rv0) == Big1)
2279
                                        goto ovfl;
2280
                                word0(rv) = Big0;
2281
                                word1(rv) = Big1;
2282
                                goto cont;
2283
                                }
2284
                        else
2285
                                word0(rv) += P*Exp_msk1;
2286
                        }
2287
                else {
2288
#ifdef Avoid_Underflow
2289
                        if (scale && y <= 2*P*Exp_msk1) {
2290
                                if (aadj <= 0x7fffffff) {
2291
                                        if ((z = aadj) <= 0)
2292
                                                z = 1;
2293
                                        aadj = z;
2294
                                        aadj1 = dsign ? aadj : -aadj;
2295
                                        }
2296
                                word0(aadj1) += (2*P+1)*Exp_msk1 - y;
2297
                                }
2298
                        adj = aadj1 * ulp(dval(rv));
2299
                        dval(rv) += adj;
2300
#else
2301
#ifdef Sudden_Underflow
2302
                        if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2303
                                dval(rv0) = dval(rv);
2304
                                word0(rv) += P*Exp_msk1;
2305
                                adj = aadj1 * ulp(dval(rv));
2306
                                dval(rv) += adj;
2307
#ifdef IBM
2308
                                if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
2309
#else
2310
                                if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2311
#endif
2312
                                        {
2313
                                        if (word0(rv0) == Tiny0
2314
                                         && word1(rv0) == Tiny1)
2315
                                                goto undfl;
2316
                                        word0(rv) = Tiny0;
2317
                                        word1(rv) = Tiny1;
2318
                                        goto cont;
2319
                                        }
2320
                                else
2321
                                        word0(rv) -= P*Exp_msk1;
2322
                                }
2323
                        else {
2324
                                adj = aadj1 * ulp(dval(rv));
2325
                                dval(rv) += adj;
2326
                                }
2327
#else /*Sudden_Underflow*/
2328
                        /* Compute adj so that the IEEE rounding rules will
2329
                         * correctly round rv + adj in some half-way cases.
2330
                         * If rv * ulp(rv) is denormalized (i.e.,
2331
                         * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2332
                         * trouble from bits lost to denormalization;
2333
                         * example: 1.2e-307 .
2334
                         */
2335
                        if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2336
                                aadj1 = (double)(int)(aadj + 0.5);
2337
                                if (!dsign)
2338
                                        aadj1 = -aadj1;
2339
                                }
2340
                        adj = aadj1 * ulp(dval(rv));
2341
                        dval(rv) += adj;
2342
#endif /*Sudden_Underflow*/
2343
#endif /*Avoid_Underflow*/
2344
                        }
2345
                z = word0(rv) & Exp_mask;
2346
#ifndef SET_INEXACT
2347
#ifdef Avoid_Underflow
2348
                if (!scale)
2349
#endif
2350
                if (y == z) {
2351
                        /* Can we stop now? */
2352
                        L = (Long)aadj;
2353
                        aadj -= L;
2354
                        /* The tolerances below are conservative. */
2355
                        if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2356
                                if (aadj < .4999999 || aadj > .5000001)
2357
                                        break;
2358
                                }
2359
                        else if (aadj < .4999999/FLT_RADIX)
2360
                                break;
2361
                        }
2362
#endif
2363
 cont:
2364
                Bfree(bb);
2365
                Bfree(bd);
2366
                Bfree(bs);
2367
                Bfree(delta);
2368
                }
2369
#ifdef SET_INEXACT
2370
        if (inexact) {
2371
                if (!oldinexact) {
2372
                        word0(rv0) = Exp_1 + (70 << Exp_shift);
2373
                        word1(rv0) = 0;
2374
                        dval(rv0) += 1.;
2375
                        }
2376
                }
2377
        else if (!oldinexact)
2378
                clear_inexact();
2379
#endif
2380
#ifdef Avoid_Underflow
2381
        if (scale) {
2382
                word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2383
                word1(rv0) = 0;
2384
                dval(rv) *= dval(rv0);
2385
#ifndef NO_ERRNO
2386
                /* try to avoid the bug of testing an 8087 register value */
2387
                if (word0(rv) == 0 && word1(rv) == 0)
2388
                        errno = ERANGE;
2389
#endif
2390
                }
2391
#endif /* Avoid_Underflow */
2392
#ifdef SET_INEXACT
2393
        if (inexact && !(word0(rv) & Exp_mask)) {
2394
                /* set underflow bit */
2395
                dval(rv0) = 1e-300;
2396
                dval(rv0) *= dval(rv0);
2397
                }
2398
#endif
2399
 retfree:
2400
        Bfree(bb);
2401
        Bfree(bd);
2402
        Bfree(bs);
2403
        Bfree(bd0);
2404
        Bfree(delta);
2405
 ret:
2406
        if (se)
2407
                *se = (char *)s;
2408
        return sign ? -dval(rv) : dval(rv);
2409
        }
2410

    
2411
 static int
2412
quorem
2413
#ifdef KR_headers
2414
        (b, S) Bigint *b, *S;
2415
#else
2416
        (Bigint *b, Bigint *S)
2417
#endif
2418
{
2419
        int n;
2420
        ULong *bx, *bxe, q, *sx, *sxe;
2421
#ifdef ULLong
2422
        ULLong borrow, carry, y, ys;
2423
#else
2424
        ULong borrow, carry, y, ys;
2425
#ifdef Pack_32
2426
        ULong si, z, zs;
2427
#endif
2428
#endif
2429

    
2430
        n = S->wds;
2431
#ifdef DEBUG
2432
        /*debug*/ if (b->wds > n)
2433
        /*debug*/        Bug("oversize b in quorem");
2434
#endif
2435
        if (b->wds < n)
2436
                return 0;
2437
        sx = S->x;
2438
        sxe = sx + --n;
2439
        bx = b->x;
2440
        bxe = bx + n;
2441
        q = *bxe / (*sxe + 1);        /* ensure q <= true quotient */
2442
#ifdef DEBUG
2443
        /*debug*/ if (q > 9)
2444
        /*debug*/        Bug("oversized quotient in quorem");
2445
#endif
2446
        if (q) {
2447
                borrow = 0;
2448
                carry = 0;
2449
                do {
2450
#ifdef ULLong
2451
                        ys = *sx++ * (ULLong)q + carry;
2452
                        carry = ys >> 32;
2453
                        y = *bx - (ys & FFFFFFFF) - borrow;
2454
                        borrow = y >> 32 & (ULong)1;
2455
                        *bx++ = y & FFFFFFFF;
2456
#else
2457
#ifdef Pack_32
2458
                        si = *sx++;
2459
                        ys = (si & 0xffff) * q + carry;
2460
                        zs = (si >> 16) * q + (ys >> 16);
2461
                        carry = zs >> 16;
2462
                        y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2463
                        borrow = (y & 0x10000) >> 16;
2464
                        z = (*bx >> 16) - (zs & 0xffff) - borrow;
2465
                        borrow = (z & 0x10000) >> 16;
2466
                        Storeinc(bx, z, y);
2467
#else
2468
                        ys = *sx++ * q + carry;
2469
                        carry = ys >> 16;
2470
                        y = *bx - (ys & 0xffff) - borrow;
2471
                        borrow = (y & 0x10000) >> 16;
2472
                        *bx++ = y & 0xffff;
2473
#endif
2474
#endif
2475
                        }
2476
                        while(sx <= sxe);
2477
                if (!*bxe) {
2478
                        bx = b->x;
2479
                        while(--bxe > bx && !*bxe)
2480
                                --n;
2481
                        b->wds = n;
2482
                        }
2483
                }
2484
        if (cmp(b, S) >= 0) {
2485
                q++;
2486
                borrow = 0;
2487
                carry = 0;
2488
                bx = b->x;
2489
                sx = S->x;
2490
                do {
2491
#ifdef ULLong
2492
                        ys = *sx++ + carry;
2493
                        carry = ys >> 32;
2494
                        y = *bx - (ys & FFFFFFFF) - borrow;
2495
                        borrow = y >> 32 & (ULong)1;
2496
                        *bx++ = y & FFFFFFFF;
2497
#else
2498
#ifdef Pack_32
2499
                        si = *sx++;
2500
                        ys = (si & 0xffff) + carry;
2501
                        zs = (si >> 16) + (ys >> 16);
2502
                        carry = zs >> 16;
2503
                        y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2504
                        borrow = (y & 0x10000) >> 16;
2505
                        z = (*bx >> 16) - (zs & 0xffff) - borrow;
2506
                        borrow = (z & 0x10000) >> 16;
2507
                        Storeinc(bx, z, y);
2508
#else
2509
                        ys = *sx++ + carry;
2510
                        carry = ys >> 16;
2511
                        y = *bx - (ys & 0xffff) - borrow;
2512
                        borrow = (y & 0x10000) >> 16;
2513
                        *bx++ = y & 0xffff;
2514
#endif
2515
#endif
2516
                        }
2517
                        while(sx <= sxe);
2518
                bx = b->x;
2519
                bxe = bx + n;
2520
                if (!*bxe) {
2521
                        while(--bxe > bx && !*bxe)
2522
                                --n;
2523
                        b->wds = n;
2524
                        }
2525
                }
2526
        return q;
2527
        }
2528

    
2529
#ifndef MULTIPLE_THREADS
2530
 static char *dtoa_result;
2531
#endif
2532

    
2533
 static char *
2534
#ifdef KR_headers
2535
rv_alloc(i) int i;
2536
#else
2537
rv_alloc(int i)
2538
#endif
2539
{
2540
        int j, k, *r;
2541

    
2542
        j = sizeof(ULong);
2543
        for(k = 0;
2544
                sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
2545
                j <<= 1)
2546
                        k++;
2547
        r = (int*)Balloc(k);
2548
        *r = k;
2549
        return
2550
#ifndef MULTIPLE_THREADS
2551
        dtoa_result =
2552
#endif
2553
                (char *)(r+1);
2554
        }
2555

    
2556
 static char *
2557
#ifdef KR_headers
2558
nrv_alloc(s, rve, n) char *s, **rve; int n;
2559
#else
2560
nrv_alloc(const char *s, char **rve, int n)
2561
#endif
2562
{
2563
        char *rv, *t;
2564

    
2565
        t = rv = rv_alloc(n);
2566
        while ((*t = *s++)) t++;
2567
        if (rve)
2568
                *rve = t;
2569
        return rv;
2570
        }
2571

    
2572
/* freedtoa(s) must be used to free values s returned by dtoa
2573
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
2574
 * but for consistency with earlier versions of dtoa, it is optional
2575
 * when MULTIPLE_THREADS is not defined.
2576
 */
2577

    
2578
 void
2579
#ifdef KR_headers
2580
freedtoa(s) char *s;
2581
#else
2582
freedtoa(char *s)
2583
#endif
2584
{
2585
        Bigint *b = (Bigint *)((int *)s - 1);
2586
        b->maxwds = 1 << (b->k = *(int*)b);
2587
        Bfree(b);
2588
#ifndef MULTIPLE_THREADS
2589
        if (s == dtoa_result)
2590
                dtoa_result = 0;
2591
#endif
2592
        }
2593

    
2594
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2595
 *
2596
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2597
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2598
 *
2599
 * Modifications:
2600
 *        1. Rather than iterating, we use a simple numeric overestimate
2601
 *           to determine k = floor(log10(d)).  We scale relevant
2602
 *           quantities using O(log2(k)) rather than O(k) multiplications.
2603
 *        2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2604
 *           try to generate digits strictly left to right.  Instead, we
2605
 *           compute with fewer bits and propagate the carry if necessary
2606
 *           when rounding the final digit up.  This is often faster.
2607
 *        3. Under the assumption that input will be rounded nearest,
2608
 *           mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2609
 *           That is, we allow equality in stopping tests when the
2610
 *           round-nearest rule will give the same floating-point value
2611
 *           as would satisfaction of the stopping test with strict
2612
 *           inequality.
2613
 *        4. We remove common factors of powers of 2 from relevant
2614
 *           quantities.
2615
 *        5. When converting floating-point integers less than 1e16,
2616
 *           we use floating-point arithmetic rather than resorting
2617
 *           to multiple-precision integers.
2618
 *        6. When asked to produce fewer than 15 digits, we first try
2619
 *           to get by with floating-point arithmetic; we resort to
2620
 *           multiple-precision integer arithmetic only if we cannot
2621
 *           guarantee that the floating-point calculation has given
2622
 *           the correctly rounded result.  For k requested digits and
2623
 *           "uniformly" distributed input, the probability is
2624
 *           something like 10^(k-15) that we must resort to the Long
2625
 *           calculation.
2626
 */
2627

    
2628
 char *
2629
dtoa
2630
#ifdef KR_headers
2631
        (d, mode, ndigits, decpt, sign, rve)
2632
        double d; int mode, ndigits, *decpt, *sign; char **rve;
2633
#else
2634
        (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
2635
#endif
2636
{
2637
 /*        Arguments ndigits, decpt, sign are similar to those
2638
        of ecvt and fcvt; trailing zeros are suppressed from
2639
        the returned string.  If not null, *rve is set to point
2640
        to the end of the return value.  If d is +-Infinity or NaN,
2641
        then *decpt is set to 9999.
2642

2643
        mode:
2644
                0 ==> shortest string that yields d when read in
2645
                        and rounded to nearest.
2646
                1 ==> like 0, but with Steele & White stopping rule;
2647
                        e.g. with IEEE P754 arithmetic , mode 0 gives
2648
                        1e23 whereas mode 1 gives 9.999999999999999e22.
2649
                2 ==> max(1,ndigits) significant digits.  This gives a
2650
                        return value similar to that of ecvt, except
2651
                        that trailing zeros are suppressed.
2652
                3 ==> through ndigits past the decimal point.  This
2653
                        gives a return value similar to that from fcvt,
2654
                        except that trailing zeros are suppressed, and
2655
                        ndigits can be negative.
2656
                4,5 ==> similar to 2 and 3, respectively, but (in
2657
                        round-nearest mode) with the tests of mode 0 to
2658
                        possibly return a shorter string that rounds to d.
2659
                        With IEEE arithmetic and compilation with
2660
                        -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2661
                        as modes 2 and 3 when FLT_ROUNDS != 1.
2662
                6-9 ==> Debugging modes similar to mode - 4:  don't try
2663
                        fast floating-point estimate (if applicable).
2664

2665
                Values of mode other than 0-9 are treated as mode 0.
2666

2667
                Sufficient space is allocated to the return value
2668
                to hold the suppressed trailing zeros.
2669
        */
2670

    
2671
        int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2672
                j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2673
                spec_case, try_quick, bias_round_up;
2674
        Long L;
2675
#ifndef Sudden_Underflow
2676
        int denorm;
2677
        ULong x;
2678
#endif
2679
        Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2680
        double d2, ds, eps;
2681
        char *s, *s0;
2682
#ifdef Honor_FLT_ROUNDS
2683
        int rounding;
2684
#endif
2685
#ifdef SET_INEXACT
2686
        int inexact, oldinexact;
2687
#endif
2688

    
2689
        /* In mode 2 and 3 we bias rounding up when there are ties. */
2690
        bias_round_up = mode == 2 || mode == 3;
2691

    
2692
        ilim = ilim1 = 0; /* to avoid Google3 compiler warnings */
2693

    
2694
#ifndef MULTIPLE_THREADS
2695
        if (dtoa_result) {
2696
                freedtoa(dtoa_result);
2697
                dtoa_result = 0;
2698
                }
2699
#endif
2700

    
2701
        if (word0(d) & Sign_bit) {
2702
                /* set sign for everything, including 0's and NaNs */
2703
                *sign = 1;
2704
                word0(d) &= ~Sign_bit;        /* clear sign bit */
2705
                }
2706
        else
2707
                *sign = 0;
2708

    
2709
#if defined(IEEE_Arith) + defined(VAX)
2710
#ifdef IEEE_Arith
2711
        if ((word0(d) & Exp_mask) == Exp_mask)
2712
#else
2713
        if (word0(d)  == 0x8000)
2714
#endif
2715
                {
2716
                /* Infinity or NaN */
2717
                *decpt = 9999;
2718
#ifdef IEEE_Arith
2719
                if (!word1(d) && !(word0(d) & 0xfffff))
2720
                        return nrv_alloc("Infinity", rve, 8);
2721
#endif
2722
                return nrv_alloc("NaN", rve, 3);
2723
                }
2724
#endif
2725
#ifdef IBM
2726
        dval(d) += 0; /* normalize */
2727
#endif
2728
        if (!dval(d)) {
2729
                *decpt = 1;
2730
                return nrv_alloc("0", rve, 1);
2731
                }
2732

    
2733
#ifdef SET_INEXACT
2734
        try_quick = oldinexact = get_inexact();
2735
        inexact = 1;
2736
#endif
2737
#ifdef Honor_FLT_ROUNDS
2738
        if ((rounding = Flt_Rounds) >= 2) {
2739
                if (*sign)
2740
                        rounding = rounding == 2 ? 0 : 2;
2741
                else
2742
                        if (rounding != 2)
2743
                                rounding = 0;
2744
                }
2745
#endif
2746

    
2747
        b = d2b(dval(d), &be, &bbits);
2748
#ifdef Sudden_Underflow
2749
        i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2750
#else
2751
        if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2752
#endif
2753
                dval(d2) = dval(d);
2754
                word0(d2) &= Frac_mask1;
2755
                word0(d2) |= Exp_11;
2756
#ifdef IBM
2757
                if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2758
                        dval(d2) /= 1 << j;
2759
#endif
2760

    
2761
                /* log(x)        ~=~ log(1.5) + (x-1.5)/1.5
2762
                 * log10(x)         =  log(x) / log(10)
2763
                 *                ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2764
                 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2765
                 *
2766
                 * This suggests computing an approximation k to log10(d) by
2767
                 *
2768
                 * k = (i - Bias)*0.301029995663981
2769
                 *        + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2770
                 *
2771
                 * We want k to be too large rather than too small.
2772
                 * The error in the first-order Taylor series approximation
2773
                 * is in our favor, so we just round up the constant enough
2774
                 * to compensate for any error in the multiplication of
2775
                 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2776
                 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2777
                 * adding 1e-13 to the constant term more than suffices.
2778
                 * Hence we adjust the constant term to 0.1760912590558.
2779
                 * (We could get a more accurate k by invoking log10,
2780
                 *  but this is probably not worthwhile.)
2781
                 */
2782

    
2783
                i -= Bias;
2784
#ifdef IBM
2785
                i <<= 2;
2786
                i += j;
2787
#endif
2788
#ifndef Sudden_Underflow
2789
                denorm = 0;
2790
                }
2791
        else {
2792
                /* d is denormalized */
2793

    
2794
                i = bbits + be + (Bias + (P-1) - 1);
2795
                x = i > 32  ? (word0(d) << (64 - i)) | (word1(d) >> (i - 32))
2796
                            : word1(d) << (32 - i);
2797
                dval(d2) = x;
2798
                word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2799
                i -= (Bias + (P-1) - 1) + 1;
2800
                denorm = 1;
2801
                }
2802
#endif
2803
        ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2804
        k = (int)ds;
2805
        if (ds < 0. && ds != k)
2806
                k--;        /* want k = floor(ds) */
2807
        k_check = 1;
2808
        if (k >= 0 && k <= Ten_pmax) {
2809
                if (dval(d) < tens[k])
2810
                        k--;
2811
                k_check = 0;
2812
                }
2813
        j = bbits - i - 1;
2814
        if (j >= 0) {
2815
                b2 = 0;
2816
                s2 = j;
2817
                }
2818
        else {
2819
                b2 = -j;
2820
                s2 = 0;
2821
                }
2822
        if (k >= 0) {
2823
                b5 = 0;
2824
                s5 = k;
2825
                s2 += k;
2826
                }
2827
        else {
2828
                b2 -= k;
2829
                b5 = -k;
2830
                s5 = 0;
2831
                }
2832
        if (mode < 0 || mode > 9)
2833
                mode = 0;
2834

    
2835
#ifndef SET_INEXACT
2836
#ifdef Check_FLT_ROUNDS
2837
        try_quick = Rounding == 1;
2838
#else
2839
        try_quick = 1;
2840
#endif
2841
#endif /*SET_INEXACT*/
2842

    
2843
        if (mode > 5) {
2844
                mode -= 4;
2845
                try_quick = 0;
2846
                }
2847
        leftright = 1;
2848
        switch(mode) {
2849
                case 0:
2850
                case 1:
2851
                        ilim = ilim1 = -1;
2852
                        i = 18;
2853
                        ndigits = 0;
2854
                        break;
2855
                case 2:
2856
                        leftright = 0;
2857
                        /* no break */
2858
                case 4:
2859
                        if (ndigits <= 0)
2860
                                ndigits = 1;
2861
                        ilim = ilim1 = i = ndigits;
2862
                        break;
2863
                case 3:
2864
                        leftright = 0;
2865
                        /* no break */
2866
                case 5:
2867
                        i = ndigits + k + 1;
2868
                        ilim = i;
2869
                        ilim1 = i - 1;
2870
                        if (i <= 0)
2871
                                i = 1;
2872
                }
2873
        s = s0 = rv_alloc(i);
2874

    
2875
#ifdef Honor_FLT_ROUNDS
2876
        if (mode > 1 && rounding != 1)
2877
                leftright = 0;
2878
#endif
2879

    
2880
        if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2881

    
2882
                /* Try to get by with floating-point arithmetic. */
2883

    
2884
                i = 0;
2885
                dval(d2) = dval(d);
2886
                k0 = k;
2887
                ilim0 = ilim;
2888
                ieps = 2; /* conservative */
2889
                if (k > 0) {
2890
                        ds = tens[k&0xf];
2891
                        j = k >> 4;
2892
                        if (j & Bletch) {
2893
                                /* prevent overflows */
2894
                                j &= Bletch - 1;
2895
                                dval(d) /= bigtens[n_bigtens-1];
2896
                                ieps++;
2897
                                }
2898
                        for(; j; j >>= 1, i++)
2899
                                if (j & 1) {
2900
                                        ieps++;
2901
                                        ds *= bigtens[i];
2902
                                        }
2903
                        dval(d) /= ds;
2904
                        }
2905
                else if ((j1 = -k)) {
2906
                        dval(d) *= tens[j1 & 0xf];
2907
                        for(j = j1 >> 4; j; j >>= 1, i++)
2908
                                if (j & 1) {
2909
                                        ieps++;
2910
                                        dval(d) *= bigtens[i];
2911
                                        }
2912
                        }
2913
                if (k_check && dval(d) < 1. && ilim > 0) {
2914
                        if (ilim1 <= 0)
2915
                                goto fast_failed;
2916
                        ilim = ilim1;
2917
                        k--;
2918
                        dval(d) *= 10.;
2919
                        ieps++;
2920
                        }
2921
                dval(eps) = ieps*dval(d) + 7.;
2922
                word0(eps) -= (P-1)*Exp_msk1;
2923
                if (ilim == 0) {
2924
                        S = mhi = 0;
2925
                        dval(d) -= 5.;
2926
                        if (dval(d) > dval(eps))
2927
                                goto one_digit;
2928
                        if (dval(d) < -dval(eps))
2929
                                goto no_digits;
2930
                        goto fast_failed;
2931
                        }
2932
#ifndef No_leftright
2933
                if (leftright) {
2934
                        /* Use Steele & White method of only
2935
                         * generating digits needed.
2936
                         */
2937
                        dval(eps) = 0.5/tens[ilim-1] - dval(eps);
2938
                        for(i = 0;;) {
2939
                                L = dval(d);
2940
                                dval(d) -= L;
2941
                                *s++ = '0' + (int)L;
2942
                                if (dval(d) < dval(eps))
2943
                                        goto ret1;
2944
                                if (1. - dval(d) < dval(eps))
2945
                                        goto bump_up;
2946
                                if (++i >= ilim)
2947
                                        break;
2948
                                dval(eps) *= 10.;
2949
                                dval(d) *= 10.;
2950
                                }
2951
                        }
2952
                else {
2953
#endif
2954
                        /* Generate ilim digits, then fix them up. */
2955
                        dval(eps) *= tens[ilim-1];
2956
                        for(i = 1;; i++, dval(d) *= 10.) {
2957
                                L = (Long)(dval(d));
2958
                                if (!(dval(d) -= L))
2959
                                        ilim = i;
2960
                                *s++ = '0' + (int)L;
2961
                                if (i == ilim) {
2962
                                        if (dval(d) > 0.5 + dval(eps))
2963
                                                goto bump_up;
2964
                                        else if (dval(d) < 0.5 - dval(eps)) {
2965
                                                while(*--s == '0');
2966
                                                s++;
2967
                                                goto ret1;
2968
                                                }
2969
                                        break;
2970
                                        }
2971
                                }
2972
#ifndef No_leftright
2973
                        }
2974
#endif
2975
 fast_failed:
2976
                s = s0;
2977
                dval(d) = dval(d2);
2978
                k = k0;
2979
                ilim = ilim0;
2980
                }
2981

    
2982
        /* Do we have a "small" integer? */
2983

    
2984
        if (be >= 0 && k <= Int_max) {
2985
                /* Yes. */
2986
                ds = tens[k];
2987
                if (ndigits < 0 && ilim <= 0) {
2988
                        S = mhi = 0;
2989
                        if (ilim < 0 || dval(d) < 5*ds || ((dval(d) == 5*ds) && !bias_round_up))
2990
                                goto no_digits;
2991
                        goto one_digit;
2992
                        }
2993

    
2994
                /* Limit looping by the number of digits to produce.
2995
                 * Firefox had a crash bug because some plugins reduce
2996
                 * the precision of double arithmetic.  With reduced
2997
                 * precision "dval(d) -= L*ds" might be imprecise and
2998
                 * d might not become zero and the loop might not
2999
                 * terminate.
3000
                 *
3001
                 * See https://bugzilla.mozilla.org/show_bug.cgi?id=358569
3002
                 */
3003
                for(i = 1; i <= k+1; i++, dval(d) *= 10.) {
3004
                        L = (Long)(dval(d) / ds);
3005
                        dval(d) -= L*ds;
3006
#ifdef Check_FLT_ROUNDS
3007
                        /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3008
                        if (dval(d) < 0) {
3009
                                L--;
3010
                                dval(d) += ds;
3011
                                }
3012
#endif
3013
                        *s++ = '0' + (int)L;
3014
                        if (!dval(d)) {
3015
#ifdef SET_INEXACT
3016
                                inexact = 0;
3017
#endif
3018
                                break;
3019
                                }
3020
                        if (i == ilim) {
3021
#ifdef Honor_FLT_ROUNDS
3022
                                if (mode > 1)
3023
                                switch(rounding) {
3024
                                  case 0: goto ret1;
3025
                                  case 2: goto bump_up;
3026
                                  }
3027
#endif
3028
                                dval(d) += dval(d);
3029
                                if (dval(d) > ds || (dval(d) == ds && ((L & 1) || bias_round_up))) {
3030
 bump_up:
3031
                                        while(*--s == '9')
3032
                                                if (s == s0) {
3033
                                                        k++;
3034
                                                        *s = '0';
3035
                                                        break;
3036
                                                        }
3037
                                        ++*s++;
3038
                                        }
3039
                                break;
3040
                                }
3041
                        }
3042
                goto ret1;
3043
                }
3044

    
3045
        m2 = b2;
3046
        m5 = b5;
3047
        mhi = mlo = 0;
3048
        if (leftright) {
3049
                i =
3050
#ifndef Sudden_Underflow
3051
                        denorm ? be + (Bias + (P-1) - 1 + 1) :
3052
#endif
3053
#ifdef IBM
3054
                        1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
3055
#else
3056
                        1 + P - bbits;
3057
#endif
3058
                b2 += i;
3059
                s2 += i;
3060
                mhi = i2b(1);
3061
                }
3062
        if (m2 > 0 && s2 > 0) {
3063
                i = m2 < s2 ? m2 : s2;
3064
                b2 -= i;
3065
                m2 -= i;
3066
                s2 -= i;
3067
                }
3068
        if (b5 > 0) {
3069
                if (leftright) {
3070
                        if (m5 > 0) {
3071
                                mhi = pow5mult(mhi, m5);
3072
                                b1 = mult(mhi, b);
3073
                                Bfree(b);
3074
                                b = b1;
3075
                                }
3076
                        if ((j = b5 - m5))
3077
                                b = pow5mult(b, j);
3078
                        }
3079
                else
3080
                        b = pow5mult(b, b5);
3081
                }
3082
        S = i2b(1);
3083
        if (s5 > 0)
3084
                S = pow5mult(S, s5);
3085

    
3086
        /* Check for special case that d is a normalized power of 2. */
3087

    
3088
        spec_case = 0;
3089
        if ((mode < 2 || leftright)
3090
#ifdef Honor_FLT_ROUNDS
3091
                        && rounding == 1
3092
#endif
3093
                                ) {
3094
                if (!word1(d) && !(word0(d) & Bndry_mask)
3095
#ifndef Sudden_Underflow
3096
                 && word0(d) & (Exp_mask & ~Exp_msk1)
3097
#endif
3098
                                ) {
3099
                        /* The special case */
3100
                        b2 += Log2P;
3101
                        s2 += Log2P;
3102
                        spec_case = 1;
3103
                        }
3104
                }
3105

    
3106
        /* Arrange for convenient computation of quotients:
3107
         * shift left if necessary so divisor has 4 leading 0 bits.
3108
         *
3109
         * Perhaps we should just compute leading 28 bits of S once
3110
         * and for all and pass them and a shift to quorem, so it
3111
         * can do shifts and ors to compute the numerator for q.
3112
         */
3113
#ifdef Pack_32
3114
        if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
3115
                i = 32 - i;
3116
#else
3117
        if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
3118
                i = 16 - i;
3119
#endif
3120
        if (i > 4) {
3121
                i -= 4;
3122
                b2 += i;
3123
                m2 += i;
3124
                s2 += i;
3125
                }
3126
        else if (i < 4) {
3127
                i += 28;
3128
                b2 += i;
3129
                m2 += i;
3130
                s2 += i;
3131
                }
3132
        if (b2 > 0)
3133
                b = lshift(b, b2);
3134
        if (s2 > 0)
3135
                S = lshift(S, s2);
3136
        if (k_check) {
3137
                if (cmp(b,S) < 0) {
3138
                        k--;
3139
                        b = multadd(b, 10, 0);        /* we botched the k estimate */
3140
                        if (leftright)
3141
                                mhi = multadd(mhi, 10, 0);
3142
                        ilim = ilim1;
3143
                        }
3144
                }
3145
        if (ilim <= 0 && (mode == 3 || mode == 5)) {
3146
                S = multadd(S, 5, 0);
3147
                if (ilim < 0 || cmp(b, S) < 0 || ((cmp(b, S) == 0) && !bias_round_up)) {
3148
                        /* no digits, fcvt style */
3149
 no_digits:
3150
                        k = -1 - ndigits;
3151
                        goto ret;
3152
                        }
3153
 one_digit:
3154
                *s++ = '1';
3155
                k++;
3156
                goto ret;
3157
                }
3158
        if (leftright) {
3159
                if (m2 > 0)
3160
                        mhi = lshift(mhi, m2);
3161

    
3162
                /* Compute mlo -- check for special case
3163
                 * that d is a normalized power of 2.
3164
                 */
3165

    
3166
                mlo = mhi;
3167
                if (spec_case) {
3168
                        mhi = Balloc(mhi->k);
3169
                        Bcopy(mhi, mlo);
3170
                        mhi = lshift(mhi, Log2P);
3171
                        }
3172

    
3173
                for(i = 1;;i++) {
3174
                        dig = quorem(b,S) + '0';
3175
                        /* Do we yet have the shortest decimal string
3176
                         * that will round to d?
3177
                         */
3178
                        j = cmp(b, mlo);
3179
                        delta = diff(S, mhi);
3180
                        j1 = delta->sign ? 1 : cmp(b, delta);
3181
                        Bfree(delta);
3182
#ifndef ROUND_BIASED
3183
                        if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3184
#ifdef Honor_FLT_ROUNDS
3185
                                && rounding >= 1
3186
#endif
3187
                                                                   ) {
3188
                                if (dig == '9')
3189
                                        goto round_9_up;
3190
                                if (j > 0)
3191
                                        dig++;
3192
#ifdef SET_INEXACT
3193
                                else if (!b->x[0] && b->wds <= 1)
3194
                                        inexact = 0;
3195
#endif
3196
                                *s++ = dig;
3197
                                goto ret;
3198
                                }
3199
#endif
3200
                        if (j < 0 || (j == 0 && mode != 1
3201
#ifndef ROUND_BIASED
3202
                                                        && !(word1(d) & 1)
3203
#endif
3204
                                        )) {
3205
                                if (!b->x[0] && b->wds <= 1) {
3206
#ifdef SET_INEXACT
3207
                                        inexact = 0;
3208
#endif
3209
                                        goto accept_dig;
3210
                                        }
3211
#ifdef Honor_FLT_ROUNDS
3212
                                if (mode > 1)
3213
                                 switch(rounding) {
3214
                                  case 0: goto accept_dig;
3215
                                  case 2: goto keep_dig;
3216
                                  }
3217
#endif /*Honor_FLT_ROUNDS*/
3218
                                if (j1 > 0) {
3219
                                        b = lshift(b, 1);
3220
                                        j1 = cmp(b, S);
3221
                                        if ((j1 > 0 || (j1 == 0 && ((dig & 1) || bias_round_up)))
3222
                                            && dig++ == '9')
3223
                                                goto round_9_up;
3224
                                        }
3225
 accept_dig:
3226
                                *s++ = dig;
3227
                                goto ret;
3228
                                }
3229
                        if (j1 > 0) {
3230
#ifdef Honor_FLT_ROUNDS
3231
                                if (!rounding)
3232
                                        goto accept_dig;
3233
#endif
3234
                                if (dig == '9') { /* possible if i == 1 */
3235
 round_9_up:
3236
                                        *s++ = '9';
3237
                                        goto roundoff;
3238
                                        }
3239
                                *s++ = dig + 1;
3240
                                goto ret;
3241
                                }
3242
#ifdef Honor_FLT_ROUNDS
3243
 keep_dig:
3244
#endif
3245
                        *s++ = dig;
3246
                        if (i == ilim)
3247
                                break;
3248
                        b = multadd(b, 10, 0);
3249
                        if (mlo == mhi)
3250
                                mlo = mhi = multadd(mhi, 10, 0);
3251
                        else {
3252
                                mlo = multadd(mlo, 10, 0);
3253
                                mhi = multadd(mhi, 10, 0);
3254
                                }
3255
                        }
3256
                }
3257
        else
3258
                for(i = 1;; i++) {
3259
                        *s++ = dig = quorem(b,S) + '0';
3260
                        if (!b->x[0] && b->wds <= 1) {
3261
#ifdef SET_INEXACT
3262
                                inexact = 0;
3263
#endif
3264
                                goto ret;
3265
                                }
3266
                        if (i >= ilim)
3267
                                break;
3268
                        b = multadd(b, 10, 0);
3269
                        }
3270

    
3271
        /* Round off last digit */
3272

    
3273
#ifdef Honor_FLT_ROUNDS
3274
        switch(rounding) {
3275
          case 0: goto trimzeros;
3276
          case 2: goto roundoff;
3277
          }
3278
#endif
3279
        b = lshift(b, 1);
3280
        j = cmp(b, S);
3281
        if (j > 0 || (j == 0 && ((dig & 1) || bias_round_up))) {
3282
 roundoff:
3283
                while(*--s == '9')
3284
                        if (s == s0) {
3285
                                k++;
3286
                                *s++ = '1';
3287
                                goto ret;
3288
                                }
3289
                ++*s++;
3290
                }
3291
        else {
3292
/* trimzeros:  (never used) */
3293
                while(*--s == '0');
3294
                s++;
3295
                }
3296
 ret:
3297
        Bfree(S);
3298
        if (mhi) {
3299
                if (mlo && mlo != mhi)
3300
                        Bfree(mlo);
3301
                Bfree(mhi);
3302
                }
3303
 ret1:
3304
#ifdef SET_INEXACT
3305
        if (inexact) {
3306
                if (!oldinexact) {
3307
                        word0(d) = Exp_1 + (70 << Exp_shift);
3308
                        word1(d) = 0;
3309
                        dval(d) += 1.;
3310
                        }
3311
                }
3312
        else if (!oldinexact)
3313
                clear_inexact();
3314
#endif
3315
        Bfree(b);
3316
        *s = 0;
3317
        *decpt = k + 1;
3318
        if (rve)
3319
                *rve = s;
3320
        return s0;
3321
        }
3322
#ifdef __cplusplus
3323
}
3324
#endif