Coverage Report

Created: 2026-06-07 07:11

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