• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

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

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.