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