Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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