Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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
213extern void *MALLOC(size_t);
214#else
215#define MALLOC xmalloc
216#endif
217#ifdef FREE
218extern 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))
228static 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
276extern "C" {
277#if 0
278} /* satisfy cc-mode */
279#endif
280#endif
281
282#ifndef hexdigit
283static const char hexdigits[] = "0123456789abcdef0123456789ABCDEF";
284#endif
285
286#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1
287Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined.
288#endif
289
290typedef union { double d; ULong L[2]; } U;
291
292#ifdef YES_ALIAS
293typedef 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
303typedef 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)))
448extern 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
494struct Bigint {
495 struct Bigint *next;
496 int k, maxwds, sign, wds;
497 ULong x[1];
498};
499
500typedef struct Bigint Bigint;
501
502static Bigint *freelist[Kmax+1];
503
504static Bigint *
505Balloc(int k)
506{
507 int x;
508 Bigint *rv;
509#ifndef Omit_Private_Memory
510 size_t len;
511#endif
512
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 }
535 rv->sign = rv->wds = 0;
536 return rv;
537}
538
539static void
540Bfree(Bigint *v)
541{
542 if (v) {
543 if (v->k > Kmax) {
544 FREE(v);
545 return;
546 }
548 v->next = freelist[v->k];
549 freelist[v->k] = v;
551 }
552}
553
554#define Bcopy(x,y) memcpy((char *)&(x)->sign, (char *)&(y)->sign, \
555(y)->wds*sizeof(Long) + 2*sizeof(int))
556
557static Bigint *
558multadd(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
608static Bigint *
609s2b(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
642static int
643hi0bits(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
671static int
672lo0bits(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
714static Bigint *
715i2b(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
725static Bigint *
726mult(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
826static Bigint *p5s;
827
828static Bigint *
829pow5mult(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
844 if (!(p5 = p5s)) {
845 p5 = p5s = i2b(625);
846 p5->next = 0;
847 }
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
865 if (!(p51 = p5->next)) {
866 p51 = p5->next = mult(p5,p5);
867 p51->next = 0;
868 }
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
880static Bigint *
881lshift(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
934static int
935cmp(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
963NO_SANITIZE("unsigned-integer-overflow", static Bigint * diff(Bigint *a, Bigint *b));
964static Bigint *
965diff(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
1050static double
1051ulp(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
1088static double
1089b2d(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
1142ret_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
1153static Bigint *
1154d2b(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
1290static double
1291ratio(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
1326static const double
1327tens[] = {
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
1336static const double
1337#ifdef IEEE_Arith
1338bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1339static 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
1353bigtens[] = { 1e16, 1e32, 1e64 };
1354static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1355#define n_bigtens 3
1356#else
1357bigtens[] = { 1e16, 1e32 };
1358static 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
1377static int
1378match(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
1394static void
1395hexnan(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
1443NO_SANITIZE("unsigned-integer-overflow", double strtod(const char *s00, char **se));
1444double
1445strtod(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 }
1492break2:
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 if (!*s) goto ret;
1504 s1 = strchr(hexdigit, *s);
1505 }
1506 if (s1 != NULL) {
1507 do {
1508 adj += aadj * ((s1 - hexdigit) & 15);
1509 nd0 += 4;
1510 aadj /= 16;
1511 } while (*++s && (s1 = strchr(hexdigit, *s)));
1512 }
1513
1514 if (*s == '.') {
1515 dsign = 1;
1516 if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
1517 if (nd0 < 0) {
1518 while (*s == '0') {
1519 s++;
1520 nd0 -= 4;
1521 }
1522 }
1523 for (; *s && (s1 = strchr(hexdigit, *s)); ++s) {
1524 adj += aadj * ((s1 - hexdigit) & 15);
1525 if ((aadj /= 16) == 0.0) {
1526 while (*++s && strchr(hexdigit, *s));
1527 break;
1528 }
1529 }
1530 }
1531 else {
1532 dsign = 0;
1533 }
1534
1535 if (*s == 'P' || *s == 'p') {
1536 dsign = 0x2C - *++s; /* +: 2B, -: 2D */
1537 if (abs(dsign) == 1) s++;
1538 else dsign = 1;
1539
1540 nd = 0;
1541 c = *s;
1542 if (c < '0' || '9' < c) goto ret0;
1543 do {
1544 nd *= 10;
1545 nd += c;
1546 nd -= '0';
1547 c = *++s;
1548 /* Float("0x0."+("0"*267)+"1fp2095") */
1549 if (nd + dsign * nd0 > 2095) {
1550 while ('0' <= c && c <= '9') c = *++s;
1551 break;
1552 }
1553 } while ('0' <= c && c <= '9');
1554 nd0 += nd * dsign;
1555 }
1556 else {
1557 if (dsign) goto ret0;
1558 }
1559 dval(rv) = ldexp(adj, nd0);
1560 goto ret;
1561 }
1562 nz0 = 1;
1563 while (*++s == '0') ;
1564 if (!*s)
1565 goto ret;
1566 }
1567 s0 = s;
1568 y = z = 0;
1569 for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1570 if (nd < 9)
1571 y = 10*y + c - '0';
1572 else if (nd < DBL_DIG + 2)
1573 z = 10*z + c - '0';
1574 nd0 = nd;
1575#ifdef USE_LOCALE
1576 s1 = localeconv()->decimal_point;
1577 if (c == *s1) {
1578 c = '.';
1579 if (*++s1) {
1580 s2 = s;
1581 for (;;) {
1582 if (*++s2 != *s1) {
1583 c = 0;
1584 break;
1585 }
1586 if (!*++s1) {
1587 s = s2;
1588 break;
1589 }
1590 }
1591 }
1592 }
1593#endif
1594 if (c == '.') {
1595 if (!ISDIGIT(s[1]))
1596 goto dig_done;
1597 c = *++s;
1598 if (!nd) {
1599 for (; c == '0'; c = *++s)
1600 nz++;
1601 if (c > '0' && c <= '9') {
1602 s0 = s;
1603 nf += nz;
1604 nz = 0;
1605 goto have_dig;
1606 }
1607 goto dig_done;
1608 }
1609 for (; c >= '0' && c <= '9'; c = *++s) {
1610have_dig:
1611 nz++;
1612 if (nd > DBL_DIG * 4) {
1613 continue;
1614 }
1615 if (c -= '0') {
1616 nf += nz;
1617 for (i = 1; i < nz; i++)
1618 if (nd++ < 9)
1619 y *= 10;
1620 else if (nd <= DBL_DIG + 2)
1621 z *= 10;
1622 if (nd++ < 9)
1623 y = 10*y + c;
1624 else if (nd <= DBL_DIG + 2)
1625 z = 10*z + c;
1626 nz = 0;
1627 }
1628 }
1629 }
1630dig_done:
1631 e = 0;
1632 if (c == 'e' || c == 'E') {
1633 if (!nd && !nz && !nz0) {
1634 goto ret0;
1635 }
1636 s00 = s;
1637 esign = 0;
1638 switch (c = *++s) {
1639 case '-':
1640 esign = 1;
1641 case '+':
1642 c = *++s;
1643 }
1644 if (c >= '0' && c <= '9') {
1645 while (c == '0')
1646 c = *++s;
1647 if (c > '0' && c <= '9') {
1648 L = c - '0';
1649 s1 = s;
1650 while ((c = *++s) >= '0' && c <= '9')
1651 L = 10*L + c - '0';
1652 if (s - s1 > 8 || L > 19999)
1653 /* Avoid confusion from exponents
1654 * so large that e might overflow.
1655 */
1656 e = 19999; /* safe for 16 bit ints */
1657 else
1658 e = (int)L;
1659 if (esign)
1660 e = -e;
1661 }
1662 else
1663 e = 0;
1664 }
1665 else
1666 s = s00;
1667 }
1668 if (!nd) {
1669 if (!nz && !nz0) {
1670#ifdef INFNAN_CHECK
1671 /* Check for Nan and Infinity */
1672 switch (c) {
1673 case 'i':
1674 case 'I':
1675 if (match(&s,"nf")) {
1676 --s;
1677 if (!match(&s,"inity"))
1678 ++s;
1679 word0(rv) = 0x7ff00000;
1680 word1(rv) = 0;
1681 goto ret;
1682 }
1683 break;
1684 case 'n':
1685 case 'N':
1686 if (match(&s, "an")) {
1687 word0(rv) = NAN_WORD0;
1688 word1(rv) = NAN_WORD1;
1689#ifndef No_Hex_NaN
1690 if (*s == '(') /*)*/
1691 hexnan(&rv, &s);
1692#endif
1693 goto ret;
1694 }
1695 }
1696#endif /* INFNAN_CHECK */
1697ret0:
1698 s = s00;
1699 sign = 0;
1700 }
1701 goto ret;
1702 }
1703 e1 = e -= nf;
1704
1705 /* Now we have nd0 digits, starting at s0, followed by a
1706 * decimal point, followed by nd-nd0 digits. The number we're
1707 * after is the integer represented by those digits times
1708 * 10**e */
1709
1710 if (!nd0)
1711 nd0 = nd;
1712 k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
1713 dval(rv) = y;
1714 if (k > 9) {
1715#ifdef SET_INEXACT
1716 if (k > DBL_DIG)
1717 oldinexact = get_inexact();
1718#endif
1719 dval(rv) = tens[k - 9] * dval(rv) + z;
1720 }
1721 bd0 = bb = bd = bs = delta = 0;
1722 if (nd <= DBL_DIG
1723#ifndef RND_PRODQUOT
1724#ifndef Honor_FLT_ROUNDS
1725 && Flt_Rounds == 1
1726#endif
1727#endif
1728 ) {
1729 if (!e)
1730 goto ret;
1731 if (e > 0) {
1732 if (e <= Ten_pmax) {
1733#ifdef VAX
1734 goto vax_ovfl_check;
1735#else
1736#ifdef Honor_FLT_ROUNDS
1737 /* round correctly FLT_ROUNDS = 2 or 3 */
1738 if (sign) {
1739 dval(rv) = -dval(rv);
1740 sign = 0;
1741 }
1742#endif
1743 /* rv = */ rounded_product(dval(rv), tens[e]);
1744 goto ret;
1745#endif
1746 }
1747 i = DBL_DIG - nd;
1748 if (e <= Ten_pmax + i) {
1749 /* A fancier test would sometimes let us do
1750 * this for larger i values.
1751 */
1752#ifdef Honor_FLT_ROUNDS
1753 /* round correctly FLT_ROUNDS = 2 or 3 */
1754 if (sign) {
1755 dval(rv) = -dval(rv);
1756 sign = 0;
1757 }
1758#endif
1759 e -= i;
1760 dval(rv) *= tens[i];
1761#ifdef VAX
1762 /* VAX exponent range is so narrow we must
1763 * worry about overflow here...
1764 */
1765vax_ovfl_check:
1766 word0(rv) -= P*Exp_msk1;
1767 /* rv = */ rounded_product(dval(rv), tens[e]);
1768 if ((word0(rv) & Exp_mask)
1769 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1770 goto ovfl;
1771 word0(rv) += P*Exp_msk1;
1772#else
1773 /* rv = */ rounded_product(dval(rv), tens[e]);
1774#endif
1775 goto ret;
1776 }
1777 }
1778#ifndef Inaccurate_Divide
1779 else if (e >= -Ten_pmax) {
1780#ifdef Honor_FLT_ROUNDS
1781 /* round correctly FLT_ROUNDS = 2 or 3 */
1782 if (sign) {
1783 dval(rv) = -dval(rv);
1784 sign = 0;
1785 }
1786#endif
1787 /* rv = */ rounded_quotient(dval(rv), tens[-e]);
1788 goto ret;
1789 }
1790#endif
1791 }
1792 e1 += nd - k;
1793
1794#ifdef IEEE_Arith
1795#ifdef SET_INEXACT
1796 inexact = 1;
1797 if (k <= DBL_DIG)
1798 oldinexact = get_inexact();
1799#endif
1800#ifdef Avoid_Underflow
1801 scale = 0;
1802#endif
1803#ifdef Honor_FLT_ROUNDS
1804 if ((rounding = Flt_Rounds) >= 2) {
1805 if (sign)
1806 rounding = rounding == 2 ? 0 : 2;
1807 else
1808 if (rounding != 2)
1809 rounding = 0;
1810 }
1811#endif
1812#endif /*IEEE_Arith*/
1813
1814 /* Get starting approximation = rv * 10**e1 */
1815
1816 if (e1 > 0) {
1817 if ((i = e1 & 15) != 0)
1818 dval(rv) *= tens[i];
1819 if (e1 &= ~15) {
1820 if (e1 > DBL_MAX_10_EXP) {
1821ovfl:
1822#ifndef NO_ERRNO
1823 errno = ERANGE;
1824#endif
1825 /* Can't trust HUGE_VAL */
1826#ifdef IEEE_Arith
1827#ifdef Honor_FLT_ROUNDS
1828 switch (rounding) {
1829 case 0: /* toward 0 */
1830 case 3: /* toward -infinity */
1831 word0(rv) = Big0;
1832 word1(rv) = Big1;
1833 break;
1834 default:
1835 word0(rv) = Exp_mask;
1836 word1(rv) = 0;
1837 }
1838#else /*Honor_FLT_ROUNDS*/
1839 word0(rv) = Exp_mask;
1840 word1(rv) = 0;
1841#endif /*Honor_FLT_ROUNDS*/
1842#ifdef SET_INEXACT
1843 /* set overflow bit */
1844 dval(rv0) = 1e300;
1845 dval(rv0) *= dval(rv0);
1846#endif
1847#else /*IEEE_Arith*/
1848 word0(rv) = Big0;
1849 word1(rv) = Big1;
1850#endif /*IEEE_Arith*/
1851 if (bd0)
1852 goto retfree;
1853 goto ret;
1854 }
1855 e1 >>= 4;
1856 for (j = 0; e1 > 1; j++, e1 >>= 1)
1857 if (e1 & 1)
1858 dval(rv) *= bigtens[j];
1859 /* The last multiplication could overflow. */
1860 word0(rv) -= P*Exp_msk1;
1861 dval(rv) *= bigtens[j];
1862 if ((z = word0(rv) & Exp_mask)
1864 goto ovfl;
1865 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1866 /* set to largest number */
1867 /* (Can't trust DBL_MAX) */
1868 word0(rv) = Big0;
1869 word1(rv) = Big1;
1870 }
1871 else
1872 word0(rv) += P*Exp_msk1;
1873 }
1874 }
1875 else if (e1 < 0) {
1876 e1 = -e1;
1877 if ((i = e1 & 15) != 0)
1878 dval(rv) /= tens[i];
1879 if (e1 >>= 4) {
1880 if (e1 >= 1 << n_bigtens)
1881 goto undfl;
1882#ifdef Avoid_Underflow
1883 if (e1 & Scale_Bit)
1884 scale = 2*P;
1885 for (j = 0; e1 > 0; j++, e1 >>= 1)
1886 if (e1 & 1)
1887 dval(rv) *= tinytens[j];
1888 if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1889 >> Exp_shift)) > 0) {
1890 /* scaled rv is denormal; zap j low bits */
1891 if (j >= 32) {
1892 word1(rv) = 0;
1893 if (j >= 53)
1894 word0(rv) = (P+2)*Exp_msk1;
1895 else
1896 word0(rv) &= 0xffffffff << (j-32);
1897 }
1898 else
1899 word1(rv) &= 0xffffffff << j;
1900 }
1901#else
1902 for (j = 0; e1 > 1; j++, e1 >>= 1)
1903 if (e1 & 1)
1904 dval(rv) *= tinytens[j];
1905 /* The last multiplication could underflow. */
1906 dval(rv0) = dval(rv);
1907 dval(rv) *= tinytens[j];
1908 if (!dval(rv)) {
1909 dval(rv) = 2.*dval(rv0);
1910 dval(rv) *= tinytens[j];
1911#endif
1912 if (!dval(rv)) {
1913undfl:
1914 dval(rv) = 0.;
1915#ifndef NO_ERRNO
1916 errno = ERANGE;
1917#endif
1918 if (bd0)
1919 goto retfree;
1920 goto ret;
1921 }
1922#ifndef Avoid_Underflow
1923 word0(rv) = Tiny0;
1924 word1(rv) = Tiny1;
1925 /* The refinement below will clean
1926 * this approximation up.
1927 */
1928 }
1929#endif
1930 }
1931 }
1932
1933 /* Now the hard part -- adjusting rv to the correct value.*/
1934
1935 /* Put digits into bd: true value = bd * 10^e */
1936
1937 bd0 = s2b(s0, nd0, nd, y);
1938
1939 for (;;) {
1940 bd = Balloc(bd0->k);
1941 Bcopy(bd, bd0);
1942 bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1943 bs = i2b(1);
1944
1945 if (e >= 0) {
1946 bb2 = bb5 = 0;
1947 bd2 = bd5 = e;
1948 }
1949 else {
1950 bb2 = bb5 = -e;
1951 bd2 = bd5 = 0;
1952 }
1953 if (bbe >= 0)
1954 bb2 += bbe;
1955 else
1956 bd2 -= bbe;
1957 bs2 = bb2;
1958#ifdef Honor_FLT_ROUNDS
1959 if (rounding != 1)
1960 bs2++;
1961#endif
1962#ifdef Avoid_Underflow
1963 j = bbe - scale;
1964 i = j + bbbits - 1; /* logb(rv) */
1965 if (i < Emin) /* denormal */
1966 j += P - Emin;
1967 else
1968 j = P + 1 - bbbits;
1969#else /*Avoid_Underflow*/
1970#ifdef Sudden_Underflow
1971#ifdef IBM
1972 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1973#else
1974 j = P + 1 - bbbits;
1975#endif
1976#else /*Sudden_Underflow*/
1977 j = bbe;
1978 i = j + bbbits - 1; /* logb(rv) */
1979 if (i < Emin) /* denormal */
1980 j += P - Emin;
1981 else
1982 j = P + 1 - bbbits;
1983#endif /*Sudden_Underflow*/
1984#endif /*Avoid_Underflow*/
1985 bb2 += j;
1986 bd2 += j;
1987#ifdef Avoid_Underflow
1988 bd2 += scale;
1989#endif
1990 i = bb2 < bd2 ? bb2 : bd2;
1991 if (i > bs2)
1992 i = bs2;
1993 if (i > 0) {
1994 bb2 -= i;
1995 bd2 -= i;
1996 bs2 -= i;
1997 }
1998 if (bb5 > 0) {
1999 bs = pow5mult(bs, bb5);
2000 bb1 = mult(bs, bb);
2001 Bfree(bb);
2002 bb = bb1;
2003 }
2004 if (bb2 > 0)
2005 bb = lshift(bb, bb2);
2006 if (bd5 > 0)
2007 bd = pow5mult(bd, bd5);
2008 if (bd2 > 0)
2009 bd = lshift(bd, bd2);
2010 if (bs2 > 0)
2011 bs = lshift(bs, bs2);
2012 delta = diff(bb, bd);
2013 dsign = delta->sign;
2014 delta->sign = 0;
2015 i = cmp(delta, bs);
2016#ifdef Honor_FLT_ROUNDS
2017 if (rounding != 1) {
2018 if (i < 0) {
2019 /* Error is less than an ulp */
2020 if (!delta->x[0] && delta->wds <= 1) {
2021 /* exact */
2022#ifdef SET_INEXACT
2023 inexact = 0;
2024#endif
2025 break;
2026 }
2027 if (rounding) {
2028 if (dsign) {
2029 adj = 1.;
2030 goto apply_adj;
2031 }
2032 }
2033 else if (!dsign) {
2034 adj = -1.;
2035 if (!word1(rv)
2036 && !(word0(rv) & Frac_mask)) {
2037 y = word0(rv) & Exp_mask;
2038#ifdef Avoid_Underflow
2039 if (!scale || y > 2*P*Exp_msk1)
2040#else
2041 if (y)
2042#endif
2043 {
2044 delta = lshift(delta,Log2P);
2045 if (cmp(delta, bs) <= 0)
2046 adj = -0.5;
2047 }
2048 }
2049apply_adj:
2050#ifdef Avoid_Underflow
2051 if (scale && (y = word0(rv) & Exp_mask)
2052 <= 2*P*Exp_msk1)
2053 word0(adj) += (2*P+1)*Exp_msk1 - y;
2054#else
2055#ifdef Sudden_Underflow
2056 if ((word0(rv) & Exp_mask) <=
2057 P*Exp_msk1) {
2058 word0(rv) += P*Exp_msk1;
2059 dval(rv) += adj*ulp(dval(rv));
2060 word0(rv) -= P*Exp_msk1;
2061 }
2062 else
2063#endif /*Sudden_Underflow*/
2064#endif /*Avoid_Underflow*/
2065 dval(rv) += adj*ulp(dval(rv));
2066 }
2067 break;
2068 }
2069 adj = ratio(delta, bs);
2070 if (adj < 1.)
2071 adj = 1.;
2072 if (adj <= 0x7ffffffe) {
2073 /* adj = rounding ? ceil(adj) : floor(adj); */
2074 y = adj;
2075 if (y != adj) {
2076 if (!((rounding>>1) ^ dsign))
2077 y++;
2078 adj = y;
2079 }
2080 }
2081#ifdef Avoid_Underflow
2082 if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2083 word0(adj) += (2*P+1)*Exp_msk1 - y;
2084#else
2085#ifdef Sudden_Underflow
2086 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2087 word0(rv) += P*Exp_msk1;
2088 adj *= ulp(dval(rv));
2089 if (dsign)
2090 dval(rv) += adj;
2091 else
2092 dval(rv) -= adj;
2093 word0(rv) -= P*Exp_msk1;
2094 goto cont;
2095 }
2096#endif /*Sudden_Underflow*/
2097#endif /*Avoid_Underflow*/
2098 adj *= ulp(dval(rv));
2099 if (dsign)
2100 dval(rv) += adj;
2101 else
2102 dval(rv) -= adj;
2103 goto cont;
2104 }
2105#endif /*Honor_FLT_ROUNDS*/
2106
2107 if (i < 0) {
2108 /* Error is less than half an ulp -- check for
2109 * special case of mantissa a power of two.
2110 */
2111 if (dsign || word1(rv) || word0(rv) & Bndry_mask
2112#ifdef IEEE_Arith
2113#ifdef Avoid_Underflow
2114 || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2115#else
2116 || (word0(rv) & Exp_mask) <= Exp_msk1
2117#endif
2118#endif
2119 ) {
2120#ifdef SET_INEXACT
2121 if (!delta->x[0] && delta->wds <= 1)
2122 inexact = 0;
2123#endif
2124 break;
2125 }
2126 if (!delta->x[0] && delta->wds <= 1) {
2127 /* exact result */
2128#ifdef SET_INEXACT
2129 inexact = 0;
2130#endif
2131 break;
2132 }
2133 delta = lshift(delta,Log2P);
2134 if (cmp(delta, bs) > 0)
2135 goto drop_down;
2136 break;
2137 }
2138 if (i == 0) {
2139 /* exactly half-way between */
2140 if (dsign) {
2141 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2142 && word1(rv) == (
2143#ifdef Avoid_Underflow
2144 (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2145 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2146#endif
2147 0xffffffff)) {
2148 /*boundary case -- increment exponent*/
2149 word0(rv) = (word0(rv) & Exp_mask)
2150 + Exp_msk1
2151#ifdef IBM
2152 | Exp_msk1 >> 4
2153#endif
2154 ;
2155 word1(rv) = 0;
2156#ifdef Avoid_Underflow
2157 dsign = 0;
2158#endif
2159 break;
2160 }
2161 }
2162 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2163drop_down:
2164 /* boundary case -- decrement exponent */
2165#ifdef Sudden_Underflow /*{{*/
2166 L = word0(rv) & Exp_mask;
2167#ifdef IBM
2168 if (L < Exp_msk1)
2169#else
2170#ifdef Avoid_Underflow
2171 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2172#else
2173 if (L <= Exp_msk1)
2174#endif /*Avoid_Underflow*/
2175#endif /*IBM*/
2176 goto undfl;
2177 L -= Exp_msk1;
2178#else /*Sudden_Underflow}{*/
2179#ifdef Avoid_Underflow
2180 if (scale) {
2181 L = word0(rv) & Exp_mask;
2182 if (L <= (2*P+1)*Exp_msk1) {
2183 if (L > (P+2)*Exp_msk1)
2184 /* round even ==> */
2185 /* accept rv */
2186 break;
2187 /* rv = smallest denormal */
2188 goto undfl;
2189 }
2190 }
2191#endif /*Avoid_Underflow*/
2192 L = (word0(rv) & Exp_mask) - Exp_msk1;
2193#endif /*Sudden_Underflow}}*/
2194 word0(rv) = L | Bndry_mask1;
2195 word1(rv) = 0xffffffff;
2196#ifdef IBM
2197 goto cont;
2198#else
2199 break;
2200#endif
2201 }
2202#ifndef ROUND_BIASED
2203 if (!(word1(rv) & LSB))
2204 break;
2205#endif
2206 if (dsign)
2207 dval(rv) += ulp(dval(rv));
2208#ifndef ROUND_BIASED
2209 else {
2210 dval(rv) -= ulp(dval(rv));
2211#ifndef Sudden_Underflow
2212 if (!dval(rv))
2213 goto undfl;
2214#endif
2215 }
2216#ifdef Avoid_Underflow
2217 dsign = 1 - dsign;
2218#endif
2219#endif
2220 break;
2221 }
2222 if ((aadj = ratio(delta, bs)) <= 2.) {
2223 if (dsign)
2224 aadj = dval(aadj1) = 1.;
2225 else if (word1(rv) || word0(rv) & Bndry_mask) {
2226#ifndef Sudden_Underflow
2227 if (word1(rv) == Tiny1 && !word0(rv))
2228 goto undfl;
2229#endif
2230 aadj = 1.;
2231 dval(aadj1) = -1.;
2232 }
2233 else {
2234 /* special case -- power of FLT_RADIX to be */
2235 /* rounded down... */
2236
2237 if (aadj < 2./FLT_RADIX)
2238 aadj = 1./FLT_RADIX;
2239 else
2240 aadj *= 0.5;
2241 dval(aadj1) = -aadj;
2242 }
2243 }
2244 else {
2245 aadj *= 0.5;
2246 dval(aadj1) = dsign ? aadj : -aadj;
2247#ifdef Check_FLT_ROUNDS
2248 switch (Rounding) {
2249 case 2: /* towards +infinity */
2250 dval(aadj1) -= 0.5;
2251 break;
2252 case 0: /* towards 0 */
2253 case 3: /* towards -infinity */
2254 dval(aadj1) += 0.5;
2255 }
2256#else
2257 if (Flt_Rounds == 0)
2258 dval(aadj1) += 0.5;
2259#endif /*Check_FLT_ROUNDS*/
2260 }
2261 y = word0(rv) & Exp_mask;
2262
2263 /* Check for overflow */
2264
2265 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2266 dval(rv0) = dval(rv);
2267 word0(rv) -= P*Exp_msk1;
2268 adj = dval(aadj1) * ulp(dval(rv));
2269 dval(rv) += adj;
2270 if ((word0(rv) & Exp_mask) >=
2272 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2273 goto ovfl;
2274 word0(rv) = Big0;
2275 word1(rv) = Big1;
2276 goto cont;
2277 }
2278 else
2279 word0(rv) += P*Exp_msk1;
2280 }
2281 else {
2282#ifdef Avoid_Underflow
2283 if (scale && y <= 2*P*Exp_msk1) {
2284 if (aadj <= 0x7fffffff) {
2285 if ((z = (int)aadj) <= 0)
2286 z = 1;
2287 aadj = z;
2288 dval(aadj1) = dsign ? aadj : -aadj;
2289 }
2290 word0(aadj1) += (2*P+1)*Exp_msk1 - y;
2291 }
2292 adj = dval(aadj1) * ulp(dval(rv));
2293 dval(rv) += adj;
2294#else
2295#ifdef Sudden_Underflow
2296 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2297 dval(rv0) = dval(rv);
2298 word0(rv) += P*Exp_msk1;
2299 adj = dval(aadj1) * ulp(dval(rv));
2300 dval(rv) += adj;
2301#ifdef IBM
2302 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2303#else
2304 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2305#endif
2306 {
2307 if (word0(rv0) == Tiny0 && word1(rv0) == Tiny1)
2308 goto undfl;
2309 word0(rv) = Tiny0;
2310 word1(rv) = Tiny1;
2311 goto cont;
2312 }
2313 else
2314 word0(rv) -= P*Exp_msk1;
2315 }
2316 else {
2317 adj = dval(aadj1) * ulp(dval(rv));
2318 dval(rv) += adj;
2319 }
2320#else /*Sudden_Underflow*/
2321 /* Compute adj so that the IEEE rounding rules will
2322 * correctly round rv + adj in some half-way cases.
2323 * If rv * ulp(rv) is denormalized (i.e.,
2324 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2325 * trouble from bits lost to denormalization;
2326 * example: 1.2e-307 .
2327 */
2328 if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2329 dval(aadj1) = (double)(int)(aadj + 0.5);
2330 if (!dsign)
2331 dval(aadj1) = -dval(aadj1);
2332 }
2333 adj = dval(aadj1) * ulp(dval(rv));
2334 dval(rv) += adj;
2335#endif /*Sudden_Underflow*/
2336#endif /*Avoid_Underflow*/
2337 }
2338 z = word0(rv) & Exp_mask;
2339#ifndef SET_INEXACT
2340#ifdef Avoid_Underflow
2341 if (!scale)
2342#endif
2343 if (y == z) {
2344 /* Can we stop now? */
2345 L = (Long)aadj;
2346 aadj -= L;
2347 /* The tolerances below are conservative. */
2348 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2349 if (aadj < .4999999 || aadj > .5000001)
2350 break;
2351 }
2352 else if (aadj < .4999999/FLT_RADIX)
2353 break;
2354 }
2355#endif
2356cont:
2357 Bfree(bb);
2358 Bfree(bd);
2359 Bfree(bs);
2360 Bfree(delta);
2361 }
2362#ifdef SET_INEXACT
2363 if (inexact) {
2364 if (!oldinexact) {
2365 word0(rv0) = Exp_1 + (70 << Exp_shift);
2366 word1(rv0) = 0;
2367 dval(rv0) += 1.;
2368 }
2369 }
2370 else if (!oldinexact)
2371 clear_inexact();
2372#endif
2373#ifdef Avoid_Underflow
2374 if (scale) {
2375 word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2376 word1(rv0) = 0;
2377 dval(rv) *= dval(rv0);
2378#ifndef NO_ERRNO
2379 /* try to avoid the bug of testing an 8087 register value */
2380 if (word0(rv) == 0 && word1(rv) == 0)
2381 errno = ERANGE;
2382#endif
2383 }
2384#endif /* Avoid_Underflow */
2385#ifdef SET_INEXACT
2386 if (inexact && !(word0(rv) & Exp_mask)) {
2387 /* set underflow bit */
2388 dval(rv0) = 1e-300;
2389 dval(rv0) *= dval(rv0);
2390 }
2391#endif
2392retfree:
2393 Bfree(bb);
2394 Bfree(bd);
2395 Bfree(bs);
2396 Bfree(bd0);
2397 Bfree(delta);
2398ret:
2399 if (se)
2400 *se = (char *)s;
2401 return sign ? -dval(rv) : dval(rv);
2402}
2403
2404NO_SANITIZE("unsigned-integer-overflow", static int quorem(Bigint *b, Bigint *S));
2405static int
2406quorem(Bigint *b, Bigint *S)
2407{
2408 int n;
2409 ULong *bx, *bxe, q, *sx, *sxe;
2410#ifdef ULLong
2411 ULLong borrow, carry, y, ys;
2412#else
2413 ULong borrow, carry, y, ys;
2414#ifdef Pack_32
2415 ULong si, z, zs;
2416#endif
2417#endif
2418
2419 n = S->wds;
2420#ifdef DEBUG
2421 /*debug*/ if (b->wds > n)
2422 /*debug*/ Bug("oversize b in quorem");
2423#endif
2424 if (b->wds < n)
2425 return 0;
2426 sx = S->x;
2427 sxe = sx + --n;
2428 bx = b->x;
2429 bxe = bx + n;
2430 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
2431#ifdef DEBUG
2432 /*debug*/ if (q > 9)
2433 /*debug*/ Bug("oversized quotient in quorem");
2434#endif
2435 if (q) {
2436 borrow = 0;
2437 carry = 0;
2438 do {
2439#ifdef ULLong
2440 ys = *sx++ * (ULLong)q + carry;
2441 carry = ys >> 32;
2442 y = *bx - (ys & FFFFFFFF) - borrow;
2443 borrow = y >> 32 & (ULong)1;
2444 *bx++ = (ULong)(y & FFFFFFFF);
2445#else
2446#ifdef Pack_32
2447 si = *sx++;
2448 ys = (si & 0xffff) * q + carry;
2449 zs = (si >> 16) * q + (ys >> 16);
2450 carry = zs >> 16;
2451 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2452 borrow = (y & 0x10000) >> 16;
2453 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2454 borrow = (z & 0x10000) >> 16;
2455 Storeinc(bx, z, y);
2456#else
2457 ys = *sx++ * q + carry;
2458 carry = ys >> 16;
2459 y = *bx - (ys & 0xffff) - borrow;
2460 borrow = (y & 0x10000) >> 16;
2461 *bx++ = y & 0xffff;
2462#endif
2463#endif
2464 } while (sx <= sxe);
2465 if (!*bxe) {
2466 bx = b->x;
2467 while (--bxe > bx && !*bxe)
2468 --n;
2469 b->wds = n;
2470 }
2471 }
2472 if (cmp(b, S) >= 0) {
2473 q++;
2474 borrow = 0;
2475 carry = 0;
2476 bx = b->x;
2477 sx = S->x;
2478 do {
2479#ifdef ULLong
2480 ys = *sx++ + carry;
2481 carry = ys >> 32;
2482 y = *bx - (ys & FFFFFFFF) - borrow;
2483 borrow = y >> 32 & (ULong)1;
2484 *bx++ = (ULong)(y & FFFFFFFF);
2485#else
2486#ifdef Pack_32
2487 si = *sx++;
2488 ys = (si & 0xffff) + carry;
2489 zs = (si >> 16) + (ys >> 16);
2490 carry = zs >> 16;
2491 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2492 borrow = (y & 0x10000) >> 16;
2493 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2494 borrow = (z & 0x10000) >> 16;
2495 Storeinc(bx, z, y);
2496#else
2497 ys = *sx++ + carry;
2498 carry = ys >> 16;
2499 y = *bx - (ys & 0xffff) - borrow;
2500 borrow = (y & 0x10000) >> 16;
2501 *bx++ = y & 0xffff;
2502#endif
2503#endif
2504 } while (sx <= sxe);
2505 bx = b->x;
2506 bxe = bx + n;
2507 if (!*bxe) {
2508 while (--bxe > bx && !*bxe)
2509 --n;
2510 b->wds = n;
2511 }
2512 }
2513 return q;
2514}
2515
2516#ifndef MULTIPLE_THREADS
2517static char *dtoa_result;
2518#endif
2519
2520#ifndef MULTIPLE_THREADS
2521static char *
2522rv_alloc(int i)
2523{
2524 return dtoa_result = xmalloc(i);
2525}
2526#else
2527#define rv_alloc(i) xmalloc(i)
2528#endif
2529
2530static char *
2531nrv_alloc(const char *s, char **rve, size_t n)
2532{
2533 char *rv, *t;
2534
2535 t = rv = rv_alloc(n);
2536 while ((*t = *s++) != 0) t++;
2537 if (rve)
2538 *rve = t;
2539 return rv;
2540}
2541
2542#define rv_strdup(s, rve) nrv_alloc((s), (rve), strlen(s)+1)
2543
2544#ifndef MULTIPLE_THREADS
2545/* freedtoa(s) must be used to free values s returned by dtoa
2546 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2547 * but for consistency with earlier versions of dtoa, it is optional
2548 * when MULTIPLE_THREADS is not defined.
2549 */
2550
2551static void
2552freedtoa(char *s)
2553{
2554 xfree(s);
2555}
2556#endif
2557
2558static const char INFSTR[] = "Infinity";
2559static const char NANSTR[] = "NaN";
2560static const char ZEROSTR[] = "0";
2561
2562/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2563 *
2564 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2565 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2566 *
2567 * Modifications:
2568 * 1. Rather than iterating, we use a simple numeric overestimate
2569 * to determine k = floor(log10(d)). We scale relevant
2570 * quantities using O(log2(k)) rather than O(k) multiplications.
2571 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2572 * try to generate digits strictly left to right. Instead, we
2573 * compute with fewer bits and propagate the carry if necessary
2574 * when rounding the final digit up. This is often faster.
2575 * 3. Under the assumption that input will be rounded nearest,
2576 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2577 * That is, we allow equality in stopping tests when the
2578 * round-nearest rule will give the same floating-point value
2579 * as would satisfaction of the stopping test with strict
2580 * inequality.
2581 * 4. We remove common factors of powers of 2 from relevant
2582 * quantities.
2583 * 5. When converting floating-point integers less than 1e16,
2584 * we use floating-point arithmetic rather than resorting
2585 * to multiple-precision integers.
2586 * 6. When asked to produce fewer than 15 digits, we first try
2587 * to get by with floating-point arithmetic; we resort to
2588 * multiple-precision integer arithmetic only if we cannot
2589 * guarantee that the floating-point calculation has given
2590 * the correctly rounded result. For k requested digits and
2591 * "uniformly" distributed input, the probability is
2592 * something like 10^(k-15) that we must resort to the Long
2593 * calculation.
2594 */
2595
2596char *
2597dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
2598{
2599 /* Arguments ndigits, decpt, sign are similar to those
2600 of ecvt and fcvt; trailing zeros are suppressed from
2601 the returned string. If not null, *rve is set to point
2602 to the end of the return value. If d is +-Infinity or NaN,
2603 then *decpt is set to 9999.
2604
2605 mode:
2606 0 ==> shortest string that yields d when read in
2607 and rounded to nearest.
2608 1 ==> like 0, but with Steele & White stopping rule;
2609 e.g. with IEEE P754 arithmetic , mode 0 gives
2610 1e23 whereas mode 1 gives 9.999999999999999e22.
2611 2 ==> max(1,ndigits) significant digits. This gives a
2612 return value similar to that of ecvt, except
2613 that trailing zeros are suppressed.
2614 3 ==> through ndigits past the decimal point. This
2615 gives a return value similar to that from fcvt,
2616 except that trailing zeros are suppressed, and
2617 ndigits can be negative.
2618 4,5 ==> similar to 2 and 3, respectively, but (in
2619 round-nearest mode) with the tests of mode 0 to
2620 possibly return a shorter string that rounds to d.
2621 With IEEE arithmetic and compilation with
2622 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2623 as modes 2 and 3 when FLT_ROUNDS != 1.
2624 6-9 ==> Debugging modes similar to mode - 4: don't try
2625 fast floating-point estimate (if applicable).
2626
2627 Values of mode other than 0-9 are treated as mode 0.
2628
2629 Sufficient space is allocated to the return value
2630 to hold the suppressed trailing zeros.
2631 */
2632
2633 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2634 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2635 spec_case, try_quick, half = 0;
2636 Long L;
2637#ifndef Sudden_Underflow
2638 int denorm;
2639 ULong x;
2640#endif
2641 Bigint *b, *b1, *delta, *mlo = 0, *mhi = 0, *S;
2642 double ds;
2643 double_u d, d2, eps;
2644 char *s, *s0;
2645#ifdef Honor_FLT_ROUNDS
2646 int rounding;
2647#endif
2648#ifdef SET_INEXACT
2649 int inexact, oldinexact;
2650#endif
2651
2652 dval(d) = d_;
2653
2654#ifndef MULTIPLE_THREADS
2655 if (dtoa_result) {
2656 freedtoa(dtoa_result);
2657 dtoa_result = 0;
2658 }
2659#endif
2660
2661 if (word0(d) & Sign_bit) {
2662 /* set sign for everything, including 0's and NaNs */
2663 *sign = 1;
2664 word0(d) &= ~Sign_bit; /* clear sign bit */
2665 }
2666 else
2667 *sign = 0;
2668
2669#if defined(IEEE_Arith) + defined(VAX)
2670#ifdef IEEE_Arith
2671 if ((word0(d) & Exp_mask) == Exp_mask)
2672#else
2673 if (word0(d) == 0x8000)
2674#endif
2675 {
2676 /* Infinity or NaN */
2677 *decpt = 9999;
2678#ifdef IEEE_Arith
2679 if (!word1(d) && !(word0(d) & 0xfffff))
2680 return rv_strdup(INFSTR, rve);
2681#endif
2682 return rv_strdup(NANSTR, rve);
2683 }
2684#endif
2685#ifdef IBM
2686 dval(d) += 0; /* normalize */
2687#endif
2688 if (!dval(d)) {
2689 *decpt = 1;
2690 return rv_strdup(ZEROSTR, rve);
2691 }
2692
2693#ifdef SET_INEXACT
2694 try_quick = oldinexact = get_inexact();
2695 inexact = 1;
2696#endif
2697#ifdef Honor_FLT_ROUNDS
2698 if ((rounding = Flt_Rounds) >= 2) {
2699 if (*sign)
2700 rounding = rounding == 2 ? 0 : 2;
2701 else
2702 if (rounding != 2)
2703 rounding = 0;
2704 }
2705#endif
2706
2707 b = d2b(dval(d), &be, &bbits);
2708#ifdef Sudden_Underflow
2709 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2710#else
2711 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
2712#endif
2713 dval(d2) = dval(d);
2714 word0(d2) &= Frac_mask1;
2715 word0(d2) |= Exp_11;
2716#ifdef IBM
2717 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2718 dval(d2) /= 1 << j;
2719#endif
2720
2721 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2722 * log10(x) = log(x) / log(10)
2723 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2724 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2725 *
2726 * This suggests computing an approximation k to log10(d) by
2727 *
2728 * k = (i - Bias)*0.301029995663981
2729 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2730 *
2731 * We want k to be too large rather than too small.
2732 * The error in the first-order Taylor series approximation
2733 * is in our favor, so we just round up the constant enough
2734 * to compensate for any error in the multiplication of
2735 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2736 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2737 * adding 1e-13 to the constant term more than suffices.
2738 * Hence we adjust the constant term to 0.1760912590558.
2739 * (We could get a more accurate k by invoking log10,
2740 * but this is probably not worthwhile.)
2741 */
2742
2743 i -= Bias;
2744#ifdef IBM
2745 i <<= 2;
2746 i += j;
2747#endif
2748#ifndef Sudden_Underflow
2749 denorm = 0;
2750 }
2751 else {
2752 /* d is denormalized */
2753
2754 i = bbits + be + (Bias + (P-1) - 1);
2755 x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
2756 : word1(d) << (32 - i);
2757 dval(d2) = x;
2758 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2759 i -= (Bias + (P-1) - 1) + 1;
2760 denorm = 1;
2761 }
2762#endif
2763 ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2764 k = (int)ds;
2765 if (ds < 0. && ds != k)
2766 k--; /* want k = floor(ds) */
2767 k_check = 1;
2768 if (k >= 0 && k <= Ten_pmax) {
2769 if (dval(d) < tens[k])
2770 k--;
2771 k_check = 0;
2772 }
2773 j = bbits - i - 1;
2774 if (j >= 0) {
2775 b2 = 0;
2776 s2 = j;
2777 }
2778 else {
2779 b2 = -j;
2780 s2 = 0;
2781 }
2782 if (k >= 0) {
2783 b5 = 0;
2784 s5 = k;
2785 s2 += k;
2786 }
2787 else {
2788 b2 -= k;
2789 b5 = -k;
2790 s5 = 0;
2791 }
2792 if (mode < 0 || mode > 9)
2793 mode = 0;
2794
2795#ifndef SET_INEXACT
2796#ifdef Check_FLT_ROUNDS
2797 try_quick = Rounding == 1;
2798#else
2799 try_quick = 1;
2800#endif
2801#endif /*SET_INEXACT*/
2802
2803 if (mode > 5) {
2804 mode -= 4;
2805 try_quick = 0;
2806 }
2807 leftright = 1;
2808 ilim = ilim1 = -1;
2809 switch (mode) {
2810 case 0:
2811 case 1:
2812 i = 18;
2813 ndigits = 0;
2814 break;
2815 case 2:
2816 leftright = 0;
2817 /* no break */
2818 case 4:
2819 if (ndigits <= 0)
2820 ndigits = 1;
2821 ilim = ilim1 = i = ndigits;
2822 break;
2823 case 3:
2824 leftright = 0;
2825 /* no break */
2826 case 5:
2827 i = ndigits + k + 1;
2828 ilim = i;
2829 ilim1 = i - 1;
2830 if (i <= 0)
2831 i = 1;
2832 }
2833 s = s0 = rv_alloc(i+1);
2834
2835#ifdef Honor_FLT_ROUNDS
2836 if (mode > 1 && rounding != 1)
2837 leftright = 0;
2838#endif
2839
2840 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2841
2842 /* Try to get by with floating-point arithmetic. */
2843
2844 i = 0;
2845 dval(d2) = dval(d);
2846 k0 = k;
2847 ilim0 = ilim;
2848 ieps = 2; /* conservative */
2849 if (k > 0) {
2850 ds = tens[k&0xf];
2851 j = k >> 4;
2852 if (j & Bletch) {
2853 /* prevent overflows */
2854 j &= Bletch - 1;
2855 dval(d) /= bigtens[n_bigtens-1];
2856 ieps++;
2857 }
2858 for (; j; j >>= 1, i++)
2859 if (j & 1) {
2860 ieps++;
2861 ds *= bigtens[i];
2862 }
2863 dval(d) /= ds;
2864 }
2865 else if ((j1 = -k) != 0) {
2866 dval(d) *= tens[j1 & 0xf];
2867 for (j = j1 >> 4; j; j >>= 1, i++)
2868 if (j & 1) {
2869 ieps++;
2870 dval(d) *= bigtens[i];
2871 }
2872 }
2873 if (k_check && dval(d) < 1. && ilim > 0) {
2874 if (ilim1 <= 0)
2875 goto fast_failed;
2876 ilim = ilim1;
2877 k--;
2878 dval(d) *= 10.;
2879 ieps++;
2880 }
2881 dval(eps) = ieps*dval(d) + 7.;
2882 word0(eps) -= (P-1)*Exp_msk1;
2883 if (ilim == 0) {
2884 S = mhi = 0;
2885 dval(d) -= 5.;
2886 if (dval(d) > dval(eps))
2887 goto one_digit;
2888 if (dval(d) < -dval(eps))
2889 goto no_digits;
2890 goto fast_failed;
2891 }
2892#ifndef No_leftright
2893 if (leftright) {
2894 /* Use Steele & White method of only
2895 * generating digits needed.
2896 */
2897 dval(eps) = 0.5/tens[ilim-1] - dval(eps);
2898 for (i = 0;;) {
2899 L = (int)dval(d);
2900 dval(d) -= L;
2901 *s++ = '0' + (int)L;
2902 if (dval(d) < dval(eps))
2903 goto ret1;
2904 if (1. - dval(d) < dval(eps))
2905 goto bump_up;
2906 if (++i >= ilim)
2907 break;
2908 dval(eps) *= 10.;
2909 dval(d) *= 10.;
2910 }
2911 }
2912 else {
2913#endif
2914 /* Generate ilim digits, then fix them up. */
2915 dval(eps) *= tens[ilim-1];
2916 for (i = 1;; i++, dval(d) *= 10.) {
2917 L = (Long)(dval(d));
2918 if (!(dval(d) -= L))
2919 ilim = i;
2920 *s++ = '0' + (int)L;
2921 if (i == ilim) {
2922 if (dval(d) > 0.5 + dval(eps))
2923 goto bump_up;
2924 else if (dval(d) < 0.5 - dval(eps)) {
2925 while (*--s == '0') ;
2926 s++;
2927 goto ret1;
2928 }
2929 half = 1;
2930 if ((*(s-1) - '0') & 1) {
2931 goto bump_up;
2932 }
2933 break;
2934 }
2935 }
2936#ifndef No_leftright
2937 }
2938#endif
2939fast_failed:
2940 s = s0;
2941 dval(d) = dval(d2);
2942 k = k0;
2943 ilim = ilim0;
2944 }
2945
2946 /* Do we have a "small" integer? */
2947
2948 if (be >= 0 && k <= Int_max) {
2949 /* Yes. */
2950 ds = tens[k];
2951 if (ndigits < 0 && ilim <= 0) {
2952 S = mhi = 0;
2953 if (ilim < 0 || dval(d) <= 5*ds)
2954 goto no_digits;
2955 goto one_digit;
2956 }
2957 for (i = 1;; i++, dval(d) *= 10.) {
2958 L = (Long)(dval(d) / ds);
2959 dval(d) -= L*ds;
2960#ifdef Check_FLT_ROUNDS
2961 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2962 if (dval(d) < 0) {
2963 L--;
2964 dval(d) += ds;
2965 }
2966#endif
2967 *s++ = '0' + (int)L;
2968 if (!dval(d)) {
2969#ifdef SET_INEXACT
2970 inexact = 0;
2971#endif
2972 break;
2973 }
2974 if (i == ilim) {
2975#ifdef Honor_FLT_ROUNDS
2976 if (mode > 1)
2977 switch (rounding) {
2978 case 0: goto ret1;
2979 case 2: goto bump_up;
2980 }
2981#endif
2982 dval(d) += dval(d);
2983 if (dval(d) > ds || (dval(d) == ds && (L & 1))) {
2984bump_up:
2985 while (*--s == '9')
2986 if (s == s0) {
2987 k++;
2988 *s = '0';
2989 break;
2990 }
2991 ++*s++;
2992 }
2993 break;
2994 }
2995 }
2996 goto ret1;
2997 }
2998
2999 m2 = b2;
3000 m5 = b5;
3001 if (leftright) {
3002 i =
3003#ifndef Sudden_Underflow
3004 denorm ? be + (Bias + (P-1) - 1 + 1) :
3005#endif
3006#ifdef IBM
3007 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
3008#else
3009 1 + P - bbits;
3010#endif
3011 b2 += i;
3012 s2 += i;
3013 mhi = i2b(1);
3014 }
3015 if (m2 > 0 && s2 > 0) {
3016 i = m2 < s2 ? m2 : s2;
3017 b2 -= i;
3018 m2 -= i;
3019 s2 -= i;
3020 }
3021 if (b5 > 0) {
3022 if (leftright) {
3023 if (m5 > 0) {
3024 mhi = pow5mult(mhi, m5);
3025 b1 = mult(mhi, b);
3026 Bfree(b);
3027 b = b1;
3028 }
3029 if ((j = b5 - m5) != 0)
3030 b = pow5mult(b, j);
3031 }
3032 else
3033 b = pow5mult(b, b5);
3034 }
3035 S = i2b(1);
3036 if (s5 > 0)
3037 S = pow5mult(S, s5);
3038
3039 /* Check for special case that d is a normalized power of 2. */
3040
3041 spec_case = 0;
3042 if ((mode < 2 || leftright)
3043#ifdef Honor_FLT_ROUNDS
3044 && rounding == 1
3045#endif
3046 ) {
3047 if (!word1(d) && !(word0(d) & Bndry_mask)
3048#ifndef Sudden_Underflow
3049 && word0(d) & (Exp_mask & ~Exp_msk1)
3050#endif
3051 ) {
3052 /* The special case */
3053 b2 += Log2P;
3054 s2 += Log2P;
3055 spec_case = 1;
3056 }
3057 }
3058
3059 /* Arrange for convenient computation of quotients:
3060 * shift left if necessary so divisor has 4 leading 0 bits.
3061 *
3062 * Perhaps we should just compute leading 28 bits of S once
3063 * and for all and pass them and a shift to quorem, so it
3064 * can do shifts and ors to compute the numerator for q.
3065 */
3066#ifdef Pack_32
3067 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
3068 i = 32 - i;
3069#else
3070 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) != 0)
3071 i = 16 - i;
3072#endif
3073 if (i > 4) {
3074 i -= 4;
3075 b2 += i;
3076 m2 += i;
3077 s2 += i;
3078 }
3079 else if (i < 4) {
3080 i += 28;
3081 b2 += i;
3082 m2 += i;
3083 s2 += i;
3084 }
3085 if (b2 > 0)
3086 b = lshift(b, b2);
3087 if (s2 > 0)
3088 S = lshift(S, s2);
3089 if (k_check) {
3090 if (cmp(b,S) < 0) {
3091 k--;
3092 b = multadd(b, 10, 0); /* we botched the k estimate */
3093 if (leftright)
3094 mhi = multadd(mhi, 10, 0);
3095 ilim = ilim1;
3096 }
3097 }
3098 if (ilim <= 0 && (mode == 3 || mode == 5)) {
3099 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3100 /* no digits, fcvt style */
3101no_digits:
3102 k = -1 - ndigits;
3103 goto ret;
3104 }
3105one_digit:
3106 *s++ = '1';
3107 k++;
3108 goto ret;
3109 }
3110 if (leftright) {
3111 if (m2 > 0)
3112 mhi = lshift(mhi, m2);
3113
3114 /* Compute mlo -- check for special case
3115 * that d is a normalized power of 2.
3116 */
3117
3118 mlo = mhi;
3119 if (spec_case) {
3120 mhi = Balloc(mhi->k);
3121 Bcopy(mhi, mlo);
3122 mhi = lshift(mhi, Log2P);
3123 }
3124
3125 for (i = 1;;i++) {
3126 dig = quorem(b,S) + '0';
3127 /* Do we yet have the shortest decimal string
3128 * that will round to d?
3129 */
3130 j = cmp(b, mlo);
3131 delta = diff(S, mhi);
3132 j1 = delta->sign ? 1 : cmp(b, delta);
3133 Bfree(delta);
3134#ifndef ROUND_BIASED
3135 if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3136#ifdef Honor_FLT_ROUNDS
3137 && rounding >= 1
3138#endif
3139 ) {
3140 if (dig == '9')
3141 goto round_9_up;
3142 if (j > 0)
3143 dig++;
3144#ifdef SET_INEXACT
3145 else if (!b->x[0] && b->wds <= 1)
3146 inexact = 0;
3147#endif
3148 *s++ = dig;
3149 goto ret;
3150 }
3151#endif
3152 if (j < 0 || (j == 0 && mode != 1
3153#ifndef ROUND_BIASED
3154 && !(word1(d) & 1)
3155#endif
3156 )) {
3157 if (!b->x[0] && b->wds <= 1) {
3158#ifdef SET_INEXACT
3159 inexact = 0;
3160#endif
3161 goto accept_dig;
3162 }
3163#ifdef Honor_FLT_ROUNDS
3164 if (mode > 1)
3165 switch (rounding) {
3166 case 0: goto accept_dig;
3167 case 2: goto keep_dig;
3168 }
3169#endif /*Honor_FLT_ROUNDS*/
3170 if (j1 > 0) {
3171 b = lshift(b, 1);
3172 j1 = cmp(b, S);
3173 if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9')
3174 goto round_9_up;
3175 }
3176accept_dig:
3177 *s++ = dig;
3178 goto ret;
3179 }
3180 if (j1 > 0) {
3181#ifdef Honor_FLT_ROUNDS
3182 if (!rounding)
3183 goto accept_dig;
3184#endif
3185 if (dig == '9') { /* possible if i == 1 */
3186round_9_up:
3187 *s++ = '9';
3188 goto roundoff;
3189 }
3190 *s++ = dig + 1;
3191 goto ret;
3192 }
3193#ifdef Honor_FLT_ROUNDS
3194keep_dig:
3195#endif
3196 *s++ = dig;
3197 if (i == ilim)
3198 break;
3199 b = multadd(b, 10, 0);
3200 if (mlo == mhi)
3201 mlo = mhi = multadd(mhi, 10, 0);
3202 else {
3203 mlo = multadd(mlo, 10, 0);
3204 mhi = multadd(mhi, 10, 0);
3205 }
3206 }
3207 }
3208 else
3209 for (i = 1;; i++) {
3210 *s++ = dig = quorem(b,S) + '0';
3211 if (!b->x[0] && b->wds <= 1) {
3212#ifdef SET_INEXACT
3213 inexact = 0;
3214#endif
3215 goto ret;
3216 }
3217 if (i >= ilim)
3218 break;
3219 b = multadd(b, 10, 0);
3220 }
3221
3222 /* Round off last digit */
3223
3224#ifdef Honor_FLT_ROUNDS
3225 switch (rounding) {
3226 case 0: goto trimzeros;
3227 case 2: goto roundoff;
3228 }
3229#endif
3230 b = lshift(b, 1);
3231 j = cmp(b, S);
3232 if (j > 0 || (j == 0 && (dig & 1))) {
3233 roundoff:
3234 while (*--s == '9')
3235 if (s == s0) {
3236 k++;
3237 *s++ = '1';
3238 goto ret;
3239 }
3240 if (!half || (*s - '0') & 1)
3241 ++*s;
3242 }
3243 else {
3244 while (*--s == '0') ;
3245 }
3246 s++;
3247ret:
3248 Bfree(S);
3249 if (mhi) {
3250 if (mlo && mlo != mhi)
3251 Bfree(mlo);
3252 Bfree(mhi);
3253 }
3254ret1:
3255#ifdef SET_INEXACT
3256 if (inexact) {
3257 if (!oldinexact) {
3258 word0(d) = Exp_1 + (70 << Exp_shift);
3259 word1(d) = 0;
3260 dval(d) += 1.;
3261 }
3262 }
3263 else if (!oldinexact)
3264 clear_inexact();
3265#endif
3266 Bfree(b);
3267 *s = 0;
3268 *decpt = k + 1;
3269 if (rve)
3270 *rve = s;
3271 return s0;
3272}
3273
3274/*-
3275 * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
3276 * All rights reserved.
3277 *
3278 * Redistribution and use in source and binary forms, with or without
3279 * modification, are permitted provided that the following conditions
3280 * are met:
3281 * 1. Redistributions of source code must retain the above copyright
3282 * notice, this list of conditions and the following disclaimer.
3283 * 2. Redistributions in binary form must reproduce the above copyright
3284 * notice, this list of conditions and the following disclaimer in the
3285 * documentation and/or other materials provided with the distribution.
3286 *
3287 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3288 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3289 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3290 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3291 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3292 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3293 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3294 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3295 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3296 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3297 * SUCH DAMAGE.
3298 */
3299
3300#define DBL_MANH_SIZE 20
3301#define DBL_MANL_SIZE 32
3302#define DBL_ADJ (DBL_MAX_EXP - 2)
3303#define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1)
3304#define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
3305#define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift)))
3306#define dmanh_get(u) ((uint32_t)(word0(u) & Frac_mask))
3307#define dmanl_get(u) ((uint32_t)word1(u))
3308
3309
3310/*
3311 * This procedure converts a double-precision number in IEEE format
3312 * into a string of hexadecimal digits and an exponent of 2. Its
3313 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
3314 * following exceptions:
3315 *
3316 * - An ndigits < 0 causes it to use as many digits as necessary to
3317 * represent the number exactly.
3318 * - The additional xdigs argument should point to either the string
3319 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
3320 * which case is desired.
3321 * - This routine does not repeat dtoa's mistake of setting decpt
3322 * to 9999 in the case of an infinity or NaN. INT_MAX is used
3323 * for this purpose instead.
3324 *
3325 * Note that the C99 standard does not specify what the leading digit
3326 * should be for non-zero numbers. For instance, 0x1.3p3 is the same
3327 * as 0x2.6p2 is the same as 0x4.cp3. This implementation always makes
3328 * the leading digit a 1. This ensures that the exponent printed is the
3329 * actual base-2 exponent, i.e., ilogb(d).
3330 *
3331 * Inputs: d, xdigs, ndigits
3332 * Outputs: decpt, sign, rve
3333 */
3334char *
3335hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve)
3336{
3337 U u;
3338 char *s, *s0;
3339 int bufsize;
3340 uint32_t manh, manl;
3341
3342 u.d = d;
3343 if (word0(u) & Sign_bit) {
3344 /* set sign for everything, including 0's and NaNs */
3345 *sign = 1;
3346 word0(u) &= ~Sign_bit; /* clear sign bit */
3347 }
3348 else
3349 *sign = 0;
3350
3351 if (isinf(d)) { /* FP_INFINITE */
3352 *decpt = INT_MAX;
3353 return rv_strdup(INFSTR, rve);
3354 }
3355 else if (isnan(d)) { /* FP_NAN */
3356 *decpt = INT_MAX;
3357 return rv_strdup(NANSTR, rve);
3358 }
3359 else if (d == 0.0) { /* FP_ZERO */
3360 *decpt = 1;
3361 return rv_strdup(ZEROSTR, rve);
3362 }
3363 else if (dexp_get(u)) { /* FP_NORMAL */
3364 *decpt = dexp_get(u) - DBL_ADJ;
3365 }
3366 else { /* FP_SUBNORMAL */
3367 u.d *= 5.363123171977039e+154 /* 0x1p514 */;
3368 *decpt = dexp_get(u) - (514 + DBL_ADJ);
3369 }
3370
3371 if (ndigits == 0) /* dtoa() compatibility */
3372 ndigits = 1;
3373
3374 /*
3375 * If ndigits < 0, we are expected to auto-size, so we allocate
3376 * enough space for all the digits.
3377 */
3378 bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
3379 s0 = rv_alloc(bufsize+1);
3380
3381 /* Round to the desired number of digits. */
3382 if (SIGFIGS > ndigits && ndigits > 0) {
3383 float redux = 1.0f;
3384 int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
3385 dexp_set(u, offset);
3386 u.d += redux;
3387 u.d -= redux;
3388 *decpt += dexp_get(u) - offset;
3389 }
3390
3391 manh = dmanh_get(u);
3392 manl = dmanl_get(u);
3393 *s0 = '1';
3394 for (s = s0 + 1; s < s0 + bufsize; s++) {
3395 *s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
3396 manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
3397 manl <<= 4;
3398 }
3399
3400 /* If ndigits < 0, we are expected to auto-size the precision. */
3401 if (ndigits < 0) {
3402 for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
3403 ;
3404 }
3405
3406 s = s0 + ndigits;
3407 *s = '\0';
3408 if (rve != NULL)
3409 *rve = s;
3410 return (s0);
3411}
3412
3413#ifdef __cplusplus
3414#if 0
3415{ /* satisfy cc-mode */
3416#endif
3417}
3418#endif
int errno
#define DBL_MANT_DIG
Definition: acosh.c:19
#define L(x)
Definition: asm.h:125
#define dexp_set(u, v)
Definition: dtoa.c:3305
#define Exp_shift
Definition: dtoa.c:333
#define rounded_product(a, b)
Definition: dtoa.c:450
#define Exp_msk11
Definition: dtoa.c:336
#define Tiny0
Definition: dtoa.c:353
#define Big1
Definition: dtoa.c:455
#define Exp_msk1
Definition: dtoa.c:335
#define FREE_DTOA_LOCK(n)
Definition: dtoa.c:489
#define d1
#define P
Definition: dtoa.c:338
#define IEEE_Arith
Definition: dtoa.c:237
#define Bndry_mask
Definition: dtoa.c:348
#define Ebits
Definition: dtoa.c:343
#define rounded_quotient(a, b)
Definition: dtoa.c:451
#define word1(x)
Definition: dtoa.c:306
#define IEEE_LITTLE_ENDIAN
Definition: dtoa.c:169
#define MALLOC
Definition: dtoa.c:215
#define Frac_mask
Definition: dtoa.c:344
#define Tiny1
Definition: dtoa.c:354
#define Frac_mask1
Definition: dtoa.c:345
#define Quick_max
Definition: dtoa.c:355
#define Bias
Definition: dtoa.c:339
#define PRIVATE_mem
Definition: dtoa.c:227
#define dmanh_get(u)
Definition: dtoa.c:3306
#define n_bigtens
Definition: dtoa.c:1350
#define LSB
Definition: dtoa.c:350
#define dval(x)
Definition: dtoa.c:311
#define Exp_mask
Definition: dtoa.c:337
#define Kmax
Definition: dtoa.c:492
#define Ten_pmax
Definition: dtoa.c:346
#define Sign_bit
Definition: dtoa.c:351
char * hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve)
Definition: dtoa.c:3335
U double_u
Definition: dtoa.c:303
char * dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
Definition: dtoa.c:2597
#define Bletch
Definition: dtoa.c:347
#define dexp_get(u)
Definition: dtoa.c:3304
#define DBL_MANL_SIZE
Definition: dtoa.c:3301
#define Flt_Rounds
Definition: dtoa.c:368
#define FREE
Definition: dtoa.c:220
#define Storeinc(a, b, c)
Definition: dtoa.c:319
#define rv_alloc(i)
Definition: dtoa.c:2527
#define rv_strdup(s, rve)
Definition: dtoa.c:2542
#define word0(x)
Definition: dtoa.c:305
#define ACQUIRE_DTOA_LOCK(n)
Definition: dtoa.c:488
#define Exp_1
Definition: dtoa.c:341
#define DBL_ADJ
Definition: dtoa.c:3302
#define Exp_shift1
Definition: dtoa.c:334
#define Rounding
Definition: dtoa.c:377
#define Emin
Definition: dtoa.c:340
NO_SANITIZE("unsigned-integer-overflow", static Bigint *diff(Bigint *a, Bigint *b))
#define dmanl_get(u)
Definition: dtoa.c:3307
#define Bcopy(x, y)
Definition: dtoa.c:554
#define DBL_MANH_SIZE
Definition: dtoa.c:3300
#define Bndry_mask1
Definition: dtoa.c:349
#define FFFFFFFF
Definition: dtoa.c:461
double strtod(const char *s00, char **se)
Definition: dtoa.c:1445
#define Scale_Bit
Definition: dtoa.c:1349
#define d0
#define Log2P
Definition: dtoa.c:352
#define Big0
Definition: dtoa.c:454
#define Avoid_Underflow
Definition: dtoa.c:358
#define Exp_11
Definition: dtoa.c:342
#define SIGFIGS
Definition: dtoa.c:3303
#define Int_max
Definition: dtoa.c:356
struct Bigint Bigint
Definition: dtoa.c:500
#define S(s)
#define DBL_MAX_EXP
Definition: numeric.c:46
#define FLT_RADIX
Definition: numeric.c:31
#define DBL_MAX_10_EXP
Definition: numeric.c:52
#define DBL_DIG
Definition: numeric.c:55
#define hexdigits
__uint32_t uint32_t
#define NULL
int abs(int)
#define xfree
double j1(double)
const char size_t n
#define xmalloc
#define isinf(__x)
uint32_t i
#define isnan(__x)
__inline__ const void *__restrict__ size_t len
const char * s2
int VALUE v
#define INT_MAX
#define ERANGE
char * strchr(const char *, int)
Definition: strchr.c:8
double ldexp(double, int)
#define ISDIGIT(c)
__inline__ int
Definition: dtoa.c:494
int k
Definition: dtoa.c:496
struct Bigint * next
Definition: dtoa.c:495
int sign
Definition: dtoa.c:496
int maxwds
Definition: dtoa.c:496
int wds
Definition: dtoa.c:496
ULong x[1]
Definition: dtoa.c:497
Definition: dtoa.c:290
double d
Definition: dtoa.c:290
#define hexdigit
Definition: util.c:31