Coverage Report

Created: 2024-05-20 06:23

/src/nspr/pr/src/misc/prdtoa.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/*
7
 * This file is based on the third-party code dtoa.c.  We minimize our
8
 * modifications to third-party code to make it easy to merge new versions.
9
 * The author of dtoa.c was not willing to add the parentheses suggested by
10
 * GCC, so we suppress these warnings.
11
 */
12
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
13
#pragma GCC diagnostic ignored "-Wparentheses"
14
#endif
15
16
#include "primpl.h"
17
#include "prbit.h"
18
19
#define MULTIPLE_THREADS
20
0
#define ACQUIRE_DTOA_LOCK(n)    PR_Lock(dtoa_lock[n])
21
0
#define FREE_DTOA_LOCK(n)   PR_Unlock(dtoa_lock[n])
22
23
static PRLock *dtoa_lock[2];
24
25
void _PR_InitDtoa(void)
26
1
{
27
1
    dtoa_lock[0] = PR_NewLock();
28
1
    dtoa_lock[1] = PR_NewLock();
29
1
}
30
31
void _PR_CleanupDtoa(void)
32
0
{
33
0
    PR_DestroyLock(dtoa_lock[0]);
34
0
    dtoa_lock[0] = NULL;
35
0
    PR_DestroyLock(dtoa_lock[1]);
36
0
    dtoa_lock[1] = NULL;
37
38
    /* FIXME: deal with freelist and p5s. */
39
0
}
40
41
#if !defined(__ARM_EABI__) \
42
    && (defined(__arm) || defined(__arm__) || defined(__arm26__) \
43
    || defined(__arm32__))
44
#define IEEE_ARM
45
#elif defined(IS_LITTLE_ENDIAN)
46
#define IEEE_8087
47
#else
48
#define IEEE_MC68k
49
#endif
50
51
0
#define Long PRInt32
52
0
#define ULong PRUint32
53
#define NO_LONG_LONG
54
55
#define No_Hex_NaN
56
57
/****************************************************************
58
 *
59
 * The author of this software is David M. Gay.
60
 *
61
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
62
 *
63
 * Permission to use, copy, modify, and distribute this software for any
64
 * purpose without fee is hereby granted, provided that this entire notice
65
 * is included in all copies of any software which is or includes a copy
66
 * or modification of this software and in all copies of the supporting
67
 * documentation for such software.
68
 *
69
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
70
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
71
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
72
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
73
 *
74
 ***************************************************************/
75
76
/* Please send bug reports to David M. Gay (dmg at acm dot org,
77
 * with " at " changed at "@" and " dot " changed to ".").  */
78
79
/* On a machine with IEEE extended-precision registers, it is
80
 * necessary to specify double-precision (53-bit) rounding precision
81
 * before invoking strtod or dtoa.  If the machine uses (the equivalent
82
 * of) Intel 80x87 arithmetic, the call
83
 *  _control87(PC_53, MCW_PC);
84
 * does this with many compilers.  Whether this or another call is
85
 * appropriate depends on the compiler; for this to work, it may be
86
 * necessary to #include "float.h" or another system-dependent header
87
 * file.
88
 */
89
90
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
91
 *
92
 * This strtod returns a nearest machine number to the input decimal
93
 * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
94
 * broken by the IEEE round-even rule.  Otherwise ties are broken by
95
 * biased rounding (add half and chop).
96
 *
97
 * Inspired loosely by William D. Clinger's paper "How to Read Floating
98
 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
99
 *
100
 * Modifications:
101
 *
102
 *  1. We only require IEEE, IBM, or VAX double-precision
103
 *      arithmetic (not IEEE double-extended).
104
 *  2. We get by with floating-point arithmetic in a case that
105
 *      Clinger missed -- when we're computing d * 10^n
106
 *      for a small integer d and the integer n is not too
107
 *      much larger than 22 (the maximum integer k for which
108
 *      we can represent 10^k exactly), we may be able to
109
 *      compute (d*10^k) * 10^(e-k) with just one roundoff.
110
 *  3. Rather than a bit-at-a-time adjustment of the binary
111
 *      result in the hard case, we use floating-point
112
 *      arithmetic to determine the adjustment to within
113
 *      one bit; only in really hard cases do we need to
114
 *      compute a second residual.
115
 *  4. Because of 3., we don't need a large table of powers of 10
116
 *      for ten-to-e (just some small tables, e.g. of 10^k
117
 *      for 0 <= k <= 22).
118
 */
119
120
/*
121
 * #define IEEE_8087 for IEEE-arithmetic machines where the least
122
 *  significant byte has the lowest address.
123
 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
124
 *  significant byte has the lowest address.
125
 * #define IEEE_ARM for IEEE-arithmetic machines where the two words
126
 *  in a double are stored in big endian order but the two shorts
127
 *  in a word are still stored in little endian order.
128
 * #define Long int on machines with 32-bit ints and 64-bit longs.
129
 * #define IBM for IBM mainframe-style floating-point arithmetic.
130
 * #define VAX for VAX-style floating-point arithmetic (D_floating).
131
 * #define No_leftright to omit left-right logic in fast floating-point
132
 *  computation of dtoa.
133
 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
134
 *  and strtod and dtoa should round accordingly.
135
 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
136
 *  and Honor_FLT_ROUNDS is not #defined.
137
 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
138
 *  that use extended-precision instructions to compute rounded
139
 *  products and quotients) with IBM.
140
 * #define ROUND_BIASED for IEEE-format with biased rounding.
141
 * #define Inaccurate_Divide for IEEE-format with correctly rounded
142
 *  products but inaccurate quotients, e.g., for Intel i860.
143
 * #define NO_LONG_LONG on machines that do not have a "long long"
144
 *  integer type (of >= 64 bits).  On such machines, you can
145
 *  #define Just_16 to store 16 bits per 32-bit Long when doing
146
 *  high-precision integer arithmetic.  Whether this speeds things
147
 *  up or slows things down depends on the machine and the number
148
 *  being converted.  If long long is available and the name is
149
 *  something other than "long long", #define Llong to be the name,
150
 *  and if "unsigned Llong" does not work as an unsigned version of
151
 *  Llong, #define #ULLong to be the corresponding unsigned type.
152
 * #define KR_headers for old-style C function headers.
153
 * #define Bad_float_h if your system lacks a float.h or if it does not
154
 *  define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
155
 *  FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
156
 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
157
 *  if memory is available and otherwise does something you deem
158
 *  appropriate.  If MALLOC is undefined, malloc will be invoked
159
 *  directly -- and assumed always to succeed.  Similarly, if you
160
 *  want something other than the system's free() to be called to
161
 *  recycle memory acquired from MALLOC, #define FREE to be the
162
 *  name of the alternate routine.  (FREE or free is only called in
163
 *  pathological cases, e.g., in a dtoa call after a dtoa return in
164
 *  mode 3 with thousands of digits requested.)
165
 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
166
 *  memory allocations from a private pool of memory when possible.
167
 *  When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
168
 *  unless #defined to be a different length.  This default length
169
 *  suffices to get rid of MALLOC calls except for unusual cases,
170
 *  such as decimal-to-binary conversion of a very long string of
171
 *  digits.  The longest string dtoa can return is about 751 bytes
172
 *  long.  For conversions by strtod of strings of 800 digits and
173
 *  all dtoa conversions in single-threaded executions with 8-byte
174
 *  pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
175
 *  pointers, PRIVATE_MEM >= 7112 appears adequate.
176
 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
177
 *  Infinity and NaN (case insensitively).  On some systems (e.g.,
178
 *  some HP systems), it may be necessary to #define NAN_WORD0
179
 *  appropriately -- to the most significant word of a quiet NaN.
180
 *  (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
181
 *  When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
182
 *  strtod also accepts (case insensitively) strings of the form
183
 *  NaN(x), where x is a string of hexadecimal digits and spaces;
184
 *  if there is only one string of hexadecimal digits, it is taken
185
 *  for the 52 fraction bits of the resulting NaN; if there are two
186
 *  or more strings of hex digits, the first is for the high 20 bits,
187
 *  the second and subsequent for the low 32 bits, with intervening
188
 *  white space ignored; but if this results in none of the 52
189
 *  fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
190
 *  and NAN_WORD1 are used instead.
191
 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
192
 *  multiple threads.  In this case, you must provide (or suitably
193
 *  #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
194
 *  by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
195
 *  in pow5mult, ensures lazy evaluation of only one copy of high
196
 *  powers of 5; omitting this lock would introduce a small
197
 *  probability of wasting memory, but would otherwise be harmless.)
198
 *  You must also invoke freedtoa(s) to free the value s returned by
199
 *  dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
200
 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
201
 *  avoids underflows on inputs whose result does not underflow.
202
 *  If you #define NO_IEEE_Scale on a machine that uses IEEE-format
203
 *  floating-point numbers and flushes underflows to zero rather
204
 *  than implementing gradual underflow, then you must also #define
205
 *  Sudden_Underflow.
206
 * #define USE_LOCALE to use the current locale's decimal_point value.
207
 * #define SET_INEXACT if IEEE arithmetic is being used and extra
208
 *  computation should be done to set the inexact flag when the
209
 *  result is inexact and avoid setting inexact when the result
210
 *  is exact.  In this case, dtoa.c must be compiled in
211
 *  an environment, perhaps provided by #include "dtoa.c" in a
212
 *  suitable wrapper, that defines two functions,
213
 *      int get_inexact(void);
214
 *      void clear_inexact(void);
215
 *  such that get_inexact() returns a nonzero value if the
216
 *  inexact bit is already set, and clear_inexact() sets the
217
 *  inexact bit to 0.  When SET_INEXACT is #defined, strtod
218
 *  also does extra computations to set the underflow and overflow
219
 *  flags when appropriate (i.e., when the result is tiny and
220
 *  inexact or when it is a numeric value rounded to +-infinity).
221
 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
222
 *  the result overflows to +-Infinity or underflows to 0.
223
 */
224
225
#ifndef Long
226
#define Long long
227
#endif
228
#ifndef ULong
229
typedef unsigned Long ULong;
230
#endif
231
232
#ifdef DEBUG
233
#include "stdio.h"
234
0
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
235
#endif
236
237
#include "stdlib.h"
238
#include "string.h"
239
240
#ifdef USE_LOCALE
241
#include "locale.h"
242
#endif
243
244
#ifdef MALLOC
245
#ifdef KR_headers
246
extern char *MALLOC();
247
#else
248
extern void *MALLOC(size_t);
249
#endif
250
#else
251
0
#define MALLOC malloc
252
#endif
253
254
#ifndef Omit_Private_Memory
255
#ifndef PRIVATE_MEM
256
0
#define PRIVATE_MEM 2304
257
#endif
258
0
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
259
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
260
#endif
261
262
#undef IEEE_Arith
263
#undef Avoid_Underflow
264
#ifdef IEEE_MC68k
265
#define IEEE_Arith
266
#endif
267
#ifdef IEEE_8087
268
#define IEEE_Arith
269
#endif
270
#ifdef IEEE_ARM
271
#define IEEE_Arith
272
#endif
273
274
#include "errno.h"
275
276
#ifdef Bad_float_h
277
278
#ifdef IEEE_Arith
279
#define DBL_DIG 15
280
#define DBL_MAX_10_EXP 308
281
#define DBL_MAX_EXP 1024
282
#define FLT_RADIX 2
283
#endif /*IEEE_Arith*/
284
285
#ifdef IBM
286
#define DBL_DIG 16
287
#define DBL_MAX_10_EXP 75
288
#define DBL_MAX_EXP 63
289
#define FLT_RADIX 16
290
#define DBL_MAX 7.2370055773322621e+75
291
#endif
292
293
#ifdef VAX
294
#define DBL_DIG 16
295
#define DBL_MAX_10_EXP 38
296
#define DBL_MAX_EXP 127
297
#define FLT_RADIX 2
298
#define DBL_MAX 1.7014118346046923e+38
299
#endif
300
301
#ifndef LONG_MAX
302
#define LONG_MAX 2147483647
303
#endif
304
305
#else /* ifndef Bad_float_h */
306
#include "float.h"
307
#endif /* Bad_float_h */
308
309
#ifndef __MATH_H__
310
#include "math.h"
311
#endif
312
313
#ifdef __cplusplus
314
extern "C" {
315
#endif
316
317
#ifndef CONST
318
#ifdef KR_headers
319
#define CONST /* blank */
320
#else
321
0
#define CONST const
322
#endif
323
#endif
324
325
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) + defined(VAX) + defined(IBM) != 1
326
Exactly one of IEEE_8087, IEEE_MC68k, IEEE_ARM, VAX, or IBM should be defined.
327
#endif
328
329
typedef union {
330
    double d;
331
    ULong L[2];
332
} U;
333
334
0
#define dval(x) (x).d
335
#ifdef IEEE_8087
336
0
#define word0(x) (x).L[1]
337
0
#define word1(x) (x).L[0]
338
#else
339
#define word0(x) (x).L[0]
340
#define word1(x) (x).L[1]
341
#endif
342
343
/* The following definition of Storeinc is appropriate for MIPS processors.
344
 * An alternative that might be better on some machines is
345
 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
346
 */
347
#if defined(IEEE_8087) + defined(IEEE_ARM) + defined(VAX)
348
0
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
349
0
((unsigned short *)a)[0] = (unsigned short)c, a++)
350
#else
351
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
352
((unsigned short *)a)[1] = (unsigned short)c, a++)
353
#endif
354
355
/* #define P DBL_MANT_DIG */
356
/* Ten_pmax = floor(P*log(2)/log(5)) */
357
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
358
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
359
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
360
361
#ifdef IEEE_Arith
362
0
#define Exp_shift  20
363
0
#define Exp_shift1 20
364
0
#define Exp_msk1    0x100000
365
#define Exp_msk11   0x100000
366
0
#define Exp_mask  0x7ff00000
367
0
#define P 53
368
0
#define Bias 1023
369
0
#define Emin (-1022)
370
0
#define Exp_1  0x3ff00000
371
0
#define Exp_11 0x3ff00000
372
0
#define Ebits 11
373
0
#define Frac_mask  0xfffff
374
0
#define Frac_mask1 0xfffff
375
0
#define Ten_pmax 22
376
0
#define Bletch 0x10
377
0
#define Bndry_mask  0xfffff
378
0
#define Bndry_mask1 0xfffff
379
0
#define LSB 1
380
0
#define Sign_bit 0x80000000
381
0
#define Log2P 1
382
#define Tiny0 0
383
0
#define Tiny1 1
384
0
#define Quick_max 14
385
0
#define Int_max 14
386
#ifndef NO_IEEE_Scale
387
#define Avoid_Underflow
388
#ifdef Flush_Denorm /* debugging option */
389
#undef Sudden_Underflow
390
#endif
391
#endif
392
393
#ifndef Flt_Rounds
394
#ifdef FLT_ROUNDS
395
0
#define Flt_Rounds FLT_ROUNDS
396
#else
397
#define Flt_Rounds 1
398
#endif
399
#endif /*Flt_Rounds*/
400
401
#ifdef Honor_FLT_ROUNDS
402
#define Rounding rounding
403
#undef Check_FLT_ROUNDS
404
#define Check_FLT_ROUNDS
405
#else
406
#define Rounding Flt_Rounds
407
#endif
408
409
#else /* ifndef IEEE_Arith */
410
#undef Check_FLT_ROUNDS
411
#undef Honor_FLT_ROUNDS
412
#undef SET_INEXACT
413
#undef  Sudden_Underflow
414
#define Sudden_Underflow
415
#ifdef IBM
416
#undef Flt_Rounds
417
#define Flt_Rounds 0
418
#define Exp_shift  24
419
#define Exp_shift1 24
420
#define Exp_msk1   0x1000000
421
#define Exp_msk11  0x1000000
422
#define Exp_mask  0x7f000000
423
#define P 14
424
#define Bias 65
425
#define Exp_1  0x41000000
426
#define Exp_11 0x41000000
427
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
428
#define Frac_mask  0xffffff
429
#define Frac_mask1 0xffffff
430
#define Bletch 4
431
#define Ten_pmax 22
432
#define Bndry_mask  0xefffff
433
#define Bndry_mask1 0xffffff
434
#define LSB 1
435
#define Sign_bit 0x80000000
436
#define Log2P 4
437
#define Tiny0 0x100000
438
#define Tiny1 0
439
#define Quick_max 14
440
#define Int_max 15
441
#else /* VAX */
442
#undef Flt_Rounds
443
#define Flt_Rounds 1
444
#define Exp_shift  23
445
#define Exp_shift1 7
446
#define Exp_msk1    0x80
447
#define Exp_msk11   0x800000
448
#define Exp_mask  0x7f80
449
#define P 56
450
#define Bias 129
451
#define Exp_1  0x40800000
452
#define Exp_11 0x4080
453
#define Ebits 8
454
#define Frac_mask  0x7fffff
455
#define Frac_mask1 0xffff007f
456
#define Ten_pmax 24
457
#define Bletch 2
458
#define Bndry_mask  0xffff007f
459
#define Bndry_mask1 0xffff007f
460
#define LSB 0x10000
461
#define Sign_bit 0x8000
462
#define Log2P 1
463
#define Tiny0 0x80
464
#define Tiny1 0
465
#define Quick_max 15
466
#define Int_max 15
467
#endif /* IBM, VAX */
468
#endif /* IEEE_Arith */
469
470
#ifndef IEEE_Arith
471
#define ROUND_BIASED
472
#endif
473
474
#ifdef RND_PRODQUOT
475
#define rounded_product(a,b) a = rnd_prod(a, b)
476
#define rounded_quotient(a,b) a = rnd_quot(a, b)
477
#ifdef KR_headers
478
extern double rnd_prod(), rnd_quot();
479
#else
480
extern double rnd_prod(double, double), rnd_quot(double, double);
481
#endif
482
#else
483
0
#define rounded_product(a,b) a *= b
484
0
#define rounded_quotient(a,b) a /= b
485
#endif
486
487
0
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
488
0
#define Big1 0xffffffff
489
490
#ifndef Pack_32
491
#define Pack_32
492
#endif
493
494
#ifdef KR_headers
495
#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
496
#else
497
#define FFFFFFFF 0xffffffffUL
498
#endif
499
500
#ifdef NO_LONG_LONG
501
#undef ULLong
502
#ifdef Just_16
503
#undef Pack_32
504
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
505
 * This makes some inner loops simpler and sometimes saves work
506
 * during multiplications, but it often seems to make things slightly
507
 * slower.  Hence the default is now to store 32 bits per Long.
508
 */
509
#endif
510
#else   /* long long available */
511
#ifndef Llong
512
#define Llong long long
513
#endif
514
#ifndef ULLong
515
#define ULLong unsigned Llong
516
#endif
517
#endif /* NO_LONG_LONG */
518
519
#ifndef MULTIPLE_THREADS
520
#define ACQUIRE_DTOA_LOCK(n)    /*nothing*/
521
#define FREE_DTOA_LOCK(n)   /*nothing*/
522
#endif
523
524
0
#define Kmax 7
525
526
struct
527
    Bigint {
528
    struct Bigint *next;
529
    int k, maxwds, sign, wds;
530
    ULong x[1];
531
};
532
533
typedef struct Bigint Bigint;
534
535
static Bigint *freelist[Kmax+1];
536
537
static Bigint *
538
Balloc
539
#ifdef KR_headers
540
(k) int k;
541
#else
542
(int k)
543
#endif
544
0
{
545
0
    int x;
546
0
    Bigint *rv;
547
0
#ifndef Omit_Private_Memory
548
0
    unsigned int len;
549
0
#endif
550
551
0
    ACQUIRE_DTOA_LOCK(0);
552
    /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
553
    /* but this case seems very unlikely. */
554
0
    if (k <= Kmax && (rv = freelist[k])) {
555
0
        freelist[k] = rv->next;
556
0
    }
557
0
    else {
558
0
        x = 1 << k;
559
#ifdef Omit_Private_Memory
560
        rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
561
#else
562
0
        len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
563
0
              /sizeof(double);
564
0
        if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
565
0
            rv = (Bigint*)pmem_next;
566
0
            pmem_next += len;
567
0
        }
568
0
        else {
569
0
            rv = (Bigint*)MALLOC(len*sizeof(double));
570
0
        }
571
0
#endif
572
0
        rv->k = k;
573
0
        rv->maxwds = x;
574
0
    }
575
0
    FREE_DTOA_LOCK(0);
576
0
    rv->sign = rv->wds = 0;
577
0
    return rv;
578
0
}
579
580
static void
581
Bfree
582
#ifdef KR_headers
583
(v) Bigint *v;
584
#else
585
(Bigint *v)
586
#endif
587
0
{
588
0
    if (v) {
589
0
        if (v->k > Kmax)
590
#ifdef FREE
591
            FREE((void*)v);
592
#else
593
0
            free((void*)v);
594
0
#endif
595
0
        else {
596
0
            ACQUIRE_DTOA_LOCK(0);
597
0
            v->next = freelist[v->k];
598
0
            freelist[v->k] = v;
599
0
            FREE_DTOA_LOCK(0);
600
0
        }
601
0
    }
602
0
}
603
604
0
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
605
0
y->wds*sizeof(Long) + 2*sizeof(int))
606
607
static Bigint *
608
multadd
609
#ifdef KR_headers
610
(b, m, a) Bigint *b; int m, a;
611
#else
612
(Bigint *b, int m, int a)   /* multiply by m and add a */
613
#endif
614
0
{
615
0
    int i, wds;
616
#ifdef ULLong
617
    ULong *x;
618
    ULLong carry, y;
619
#else
620
0
    ULong carry, *x, y;
621
0
#ifdef Pack_32
622
0
    ULong xi, z;
623
0
#endif
624
0
#endif
625
0
    Bigint *b1;
626
627
0
    wds = b->wds;
628
0
    x = b->x;
629
0
    i = 0;
630
0
    carry = a;
631
0
    do {
632
#ifdef ULLong
633
        y = *x * (ULLong)m + carry;
634
        carry = y >> 32;
635
        *x++ = y & FFFFFFFF;
636
#else
637
0
#ifdef Pack_32
638
0
        xi = *x;
639
0
        y = (xi & 0xffff) * m + carry;
640
0
        z = (xi >> 16) * m + (y >> 16);
641
0
        carry = z >> 16;
642
0
        *x++ = (z << 16) + (y & 0xffff);
643
#else
644
        y = *x * m + carry;
645
        carry = y >> 16;
646
        *x++ = y & 0xffff;
647
#endif
648
0
#endif
649
0
    }
650
0
    while(++i < wds);
651
0
    if (carry) {
652
0
        if (wds >= b->maxwds) {
653
0
            b1 = Balloc(b->k+1);
654
0
            Bcopy(b1, b);
655
0
            Bfree(b);
656
0
            b = b1;
657
0
        }
658
0
        b->x[wds++] = carry;
659
0
        b->wds = wds;
660
0
    }
661
0
    return b;
662
0
}
663
664
static Bigint *
665
s2b
666
#ifdef KR_headers
667
(s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
668
#else
669
(CONST char *s, int nd0, int nd, ULong y9)
670
#endif
671
0
{
672
0
    Bigint *b;
673
0
    int i, k;
674
0
    Long x, y;
675
676
0
    x = (nd + 8) / 9;
677
0
    for(k = 0, y = 1; x > y; y <<= 1, k++) ;
678
0
#ifdef Pack_32
679
0
    b = Balloc(k);
680
0
    b->x[0] = y9;
681
0
    b->wds = 1;
682
#else
683
    b = Balloc(k+1);
684
    b->x[0] = y9 & 0xffff;
685
    b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
686
#endif
687
688
0
    i = 9;
689
0
    if (9 < nd0) {
690
0
        s += 9;
691
0
        do {
692
0
            b = multadd(b, 10, *s++ - '0');
693
0
        }
694
0
        while(++i < nd0);
695
0
        s++;
696
0
    }
697
0
    else {
698
0
        s += 10;
699
0
    }
700
0
    for(; i < nd; i++) {
701
0
        b = multadd(b, 10, *s++ - '0');
702
0
    }
703
0
    return b;
704
0
}
705
706
static int
707
hi0bits
708
#ifdef KR_headers
709
(x) register ULong x;
710
#else
711
(register ULong x)
712
#endif
713
0
{
714
0
#ifdef PR_HAVE_BUILTIN_BITSCAN32
715
0
    return( (!x) ? 32 : pr_bitscan_clz32(x) );
716
#else
717
    register int k = 0;
718
719
    if (!(x & 0xffff0000)) {
720
        k = 16;
721
        x <<= 16;
722
    }
723
    if (!(x & 0xff000000)) {
724
        k += 8;
725
        x <<= 8;
726
    }
727
    if (!(x & 0xf0000000)) {
728
        k += 4;
729
        x <<= 4;
730
    }
731
    if (!(x & 0xc0000000)) {
732
        k += 2;
733
        x <<= 2;
734
    }
735
    if (!(x & 0x80000000)) {
736
        k++;
737
        if (!(x & 0x40000000)) {
738
            return 32;
739
        }
740
    }
741
    return k;
742
#endif /* PR_HAVE_BUILTIN_BITSCAN32 */
743
0
}
744
745
static int
746
lo0bits
747
#ifdef KR_headers
748
(y) ULong *y;
749
#else
750
(ULong *y)
751
#endif
752
0
{
753
0
#ifdef PR_HAVE_BUILTIN_BITSCAN32
754
0
    int k;
755
0
    ULong x = *y;
756
757
0
    if (x>1) {
758
0
        *y = ( x >> (k = pr_bitscan_ctz32(x)) );
759
0
    }
760
0
    else {
761
0
        k = ((x ^ 1) << 5);
762
0
    }
763
#else
764
    register int k;
765
    register ULong x = *y;
766
767
    if (x & 7) {
768
        if (x & 1) {
769
            return 0;
770
        }
771
        if (x & 2) {
772
            *y = x >> 1;
773
            return 1;
774
        }
775
        *y = x >> 2;
776
        return 2;
777
    }
778
    k = 0;
779
    if (!(x & 0xffff)) {
780
        k = 16;
781
        x >>= 16;
782
    }
783
    if (!(x & 0xff)) {
784
        k += 8;
785
        x >>= 8;
786
    }
787
    if (!(x & 0xf)) {
788
        k += 4;
789
        x >>= 4;
790
    }
791
    if (!(x & 0x3)) {
792
        k += 2;
793
        x >>= 2;
794
    }
795
    if (!(x & 1)) {
796
        k++;
797
        x >>= 1;
798
        if (!x) {
799
            return 32;
800
        }
801
    }
802
    *y = x;
803
#endif /* PR_HAVE_BUILTIN_BITSCAN32 */
804
0
    return k;
805
0
}
806
807
static Bigint *
808
i2b
809
#ifdef KR_headers
810
(i) int i;
811
#else
812
(int i)
813
#endif
814
0
{
815
0
    Bigint *b;
816
817
0
    b = Balloc(1);
818
0
    b->x[0] = i;
819
0
    b->wds = 1;
820
0
    return b;
821
0
}
822
823
static Bigint *
824
mult
825
#ifdef KR_headers
826
(a, b) Bigint *a, *b;
827
#else
828
(Bigint *a, Bigint *b)
829
#endif
830
0
{
831
0
    Bigint *c;
832
0
    int k, wa, wb, wc;
833
0
    ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
834
0
    ULong y;
835
#ifdef ULLong
836
    ULLong carry, z;
837
#else
838
0
    ULong carry, z;
839
0
#ifdef Pack_32
840
0
    ULong z2;
841
0
#endif
842
0
#endif
843
844
0
    if (a->wds < b->wds) {
845
0
        c = a;
846
0
        a = b;
847
0
        b = c;
848
0
    }
849
0
    k = a->k;
850
0
    wa = a->wds;
851
0
    wb = b->wds;
852
0
    wc = wa + wb;
853
0
    if (wc > a->maxwds) {
854
0
        k++;
855
0
    }
856
0
    c = Balloc(k);
857
0
    for(x = c->x, xa = x + wc; x < xa; x++) {
858
0
        *x = 0;
859
0
    }
860
0
    xa = a->x;
861
0
    xae = xa + wa;
862
0
    xb = b->x;
863
0
    xbe = xb + wb;
864
0
    xc0 = c->x;
865
#ifdef ULLong
866
    for(; xb < xbe; xc0++) {
867
        if (y = *xb++) {
868
            x = xa;
869
            xc = xc0;
870
            carry = 0;
871
            do {
872
                z = *x++ * (ULLong)y + *xc + carry;
873
                carry = z >> 32;
874
                *xc++ = z & FFFFFFFF;
875
            }
876
            while(x < xae);
877
            *xc = carry;
878
        }
879
    }
880
#else
881
0
#ifdef Pack_32
882
0
    for(; xb < xbe; xb++, xc0++) {
883
0
        if (y = *xb & 0xffff) {
884
0
            x = xa;
885
0
            xc = xc0;
886
0
            carry = 0;
887
0
            do {
888
0
                z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
889
0
                carry = z >> 16;
890
0
                z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
891
0
                carry = z2 >> 16;
892
0
                Storeinc(xc, z2, z);
893
0
            }
894
0
            while(x < xae);
895
0
            *xc = carry;
896
0
        }
897
0
        if (y = *xb >> 16) {
898
0
            x = xa;
899
0
            xc = xc0;
900
0
            carry = 0;
901
0
            z2 = *xc;
902
0
            do {
903
0
                z = (*x & 0xffff) * y + (*xc >> 16) + carry;
904
0
                carry = z >> 16;
905
0
                Storeinc(xc, z, z2);
906
0
                z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
907
0
                carry = z2 >> 16;
908
0
            }
909
0
            while(x < xae);
910
0
            *xc = z2;
911
0
        }
912
0
    }
913
#else
914
    for(; xb < xbe; xc0++) {
915
        if (y = *xb++) {
916
            x = xa;
917
            xc = xc0;
918
            carry = 0;
919
            do {
920
                z = *x++ * y + *xc + carry;
921
                carry = z >> 16;
922
                *xc++ = z & 0xffff;
923
            }
924
            while(x < xae);
925
            *xc = carry;
926
        }
927
    }
928
#endif
929
0
#endif
930
0
    for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
931
0
    c->wds = wc;
932
0
    return c;
933
0
}
934
935
static Bigint *p5s;
936
937
static Bigint *
938
pow5mult
939
#ifdef KR_headers
940
(b, k) Bigint *b; int k;
941
#else
942
(Bigint *b, int k)
943
#endif
944
0
{
945
0
    Bigint *b1, *p5, *p51;
946
0
    int i;
947
0
    static int p05[3] = { 5, 25, 125 };
948
949
0
    if (i = k & 3) {
950
0
        b = multadd(b, p05[i-1], 0);
951
0
    }
952
953
0
    if (!(k >>= 2)) {
954
0
        return b;
955
0
    }
956
0
    if (!(p5 = p5s)) {
957
        /* first time */
958
0
#ifdef MULTIPLE_THREADS
959
0
        ACQUIRE_DTOA_LOCK(1);
960
0
        if (!(p5 = p5s)) {
961
0
            p5 = p5s = i2b(625);
962
0
            p5->next = 0;
963
0
        }
964
0
        FREE_DTOA_LOCK(1);
965
#else
966
        p5 = p5s = i2b(625);
967
        p5->next = 0;
968
#endif
969
0
    }
970
0
    for(;;) {
971
0
        if (k & 1) {
972
0
            b1 = mult(b, p5);
973
0
            Bfree(b);
974
0
            b = b1;
975
0
        }
976
0
        if (!(k >>= 1)) {
977
0
            break;
978
0
        }
979
0
        if (!(p51 = p5->next)) {
980
0
#ifdef MULTIPLE_THREADS
981
0
            ACQUIRE_DTOA_LOCK(1);
982
0
            if (!(p51 = p5->next)) {
983
0
                p51 = p5->next = mult(p5,p5);
984
0
                p51->next = 0;
985
0
            }
986
0
            FREE_DTOA_LOCK(1);
987
#else
988
            p51 = p5->next = mult(p5,p5);
989
            p51->next = 0;
990
#endif
991
0
        }
992
0
        p5 = p51;
993
0
    }
994
0
    return b;
995
0
}
996
997
static Bigint *
998
lshift
999
#ifdef KR_headers
1000
(b, k) Bigint *b; int k;
1001
#else
1002
(Bigint *b, int k)
1003
#endif
1004
0
{
1005
0
    int i, k1, n, n1;
1006
0
    Bigint *b1;
1007
0
    ULong *x, *x1, *xe, z;
1008
1009
0
#ifdef Pack_32
1010
0
    n = k >> 5;
1011
#else
1012
    n = k >> 4;
1013
#endif
1014
0
    k1 = b->k;
1015
0
    n1 = n + b->wds + 1;
1016
0
    for(i = b->maxwds; n1 > i; i <<= 1) {
1017
0
        k1++;
1018
0
    }
1019
0
    b1 = Balloc(k1);
1020
0
    x1 = b1->x;
1021
0
    for(i = 0; i < n; i++) {
1022
0
        *x1++ = 0;
1023
0
    }
1024
0
    x = b->x;
1025
0
    xe = x + b->wds;
1026
0
#ifdef Pack_32
1027
0
    if (k &= 0x1f) {
1028
0
        k1 = 32 - k;
1029
0
        z = 0;
1030
0
        do {
1031
0
            *x1++ = *x << k | z;
1032
0
            z = *x++ >> k1;
1033
0
        }
1034
0
        while(x < xe);
1035
0
        if (*x1 = z) {
1036
0
            ++n1;
1037
0
        }
1038
0
    }
1039
#else
1040
    if (k &= 0xf) {
1041
        k1 = 16 - k;
1042
        z = 0;
1043
        do {
1044
            *x1++ = *x << k  & 0xffff | z;
1045
            z = *x++ >> k1;
1046
        }
1047
        while(x < xe);
1048
        if (*x1 = z) {
1049
            ++n1;
1050
        }
1051
    }
1052
#endif
1053
0
    else do {
1054
0
            *x1++ = *x++;
1055
0
        }
1056
0
        while(x < xe);
1057
0
    b1->wds = n1 - 1;
1058
0
    Bfree(b);
1059
0
    return b1;
1060
0
}
1061
1062
static int
1063
cmp
1064
#ifdef KR_headers
1065
(a, b) Bigint *a, *b;
1066
#else
1067
(Bigint *a, Bigint *b)
1068
#endif
1069
0
{
1070
0
    ULong *xa, *xa0, *xb, *xb0;
1071
0
    int i, j;
1072
1073
0
    i = a->wds;
1074
0
    j = b->wds;
1075
0
#ifdef DEBUG
1076
0
    if (i > 1 && !a->x[i-1]) {
1077
0
        Bug("cmp called with a->x[a->wds-1] == 0");
1078
0
    }
1079
0
    if (j > 1 && !b->x[j-1]) {
1080
0
        Bug("cmp called with b->x[b->wds-1] == 0");
1081
0
    }
1082
0
#endif
1083
0
    if (i -= j) {
1084
0
        return i;
1085
0
    }
1086
0
    xa0 = a->x;
1087
0
    xa = xa0 + j;
1088
0
    xb0 = b->x;
1089
0
    xb = xb0 + j;
1090
0
    for(;;) {
1091
0
        if (*--xa != *--xb) {
1092
0
            return *xa < *xb ? -1 : 1;
1093
0
        }
1094
0
        if (xa <= xa0) {
1095
0
            break;
1096
0
        }
1097
0
    }
1098
0
    return 0;
1099
0
}
1100
1101
static Bigint *
1102
diff
1103
#ifdef KR_headers
1104
(a, b) Bigint *a, *b;
1105
#else
1106
(Bigint *a, Bigint *b)
1107
#endif
1108
0
{
1109
0
    Bigint *c;
1110
0
    int i, wa, wb;
1111
0
    ULong *xa, *xae, *xb, *xbe, *xc;
1112
#ifdef ULLong
1113
    ULLong borrow, y;
1114
#else
1115
0
    ULong borrow, y;
1116
0
#ifdef Pack_32
1117
0
    ULong z;
1118
0
#endif
1119
0
#endif
1120
1121
0
    i = cmp(a,b);
1122
0
    if (!i) {
1123
0
        c = Balloc(0);
1124
0
        c->wds = 1;
1125
0
        c->x[0] = 0;
1126
0
        return c;
1127
0
    }
1128
0
    if (i < 0) {
1129
0
        c = a;
1130
0
        a = b;
1131
0
        b = c;
1132
0
        i = 1;
1133
0
    }
1134
0
    else {
1135
0
        i = 0;
1136
0
    }
1137
0
    c = Balloc(a->k);
1138
0
    c->sign = i;
1139
0
    wa = a->wds;
1140
0
    xa = a->x;
1141
0
    xae = xa + wa;
1142
0
    wb = b->wds;
1143
0
    xb = b->x;
1144
0
    xbe = xb + wb;
1145
0
    xc = c->x;
1146
0
    borrow = 0;
1147
#ifdef ULLong
1148
    do {
1149
        y = (ULLong)*xa++ - *xb++ - borrow;
1150
        borrow = y >> 32 & (ULong)1;
1151
        *xc++ = y & FFFFFFFF;
1152
    }
1153
    while(xb < xbe);
1154
    while(xa < xae) {
1155
        y = *xa++ - borrow;
1156
        borrow = y >> 32 & (ULong)1;
1157
        *xc++ = y & FFFFFFFF;
1158
    }
1159
#else
1160
0
#ifdef Pack_32
1161
0
    do {
1162
0
        y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1163
0
        borrow = (y & 0x10000) >> 16;
1164
0
        z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1165
0
        borrow = (z & 0x10000) >> 16;
1166
0
        Storeinc(xc, z, y);
1167
0
    }
1168
0
    while(xb < xbe);
1169
0
    while(xa < xae) {
1170
0
        y = (*xa & 0xffff) - borrow;
1171
0
        borrow = (y & 0x10000) >> 16;
1172
0
        z = (*xa++ >> 16) - borrow;
1173
0
        borrow = (z & 0x10000) >> 16;
1174
0
        Storeinc(xc, z, y);
1175
0
    }
1176
#else
1177
    do {
1178
        y = *xa++ - *xb++ - borrow;
1179
        borrow = (y & 0x10000) >> 16;
1180
        *xc++ = y & 0xffff;
1181
    }
1182
    while(xb < xbe);
1183
    while(xa < xae) {
1184
        y = *xa++ - borrow;
1185
        borrow = (y & 0x10000) >> 16;
1186
        *xc++ = y & 0xffff;
1187
    }
1188
#endif
1189
0
#endif
1190
0
    while(!*--xc) {
1191
0
        wa--;
1192
0
    }
1193
0
    c->wds = wa;
1194
0
    return c;
1195
0
}
1196
1197
static double
1198
ulp
1199
#ifdef KR_headers
1200
(dx) double dx;
1201
#else
1202
(double dx)
1203
#endif
1204
0
{
1205
0
    register Long L;
1206
0
    U x, a;
1207
1208
0
    dval(x) = dx;
1209
0
    L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1210
#ifndef Avoid_Underflow
1211
#ifndef Sudden_Underflow
1212
    if (L > 0) {
1213
#endif
1214
#endif
1215
#ifdef IBM
1216
        L |= Exp_msk1 >> 4;
1217
#endif
1218
0
        word0(a) = L;
1219
0
        word1(a) = 0;
1220
#ifndef Avoid_Underflow
1221
#ifndef Sudden_Underflow
1222
    }
1223
    else {
1224
        L = -L >> Exp_shift;
1225
        if (L < Exp_shift) {
1226
            word0(a) = 0x80000 >> L;
1227
            word1(a) = 0;
1228
        }
1229
        else {
1230
            word0(a) = 0;
1231
            L -= Exp_shift;
1232
            word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1233
        }
1234
    }
1235
#endif
1236
#endif
1237
0
    return dval(a);
1238
0
}
1239
1240
static double
1241
b2d
1242
#ifdef KR_headers
1243
(a, e) Bigint *a; int *e;
1244
#else
1245
(Bigint *a, int *e)
1246
#endif
1247
0
{
1248
0
    ULong *xa, *xa0, w, y, z;
1249
0
    int k;
1250
0
    U d;
1251
#ifdef VAX
1252
    ULong d0, d1;
1253
#else
1254
0
#define d0 word0(d)
1255
0
#define d1 word1(d)
1256
0
#endif
1257
1258
0
    xa0 = a->x;
1259
0
    xa = xa0 + a->wds;
1260
0
    y = *--xa;
1261
0
#ifdef DEBUG
1262
0
    if (!y) {
1263
0
        Bug("zero y in b2d");
1264
0
    }
1265
0
#endif
1266
0
    k = hi0bits(y);
1267
0
    *e = 32 - k;
1268
0
#ifdef Pack_32
1269
0
    if (k < Ebits) {
1270
0
        d0 = Exp_1 | y >> Ebits - k;
1271
0
        w = xa > xa0 ? *--xa : 0;
1272
0
        d1 = y << (32-Ebits) + k | w >> Ebits - k;
1273
0
        goto ret_d;
1274
0
    }
1275
0
    z = xa > xa0 ? *--xa : 0;
1276
0
    if (k -= Ebits) {
1277
0
        d0 = Exp_1 | y << k | z >> 32 - k;
1278
0
        y = xa > xa0 ? *--xa : 0;
1279
0
        d1 = z << k | y >> 32 - k;
1280
0
    }
1281
0
    else {
1282
0
        d0 = Exp_1 | y;
1283
0
        d1 = z;
1284
0
    }
1285
#else
1286
    if (k < Ebits + 16) {
1287
        z = xa > xa0 ? *--xa : 0;
1288
        d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1289
        w = xa > xa0 ? *--xa : 0;
1290
        y = xa > xa0 ? *--xa : 0;
1291
        d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1292
        goto ret_d;
1293
    }
1294
    z = xa > xa0 ? *--xa : 0;
1295
    w = xa > xa0 ? *--xa : 0;
1296
    k -= Ebits + 16;
1297
    d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1298
    y = xa > xa0 ? *--xa : 0;
1299
    d1 = w << k + 16 | y << k;
1300
#endif
1301
0
ret_d:
1302
#ifdef VAX
1303
    word0(d) = d0 >> 16 | d0 << 16;
1304
    word1(d) = d1 >> 16 | d1 << 16;
1305
#else
1306
0
#undef d0
1307
0
#undef d1
1308
0
#endif
1309
0
    return dval(d);
1310
0
}
1311
1312
static Bigint *
1313
d2b
1314
#ifdef KR_headers
1315
(dd, e, bits) double dd; int *e, *bits;
1316
#else
1317
(double dd, int *e, int *bits)
1318
#endif
1319
0
{
1320
0
    U d;
1321
0
    Bigint *b;
1322
0
    int de, k;
1323
0
    ULong *x, y, z;
1324
0
#ifndef Sudden_Underflow
1325
0
    int i;
1326
0
#endif
1327
#ifdef VAX
1328
    ULong d0, d1;
1329
#endif
1330
1331
0
    dval(d) = dd;
1332
#ifdef VAX
1333
    d0 = word0(d) >> 16 | word0(d) << 16;
1334
    d1 = word1(d) >> 16 | word1(d) << 16;
1335
#else
1336
0
#define d0 word0(d)
1337
0
#define d1 word1(d)
1338
0
#endif
1339
1340
0
#ifdef Pack_32
1341
0
    b = Balloc(1);
1342
#else
1343
    b = Balloc(2);
1344
#endif
1345
0
    x = b->x;
1346
1347
0
    z = d0 & Frac_mask;
1348
0
    d0 &= 0x7fffffff;   /* clear sign bit, which we ignore */
1349
#ifdef Sudden_Underflow
1350
    de = (int)(d0 >> Exp_shift);
1351
#ifndef IBM
1352
    z |= Exp_msk11;
1353
#endif
1354
#else
1355
0
    if (de = (int)(d0 >> Exp_shift)) {
1356
0
        z |= Exp_msk1;
1357
0
    }
1358
0
#endif
1359
0
#ifdef Pack_32
1360
0
    if (y = d1) {
1361
0
        if (k = lo0bits(&y)) {
1362
0
            x[0] = y | z << 32 - k;
1363
0
            z >>= k;
1364
0
        }
1365
0
        else {
1366
0
            x[0] = y;
1367
0
        }
1368
0
#ifndef Sudden_Underflow
1369
0
        i =
1370
0
#endif
1371
0
            b->wds = (x[1] = z) ? 2 : 1;
1372
0
    }
1373
0
    else {
1374
0
        k = lo0bits(&z);
1375
0
        x[0] = z;
1376
0
#ifndef Sudden_Underflow
1377
0
        i =
1378
0
#endif
1379
0
            b->wds = 1;
1380
0
        k += 32;
1381
0
    }
1382
#else
1383
    if (y = d1) {
1384
        if (k = lo0bits(&y))
1385
            if (k >= 16) {
1386
                x[0] = y | z << 32 - k & 0xffff;
1387
                x[1] = z >> k - 16 & 0xffff;
1388
                x[2] = z >> k;
1389
                i = 2;
1390
            }
1391
            else {
1392
                x[0] = y & 0xffff;
1393
                x[1] = y >> 16 | z << 16 - k & 0xffff;
1394
                x[2] = z >> k & 0xffff;
1395
                x[3] = z >> k+16;
1396
                i = 3;
1397
            }
1398
        else {
1399
            x[0] = y & 0xffff;
1400
            x[1] = y >> 16;
1401
            x[2] = z & 0xffff;
1402
            x[3] = z >> 16;
1403
            i = 3;
1404
        }
1405
    }
1406
    else {
1407
#ifdef DEBUG
1408
        if (!z) {
1409
            Bug("Zero passed to d2b");
1410
        }
1411
#endif
1412
        k = lo0bits(&z);
1413
        if (k >= 16) {
1414
            x[0] = z;
1415
            i = 0;
1416
        }
1417
        else {
1418
            x[0] = z & 0xffff;
1419
            x[1] = z >> 16;
1420
            i = 1;
1421
        }
1422
        k += 32;
1423
    }
1424
    while(!x[i]) {
1425
        --i;
1426
    }
1427
    b->wds = i + 1;
1428
#endif
1429
0
#ifndef Sudden_Underflow
1430
0
    if (de) {
1431
0
#endif
1432
#ifdef IBM
1433
        *e = (de - Bias - (P-1) << 2) + k;
1434
        *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1435
#else
1436
0
        *e = de - Bias - (P-1) + k;
1437
0
        *bits = P - k;
1438
0
#endif
1439
0
#ifndef Sudden_Underflow
1440
0
    }
1441
0
    else {
1442
0
        *e = de - Bias - (P-1) + 1 + k;
1443
0
#ifdef Pack_32
1444
0
        *bits = 32*i - hi0bits(x[i-1]);
1445
#else
1446
        *bits = (i+2)*16 - hi0bits(x[i]);
1447
#endif
1448
0
    }
1449
0
#endif
1450
0
    return b;
1451
0
}
1452
#undef d0
1453
#undef d1
1454
1455
static double
1456
ratio
1457
#ifdef KR_headers
1458
(a, b) Bigint *a, *b;
1459
#else
1460
(Bigint *a, Bigint *b)
1461
#endif
1462
0
{
1463
0
    U da, db;
1464
0
    int k, ka, kb;
1465
1466
0
    dval(da) = b2d(a, &ka);
1467
0
    dval(db) = b2d(b, &kb);
1468
0
#ifdef Pack_32
1469
0
    k = ka - kb + 32*(a->wds - b->wds);
1470
#else
1471
    k = ka - kb + 16*(a->wds - b->wds);
1472
#endif
1473
#ifdef IBM
1474
    if (k > 0) {
1475
        word0(da) += (k >> 2)*Exp_msk1;
1476
        if (k &= 3) {
1477
            dval(da) *= 1 << k;
1478
        }
1479
    }
1480
    else {
1481
        k = -k;
1482
        word0(db) += (k >> 2)*Exp_msk1;
1483
        if (k &= 3) {
1484
            dval(db) *= 1 << k;
1485
        }
1486
    }
1487
#else
1488
0
    if (k > 0) {
1489
0
        word0(da) += k*Exp_msk1;
1490
0
    }
1491
0
    else {
1492
0
        k = -k;
1493
0
        word0(db) += k*Exp_msk1;
1494
0
    }
1495
0
#endif
1496
0
    return dval(da) / dval(db);
1497
0
}
1498
1499
static CONST double
1500
tens[] = {
1501
    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1502
    1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1503
    1e20, 1e21, 1e22
1504
#ifdef VAX
1505
    , 1e23, 1e24
1506
#endif
1507
};
1508
1509
static CONST double
1510
#ifdef IEEE_Arith
1511
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1512
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1513
#ifdef Avoid_Underflow
1514
                                   9007199254740992.*9007199254740992.e-256
1515
                                   /* = 2^106 * 1e-53 */
1516
#else
1517
                                   1e-256
1518
#endif
1519
                                 };
1520
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1521
/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
1522
0
#define Scale_Bit 0x10
1523
0
#define n_bigtens 5
1524
#else
1525
#ifdef IBM
1526
bigtens[] = { 1e16, 1e32, 1e64 };
1527
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1528
#define n_bigtens 3
1529
#else
1530
bigtens[] = { 1e16, 1e32 };
1531
static CONST double tinytens[] = { 1e-16, 1e-32 };
1532
#define n_bigtens 2
1533
#endif
1534
#endif
1535
1536
#ifndef IEEE_Arith
1537
#undef INFNAN_CHECK
1538
#endif
1539
1540
#ifdef INFNAN_CHECK
1541
1542
#ifndef NAN_WORD0
1543
#define NAN_WORD0 0x7ff80000
1544
#endif
1545
1546
#ifndef NAN_WORD1
1547
#define NAN_WORD1 0
1548
#endif
1549
1550
static int
1551
match
1552
#ifdef KR_headers
1553
(sp, t) char **sp, *t;
1554
#else
1555
(CONST char **sp, char *t)
1556
#endif
1557
{
1558
    int c, d;
1559
    CONST char *s = *sp;
1560
1561
    while(d = *t++) {
1562
        if ((c = *++s) >= 'A' && c <= 'Z') {
1563
            c += 'a' - 'A';
1564
        }
1565
        if (c != d) {
1566
            return 0;
1567
        }
1568
    }
1569
    *sp = s + 1;
1570
    return 1;
1571
}
1572
1573
#ifndef No_Hex_NaN
1574
static void
1575
hexnan
1576
#ifdef KR_headers
1577
(rvp, sp) double *rvp; CONST char **sp;
1578
#else
1579
(double *rvp, CONST char **sp)
1580
#endif
1581
{
1582
    ULong c, x[2];
1583
    CONST char *s;
1584
    int havedig, udx0, xshift;
1585
1586
    x[0] = x[1] = 0;
1587
    havedig = xshift = 0;
1588
    udx0 = 1;
1589
    s = *sp;
1590
    while(c = *(CONST unsigned char*)++s) {
1591
        if (c >= '0' && c <= '9') {
1592
            c -= '0';
1593
        }
1594
        else if (c >= 'a' && c <= 'f') {
1595
            c += 10 - 'a';
1596
        }
1597
        else if (c >= 'A' && c <= 'F') {
1598
            c += 10 - 'A';
1599
        }
1600
        else if (c <= ' ') {
1601
            if (udx0 && havedig) {
1602
                udx0 = 0;
1603
                xshift = 1;
1604
            }
1605
            continue;
1606
        }
1607
        else if (/*(*/ c == ')' && havedig) {
1608
            *sp = s + 1;
1609
            break;
1610
        }
1611
        else {
1612
            return;    /* invalid form: don't change *sp */
1613
        }
1614
        havedig = 1;
1615
        if (xshift) {
1616
            xshift = 0;
1617
            x[0] = x[1];
1618
            x[1] = 0;
1619
        }
1620
        if (udx0) {
1621
            x[0] = (x[0] << 4) | (x[1] >> 28);
1622
        }
1623
        x[1] = (x[1] << 4) | c;
1624
    }
1625
    if ((x[0] &= 0xfffff) || x[1]) {
1626
        word0(*rvp) = Exp_mask | x[0];
1627
        word1(*rvp) = x[1];
1628
    }
1629
}
1630
#endif /*No_Hex_NaN*/
1631
#endif /* INFNAN_CHECK */
1632
1633
PR_IMPLEMENT(double)
1634
PR_strtod
1635
#ifdef KR_headers
1636
(s00, se) CONST char *s00; char **se;
1637
#else
1638
(CONST char *s00, char **se)
1639
#endif
1640
0
{
1641
0
#ifdef Avoid_Underflow
1642
0
    int scale;
1643
0
#endif
1644
0
    int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1645
0
        e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1646
0
    CONST char *s, *s0, *s1;
1647
0
    double aadj, aadj1, adj;
1648
0
    U aadj2, rv, rv0;
1649
0
    Long L;
1650
0
    ULong y, z;
1651
0
    Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1652
#ifdef SET_INEXACT
1653
    int inexact, oldinexact;
1654
#endif
1655
#ifdef Honor_FLT_ROUNDS
1656
    int rounding;
1657
#endif
1658
#ifdef USE_LOCALE
1659
    CONST char *s2;
1660
#endif
1661
1662
0
    if (!_pr_initialized) {
1663
0
        _PR_ImplicitInitialization();
1664
0
    }
1665
1666
0
    sign = nz0 = nz = 0;
1667
0
    dval(rv) = 0.;
1668
0
    for(s = s00;; s++) switch(*s) {
1669
0
            case '-':
1670
0
                sign = 1;
1671
            /* no break */
1672
0
            case '+':
1673
0
                if (*++s) {
1674
0
                    goto break2;
1675
0
                }
1676
            /* no break */
1677
0
            case 0:
1678
0
                goto ret0;
1679
0
            case '\t':
1680
0
            case '\n':
1681
0
            case '\v':
1682
0
            case '\f':
1683
0
            case '\r':
1684
0
            case ' ':
1685
0
                continue;
1686
0
            default:
1687
0
                goto break2;
1688
0
        }
1689
0
break2:
1690
0
    if (*s == '0') {
1691
0
        nz0 = 1;
1692
0
        while(*++s == '0') ;
1693
0
        if (!*s) {
1694
0
            goto ret;
1695
0
        }
1696
0
    }
1697
0
    s0 = s;
1698
0
    y = z = 0;
1699
0
    for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1700
0
        if (nd < 9) {
1701
0
            y = 10*y + c - '0';
1702
0
        }
1703
0
        else if (nd < 16) {
1704
0
            z = 10*z + c - '0';
1705
0
        }
1706
0
    nd0 = nd;
1707
#ifdef USE_LOCALE
1708
    s1 = localeconv()->decimal_point;
1709
    if (c == *s1) {
1710
        c = '.';
1711
        if (*++s1) {
1712
            s2 = s;
1713
            for(;;) {
1714
                if (*++s2 != *s1) {
1715
                    c = 0;
1716
                    break;
1717
                }
1718
                if (!*++s1) {
1719
                    s = s2;
1720
                    break;
1721
                }
1722
            }
1723
        }
1724
    }
1725
#endif
1726
0
    if (c == '.') {
1727
0
        c = *++s;
1728
0
        if (!nd) {
1729
0
            for(; c == '0'; c = *++s) {
1730
0
                nz++;
1731
0
            }
1732
0
            if (c > '0' && c <= '9') {
1733
0
                s0 = s;
1734
0
                nf += nz;
1735
0
                nz = 0;
1736
0
                goto have_dig;
1737
0
            }
1738
0
            goto dig_done;
1739
0
        }
1740
0
        for(; c >= '0' && c <= '9'; c = *++s) {
1741
0
have_dig:
1742
0
            nz++;
1743
0
            if (c -= '0') {
1744
0
                nf += nz;
1745
0
                for(i = 1; i < nz; i++)
1746
0
                    if (nd++ < 9) {
1747
0
                        y *= 10;
1748
0
                    }
1749
0
                    else if (nd <= DBL_DIG + 1) {
1750
0
                        z *= 10;
1751
0
                    }
1752
0
                if (nd++ < 9) {
1753
0
                    y = 10*y + c;
1754
0
                }
1755
0
                else if (nd <= DBL_DIG + 1) {
1756
0
                    z = 10*z + c;
1757
0
                }
1758
0
                nz = 0;
1759
0
            }
1760
0
        }
1761
0
    }
1762
0
dig_done:
1763
0
    if (nd > 64 * 1024) {
1764
0
        goto ret0;
1765
0
    }
1766
0
    e = 0;
1767
0
    if (c == 'e' || c == 'E') {
1768
0
        if (!nd && !nz && !nz0) {
1769
0
            goto ret0;
1770
0
        }
1771
0
        s00 = s;
1772
0
        esign = 0;
1773
0
        switch(c = *++s) {
1774
0
            case '-':
1775
0
                esign = 1;
1776
0
            case '+':
1777
0
                c = *++s;
1778
0
        }
1779
0
        if (c >= '0' && c <= '9') {
1780
0
            while(c == '0') {
1781
0
                c = *++s;
1782
0
            }
1783
0
            if (c > '0' && c <= '9') {
1784
0
                L = c - '0';
1785
0
                s1 = s;
1786
0
                while((c = *++s) >= '0' && c <= '9') {
1787
0
                    L = 10*L + c - '0';
1788
0
                }
1789
0
                if (s - s1 > 8 || L > 19999)
1790
                    /* Avoid confusion from exponents
1791
                     * so large that e might overflow.
1792
                     */
1793
0
                {
1794
0
                    e = 19999;    /* safe for 16 bit ints */
1795
0
                }
1796
0
                else {
1797
0
                    e = (int)L;
1798
0
                }
1799
0
                if (esign) {
1800
0
                    e = -e;
1801
0
                }
1802
0
            }
1803
0
            else {
1804
0
                e = 0;
1805
0
            }
1806
0
        }
1807
0
        else {
1808
0
            s = s00;
1809
0
        }
1810
0
    }
1811
0
    if (!nd) {
1812
0
        if (!nz && !nz0) {
1813
#ifdef INFNAN_CHECK
1814
            /* Check for Nan and Infinity */
1815
            switch(c) {
1816
                case 'i':
1817
                case 'I':
1818
                    if (match(&s,"nf")) {
1819
                        --s;
1820
                        if (!match(&s,"inity")) {
1821
                            ++s;
1822
                        }
1823
                        word0(rv) = 0x7ff00000;
1824
                        word1(rv) = 0;
1825
                        goto ret;
1826
                    }
1827
                    break;
1828
                case 'n':
1829
                case 'N':
1830
                    if (match(&s, "an")) {
1831
                        word0(rv) = NAN_WORD0;
1832
                        word1(rv) = NAN_WORD1;
1833
#ifndef No_Hex_NaN
1834
                        if (*s == '(') { /*)*/
1835
                            hexnan(&rv, &s);
1836
                        }
1837
#endif
1838
                        goto ret;
1839
                    }
1840
            }
1841
#endif /* INFNAN_CHECK */
1842
0
ret0:
1843
0
            s = s00;
1844
0
            sign = 0;
1845
0
        }
1846
0
        goto ret;
1847
0
    }
1848
0
    e1 = e -= nf;
1849
1850
    /* Now we have nd0 digits, starting at s0, followed by a
1851
     * decimal point, followed by nd-nd0 digits.  The number we're
1852
     * after is the integer represented by those digits times
1853
     * 10**e */
1854
1855
0
    if (!nd0) {
1856
0
        nd0 = nd;
1857
0
    }
1858
0
    k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1859
0
    dval(rv) = y;
1860
0
    if (k > 9) {
1861
#ifdef SET_INEXACT
1862
        if (k > DBL_DIG) {
1863
            oldinexact = get_inexact();
1864
        }
1865
#endif
1866
0
        dval(rv) = tens[k - 9] * dval(rv) + z;
1867
0
    }
1868
0
    bd0 = 0;
1869
0
    if (nd <= DBL_DIG
1870
0
#ifndef RND_PRODQUOT
1871
0
#ifndef Honor_FLT_ROUNDS
1872
0
        && Flt_Rounds == 1
1873
0
#endif
1874
0
#endif
1875
0
       ) {
1876
0
        if (!e) {
1877
0
            goto ret;
1878
0
        }
1879
0
        if (e > 0) {
1880
0
            if (e <= Ten_pmax) {
1881
#ifdef VAX
1882
                goto vax_ovfl_check;
1883
#else
1884
#ifdef Honor_FLT_ROUNDS
1885
                /* round correctly FLT_ROUNDS = 2 or 3 */
1886
                if (sign) {
1887
                    rv = -rv;
1888
                    sign = 0;
1889
                }
1890
#endif
1891
0
                /* rv = */ rounded_product(dval(rv), tens[e]);
1892
0
                goto ret;
1893
0
#endif
1894
0
            }
1895
0
            i = DBL_DIG - nd;
1896
0
            if (e <= Ten_pmax + i) {
1897
                /* A fancier test would sometimes let us do
1898
                 * this for larger i values.
1899
                 */
1900
#ifdef Honor_FLT_ROUNDS
1901
                /* round correctly FLT_ROUNDS = 2 or 3 */
1902
                if (sign) {
1903
                    rv = -rv;
1904
                    sign = 0;
1905
                }
1906
#endif
1907
0
                e -= i;
1908
0
                dval(rv) *= tens[i];
1909
#ifdef VAX
1910
                /* VAX exponent range is so narrow we must
1911
                 * worry about overflow here...
1912
                 */
1913
vax_ovfl_check:
1914
                word0(rv) -= P*Exp_msk1;
1915
                /* rv = */ rounded_product(dval(rv), tens[e]);
1916
                if ((word0(rv) & Exp_mask)
1917
                    > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1918
                    goto ovfl;
1919
                }
1920
                word0(rv) += P*Exp_msk1;
1921
#else
1922
0
                /* rv = */ rounded_product(dval(rv), tens[e]);
1923
0
#endif
1924
0
                goto ret;
1925
0
            }
1926
0
        }
1927
0
#ifndef Inaccurate_Divide
1928
0
        else if (e >= -Ten_pmax) {
1929
#ifdef Honor_FLT_ROUNDS
1930
            /* round correctly FLT_ROUNDS = 2 or 3 */
1931
            if (sign) {
1932
                rv = -rv;
1933
                sign = 0;
1934
            }
1935
#endif
1936
0
            /* rv = */ rounded_quotient(dval(rv), tens[-e]);
1937
0
            goto ret;
1938
0
        }
1939
0
#endif
1940
0
    }
1941
0
    e1 += nd - k;
1942
1943
0
#ifdef IEEE_Arith
1944
#ifdef SET_INEXACT
1945
    inexact = 1;
1946
    if (k <= DBL_DIG) {
1947
        oldinexact = get_inexact();
1948
    }
1949
#endif
1950
0
#ifdef Avoid_Underflow
1951
0
    scale = 0;
1952
0
#endif
1953
#ifdef Honor_FLT_ROUNDS
1954
    if ((rounding = Flt_Rounds) >= 2) {
1955
        if (sign) {
1956
            rounding = rounding == 2 ? 0 : 2;
1957
        }
1958
        else if (rounding != 2) {
1959
            rounding = 0;
1960
        }
1961
    }
1962
#endif
1963
0
#endif /*IEEE_Arith*/
1964
1965
    /* Get starting approximation = rv * 10**e1 */
1966
1967
0
    if (e1 > 0) {
1968
0
        if (i = e1 & 15) {
1969
0
            dval(rv) *= tens[i];
1970
0
        }
1971
0
        if (e1 &= ~15) {
1972
0
            if (e1 > DBL_MAX_10_EXP) {
1973
0
ovfl:
1974
0
#ifndef NO_ERRNO
1975
0
                PR_SetError(PR_RANGE_ERROR, 0);
1976
0
#endif
1977
                /* Can't trust HUGE_VAL */
1978
0
#ifdef IEEE_Arith
1979
#ifdef Honor_FLT_ROUNDS
1980
                switch(rounding) {
1981
                    case 0: /* toward 0 */
1982
                    case 3: /* toward -infinity */
1983
                        word0(rv) = Big0;
1984
                        word1(rv) = Big1;
1985
                        break;
1986
                    default:
1987
                        word0(rv) = Exp_mask;
1988
                        word1(rv) = 0;
1989
                }
1990
#else /*Honor_FLT_ROUNDS*/
1991
0
                word0(rv) = Exp_mask;
1992
0
                word1(rv) = 0;
1993
0
#endif /*Honor_FLT_ROUNDS*/
1994
#ifdef SET_INEXACT
1995
                /* set overflow bit */
1996
                dval(rv0) = 1e300;
1997
                dval(rv0) *= dval(rv0);
1998
#endif
1999
#else /*IEEE_Arith*/
2000
                word0(rv) = Big0;
2001
                word1(rv) = Big1;
2002
#endif /*IEEE_Arith*/
2003
0
                if (bd0) {
2004
0
                    goto retfree;
2005
0
                }
2006
0
                goto ret;
2007
0
            }
2008
0
            e1 >>= 4;
2009
0
            for(j = 0; e1 > 1; j++, e1 >>= 1)
2010
0
                if (e1 & 1) {
2011
0
                    dval(rv) *= bigtens[j];
2012
0
                }
2013
            /* The last multiplication could overflow. */
2014
0
            word0(rv) -= P*Exp_msk1;
2015
0
            dval(rv) *= bigtens[j];
2016
0
            if ((z = word0(rv) & Exp_mask)
2017
0
                > Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2018
0
                goto ovfl;
2019
0
            }
2020
0
            if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2021
                /* set to largest number */
2022
                /* (Can't trust DBL_MAX) */
2023
0
                word0(rv) = Big0;
2024
0
                word1(rv) = Big1;
2025
0
            }
2026
0
            else {
2027
0
                word0(rv) += P*Exp_msk1;
2028
0
            }
2029
0
        }
2030
0
    }
2031
0
    else if (e1 < 0) {
2032
0
        e1 = -e1;
2033
0
        if (i = e1 & 15) {
2034
0
            dval(rv) /= tens[i];
2035
0
        }
2036
0
        if (e1 >>= 4) {
2037
0
            if (e1 >= 1 << n_bigtens) {
2038
0
                goto undfl;
2039
0
            }
2040
0
#ifdef Avoid_Underflow
2041
0
            if (e1 & Scale_Bit) {
2042
0
                scale = 2*P;
2043
0
            }
2044
0
            for(j = 0; e1 > 0; j++, e1 >>= 1)
2045
0
                if (e1 & 1) {
2046
0
                    dval(rv) *= tinytens[j];
2047
0
                }
2048
0
            if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
2049
0
                                         >> Exp_shift)) > 0) {
2050
                /* scaled rv is denormal; zap j low bits */
2051
0
                if (j >= 32) {
2052
0
                    word1(rv) = 0;
2053
0
                    if (j >= 53) {
2054
0
                        word0(rv) = (P+2)*Exp_msk1;
2055
0
                    }
2056
0
                    else {
2057
0
                        word0(rv) &= 0xffffffff << j-32;
2058
0
                    }
2059
0
                }
2060
0
                else {
2061
0
                    word1(rv) &= 0xffffffff << j;
2062
0
                }
2063
0
            }
2064
#else
2065
            for(j = 0; e1 > 1; j++, e1 >>= 1)
2066
                if (e1 & 1) {
2067
                    dval(rv) *= tinytens[j];
2068
                }
2069
            /* The last multiplication could underflow. */
2070
            dval(rv0) = dval(rv);
2071
            dval(rv) *= tinytens[j];
2072
            if (!dval(rv)) {
2073
                dval(rv) = 2.*dval(rv0);
2074
                dval(rv) *= tinytens[j];
2075
#endif
2076
0
            if (!dval(rv)) {
2077
0
undfl:
2078
0
                dval(rv) = 0.;
2079
0
#ifndef NO_ERRNO
2080
0
                PR_SetError(PR_RANGE_ERROR, 0);
2081
0
#endif
2082
0
                if (bd0) {
2083
0
                    goto retfree;
2084
0
                }
2085
0
                goto ret;
2086
0
            }
2087
#ifndef Avoid_Underflow
2088
            word0(rv) = Tiny0;
2089
            word1(rv) = Tiny1;
2090
            /* The refinement below will clean
2091
             * this approximation up.
2092
             */
2093
        }
2094
#endif
2095
0
    }
2096
0
}
2097
2098
/* Now the hard part -- adjusting rv to the correct value.*/
2099
2100
/* Put digits into bd: true value = bd * 10^e */
2101
2102
0
bd0 = s2b(s0, nd0, nd, y);
2103
2104
0
for(;;) {
2105
0
    bd = Balloc(bd0->k);
2106
0
    Bcopy(bd, bd0);
2107
0
    bb = d2b(dval(rv), &bbe, &bbbits);  /* rv = bb * 2^bbe */
2108
0
    bs = i2b(1);
2109
2110
0
    if (e >= 0) {
2111
0
        bb2 = bb5 = 0;
2112
0
        bd2 = bd5 = e;
2113
0
    }
2114
0
    else {
2115
0
        bb2 = bb5 = -e;
2116
0
        bd2 = bd5 = 0;
2117
0
    }
2118
0
    if (bbe >= 0) {
2119
0
        bb2 += bbe;
2120
0
    }
2121
0
    else {
2122
0
        bd2 -= bbe;
2123
0
    }
2124
0
    bs2 = bb2;
2125
#ifdef Honor_FLT_ROUNDS
2126
    if (rounding != 1) {
2127
        bs2++;
2128
    }
2129
#endif
2130
0
#ifdef Avoid_Underflow
2131
0
    j = bbe - scale;
2132
0
    i = j + bbbits - 1; /* logb(rv) */
2133
0
    if (i < Emin) { /* denormal */
2134
0
        j += P - Emin;
2135
0
    }
2136
0
    else {
2137
0
        j = P + 1 - bbbits;
2138
0
    }
2139
#else /*Avoid_Underflow*/
2140
#ifdef Sudden_Underflow
2141
#ifdef IBM
2142
    j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2143
#else
2144
    j = P + 1 - bbbits;
2145
#endif
2146
#else /*Sudden_Underflow*/
2147
    j = bbe;
2148
    i = j + bbbits - 1; /* logb(rv) */
2149
    if (i < Emin) { /* denormal */
2150
        j += P - Emin;
2151
    }
2152
    else {
2153
        j = P + 1 - bbbits;
2154
    }
2155
#endif /*Sudden_Underflow*/
2156
#endif /*Avoid_Underflow*/
2157
0
    bb2 += j;
2158
0
    bd2 += j;
2159
0
#ifdef Avoid_Underflow
2160
0
    bd2 += scale;
2161
0
#endif
2162
0
    i = bb2 < bd2 ? bb2 : bd2;
2163
0
    if (i > bs2) {
2164
0
        i = bs2;
2165
0
    }
2166
0
    if (i > 0) {
2167
0
        bb2 -= i;
2168
0
        bd2 -= i;
2169
0
        bs2 -= i;
2170
0
    }
2171
0
    if (bb5 > 0) {
2172
0
        bs = pow5mult(bs, bb5);
2173
0
        bb1 = mult(bs, bb);
2174
0
        Bfree(bb);
2175
0
        bb = bb1;
2176
0
    }
2177
0
    if (bb2 > 0) {
2178
0
        bb = lshift(bb, bb2);
2179
0
    }
2180
0
    if (bd5 > 0) {
2181
0
        bd = pow5mult(bd, bd5);
2182
0
    }
2183
0
    if (bd2 > 0) {
2184
0
        bd = lshift(bd, bd2);
2185
0
    }
2186
0
    if (bs2 > 0) {
2187
0
        bs = lshift(bs, bs2);
2188
0
    }
2189
0
    delta = diff(bb, bd);
2190
0
    dsign = delta->sign;
2191
0
    delta->sign = 0;
2192
0
    i = cmp(delta, bs);
2193
#ifdef Honor_FLT_ROUNDS
2194
    if (rounding != 1) {
2195
        if (i < 0) {
2196
            /* Error is less than an ulp */
2197
            if (!delta->x[0] && delta->wds <= 1) {
2198
                /* exact */
2199
#ifdef SET_INEXACT
2200
                inexact = 0;
2201
#endif
2202
                break;
2203
            }
2204
            if (rounding) {
2205
                if (dsign) {
2206
                    adj = 1.;
2207
                    goto apply_adj;
2208
                }
2209
            }
2210
            else if (!dsign) {
2211
                adj = -1.;
2212
                if (!word1(rv)
2213
                    && !(word0(rv) & Frac_mask)) {
2214
                    y = word0(rv) & Exp_mask;
2215
#ifdef Avoid_Underflow
2216
                    if (!scale || y > 2*P*Exp_msk1)
2217
#else
2218
                    if (y)
2219
#endif
2220
                    {
2221
                        delta = lshift(delta,Log2P);
2222
                        if (cmp(delta, bs) <= 0) {
2223
                            adj = -0.5;
2224
                        }
2225
                    }
2226
                }
2227
apply_adj:
2228
#ifdef Avoid_Underflow
2229
                if (scale && (y = word0(rv) & Exp_mask)
2230
                    <= 2*P*Exp_msk1) {
2231
                    word0(adj) += (2*P+1)*Exp_msk1 - y;
2232
                }
2233
#else
2234
#ifdef Sudden_Underflow
2235
                if ((word0(rv) & Exp_mask) <=
2236
                    P*Exp_msk1) {
2237
                    word0(rv) += P*Exp_msk1;
2238
                    dval(rv) += adj*ulp(dval(rv));
2239
                    word0(rv) -= P*Exp_msk1;
2240
                }
2241
                else
2242
#endif /*Sudden_Underflow*/
2243
#endif /*Avoid_Underflow*/
2244
                dval(rv) += adj*ulp(dval(rv));
2245
            }
2246
            break;
2247
        }
2248
        adj = ratio(delta, bs);
2249
        if (adj < 1.) {
2250
            adj = 1.;
2251
        }
2252
        if (adj <= 0x7ffffffe) {
2253
            /* adj = rounding ? ceil(adj) : floor(adj); */
2254
            y = adj;
2255
            if (y != adj) {
2256
                if (!((rounding>>1) ^ dsign)) {
2257
                    y++;
2258
                }
2259
                adj = y;
2260
            }
2261
        }
2262
#ifdef Avoid_Underflow
2263
        if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) {
2264
            word0(adj) += (2*P+1)*Exp_msk1 - y;
2265
        }
2266
#else
2267
#ifdef Sudden_Underflow
2268
        if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2269
            word0(rv) += P*Exp_msk1;
2270
            adj *= ulp(dval(rv));
2271
            if (dsign) {
2272
                dval(rv) += adj;
2273
            }
2274
            else {
2275
                dval(rv) -= adj;
2276
            }
2277
            word0(rv) -= P*Exp_msk1;
2278
            goto cont;
2279
        }
2280
#endif /*Sudden_Underflow*/
2281
#endif /*Avoid_Underflow*/
2282
        adj *= ulp(dval(rv));
2283
        if (dsign) {
2284
            dval(rv) += adj;
2285
        }
2286
        else {
2287
            dval(rv) -= adj;
2288
        }
2289
        goto cont;
2290
    }
2291
#endif /*Honor_FLT_ROUNDS*/
2292
2293
0
    if (i < 0) {
2294
        /* Error is less than half an ulp -- check for
2295
         * special case of mantissa a power of two.
2296
         */
2297
0
        if (dsign || word1(rv) || word0(rv) & Bndry_mask
2298
0
#ifdef IEEE_Arith
2299
0
#ifdef Avoid_Underflow
2300
0
            || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2301
#else
2302
            || (word0(rv) & Exp_mask) <= Exp_msk1
2303
#endif
2304
0
#endif
2305
0
           ) {
2306
#ifdef SET_INEXACT
2307
            if (!delta->x[0] && delta->wds <= 1) {
2308
                inexact = 0;
2309
            }
2310
#endif
2311
0
            break;
2312
0
        }
2313
0
        if (!delta->x[0] && delta->wds <= 1) {
2314
            /* exact result */
2315
#ifdef SET_INEXACT
2316
            inexact = 0;
2317
#endif
2318
0
            break;
2319
0
        }
2320
0
        delta = lshift(delta,Log2P);
2321
0
        if (cmp(delta, bs) > 0) {
2322
0
            goto drop_down;
2323
0
        }
2324
0
        break;
2325
0
    }
2326
0
    if (i == 0) {
2327
        /* exactly half-way between */
2328
0
        if (dsign) {
2329
0
            if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2330
0
                &&  word1(rv) == (
2331
0
#ifdef Avoid_Underflow
2332
0
                    (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2333
0
                    ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2334
0
#endif
2335
0
                    0xffffffff)) {
2336
                /*boundary case -- increment exponent*/
2337
0
                word0(rv) = (word0(rv) & Exp_mask)
2338
0
                            + Exp_msk1
2339
#ifdef IBM
2340
                            | Exp_msk1 >> 4
2341
#endif
2342
0
                            ;
2343
0
                word1(rv) = 0;
2344
0
#ifdef Avoid_Underflow
2345
0
                dsign = 0;
2346
0
#endif
2347
0
                break;
2348
0
            }
2349
0
        }
2350
0
        else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2351
0
drop_down:
2352
            /* boundary case -- decrement exponent */
2353
#ifdef Sudden_Underflow /*{{*/
2354
            L = word0(rv) & Exp_mask;
2355
#ifdef IBM
2356
            if (L <  Exp_msk1)
2357
#else
2358
#ifdef Avoid_Underflow
2359
            if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2360
#else
2361
            if (L <= Exp_msk1)
2362
#endif /*Avoid_Underflow*/
2363
#endif /*IBM*/
2364
                goto undfl;
2365
            L -= Exp_msk1;
2366
#else /*Sudden_Underflow}{*/
2367
0
#ifdef Avoid_Underflow
2368
0
            if (scale) {
2369
0
                L = word0(rv) & Exp_mask;
2370
0
                if (L <= (2*P+1)*Exp_msk1) {
2371
0
                    if (L > (P+2)*Exp_msk1)
2372
                        /* round even ==> */
2373
                        /* accept rv */
2374
0
                    {
2375
0
                        break;
2376
0
                    }
2377
                    /* rv = smallest denormal */
2378
0
                    goto undfl;
2379
0
                }
2380
0
            }
2381
0
#endif /*Avoid_Underflow*/
2382
0
            L = (word0(rv) & Exp_mask) - Exp_msk1;
2383
0
#endif /*Sudden_Underflow}}*/
2384
0
            word0(rv) = L | Bndry_mask1;
2385
0
            word1(rv) = 0xffffffff;
2386
#ifdef IBM
2387
            goto cont;
2388
#else
2389
0
            break;
2390
0
#endif
2391
0
        }
2392
0
#ifndef ROUND_BIASED
2393
0
        if (!(word1(rv) & LSB)) {
2394
0
            break;
2395
0
        }
2396
0
#endif
2397
0
        if (dsign) {
2398
0
            dval(rv) += ulp(dval(rv));
2399
0
        }
2400
0
#ifndef ROUND_BIASED
2401
0
        else {
2402
0
            dval(rv) -= ulp(dval(rv));
2403
0
#ifndef Sudden_Underflow
2404
0
            if (!dval(rv)) {
2405
0
                goto undfl;
2406
0
            }
2407
0
#endif
2408
0
        }
2409
0
#ifdef Avoid_Underflow
2410
0
        dsign = 1 - dsign;
2411
0
#endif
2412
0
#endif
2413
0
        break;
2414
0
    }
2415
0
    if ((aadj = ratio(delta, bs)) <= 2.) {
2416
0
        if (dsign) {
2417
0
            aadj = aadj1 = 1.;
2418
0
        }
2419
0
        else if (word1(rv) || word0(rv) & Bndry_mask) {
2420
0
#ifndef Sudden_Underflow
2421
0
            if (word1(rv) == Tiny1 && !word0(rv)) {
2422
0
                goto undfl;
2423
0
            }
2424
0
#endif
2425
0
            aadj = 1.;
2426
0
            aadj1 = -1.;
2427
0
        }
2428
0
        else {
2429
            /* special case -- power of FLT_RADIX to be */
2430
            /* rounded down... */
2431
2432
0
            if (aadj < 2./FLT_RADIX) {
2433
0
                aadj = 1./FLT_RADIX;
2434
0
            }
2435
0
            else {
2436
0
                aadj *= 0.5;
2437
0
            }
2438
0
            aadj1 = -aadj;
2439
0
        }
2440
0
    }
2441
0
    else {
2442
0
        aadj *= 0.5;
2443
0
        aadj1 = dsign ? aadj : -aadj;
2444
#ifdef Check_FLT_ROUNDS
2445
        switch(Rounding) {
2446
            case 2: /* towards +infinity */
2447
                aadj1 -= 0.5;
2448
                break;
2449
            case 0: /* towards 0 */
2450
            case 3: /* towards -infinity */
2451
                aadj1 += 0.5;
2452
        }
2453
#else
2454
0
        if (Flt_Rounds == 0) {
2455
0
            aadj1 += 0.5;
2456
0
        }
2457
0
#endif /*Check_FLT_ROUNDS*/
2458
0
    }
2459
0
    y = word0(rv) & Exp_mask;
2460
2461
    /* Check for overflow */
2462
2463
0
    if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2464
0
        dval(rv0) = dval(rv);
2465
0
        word0(rv) -= P*Exp_msk1;
2466
0
        adj = aadj1 * ulp(dval(rv));
2467
0
        dval(rv) += adj;
2468
0
        if ((word0(rv) & Exp_mask) >=
2469
0
            Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2470
0
            if (word0(rv0) == Big0 && word1(rv0) == Big1) {
2471
0
                goto ovfl;
2472
0
            }
2473
0
            word0(rv) = Big0;
2474
0
            word1(rv) = Big1;
2475
0
            goto cont;
2476
0
        }
2477
0
        else {
2478
0
            word0(rv) += P*Exp_msk1;
2479
0
        }
2480
0
    }
2481
0
    else {
2482
0
#ifdef Avoid_Underflow
2483
0
        if (scale && y <= 2*P*Exp_msk1) {
2484
0
            if (aadj <= 0x7fffffff) {
2485
0
                if ((z = aadj) <= 0) {
2486
0
                    z = 1;
2487
0
                }
2488
0
                aadj = z;
2489
0
                aadj1 = dsign ? aadj : -aadj;
2490
0
            }
2491
0
            dval(aadj2) = aadj1;
2492
0
            word0(aadj2) += (2*P+1)*Exp_msk1 - y;
2493
0
            aadj1 = dval(aadj2);
2494
0
        }
2495
0
        adj = aadj1 * ulp(dval(rv));
2496
0
        dval(rv) += adj;
2497
#else
2498
#ifdef Sudden_Underflow
2499
        if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2500
            dval(rv0) = dval(rv);
2501
            word0(rv) += P*Exp_msk1;
2502
            adj = aadj1 * ulp(dval(rv));
2503
            dval(rv) += adj;
2504
#ifdef IBM
2505
            if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
2506
#else
2507
            if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2508
#endif
2509
            {
2510
                if (word0(rv0) == Tiny0
2511
                    && word1(rv0) == Tiny1) {
2512
                    goto undfl;
2513
                }
2514
                word0(rv) = Tiny0;
2515
                word1(rv) = Tiny1;
2516
                goto cont;
2517
            }
2518
            else {
2519
                word0(rv) -= P*Exp_msk1;
2520
            }
2521
        }
2522
        else {
2523
            adj = aadj1 * ulp(dval(rv));
2524
            dval(rv) += adj;
2525
        }
2526
#else /*Sudden_Underflow*/
2527
        /* Compute adj so that the IEEE rounding rules will
2528
         * correctly round rv + adj in some half-way cases.
2529
         * If rv * ulp(rv) is denormalized (i.e.,
2530
         * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2531
         * trouble from bits lost to denormalization;
2532
         * example: 1.2e-307 .
2533
         */
2534
        if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2535
            aadj1 = (double)(int)(aadj + 0.5);
2536
            if (!dsign) {
2537
                aadj1 = -aadj1;
2538
            }
2539
        }
2540
        adj = aadj1 * ulp(dval(rv));
2541
        dval(rv) += adj;
2542
#endif /*Sudden_Underflow*/
2543
#endif /*Avoid_Underflow*/
2544
0
    }
2545
0
    z = word0(rv) & Exp_mask;
2546
0
#ifndef SET_INEXACT
2547
0
#ifdef Avoid_Underflow
2548
0
    if (!scale)
2549
0
#endif
2550
0
        if (y == z) {
2551
            /* Can we stop now? */
2552
0
            L = (Long)aadj;
2553
0
            aadj -= L;
2554
            /* The tolerances below are conservative. */
2555
0
            if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2556
0
                if (aadj < .4999999 || aadj > .5000001) {
2557
0
                    break;
2558
0
                }
2559
0
            }
2560
0
            else if (aadj < .4999999/FLT_RADIX) {
2561
0
                break;
2562
0
            }
2563
0
        }
2564
0
#endif
2565
0
cont:
2566
0
    Bfree(bb);
2567
0
    Bfree(bd);
2568
0
    Bfree(bs);
2569
0
    Bfree(delta);
2570
0
}
2571
#ifdef SET_INEXACT
2572
if (inexact) {
2573
    if (!oldinexact) {
2574
        word0(rv0) = Exp_1 + (70 << Exp_shift);
2575
        word1(rv0) = 0;
2576
        dval(rv0) += 1.;
2577
    }
2578
}
2579
else if (!oldinexact) {
2580
    clear_inexact();
2581
}
2582
#endif
2583
0
#ifdef Avoid_Underflow
2584
0
if (scale) {
2585
0
    word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2586
0
    word1(rv0) = 0;
2587
0
    dval(rv) *= dval(rv0);
2588
0
#ifndef NO_ERRNO
2589
    /* try to avoid the bug of testing an 8087 register value */
2590
0
    if (word0(rv) == 0 && word1(rv) == 0) {
2591
0
        PR_SetError(PR_RANGE_ERROR, 0);
2592
0
    }
2593
0
#endif
2594
0
}
2595
0
#endif /* Avoid_Underflow */
2596
#ifdef SET_INEXACT
2597
if (inexact && !(word0(rv) & Exp_mask)) {
2598
    /* set underflow bit */
2599
    dval(rv0) = 1e-300;
2600
    dval(rv0) *= dval(rv0);
2601
}
2602
#endif
2603
0
retfree:
2604
0
Bfree(bb);
2605
0
Bfree(bd);
2606
0
Bfree(bs);
2607
0
Bfree(bd0);
2608
0
Bfree(delta);
2609
0
ret:
2610
0
if (se) {
2611
0
    *se = (char *)s;
2612
0
}
2613
0
return sign ? -dval(rv) : dval(rv);
2614
0
}
2615
2616
static int
2617
quorem
2618
#ifdef KR_headers
2619
(b, S) Bigint *b, *S;
2620
#else
2621
(Bigint *b, Bigint *S)
2622
#endif
2623
0
{
2624
0
    int n;
2625
0
    ULong *bx, *bxe, q, *sx, *sxe;
2626
#ifdef ULLong
2627
    ULLong borrow, carry, y, ys;
2628
#else
2629
0
    ULong borrow, carry, y, ys;
2630
0
#ifdef Pack_32
2631
0
    ULong si, z, zs;
2632
0
#endif
2633
0
#endif
2634
2635
0
    n = S->wds;
2636
0
#ifdef DEBUG
2637
0
    /*debug*/ if (b->wds > n)
2638
0
        /*debug*/{
2639
0
        Bug("oversize b in quorem");
2640
0
    }
2641
0
#endif
2642
0
    if (b->wds < n) {
2643
0
        return 0;
2644
0
    }
2645
0
    sx = S->x;
2646
0
    sxe = sx + --n;
2647
0
    bx = b->x;
2648
0
    bxe = bx + n;
2649
0
    q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
2650
0
#ifdef DEBUG
2651
0
    /*debug*/ if (q > 9)
2652
0
        /*debug*/{
2653
0
        Bug("oversized quotient in quorem");
2654
0
    }
2655
0
#endif
2656
0
    if (q) {
2657
0
        borrow = 0;
2658
0
        carry = 0;
2659
0
        do {
2660
#ifdef ULLong
2661
            ys = *sx++ * (ULLong)q + carry;
2662
            carry = ys >> 32;
2663
            y = *bx - (ys & FFFFFFFF) - borrow;
2664
            borrow = y >> 32 & (ULong)1;
2665
            *bx++ = y & FFFFFFFF;
2666
#else
2667
0
#ifdef Pack_32
2668
0
            si = *sx++;
2669
0
            ys = (si & 0xffff) * q + carry;
2670
0
            zs = (si >> 16) * q + (ys >> 16);
2671
0
            carry = zs >> 16;
2672
0
            y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2673
0
            borrow = (y & 0x10000) >> 16;
2674
0
            z = (*bx >> 16) - (zs & 0xffff) - borrow;
2675
0
            borrow = (z & 0x10000) >> 16;
2676
0
            Storeinc(bx, z, y);
2677
#else
2678
            ys = *sx++ * q + carry;
2679
            carry = ys >> 16;
2680
            y = *bx - (ys & 0xffff) - borrow;
2681
            borrow = (y & 0x10000) >> 16;
2682
            *bx++ = y & 0xffff;
2683
#endif
2684
0
#endif
2685
0
        }
2686
0
        while(sx <= sxe);
2687
0
        if (!*bxe) {
2688
0
            bx = b->x;
2689
0
            while(--bxe > bx && !*bxe) {
2690
0
                --n;
2691
0
            }
2692
0
            b->wds = n;
2693
0
        }
2694
0
    }
2695
0
    if (cmp(b, S) >= 0) {
2696
0
        q++;
2697
0
        borrow = 0;
2698
0
        carry = 0;
2699
0
        bx = b->x;
2700
0
        sx = S->x;
2701
0
        do {
2702
#ifdef ULLong
2703
            ys = *sx++ + carry;
2704
            carry = ys >> 32;
2705
            y = *bx - (ys & FFFFFFFF) - borrow;
2706
            borrow = y >> 32 & (ULong)1;
2707
            *bx++ = y & FFFFFFFF;
2708
#else
2709
0
#ifdef Pack_32
2710
0
            si = *sx++;
2711
0
            ys = (si & 0xffff) + carry;
2712
0
            zs = (si >> 16) + (ys >> 16);
2713
0
            carry = zs >> 16;
2714
0
            y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2715
0
            borrow = (y & 0x10000) >> 16;
2716
0
            z = (*bx >> 16) - (zs & 0xffff) - borrow;
2717
0
            borrow = (z & 0x10000) >> 16;
2718
0
            Storeinc(bx, z, y);
2719
#else
2720
            ys = *sx++ + carry;
2721
            carry = ys >> 16;
2722
            y = *bx - (ys & 0xffff) - borrow;
2723
            borrow = (y & 0x10000) >> 16;
2724
            *bx++ = y & 0xffff;
2725
#endif
2726
0
#endif
2727
0
        }
2728
0
        while(sx <= sxe);
2729
0
        bx = b->x;
2730
0
        bxe = bx + n;
2731
0
        if (!*bxe) {
2732
0
            while(--bxe > bx && !*bxe) {
2733
0
                --n;
2734
0
            }
2735
0
            b->wds = n;
2736
0
        }
2737
0
    }
2738
0
    return q;
2739
0
}
2740
2741
#ifndef MULTIPLE_THREADS
2742
static char *dtoa_result;
2743
#endif
2744
2745
static char *
2746
#ifdef KR_headers
2747
rv_alloc(i) int i;
2748
#else
2749
rv_alloc(int i)
2750
#endif
2751
0
{
2752
0
    int j, k, *r;
2753
2754
0
    j = sizeof(ULong);
2755
0
    for(k = 0;
2756
0
        sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
2757
0
        j <<= 1) {
2758
0
        k++;
2759
0
    }
2760
0
    r = (int*)Balloc(k);
2761
0
    *r = k;
2762
0
    return
2763
#ifndef MULTIPLE_THREADS
2764
        dtoa_result =
2765
#endif
2766
0
            (char *)(r+1);
2767
0
}
2768
2769
static char *
2770
#ifdef KR_headers
2771
nrv_alloc(s, rve, n) char *s, **rve; int n;
2772
#else
2773
nrv_alloc(char *s, char **rve, int n)
2774
#endif
2775
0
{
2776
0
    char *rv, *t;
2777
2778
0
    t = rv = rv_alloc(n);
2779
0
    while(*t = *s++) {
2780
0
        t++;
2781
0
    }
2782
0
    if (rve) {
2783
0
        *rve = t;
2784
0
    }
2785
0
    return rv;
2786
0
}
2787
2788
/* freedtoa(s) must be used to free values s returned by dtoa
2789
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
2790
 * but for consistency with earlier versions of dtoa, it is optional
2791
 * when MULTIPLE_THREADS is not defined.
2792
 */
2793
2794
static void
2795
#ifdef KR_headers
2796
freedtoa(s) char *s;
2797
#else
2798
freedtoa(char *s)
2799
#endif
2800
0
{
2801
0
    Bigint *b = (Bigint *)((int *)s - 1);
2802
0
    b->maxwds = 1 << (b->k = *(int*)b);
2803
0
    Bfree(b);
2804
#ifndef MULTIPLE_THREADS
2805
    if (s == dtoa_result) {
2806
        dtoa_result = 0;
2807
    }
2808
#endif
2809
0
}
2810
2811
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2812
 *
2813
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2814
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2815
 *
2816
 * Modifications:
2817
 *  1. Rather than iterating, we use a simple numeric overestimate
2818
 *     to determine k = floor(log10(d)).  We scale relevant
2819
 *     quantities using O(log2(k)) rather than O(k) multiplications.
2820
 *  2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2821
 *     try to generate digits strictly left to right.  Instead, we
2822
 *     compute with fewer bits and propagate the carry if necessary
2823
 *     when rounding the final digit up.  This is often faster.
2824
 *  3. Under the assumption that input will be rounded nearest,
2825
 *     mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2826
 *     That is, we allow equality in stopping tests when the
2827
 *     round-nearest rule will give the same floating-point value
2828
 *     as would satisfaction of the stopping test with strict
2829
 *     inequality.
2830
 *  4. We remove common factors of powers of 2 from relevant
2831
 *     quantities.
2832
 *  5. When converting floating-point integers less than 1e16,
2833
 *     we use floating-point arithmetic rather than resorting
2834
 *     to multiple-precision integers.
2835
 *  6. When asked to produce fewer than 15 digits, we first try
2836
 *     to get by with floating-point arithmetic; we resort to
2837
 *     multiple-precision integer arithmetic only if we cannot
2838
 *     guarantee that the floating-point calculation has given
2839
 *     the correctly rounded result.  For k requested digits and
2840
 *     "uniformly" distributed input, the probability is
2841
 *     something like 10^(k-15) that we must resort to the Long
2842
 *     calculation.
2843
 */
2844
2845
static char *
2846
dtoa
2847
#ifdef KR_headers
2848
(dd, mode, ndigits, decpt, sign, rve)
2849
double dd; int mode, ndigits, *decpt, *sign; char **rve;
2850
#else
2851
(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
2852
#endif
2853
0
{
2854
    /* Arguments ndigits, decpt, sign are similar to those
2855
    of ecvt and fcvt; trailing zeros are suppressed from
2856
    the returned string.  If not null, *rve is set to point
2857
    to the end of the return value.  If d is +-Infinity or NaN,
2858
    then *decpt is set to 9999.
2859
2860
    mode:
2861
       0 ==> shortest string that yields d when read in
2862
           and rounded to nearest.
2863
       1 ==> like 0, but with Steele & White stopping rule;
2864
           e.g. with IEEE P754 arithmetic , mode 0 gives
2865
           1e23 whereas mode 1 gives 9.999999999999999e22.
2866
       2 ==> max(1,ndigits) significant digits.  This gives a
2867
           return value similar to that of ecvt, except
2868
           that trailing zeros are suppressed.
2869
       3 ==> through ndigits past the decimal point.  This
2870
           gives a return value similar to that from fcvt,
2871
           except that trailing zeros are suppressed, and
2872
           ndigits can be negative.
2873
       4,5 ==> similar to 2 and 3, respectively, but (in
2874
           round-nearest mode) with the tests of mode 0 to
2875
           possibly return a shorter string that rounds to d.
2876
           With IEEE arithmetic and compilation with
2877
           -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2878
           as modes 2 and 3 when FLT_ROUNDS != 1.
2879
       6-9 ==> Debugging modes similar to mode - 4:  don't try
2880
           fast floating-point estimate (if applicable).
2881
2882
       Values of mode other than 0-9 are treated as mode 0.
2883
2884
       Sufficient space is allocated to the return value
2885
       to hold the suppressed trailing zeros.
2886
    */
2887
2888
0
    int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2889
0
        j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2890
0
        spec_case, try_quick;
2891
0
    Long L;
2892
0
#ifndef Sudden_Underflow
2893
0
    int denorm;
2894
0
    ULong x;
2895
0
#endif
2896
0
    Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2897
0
    U d, d2, eps;
2898
0
    double ds;
2899
0
    char *s, *s0;
2900
#ifdef Honor_FLT_ROUNDS
2901
    int rounding;
2902
#endif
2903
#ifdef SET_INEXACT
2904
    int inexact, oldinexact;
2905
#endif
2906
2907
#ifndef MULTIPLE_THREADS
2908
    if (dtoa_result) {
2909
        freedtoa(dtoa_result);
2910
        dtoa_result = 0;
2911
    }
2912
#endif
2913
2914
0
    dval(d) = dd;
2915
0
    if (word0(d) & Sign_bit) {
2916
        /* set sign for everything, including 0's and NaNs */
2917
0
        *sign = 1;
2918
0
        word0(d) &= ~Sign_bit;  /* clear sign bit */
2919
0
    }
2920
0
    else {
2921
0
        *sign = 0;
2922
0
    }
2923
2924
0
#if defined(IEEE_Arith) + defined(VAX)
2925
0
#ifdef IEEE_Arith
2926
0
    if ((word0(d) & Exp_mask) == Exp_mask)
2927
#else
2928
    if (word0(d)  == 0x8000)
2929
#endif
2930
0
    {
2931
        /* Infinity or NaN */
2932
0
        *decpt = 9999;
2933
0
#ifdef IEEE_Arith
2934
0
        if (!word1(d) && !(word0(d) & 0xfffff)) {
2935
0
            return nrv_alloc("Infinity", rve, 8);
2936
0
        }
2937
0
#endif
2938
0
        return nrv_alloc("NaN", rve, 3);
2939
0
    }
2940
0
#endif
2941
#ifdef IBM
2942
    dval(d) += 0; /* normalize */
2943
#endif
2944
0
    if (!dval(d)) {
2945
0
        *decpt = 1;
2946
0
        return nrv_alloc("0", rve, 1);
2947
0
    }
2948
2949
#ifdef SET_INEXACT
2950
    try_quick = oldinexact = get_inexact();
2951
    inexact = 1;
2952
#endif
2953
#ifdef Honor_FLT_ROUNDS
2954
    if ((rounding = Flt_Rounds) >= 2) {
2955
        if (*sign) {
2956
            rounding = rounding == 2 ? 0 : 2;
2957
        }
2958
        else if (rounding != 2) {
2959
            rounding = 0;
2960
        }
2961
    }
2962
#endif
2963
2964
0
    b = d2b(dval(d), &be, &bbits);
2965
#ifdef Sudden_Underflow
2966
    i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2967
#else
2968
0
    if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
2969
0
#endif
2970
0
    dval(d2) = dval(d);
2971
0
    word0(d2) &= Frac_mask1;
2972
0
    word0(d2) |= Exp_11;
2973
#ifdef IBM
2974
    if (j = 11 - hi0bits(word0(d2) & Frac_mask)) {
2975
        dval(d2) /= 1 << j;
2976
    }
2977
#endif
2978
2979
    /* log(x)   ~=~ log(1.5) + (x-1.5)/1.5
2980
     * log10(x)  =  log(x) / log(10)
2981
     *      ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2982
     * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2983
     *
2984
     * This suggests computing an approximation k to log10(d) by
2985
     *
2986
     * k = (i - Bias)*0.301029995663981
2987
     *  + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2988
     *
2989
     * We want k to be too large rather than too small.
2990
     * The error in the first-order Taylor series approximation
2991
     * is in our favor, so we just round up the constant enough
2992
     * to compensate for any error in the multiplication of
2993
     * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2994
     * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2995
     * adding 1e-13 to the constant term more than suffices.
2996
     * Hence we adjust the constant term to 0.1760912590558.
2997
     * (We could get a more accurate k by invoking log10,
2998
     *  but this is probably not worthwhile.)
2999
     */
3000
3001
0
    i -= Bias;
3002
#ifdef IBM
3003
    i <<= 2;
3004
    i += j;
3005
#endif
3006
0
#ifndef Sudden_Underflow
3007
0
    denorm = 0;
3008
0
}
3009
0
else {
3010
    /* d is denormalized */
3011
3012
0
    i = bbits + be + (Bias + (P-1) - 1);
3013
0
    x = i > 32  ? word0(d) << 64 - i | word1(d) >> i - 32
3014
0
        : word1(d) << 32 - i;
3015
0
    dval(d2) = x;
3016
0
    word0(d2) -= 31*Exp_msk1; /* adjust exponent */
3017
0
    i -= (Bias + (P-1) - 1) + 1;
3018
0
    denorm = 1;
3019
0
}
3020
0
#endif
3021
0
ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
3022
0
k = (int)ds;
3023
0
if (ds < 0. && ds != k) {
3024
0
    k--;    /* want k = floor(ds) */
3025
0
}
3026
0
k_check = 1;
3027
0
if (k >= 0 && k <= Ten_pmax) {
3028
0
    if (dval(d) < tens[k]) {
3029
0
        k--;
3030
0
    }
3031
0
    k_check = 0;
3032
0
}
3033
0
j = bbits - i - 1;
3034
0
if (j >= 0) {
3035
0
    b2 = 0;
3036
0
    s2 = j;
3037
0
}
3038
0
else {
3039
0
    b2 = -j;
3040
0
    s2 = 0;
3041
0
}
3042
0
if (k >= 0) {
3043
0
    b5 = 0;
3044
0
    s5 = k;
3045
0
    s2 += k;
3046
0
}
3047
0
else {
3048
0
    b2 -= k;
3049
0
    b5 = -k;
3050
0
    s5 = 0;
3051
0
}
3052
0
if (mode < 0 || mode > 9) {
3053
0
    mode = 0;
3054
0
}
3055
3056
0
#ifndef SET_INEXACT
3057
#ifdef Check_FLT_ROUNDS
3058
try_quick = Rounding == 1;
3059
#else
3060
0
try_quick = 1;
3061
0
#endif
3062
0
#endif /*SET_INEXACT*/
3063
3064
0
if (mode > 5) {
3065
0
    mode -= 4;
3066
0
    try_quick = 0;
3067
0
}
3068
0
leftright = 1;
3069
0
switch(mode) {
3070
0
case 0:
3071
0
case 1:
3072
0
    ilim = ilim1 = -1;
3073
0
    i = 18;
3074
0
    ndigits = 0;
3075
0
    break;
3076
0
case 2:
3077
0
    leftright = 0;
3078
/* no break */
3079
0
case 4:
3080
0
    if (ndigits <= 0) {
3081
0
        ndigits = 1;
3082
0
    }
3083
0
    ilim = ilim1 = i = ndigits;
3084
0
    break;
3085
0
case 3:
3086
0
    leftright = 0;
3087
/* no break */
3088
0
case 5:
3089
0
    i = ndigits + k + 1;
3090
0
    ilim = i;
3091
0
    ilim1 = i - 1;
3092
0
    if (i <= 0) {
3093
0
        i = 1;
3094
0
    }
3095
0
}
3096
0
s = s0 = rv_alloc(i);
3097
3098
#ifdef Honor_FLT_ROUNDS
3099
if (mode > 1 && rounding != 1) {
3100
    leftright = 0;
3101
}
3102
#endif
3103
3104
0
if (ilim >= 0 && ilim <= Quick_max && try_quick) {
3105
3106
    /* Try to get by with floating-point arithmetic. */
3107
3108
0
    i = 0;
3109
0
    dval(d2) = dval(d);
3110
0
    k0 = k;
3111
0
    ilim0 = ilim;
3112
0
    ieps = 2; /* conservative */
3113
0
    if (k > 0) {
3114
0
        ds = tens[k&0xf];
3115
0
        j = k >> 4;
3116
0
        if (j & Bletch) {
3117
            /* prevent overflows */
3118
0
            j &= Bletch - 1;
3119
0
            dval(d) /= bigtens[n_bigtens-1];
3120
0
            ieps++;
3121
0
        }
3122
0
        for(; j; j >>= 1, i++)
3123
0
            if (j & 1) {
3124
0
                ieps++;
3125
0
                ds *= bigtens[i];
3126
0
            }
3127
0
        dval(d) /= ds;
3128
0
    }
3129
0
    else if (j1 = -k) {
3130
0
        dval(d) *= tens[j1 & 0xf];
3131
0
        for(j = j1 >> 4; j; j >>= 1, i++)
3132
0
            if (j & 1) {
3133
0
                ieps++;
3134
0
                dval(d) *= bigtens[i];
3135
0
            }
3136
0
    }
3137
0
    if (k_check && dval(d) < 1. && ilim > 0) {
3138
0
        if (ilim1 <= 0) {
3139
0
            goto fast_failed;
3140
0
        }
3141
0
        ilim = ilim1;
3142
0
        k--;
3143
0
        dval(d) *= 10.;
3144
0
        ieps++;
3145
0
    }
3146
0
    dval(eps) = ieps*dval(d) + 7.;
3147
0
    word0(eps) -= (P-1)*Exp_msk1;
3148
0
    if (ilim == 0) {
3149
0
        S = mhi = 0;
3150
0
        dval(d) -= 5.;
3151
0
        if (dval(d) > dval(eps)) {
3152
0
            goto one_digit;
3153
0
        }
3154
0
        if (dval(d) < -dval(eps)) {
3155
0
            goto no_digits;
3156
0
        }
3157
0
        goto fast_failed;
3158
0
    }
3159
0
#ifndef No_leftright
3160
0
    if (leftright) {
3161
        /* Use Steele & White method of only
3162
         * generating digits needed.
3163
         */
3164
0
        dval(eps) = 0.5/tens[ilim-1] - dval(eps);
3165
0
        for(i = 0;;) {
3166
0
            L = dval(d);
3167
0
            dval(d) -= L;
3168
0
            *s++ = '0' + (int)L;
3169
0
            if (dval(d) < dval(eps)) {
3170
0
                goto ret1;
3171
0
            }
3172
0
            if (1. - dval(d) < dval(eps)) {
3173
0
                goto bump_up;
3174
0
            }
3175
0
            if (++i >= ilim) {
3176
0
                break;
3177
0
            }
3178
0
            dval(eps) *= 10.;
3179
0
            dval(d) *= 10.;
3180
0
        }
3181
0
    }
3182
0
    else {
3183
0
#endif
3184
        /* Generate ilim digits, then fix them up. */
3185
0
        dval(eps) *= tens[ilim-1];
3186
0
        for(i = 1;; i++, dval(d) *= 10.) {
3187
0
            L = (Long)(dval(d));
3188
0
            if (!(dval(d) -= L)) {
3189
0
                ilim = i;
3190
0
            }
3191
0
            *s++ = '0' + (int)L;
3192
0
            if (i == ilim) {
3193
0
                if (dval(d) > 0.5 + dval(eps)) {
3194
0
                    goto bump_up;
3195
0
                }
3196
0
                else if (dval(d) < 0.5 - dval(eps)) {
3197
0
                    while(*--s == '0');
3198
0
                    s++;
3199
0
                    goto ret1;
3200
0
                }
3201
0
                break;
3202
0
            }
3203
0
        }
3204
0
#ifndef No_leftright
3205
0
    }
3206
0
#endif
3207
0
fast_failed:
3208
0
    s = s0;
3209
0
    dval(d) = dval(d2);
3210
0
    k = k0;
3211
0
    ilim = ilim0;
3212
0
}
3213
3214
/* Do we have a "small" integer? */
3215
3216
0
if (be >= 0 && k <= Int_max) {
3217
    /* Yes. */
3218
0
    ds = tens[k];
3219
0
    if (ndigits < 0 && ilim <= 0) {
3220
0
        S = mhi = 0;
3221
0
        if (ilim < 0 || dval(d) <= 5*ds) {
3222
0
            goto no_digits;
3223
0
        }
3224
0
        goto one_digit;
3225
0
    }
3226
0
    for(i = 1; i <= k+1; i++, dval(d) *= 10.) {
3227
0
        L = (Long)(dval(d) / ds);
3228
0
        dval(d) -= L*ds;
3229
#ifdef Check_FLT_ROUNDS
3230
        /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3231
        if (dval(d) < 0) {
3232
            L--;
3233
            dval(d) += ds;
3234
        }
3235
#endif
3236
0
        *s++ = '0' + (int)L;
3237
0
        if (!dval(d)) {
3238
#ifdef SET_INEXACT
3239
            inexact = 0;
3240
#endif
3241
0
            break;
3242
0
        }
3243
0
        if (i == ilim) {
3244
#ifdef Honor_FLT_ROUNDS
3245
            if (mode > 1)
3246
                switch(rounding) {
3247
                    case 0: goto ret1;
3248
                    case 2: goto bump_up;
3249
                }
3250
#endif
3251
0
            dval(d) += dval(d);
3252
0
            if (dval(d) > ds || dval(d) == ds && L & 1) {
3253
0
bump_up:
3254
0
                while(*--s == '9')
3255
0
                    if (s == s0) {
3256
0
                        k++;
3257
0
                        *s = '0';
3258
0
                        break;
3259
0
                    }
3260
0
                ++*s++;
3261
0
            }
3262
0
            break;
3263
0
        }
3264
0
    }
3265
0
    goto ret1;
3266
0
}
3267
3268
0
m2 = b2;
3269
0
m5 = b5;
3270
0
mhi = mlo = 0;
3271
0
if (leftright) {
3272
0
    i =
3273
0
#ifndef Sudden_Underflow
3274
0
        denorm ? be + (Bias + (P-1) - 1 + 1) :
3275
0
#endif
3276
#ifdef IBM
3277
        1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
3278
#else
3279
0
        1 + P - bbits;
3280
0
#endif
3281
0
    b2 += i;
3282
0
    s2 += i;
3283
0
    mhi = i2b(1);
3284
0
}
3285
0
if (m2 > 0 && s2 > 0) {
3286
0
    i = m2 < s2 ? m2 : s2;
3287
0
    b2 -= i;
3288
0
    m2 -= i;
3289
0
    s2 -= i;
3290
0
}
3291
0
if (b5 > 0) {
3292
0
    if (leftright) {
3293
0
        if (m5 > 0) {
3294
0
            mhi = pow5mult(mhi, m5);
3295
0
            b1 = mult(mhi, b);
3296
0
            Bfree(b);
3297
0
            b = b1;
3298
0
        }
3299
0
        if (j = b5 - m5) {
3300
0
            b = pow5mult(b, j);
3301
0
        }
3302
0
    }
3303
0
    else {
3304
0
        b = pow5mult(b, b5);
3305
0
    }
3306
0
}
3307
0
S = i2b(1);
3308
0
if (s5 > 0) {
3309
0
    S = pow5mult(S, s5);
3310
0
}
3311
3312
/* Check for special case that d is a normalized power of 2. */
3313
3314
0
spec_case = 0;
3315
0
if ((mode < 2 || leftright)
3316
#ifdef Honor_FLT_ROUNDS
3317
    && rounding == 1
3318
#endif
3319
0
   ) {
3320
0
    if (!word1(d) && !(word0(d) & Bndry_mask)
3321
0
#ifndef Sudden_Underflow
3322
0
        && word0(d) & (Exp_mask & ~Exp_msk1)
3323
0
#endif
3324
0
       ) {
3325
        /* The special case */
3326
0
        b2 += Log2P;
3327
0
        s2 += Log2P;
3328
0
        spec_case = 1;
3329
0
    }
3330
0
}
3331
3332
/* Arrange for convenient computation of quotients:
3333
 * shift left if necessary so divisor has 4 leading 0 bits.
3334
 *
3335
 * Perhaps we should just compute leading 28 bits of S once
3336
 * and for all and pass them and a shift to quorem, so it
3337
 * can do shifts and ors to compute the numerator for q.
3338
 */
3339
0
#ifdef Pack_32
3340
0
if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) {
3341
0
    i = 32 - i;
3342
0
}
3343
#else
3344
if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) {
3345
    i = 16 - i;
3346
}
3347
#endif
3348
0
if (i > 4) {
3349
0
    i -= 4;
3350
0
    b2 += i;
3351
0
    m2 += i;
3352
0
    s2 += i;
3353
0
}
3354
0
else if (i < 4) {
3355
0
    i += 28;
3356
0
    b2 += i;
3357
0
    m2 += i;
3358
0
    s2 += i;
3359
0
}
3360
0
if (b2 > 0) {
3361
0
    b = lshift(b, b2);
3362
0
}
3363
0
if (s2 > 0) {
3364
0
    S = lshift(S, s2);
3365
0
}
3366
0
if (k_check) {
3367
0
    if (cmp(b,S) < 0) {
3368
0
        k--;
3369
0
        b = multadd(b, 10, 0);  /* we botched the k estimate */
3370
0
        if (leftright) {
3371
0
            mhi = multadd(mhi, 10, 0);
3372
0
        }
3373
0
        ilim = ilim1;
3374
0
    }
3375
0
}
3376
0
if (ilim <= 0 && (mode == 3 || mode == 5)) {
3377
0
    if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3378
        /* no digits, fcvt style */
3379
0
no_digits:
3380
0
        k = -1 - ndigits;
3381
0
        goto ret;
3382
0
    }
3383
0
one_digit:
3384
0
    *s++ = '1';
3385
0
    k++;
3386
0
    goto ret;
3387
0
}
3388
0
if (leftright) {
3389
0
    if (m2 > 0) {
3390
0
        mhi = lshift(mhi, m2);
3391
0
    }
3392
3393
    /* Compute mlo -- check for special case
3394
     * that d is a normalized power of 2.
3395
     */
3396
3397
0
    mlo = mhi;
3398
0
    if (spec_case) {
3399
0
        mhi = Balloc(mhi->k);
3400
0
        Bcopy(mhi, mlo);
3401
0
        mhi = lshift(mhi, Log2P);
3402
0
    }
3403
3404
0
    for(i = 1;; i++) {
3405
0
        dig = quorem(b,S) + '0';
3406
        /* Do we yet have the shortest decimal string
3407
         * that will round to d?
3408
         */
3409
0
        j = cmp(b, mlo);
3410
0
        delta = diff(S, mhi);
3411
0
        j1 = delta->sign ? 1 : cmp(b, delta);
3412
0
        Bfree(delta);
3413
0
#ifndef ROUND_BIASED
3414
0
        if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3415
#ifdef Honor_FLT_ROUNDS
3416
            && rounding >= 1
3417
#endif
3418
0
           ) {
3419
0
            if (dig == '9') {
3420
0
                goto round_9_up;
3421
0
            }
3422
0
            if (j > 0) {
3423
0
                dig++;
3424
0
            }
3425
#ifdef SET_INEXACT
3426
            else if (!b->x[0] && b->wds <= 1) {
3427
                inexact = 0;
3428
            }
3429
#endif
3430
0
            *s++ = dig;
3431
0
            goto ret;
3432
0
        }
3433
0
#endif
3434
0
        if (j < 0 || j == 0 && mode != 1
3435
0
#ifndef ROUND_BIASED
3436
0
            && !(word1(d) & 1)
3437
0
#endif
3438
0
           ) {
3439
0
            if (!b->x[0] && b->wds <= 1) {
3440
#ifdef SET_INEXACT
3441
                inexact = 0;
3442
#endif
3443
0
                goto accept_dig;
3444
0
            }
3445
#ifdef Honor_FLT_ROUNDS
3446
            if (mode > 1)
3447
                switch(rounding) {
3448
                    case 0: goto accept_dig;
3449
                    case 2: goto keep_dig;
3450
                }
3451
#endif /*Honor_FLT_ROUNDS*/
3452
0
            if (j1 > 0) {
3453
0
                b = lshift(b, 1);
3454
0
                j1 = cmp(b, S);
3455
0
                if ((j1 > 0 || j1 == 0 && dig & 1)
3456
0
                    && dig++ == '9') {
3457
0
                    goto round_9_up;
3458
0
                }
3459
0
            }
3460
0
accept_dig:
3461
0
            *s++ = dig;
3462
0
            goto ret;
3463
0
        }
3464
0
        if (j1 > 0) {
3465
#ifdef Honor_FLT_ROUNDS
3466
            if (!rounding) {
3467
                goto accept_dig;
3468
            }
3469
#endif
3470
0
            if (dig == '9') { /* possible if i == 1 */
3471
0
round_9_up:
3472
0
                *s++ = '9';
3473
0
                goto roundoff;
3474
0
            }
3475
0
            *s++ = dig + 1;
3476
0
            goto ret;
3477
0
        }
3478
#ifdef Honor_FLT_ROUNDS
3479
keep_dig:
3480
#endif
3481
0
        *s++ = dig;
3482
0
        if (i == ilim) {
3483
0
            break;
3484
0
        }
3485
0
        b = multadd(b, 10, 0);
3486
0
        if (mlo == mhi) {
3487
0
            mlo = mhi = multadd(mhi, 10, 0);
3488
0
        }
3489
0
        else {
3490
0
            mlo = multadd(mlo, 10, 0);
3491
0
            mhi = multadd(mhi, 10, 0);
3492
0
        }
3493
0
    }
3494
0
}
3495
0
else
3496
0
    for(i = 1;; i++) {
3497
0
        *s++ = dig = quorem(b,S) + '0';
3498
0
        if (!b->x[0] && b->wds <= 1) {
3499
#ifdef SET_INEXACT
3500
            inexact = 0;
3501
#endif
3502
0
            goto ret;
3503
0
        }
3504
0
        if (i >= ilim) {
3505
0
            break;
3506
0
        }
3507
0
        b = multadd(b, 10, 0);
3508
0
    }
3509
3510
/* Round off last digit */
3511
3512
#ifdef Honor_FLT_ROUNDS
3513
switch(rounding) {
3514
case 0: goto trimzeros;
3515
case 2: goto roundoff;
3516
}
3517
#endif
3518
0
b = lshift(b, 1);
3519
0
j = cmp(b, S);
3520
0
if (j > 0 || j == 0 && dig & 1) {
3521
0
roundoff:
3522
0
    while(*--s == '9')
3523
0
        if (s == s0) {
3524
0
            k++;
3525
0
            *s++ = '1';
3526
0
            goto ret;
3527
0
        }
3528
0
    ++*s++;
3529
0
}
3530
0
else {
3531
#ifdef Honor_FLT_ROUNDS
3532
trimzeros:
3533
#endif
3534
0
    while(*--s == '0');
3535
0
    s++;
3536
0
}
3537
0
ret:
3538
0
Bfree(S);
3539
0
if (mhi) {
3540
0
    if (mlo && mlo != mhi) {
3541
0
        Bfree(mlo);
3542
0
    }
3543
0
    Bfree(mhi);
3544
0
}
3545
0
ret1:
3546
#ifdef SET_INEXACT
3547
if (inexact) {
3548
    if (!oldinexact) {
3549
        word0(d) = Exp_1 + (70 << Exp_shift);
3550
        word1(d) = 0;
3551
        dval(d) += 1.;
3552
    }
3553
}
3554
else if (!oldinexact) {
3555
    clear_inexact();
3556
}
3557
#endif
3558
0
Bfree(b);
3559
0
*s = 0;
3560
0
*decpt = k + 1;
3561
0
if (rve) {
3562
0
    *rve = s;
3563
0
}
3564
0
return s0;
3565
0
}
3566
#ifdef __cplusplus
3567
}
3568
#endif
3569
3570
PR_IMPLEMENT(PRStatus)
3571
PR_dtoa(PRFloat64 d, PRIntn mode, PRIntn ndigits,
3572
        PRIntn *decpt, PRIntn *sign, char **rve, char *buf, PRSize bufsize)
3573
0
{
3574
0
    char *result;
3575
0
    PRSize resultlen;
3576
0
    PRStatus rv = PR_FAILURE;
3577
3578
0
    if (!_pr_initialized) {
3579
0
        _PR_ImplicitInitialization();
3580
0
    }
3581
3582
0
    if (mode < 0 || mode > 3) {
3583
0
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
3584
0
        return rv;
3585
0
    }
3586
0
    result = dtoa(d, mode, ndigits, decpt, sign, rve);
3587
0
    if (!result) {
3588
0
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
3589
0
        return rv;
3590
0
    }
3591
0
    resultlen = strlen(result)+1;
3592
0
    if (bufsize < resultlen) {
3593
0
        PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
3594
0
    } else {
3595
0
        memcpy(buf, result, resultlen);
3596
0
        if (rve) {
3597
0
            *rve = buf + (*rve - result);
3598
0
        }
3599
0
        rv = PR_SUCCESS;
3600
0
    }
3601
0
    freedtoa(result);
3602
0
    return rv;
3603
0
}
3604
3605
/*
3606
** conversion routines for floating point
3607
** prcsn - number of digits of precision to generate floating
3608
** point value.
3609
** This should be reparameterized so that you can send in a
3610
**   prcn for the positive and negative ranges.  For now,
3611
**   conform to the ECMA JavaScript spec which says numbers
3612
**   less than 1e-6 are in scientific notation.
3613
** Also, the ECMA spec says that there should always be a
3614
**   '+' or '-' after the 'e' in scientific notation
3615
*/
3616
PR_IMPLEMENT(void)
3617
PR_cnvtf(char *buf, int bufsz, int prcsn, double dfval)
3618
0
{
3619
0
    PRIntn decpt, sign, numdigits;
3620
0
    char *num, *nump;
3621
0
    char *bufp = buf;
3622
0
    char *endnum;
3623
0
    U fval;
3624
3625
0
    dval(fval) = dfval;
3626
    /* If anything fails, we store an empty string in 'buf' */
3627
0
    num = (char*)PR_MALLOC(bufsz);
3628
0
    if (num == NULL) {
3629
0
        buf[0] = '\0';
3630
0
        return;
3631
0
    }
3632
    /* XXX Why use mode 1? */
3633
0
    if (PR_dtoa(dval(fval),1,prcsn,&decpt,&sign,&endnum,num,bufsz)
3634
0
        == PR_FAILURE) {
3635
0
        buf[0] = '\0';
3636
0
        goto done;
3637
0
    }
3638
0
    numdigits = endnum - num;
3639
0
    nump = num;
3640
3641
0
    if (sign &&
3642
0
        !(word0(fval) == Sign_bit && word1(fval) == 0) &&
3643
0
        !((word0(fval) & Exp_mask) == Exp_mask &&
3644
0
          (word1(fval) || (word0(fval) & 0xfffff)))) {
3645
0
        *bufp++ = '-';
3646
0
    }
3647
3648
0
    if (decpt == 9999) {
3649
0
        while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */
3650
0
        goto done;
3651
0
    }
3652
3653
0
    if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) {
3654
0
        *bufp++ = *nump++;
3655
0
        if (numdigits != 1) {
3656
0
            *bufp++ = '.';
3657
0
        }
3658
3659
0
        while (*nump != '\0') {
3660
0
            *bufp++ = *nump++;
3661
0
        }
3662
0
        *bufp++ = 'e';
3663
0
        PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1);
3664
0
    } else if (decpt >= 0) {
3665
0
        if (decpt == 0) {
3666
0
            *bufp++ = '0';
3667
0
        } else {
3668
0
            while (decpt--) {
3669
0
                if (*nump != '\0') {
3670
0
                    *bufp++ = *nump++;
3671
0
                } else {
3672
0
                    *bufp++ = '0';
3673
0
                }
3674
0
            }
3675
0
        }
3676
0
        if (*nump != '\0') {
3677
0
            *bufp++ = '.';
3678
0
            while (*nump != '\0') {
3679
0
                *bufp++ = *nump++;
3680
0
            }
3681
0
        }
3682
0
        *bufp++ = '\0';
3683
0
    } else if (decpt < 0) {
3684
0
        *bufp++ = '0';
3685
0
        *bufp++ = '.';
3686
0
        while (decpt++) {
3687
0
            *bufp++ = '0';
3688
0
        }
3689
3690
0
        while (*nump != '\0') {
3691
0
            *bufp++ = *nump++;
3692
0
        }
3693
0
        *bufp++ = '\0';
3694
0
    }
3695
0
done:
3696
0
    PR_DELETE(num);
3697
0
}