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