/src/brpc/src/butil/third_party/dmg_fp/dtoa.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /**************************************************************** |
2 | | * |
3 | | * The author of this software is David M. Gay. |
4 | | * |
5 | | * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. |
6 | | * |
7 | | * Permission to use, copy, modify, and distribute this software for any |
8 | | * purpose without fee is hereby granted, provided that this entire notice |
9 | | * is included in all copies of any software which is or includes a copy |
10 | | * or modification of this software and in all copies of the supporting |
11 | | * documentation for such software. |
12 | | * |
13 | | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED |
14 | | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY |
15 | | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY |
16 | | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. |
17 | | * |
18 | | ***************************************************************/ |
19 | | |
20 | | /* Please send bug reports to David M. Gay (dmg at acm dot org, |
21 | | * with " at " changed at "@" and " dot " changed to "."). */ |
22 | | |
23 | | /* On a machine with IEEE extended-precision registers, it is |
24 | | * necessary to specify double-precision (53-bit) rounding precision |
25 | | * before invoking strtod or dtoa. If the machine uses (the equivalent |
26 | | * of) Intel 80x87 arithmetic, the call |
27 | | * _control87(PC_53, MCW_PC); |
28 | | * does this with many compilers. Whether this or another call is |
29 | | * appropriate depends on the compiler; for this to work, it may be |
30 | | * necessary to #include "float.h" or another system-dependent header |
31 | | * file. |
32 | | */ |
33 | | |
34 | | /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. |
35 | | * |
36 | | * This strtod returns a nearest machine number to the input decimal |
37 | | * string (or sets errno to ERANGE). With IEEE arithmetic, ties are |
38 | | * broken by the IEEE round-even rule. Otherwise ties are broken by |
39 | | * biased rounding (add half and chop). |
40 | | * |
41 | | * Inspired loosely by William D. Clinger's paper "How to Read Floating |
42 | | * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. |
43 | | * |
44 | | * Modifications: |
45 | | * |
46 | | * 1. We only require IEEE, IBM, or VAX double-precision |
47 | | * arithmetic (not IEEE double-extended). |
48 | | * 2. We get by with floating-point arithmetic in a case that |
49 | | * Clinger missed -- when we're computing d * 10^n |
50 | | * for a small integer d and the integer n is not too |
51 | | * much larger than 22 (the maximum integer k for which |
52 | | * we can represent 10^k exactly), we may be able to |
53 | | * compute (d*10^k) * 10^(e-k) with just one roundoff. |
54 | | * 3. Rather than a bit-at-a-time adjustment of the binary |
55 | | * result in the hard case, we use floating-point |
56 | | * arithmetic to determine the adjustment to within |
57 | | * one bit; only in really hard cases do we need to |
58 | | * compute a second residual. |
59 | | * 4. Because of 3., we don't need a large table of powers of 10 |
60 | | * for ten-to-e (just some small tables, e.g. of 10^k |
61 | | * for 0 <= k <= 22). |
62 | | */ |
63 | | |
64 | | /* |
65 | | * #define IEEE_8087 for IEEE-arithmetic machines where the least |
66 | | * significant byte has the lowest address. |
67 | | * #define IEEE_MC68k for IEEE-arithmetic machines where the most |
68 | | * significant byte has the lowest address. |
69 | | * #define Long int on machines with 32-bit ints and 64-bit longs. |
70 | | * #define IBM for IBM mainframe-style floating-point arithmetic. |
71 | | * #define VAX for VAX-style floating-point arithmetic (D_floating). |
72 | | * #define No_leftright to omit left-right logic in fast floating-point |
73 | | * computation of dtoa. |
74 | | * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 |
75 | | * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS |
76 | | * is also #defined, fegetround() will be queried for the rounding mode. |
77 | | * Note that both FLT_ROUNDS and fegetround() are specified by the C99 |
78 | | * standard (and are specified to be consistent, with fesetround() |
79 | | * affecting the value of FLT_ROUNDS), but that some (Linux) systems |
80 | | * do not work correctly in this regard, so using fegetround() is more |
81 | | * portable than using FLT_FOUNDS directly. |
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. Similarly, if you |
107 | | * want something other than the system's free() to be called to |
108 | | * recycle memory acquired from MALLOC, #define FREE to be the |
109 | | * name of the alternate routine. (FREE or free is only called in |
110 | | * pathological cases, e.g., in a dtoa call after a dtoa return in |
111 | | * mode 3 with thousands of digits requested.) |
112 | | * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making |
113 | | * memory allocations from a private pool of memory when possible. |
114 | | * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, |
115 | | * unless #defined to be a different length. This default length |
116 | | * suffices to get rid of MALLOC calls except for unusual cases, |
117 | | * such as decimal-to-binary conversion of a very long string of |
118 | | * digits. The longest string dtoa can return is about 751 bytes |
119 | | * long. For conversions by strtod of strings of 800 digits and |
120 | | * all dtoa conversions in single-threaded executions with 8-byte |
121 | | * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte |
122 | | * pointers, PRIVATE_MEM >= 7112 appears adequate. |
123 | | * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK |
124 | | * #defined automatically on IEEE systems. On such systems, |
125 | | * when INFNAN_CHECK is #defined, strtod checks |
126 | | * for Infinity and NaN (case insensitively). On some systems |
127 | | * (e.g., some HP systems), it may be necessary to #define NAN_WORD0 |
128 | | * appropriately -- to the most significant word of a quiet NaN. |
129 | | * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.) |
130 | | * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, |
131 | | * strtod also accepts (case insensitively) strings of the form |
132 | | * NaN(x), where x is a string of hexadecimal digits and spaces; |
133 | | * if there is only one string of hexadecimal digits, it is taken |
134 | | * for the 52 fraction bits of the resulting NaN; if there are two |
135 | | * or more strings of hex digits, the first is for the high 20 bits, |
136 | | * the second and subsequent for the low 32 bits, with intervening |
137 | | * white space ignored; but if this results in none of the 52 |
138 | | * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0 |
139 | | * and NAN_WORD1 are used instead. |
140 | | * #define MULTIPLE_THREADS if the system offers preemptively scheduled |
141 | | * multiple threads. In this case, you must provide (or suitably |
142 | | * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed |
143 | | * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed |
144 | | * in pow5mult, ensures lazy evaluation of only one copy of high |
145 | | * powers of 5; omitting this lock would introduce a small |
146 | | * probability of wasting memory, but would otherwise be harmless.) |
147 | | * You must also invoke freedtoa(s) to free the value s returned by |
148 | | * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. |
149 | | * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that |
150 | | * avoids underflows on inputs whose result does not underflow. |
151 | | * If you #define NO_IEEE_Scale on a machine that uses IEEE-format |
152 | | * floating-point numbers and flushes underflows to zero rather |
153 | | * than implementing gradual underflow, then you must also #define |
154 | | * Sudden_Underflow. |
155 | | * #define USE_LOCALE to use the current locale's decimal_point value. |
156 | | * #define SET_INEXACT if IEEE arithmetic is being used and extra |
157 | | * computation should be done to set the inexact flag when the |
158 | | * result is inexact and avoid setting inexact when the result |
159 | | * is exact. In this case, dtoa.c must be compiled in |
160 | | * an environment, perhaps provided by #include "dtoa.c" in a |
161 | | * suitable wrapper, that defines two functions, |
162 | | * int get_inexact(void); |
163 | | * void clear_inexact(void); |
164 | | * such that get_inexact() returns a nonzero value if the |
165 | | * inexact bit is already set, and clear_inexact() sets the |
166 | | * inexact bit to 0. When SET_INEXACT is #defined, strtod |
167 | | * also does extra computations to set the underflow and overflow |
168 | | * flags when appropriate (i.e., when the result is tiny and |
169 | | * inexact or when it is a numeric value rounded to +-infinity). |
170 | | * #define NO_ERRNO if strtod should not assign errno = ERANGE when |
171 | | * the result overflows to +-Infinity or underflows to 0. |
172 | | * #define NO_HEX_FP to omit recognition of hexadecimal floating-point |
173 | | * values by strtod. |
174 | | * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now) |
175 | | * to disable logic for "fast" testing of very long input strings |
176 | | * to strtod. This testing proceeds by initially truncating the |
177 | | * input string, then if necessary comparing the whole string with |
178 | | * a decimal expansion to decide close cases. This logic is only |
179 | | * used for input more than STRTOD_DIGLIM digits long (default 40). |
180 | | */ |
181 | | |
182 | | #define IEEE_8087 |
183 | | #define NO_HEX_FP |
184 | | |
185 | | #ifndef Long |
186 | | #if __LP64__ |
187 | 0 | #define Long int |
188 | | #else |
189 | | #define Long long |
190 | | #endif |
191 | | #endif |
192 | | #ifndef ULong |
193 | | typedef unsigned Long ULong; |
194 | | #endif |
195 | | |
196 | | #ifdef DEBUG |
197 | | #include "stdio.h" |
198 | | #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} |
199 | | #endif |
200 | | |
201 | | #include "stdlib.h" |
202 | | #include "string.h" |
203 | | |
204 | | #ifdef USE_LOCALE |
205 | | #include "locale.h" |
206 | | #endif |
207 | | |
208 | | #ifdef Honor_FLT_ROUNDS |
209 | | #ifndef Trust_FLT_ROUNDS |
210 | | #include <fenv.h> |
211 | | #endif |
212 | | #endif |
213 | | |
214 | | #ifdef MALLOC |
215 | | #ifdef KR_headers |
216 | | extern char *MALLOC(); |
217 | | #else |
218 | | extern void *MALLOC(size_t); |
219 | | #endif |
220 | | #else |
221 | 0 | #define MALLOC malloc |
222 | | #endif |
223 | | |
224 | | #ifndef Omit_Private_Memory |
225 | | #ifndef PRIVATE_MEM |
226 | 0 | #define PRIVATE_MEM 2304 |
227 | | #endif |
228 | 0 | #define PRIVATE_mem ((unsigned)((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))) |
229 | | static double private_mem[PRIVATE_mem], *pmem_next = private_mem; |
230 | | #endif |
231 | | |
232 | | #undef IEEE_Arith |
233 | | #undef Avoid_Underflow |
234 | | #ifdef IEEE_MC68k |
235 | | #define IEEE_Arith |
236 | | #endif |
237 | | #ifdef IEEE_8087 |
238 | | #define IEEE_Arith |
239 | | #endif |
240 | | |
241 | | #ifdef IEEE_Arith |
242 | | #ifndef NO_INFNAN_CHECK |
243 | | #undef INFNAN_CHECK |
244 | | #define INFNAN_CHECK |
245 | | #endif |
246 | | #else |
247 | | #undef INFNAN_CHECK |
248 | | #define NO_STRTOD_BIGCOMP |
249 | | #endif |
250 | | |
251 | | #include "errno.h" |
252 | | |
253 | | #ifdef Bad_float_h |
254 | | |
255 | | #ifdef IEEE_Arith |
256 | | #define DBL_DIG 15 |
257 | | #define DBL_MAX_10_EXP 308 |
258 | | #define DBL_MAX_EXP 1024 |
259 | | #define FLT_RADIX 2 |
260 | | #endif /*IEEE_Arith*/ |
261 | | |
262 | | #ifdef IBM |
263 | | #define DBL_DIG 16 |
264 | | #define DBL_MAX_10_EXP 75 |
265 | | #define DBL_MAX_EXP 63 |
266 | | #define FLT_RADIX 16 |
267 | | #define DBL_MAX 7.2370055773322621e+75 |
268 | | #endif |
269 | | |
270 | | #ifdef VAX |
271 | | #define DBL_DIG 16 |
272 | | #define DBL_MAX_10_EXP 38 |
273 | | #define DBL_MAX_EXP 127 |
274 | | #define FLT_RADIX 2 |
275 | | #define DBL_MAX 1.7014118346046923e+38 |
276 | | #endif |
277 | | |
278 | | #ifndef LONG_MAX |
279 | | #define LONG_MAX 2147483647 |
280 | | #endif |
281 | | |
282 | | #else /* ifndef Bad_float_h */ |
283 | | #include "float.h" |
284 | | #endif /* Bad_float_h */ |
285 | | |
286 | | #ifndef __MATH_H__ |
287 | | #include "math.h" |
288 | | #endif |
289 | | |
290 | | namespace dmg_fp { |
291 | | |
292 | | #ifndef CONST |
293 | | #ifdef KR_headers |
294 | | #define CONST /* blank */ |
295 | | #else |
296 | 0 | #define CONST const |
297 | | #endif |
298 | | #endif |
299 | | |
300 | | #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 |
301 | | Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. |
302 | | #endif |
303 | | |
304 | | typedef union { double d; ULong L[2]; } U; |
305 | | |
306 | | #ifdef IEEE_8087 |
307 | 0 | #define word0(x) (x)->L[1] |
308 | 0 | #define word1(x) (x)->L[0] |
309 | | #else |
310 | | #define word0(x) (x)->L[0] |
311 | | #define word1(x) (x)->L[1] |
312 | | #endif |
313 | 0 | #define dval(x) (x)->d |
314 | | |
315 | | #ifndef STRTOD_DIGLIM |
316 | 0 | #define STRTOD_DIGLIM 40 |
317 | | #endif |
318 | | |
319 | | #ifdef DIGLIM_DEBUG |
320 | | extern int strtod_diglim; |
321 | | #else |
322 | 0 | #define strtod_diglim STRTOD_DIGLIM |
323 | | #endif |
324 | | |
325 | | /* The following definition of Storeinc is appropriate for MIPS processors. |
326 | | * An alternative that might be better on some machines is |
327 | | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) |
328 | | */ |
329 | | #if defined(IEEE_8087) + defined(VAX) |
330 | | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ |
331 | | ((unsigned short *)a)[0] = (unsigned short)c, a++) |
332 | | #else |
333 | | #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ |
334 | | ((unsigned short *)a)[1] = (unsigned short)c, a++) |
335 | | #endif |
336 | | |
337 | | /* #define P DBL_MANT_DIG */ |
338 | | /* Ten_pmax = floor(P*log(2)/log(5)) */ |
339 | | /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ |
340 | | /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ |
341 | | /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ |
342 | | |
343 | | #ifdef IEEE_Arith |
344 | 0 | #define Exp_shift 20 |
345 | 0 | #define Exp_shift1 20 |
346 | 0 | #define Exp_msk1 0x100000 |
347 | | #define Exp_msk11 0x100000 |
348 | 0 | #define Exp_mask 0x7ff00000 |
349 | 0 | #define P 53 |
350 | | #define Nbits 53 |
351 | 0 | #define Bias 1023 |
352 | | #define Emax 1023 |
353 | 0 | #define Emin (-1022) |
354 | 0 | #define Exp_1 0x3ff00000 |
355 | 0 | #define Exp_11 0x3ff00000 |
356 | 0 | #define Ebits 11 |
357 | 0 | #define Frac_mask 0xfffff |
358 | 0 | #define Frac_mask1 0xfffff |
359 | 0 | #define Ten_pmax 22 |
360 | 0 | #define Bletch 0x10 |
361 | 0 | #define Bndry_mask 0xfffff |
362 | 0 | #define Bndry_mask1 0xfffff |
363 | 0 | #define LSB 1 |
364 | 0 | #define Sign_bit 0x80000000 |
365 | 0 | #define Log2P 1 |
366 | | #define Tiny0 0 |
367 | 0 | #define Tiny1 1 |
368 | 0 | #define Quick_max 14 |
369 | 0 | #define Int_max 14 |
370 | | #ifndef NO_IEEE_Scale |
371 | | #define Avoid_Underflow |
372 | | #ifdef Flush_Denorm /* debugging option */ |
373 | | #undef Sudden_Underflow |
374 | | #endif |
375 | | #endif |
376 | | |
377 | | #ifndef Flt_Rounds |
378 | | #ifdef FLT_ROUNDS |
379 | 0 | #define Flt_Rounds FLT_ROUNDS |
380 | | #else |
381 | | #define Flt_Rounds 1 |
382 | | #endif |
383 | | #endif /*Flt_Rounds*/ |
384 | | |
385 | | #ifdef Honor_FLT_ROUNDS |
386 | | #undef Check_FLT_ROUNDS |
387 | | #define Check_FLT_ROUNDS |
388 | | #else |
389 | | #define Rounding Flt_Rounds |
390 | | #endif |
391 | | |
392 | | #else /* ifndef IEEE_Arith */ |
393 | | #undef Check_FLT_ROUNDS |
394 | | #undef Honor_FLT_ROUNDS |
395 | | #undef SET_INEXACT |
396 | | #undef Sudden_Underflow |
397 | | #define Sudden_Underflow |
398 | | #ifdef IBM |
399 | | #undef Flt_Rounds |
400 | | #define Flt_Rounds 0 |
401 | | #define Exp_shift 24 |
402 | | #define Exp_shift1 24 |
403 | | #define Exp_msk1 0x1000000 |
404 | | #define Exp_msk11 0x1000000 |
405 | | #define Exp_mask 0x7f000000 |
406 | | #define P 14 |
407 | | #define Nbits 56 |
408 | | #define Bias 65 |
409 | | #define Emax 248 |
410 | | #define Emin (-260) |
411 | | #define Exp_1 0x41000000 |
412 | | #define Exp_11 0x41000000 |
413 | | #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ |
414 | | #define Frac_mask 0xffffff |
415 | | #define Frac_mask1 0xffffff |
416 | | #define Bletch 4 |
417 | | #define Ten_pmax 22 |
418 | | #define Bndry_mask 0xefffff |
419 | | #define Bndry_mask1 0xffffff |
420 | | #define LSB 1 |
421 | | #define Sign_bit 0x80000000 |
422 | | #define Log2P 4 |
423 | | #define Tiny0 0x100000 |
424 | | #define Tiny1 0 |
425 | | #define Quick_max 14 |
426 | | #define Int_max 15 |
427 | | #else /* VAX */ |
428 | | #undef Flt_Rounds |
429 | | #define Flt_Rounds 1 |
430 | | #define Exp_shift 23 |
431 | | #define Exp_shift1 7 |
432 | | #define Exp_msk1 0x80 |
433 | | #define Exp_msk11 0x800000 |
434 | | #define Exp_mask 0x7f80 |
435 | | #define P 56 |
436 | | #define Nbits 56 |
437 | | #define Bias 129 |
438 | | #define Emax 126 |
439 | | #define Emin (-129) |
440 | | #define Exp_1 0x40800000 |
441 | | #define Exp_11 0x4080 |
442 | | #define Ebits 8 |
443 | | #define Frac_mask 0x7fffff |
444 | | #define Frac_mask1 0xffff007f |
445 | | #define Ten_pmax 24 |
446 | | #define Bletch 2 |
447 | | #define Bndry_mask 0xffff007f |
448 | | #define Bndry_mask1 0xffff007f |
449 | | #define LSB 0x10000 |
450 | | #define Sign_bit 0x8000 |
451 | | #define Log2P 1 |
452 | | #define Tiny0 0x80 |
453 | | #define Tiny1 0 |
454 | | #define Quick_max 15 |
455 | | #define Int_max 15 |
456 | | #endif /* IBM, VAX */ |
457 | | #endif /* IEEE_Arith */ |
458 | | |
459 | | #ifndef IEEE_Arith |
460 | | #define ROUND_BIASED |
461 | | #endif |
462 | | |
463 | | #ifdef RND_PRODQUOT |
464 | | #define rounded_product(a,b) a = rnd_prod(a, b) |
465 | | #define rounded_quotient(a,b) a = rnd_quot(a, b) |
466 | | #ifdef KR_headers |
467 | | extern double rnd_prod(), rnd_quot(); |
468 | | #else |
469 | | extern double rnd_prod(double, double), rnd_quot(double, double); |
470 | | #endif |
471 | | #else |
472 | 0 | #define rounded_product(a,b) a *= b |
473 | 0 | #define rounded_quotient(a,b) a /= b |
474 | | #endif |
475 | | |
476 | 0 | #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) |
477 | 0 | #define Big1 0xffffffff |
478 | | |
479 | | #ifndef Pack_32 |
480 | | #define Pack_32 |
481 | | #endif |
482 | | |
483 | | typedef struct BCinfo BCinfo; |
484 | | struct |
485 | | BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; }; |
486 | | |
487 | | #ifdef KR_headers |
488 | | #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff) |
489 | | #else |
490 | 0 | #define FFFFFFFF 0xffffffffUL |
491 | | #endif |
492 | | |
493 | | #ifdef NO_LONG_LONG |
494 | | #undef ULLong |
495 | | #ifdef Just_16 |
496 | | #undef Pack_32 |
497 | | /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. |
498 | | * This makes some inner loops simpler and sometimes saves work |
499 | | * during multiplications, but it often seems to make things slightly |
500 | | * slower. Hence the default is now to store 32 bits per Long. |
501 | | */ |
502 | | #endif |
503 | | #else /* long long available */ |
504 | | #ifndef Llong |
505 | | #define Llong long long |
506 | | #endif |
507 | | #ifndef ULLong |
508 | 0 | #define ULLong unsigned Llong |
509 | | #endif |
510 | | #endif /* NO_LONG_LONG */ |
511 | | |
512 | | #ifndef MULTIPLE_THREADS |
513 | | #define ACQUIRE_DTOA_LOCK(n) /*nothing*/ |
514 | | #define FREE_DTOA_LOCK(n) /*nothing*/ |
515 | | #endif |
516 | | |
517 | 0 | #define Kmax 7 |
518 | | |
519 | | double strtod(const char *s00, char **se); |
520 | | char *dtoa(double d, int mode, int ndigits, |
521 | | int *decpt, int *sign, char **rve); |
522 | | |
523 | | struct |
524 | | Bigint { |
525 | | struct Bigint *next; |
526 | | int k, maxwds, sign, wds; |
527 | | ULong x[1]; |
528 | | }; |
529 | | |
530 | | typedef struct Bigint Bigint; |
531 | | |
532 | | static Bigint *freelist[Kmax+1]; |
533 | | |
534 | | static Bigint * |
535 | | Balloc |
536 | | #ifdef KR_headers |
537 | | (k) int k; |
538 | | #else |
539 | | (int k) |
540 | | #endif |
541 | 0 | { |
542 | 0 | int x; |
543 | 0 | Bigint *rv; |
544 | 0 | #ifndef Omit_Private_Memory |
545 | 0 | unsigned int len; |
546 | 0 | #endif |
547 | |
|
548 | 0 | ACQUIRE_DTOA_LOCK(0); |
549 | | /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */ |
550 | | /* but this case seems very unlikely. */ |
551 | 0 | if (k <= Kmax && (rv = freelist[k])) |
552 | 0 | freelist[k] = rv->next; |
553 | 0 | else { |
554 | 0 | x = 1 << k; |
555 | | #ifdef Omit_Private_Memory |
556 | | rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); |
557 | | #else |
558 | 0 | len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) |
559 | 0 | /sizeof(double); |
560 | 0 | if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) { |
561 | 0 | rv = (Bigint*)pmem_next; |
562 | 0 | pmem_next += len; |
563 | 0 | } |
564 | 0 | else |
565 | 0 | rv = (Bigint*)MALLOC(len*sizeof(double)); |
566 | 0 | #endif |
567 | 0 | rv->k = k; |
568 | 0 | rv->maxwds = x; |
569 | 0 | } |
570 | 0 | FREE_DTOA_LOCK(0); |
571 | 0 | rv->sign = rv->wds = 0; |
572 | 0 | return rv; |
573 | 0 | } |
574 | | |
575 | | static void |
576 | | Bfree |
577 | | #ifdef KR_headers |
578 | | (v) Bigint *v; |
579 | | #else |
580 | | (Bigint *v) |
581 | | #endif |
582 | 0 | { |
583 | 0 | if (v) { |
584 | 0 | if (v->k > Kmax) |
585 | | #ifdef FREE |
586 | | FREE((void*)v); |
587 | | #else |
588 | 0 | free((void*)v); |
589 | 0 | #endif |
590 | 0 | else { |
591 | 0 | ACQUIRE_DTOA_LOCK(0); |
592 | 0 | v->next = freelist[v->k]; |
593 | 0 | freelist[v->k] = v; |
594 | 0 | FREE_DTOA_LOCK(0); |
595 | 0 | } |
596 | 0 | } |
597 | 0 | } |
598 | | |
599 | 0 | #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ |
600 | 0 | y->wds*sizeof(Long) + 2*sizeof(int)) |
601 | | |
602 | | static Bigint * |
603 | | multadd |
604 | | #ifdef KR_headers |
605 | | (b, m, a) Bigint *b; int m, a; |
606 | | #else |
607 | | (Bigint *b, int m, int a) /* multiply by m and add a */ |
608 | | #endif |
609 | 0 | { |
610 | 0 | int i, wds; |
611 | 0 | #ifdef ULLong |
612 | 0 | ULong *x; |
613 | 0 | ULLong carry, y; |
614 | | #else |
615 | | ULong carry, *x, y; |
616 | | #ifdef Pack_32 |
617 | | ULong xi, z; |
618 | | #endif |
619 | | #endif |
620 | 0 | Bigint *b1; |
621 | |
|
622 | 0 | wds = b->wds; |
623 | 0 | x = b->x; |
624 | 0 | i = 0; |
625 | 0 | carry = a; |
626 | 0 | do { |
627 | 0 | #ifdef ULLong |
628 | 0 | y = *x * (ULLong)m + carry; |
629 | 0 | carry = y >> 32; |
630 | 0 | *x++ = y & FFFFFFFF; |
631 | | #else |
632 | | #ifdef Pack_32 |
633 | | xi = *x; |
634 | | y = (xi & 0xffff) * m + carry; |
635 | | z = (xi >> 16) * m + (y >> 16); |
636 | | carry = z >> 16; |
637 | | *x++ = (z << 16) + (y & 0xffff); |
638 | | #else |
639 | | y = *x * m + carry; |
640 | | carry = y >> 16; |
641 | | *x++ = y & 0xffff; |
642 | | #endif |
643 | | #endif |
644 | 0 | } |
645 | 0 | while(++i < wds); |
646 | 0 | if (carry) { |
647 | 0 | if (wds >= b->maxwds) { |
648 | 0 | b1 = Balloc(b->k+1); |
649 | 0 | Bcopy(b1, b); |
650 | 0 | Bfree(b); |
651 | 0 | b = b1; |
652 | 0 | } |
653 | 0 | b->x[wds++] = carry; |
654 | 0 | b->wds = wds; |
655 | 0 | } |
656 | 0 | return b; |
657 | 0 | } |
658 | | |
659 | | static Bigint * |
660 | | s2b |
661 | | #ifdef KR_headers |
662 | | (s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9; |
663 | | #else |
664 | | (CONST char *s, int nd0, int nd, ULong y9, int dplen) |
665 | | #endif |
666 | 0 | { |
667 | 0 | Bigint *b; |
668 | 0 | int i, k; |
669 | 0 | Long x, y; |
670 | |
|
671 | 0 | x = (nd + 8) / 9; |
672 | 0 | for(k = 0, y = 1; x > y; y <<= 1, k++) ; |
673 | 0 | #ifdef Pack_32 |
674 | 0 | b = Balloc(k); |
675 | 0 | b->x[0] = y9; |
676 | 0 | b->wds = 1; |
677 | | #else |
678 | | b = Balloc(k+1); |
679 | | b->x[0] = y9 & 0xffff; |
680 | | b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; |
681 | | #endif |
682 | |
|
683 | 0 | i = 9; |
684 | 0 | if (9 < nd0) { |
685 | 0 | s += 9; |
686 | 0 | do b = multadd(b, 10, *s++ - '0'); |
687 | 0 | while(++i < nd0); |
688 | 0 | s += dplen; |
689 | 0 | } |
690 | 0 | else |
691 | 0 | s += dplen + 9; |
692 | 0 | for(; i < nd; i++) |
693 | 0 | b = multadd(b, 10, *s++ - '0'); |
694 | 0 | return b; |
695 | 0 | } |
696 | | |
697 | | static int |
698 | | hi0bits |
699 | | #ifdef KR_headers |
700 | | (x) ULong x; |
701 | | #else |
702 | | (ULong x) |
703 | | #endif |
704 | 0 | { |
705 | 0 | int k = 0; |
706 | |
|
707 | 0 | if (!(x & 0xffff0000)) { |
708 | 0 | k = 16; |
709 | 0 | x <<= 16; |
710 | 0 | } |
711 | 0 | if (!(x & 0xff000000)) { |
712 | 0 | k += 8; |
713 | 0 | x <<= 8; |
714 | 0 | } |
715 | 0 | if (!(x & 0xf0000000)) { |
716 | 0 | k += 4; |
717 | 0 | x <<= 4; |
718 | 0 | } |
719 | 0 | if (!(x & 0xc0000000)) { |
720 | 0 | k += 2; |
721 | 0 | x <<= 2; |
722 | 0 | } |
723 | 0 | if (!(x & 0x80000000)) { |
724 | 0 | k++; |
725 | 0 | if (!(x & 0x40000000)) |
726 | 0 | return 32; |
727 | 0 | } |
728 | 0 | return k; |
729 | 0 | } |
730 | | |
731 | | static int |
732 | | lo0bits |
733 | | #ifdef KR_headers |
734 | | (y) ULong *y; |
735 | | #else |
736 | | (ULong *y) |
737 | | #endif |
738 | 0 | { |
739 | 0 | int k; |
740 | 0 | ULong x = *y; |
741 | |
|
742 | 0 | if (x & 7) { |
743 | 0 | if (x & 1) |
744 | 0 | return 0; |
745 | 0 | if (x & 2) { |
746 | 0 | *y = x >> 1; |
747 | 0 | return 1; |
748 | 0 | } |
749 | 0 | *y = x >> 2; |
750 | 0 | return 2; |
751 | 0 | } |
752 | 0 | k = 0; |
753 | 0 | if (!(x & 0xffff)) { |
754 | 0 | k = 16; |
755 | 0 | x >>= 16; |
756 | 0 | } |
757 | 0 | if (!(x & 0xff)) { |
758 | 0 | k += 8; |
759 | 0 | x >>= 8; |
760 | 0 | } |
761 | 0 | if (!(x & 0xf)) { |
762 | 0 | k += 4; |
763 | 0 | x >>= 4; |
764 | 0 | } |
765 | 0 | if (!(x & 0x3)) { |
766 | 0 | k += 2; |
767 | 0 | x >>= 2; |
768 | 0 | } |
769 | 0 | if (!(x & 1)) { |
770 | 0 | k++; |
771 | 0 | x >>= 1; |
772 | 0 | if (!x) |
773 | 0 | return 32; |
774 | 0 | } |
775 | 0 | *y = x; |
776 | 0 | return k; |
777 | 0 | } |
778 | | |
779 | | static Bigint * |
780 | | i2b |
781 | | #ifdef KR_headers |
782 | | (i) int i; |
783 | | #else |
784 | | (int i) |
785 | | #endif |
786 | 0 | { |
787 | 0 | Bigint *b; |
788 | |
|
789 | 0 | b = Balloc(1); |
790 | 0 | b->x[0] = i; |
791 | 0 | b->wds = 1; |
792 | 0 | return b; |
793 | 0 | } |
794 | | |
795 | | static Bigint * |
796 | | mult |
797 | | #ifdef KR_headers |
798 | | (a, b) Bigint *a, *b; |
799 | | #else |
800 | | (Bigint *a, Bigint *b) |
801 | | #endif |
802 | 0 | { |
803 | 0 | Bigint *c; |
804 | 0 | int k, wa, wb, wc; |
805 | 0 | ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; |
806 | 0 | ULong y; |
807 | 0 | #ifdef ULLong |
808 | 0 | ULLong carry, z; |
809 | | #else |
810 | | ULong carry, z; |
811 | | #ifdef Pack_32 |
812 | | ULong z2; |
813 | | #endif |
814 | | #endif |
815 | |
|
816 | 0 | if (a->wds < b->wds) { |
817 | 0 | c = a; |
818 | 0 | a = b; |
819 | 0 | b = c; |
820 | 0 | } |
821 | 0 | k = a->k; |
822 | 0 | wa = a->wds; |
823 | 0 | wb = b->wds; |
824 | 0 | wc = wa + wb; |
825 | 0 | if (wc > a->maxwds) |
826 | 0 | k++; |
827 | 0 | c = Balloc(k); |
828 | 0 | for(x = c->x, xa = x + wc; x < xa; x++) |
829 | 0 | *x = 0; |
830 | 0 | xa = a->x; |
831 | 0 | xae = xa + wa; |
832 | 0 | xb = b->x; |
833 | 0 | xbe = xb + wb; |
834 | 0 | xc0 = c->x; |
835 | 0 | #ifdef ULLong |
836 | 0 | for(; xb < xbe; xc0++) { |
837 | 0 | if ((y = *xb++)) { |
838 | 0 | x = xa; |
839 | 0 | xc = xc0; |
840 | 0 | carry = 0; |
841 | 0 | do { |
842 | 0 | z = *x++ * (ULLong)y + *xc + carry; |
843 | 0 | carry = z >> 32; |
844 | 0 | *xc++ = z & FFFFFFFF; |
845 | 0 | } |
846 | 0 | while(x < xae); |
847 | 0 | *xc = carry; |
848 | 0 | } |
849 | 0 | } |
850 | | #else |
851 | | #ifdef Pack_32 |
852 | | for(; xb < xbe; xb++, xc0++) { |
853 | | if (y = *xb & 0xffff) { |
854 | | x = xa; |
855 | | xc = xc0; |
856 | | carry = 0; |
857 | | do { |
858 | | z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; |
859 | | carry = z >> 16; |
860 | | z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; |
861 | | carry = z2 >> 16; |
862 | | Storeinc(xc, z2, z); |
863 | | } |
864 | | while(x < xae); |
865 | | *xc = carry; |
866 | | } |
867 | | if (y = *xb >> 16) { |
868 | | x = xa; |
869 | | xc = xc0; |
870 | | carry = 0; |
871 | | z2 = *xc; |
872 | | do { |
873 | | z = (*x & 0xffff) * y + (*xc >> 16) + carry; |
874 | | carry = z >> 16; |
875 | | Storeinc(xc, z, z2); |
876 | | z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; |
877 | | carry = z2 >> 16; |
878 | | } |
879 | | while(x < xae); |
880 | | *xc = z2; |
881 | | } |
882 | | } |
883 | | #else |
884 | | for(; xb < xbe; xc0++) { |
885 | | if (y = *xb++) { |
886 | | x = xa; |
887 | | xc = xc0; |
888 | | carry = 0; |
889 | | do { |
890 | | z = *x++ * y + *xc + carry; |
891 | | carry = z >> 16; |
892 | | *xc++ = z & 0xffff; |
893 | | } |
894 | | while(x < xae); |
895 | | *xc = carry; |
896 | | } |
897 | | } |
898 | | #endif |
899 | | #endif |
900 | 0 | for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; |
901 | 0 | c->wds = wc; |
902 | 0 | return c; |
903 | 0 | } |
904 | | |
905 | | static Bigint *p5s; |
906 | | |
907 | | static Bigint * |
908 | | pow5mult |
909 | | #ifdef KR_headers |
910 | | (b, k) Bigint *b; int k; |
911 | | #else |
912 | | (Bigint *b, int k) |
913 | | #endif |
914 | 0 | { |
915 | 0 | Bigint *b1, *p5, *p51; |
916 | 0 | int i; |
917 | 0 | static int p05[3] = { 5, 25, 125 }; |
918 | |
|
919 | 0 | if ((i = k & 3)) |
920 | 0 | b = multadd(b, p05[i-1], 0); |
921 | |
|
922 | 0 | if (!(k >>= 2)) |
923 | 0 | return b; |
924 | 0 | if (!(p5 = p5s)) { |
925 | | /* first time */ |
926 | 0 | #ifdef MULTIPLE_THREADS |
927 | 0 | ACQUIRE_DTOA_LOCK(1); |
928 | 0 | if (!(p5 = p5s)) { |
929 | 0 | p5 = p5s = i2b(625); |
930 | 0 | p5->next = 0; |
931 | 0 | } |
932 | 0 | FREE_DTOA_LOCK(1); |
933 | | #else |
934 | | p5 = p5s = i2b(625); |
935 | | p5->next = 0; |
936 | | #endif |
937 | 0 | } |
938 | 0 | for(;;) { |
939 | 0 | if (k & 1) { |
940 | 0 | b1 = mult(b, p5); |
941 | 0 | Bfree(b); |
942 | 0 | b = b1; |
943 | 0 | } |
944 | 0 | if (!(k >>= 1)) |
945 | 0 | break; |
946 | 0 | if (!(p51 = p5->next)) { |
947 | 0 | #ifdef MULTIPLE_THREADS |
948 | 0 | ACQUIRE_DTOA_LOCK(1); |
949 | 0 | if (!(p51 = p5->next)) { |
950 | 0 | p51 = p5->next = mult(p5,p5); |
951 | 0 | p51->next = 0; |
952 | 0 | } |
953 | 0 | FREE_DTOA_LOCK(1); |
954 | | #else |
955 | | p51 = p5->next = mult(p5,p5); |
956 | | p51->next = 0; |
957 | | #endif |
958 | 0 | } |
959 | 0 | p5 = p51; |
960 | 0 | } |
961 | 0 | return b; |
962 | 0 | } |
963 | | |
964 | | static Bigint * |
965 | | lshift |
966 | | #ifdef KR_headers |
967 | | (b, k) Bigint *b; int k; |
968 | | #else |
969 | | (Bigint *b, int k) |
970 | | #endif |
971 | 0 | { |
972 | 0 | int i, k1, n, n1; |
973 | 0 | Bigint *b1; |
974 | 0 | ULong *x, *x1, *xe, z; |
975 | |
|
976 | 0 | #ifdef Pack_32 |
977 | 0 | n = k >> 5; |
978 | | #else |
979 | | n = k >> 4; |
980 | | #endif |
981 | 0 | k1 = b->k; |
982 | 0 | n1 = n + b->wds + 1; |
983 | 0 | for(i = b->maxwds; n1 > i; i <<= 1) |
984 | 0 | k1++; |
985 | 0 | b1 = Balloc(k1); |
986 | 0 | x1 = b1->x; |
987 | 0 | for(i = 0; i < n; i++) |
988 | 0 | *x1++ = 0; |
989 | 0 | x = b->x; |
990 | 0 | xe = x + b->wds; |
991 | 0 | #ifdef Pack_32 |
992 | 0 | if (k &= 0x1f) { |
993 | 0 | k1 = 32 - k; |
994 | 0 | z = 0; |
995 | 0 | do { |
996 | 0 | *x1++ = *x << k | z; |
997 | 0 | z = *x++ >> k1; |
998 | 0 | } |
999 | 0 | while(x < xe); |
1000 | 0 | if ((*x1 = z)) |
1001 | 0 | ++n1; |
1002 | 0 | } |
1003 | | #else |
1004 | | if (k &= 0xf) { |
1005 | | k1 = 16 - k; |
1006 | | z = 0; |
1007 | | do { |
1008 | | *x1++ = *x << k & 0xffff | z; |
1009 | | z = *x++ >> k1; |
1010 | | } |
1011 | | while(x < xe); |
1012 | | if (*x1 = z) |
1013 | | ++n1; |
1014 | | } |
1015 | | #endif |
1016 | 0 | else do |
1017 | 0 | *x1++ = *x++; |
1018 | 0 | while(x < xe); |
1019 | 0 | b1->wds = n1 - 1; |
1020 | 0 | Bfree(b); |
1021 | 0 | return b1; |
1022 | 0 | } |
1023 | | |
1024 | | static int |
1025 | | cmp |
1026 | | #ifdef KR_headers |
1027 | | (a, b) Bigint *a, *b; |
1028 | | #else |
1029 | | (Bigint *a, Bigint *b) |
1030 | | #endif |
1031 | 0 | { |
1032 | 0 | ULong *xa, *xa0, *xb, *xb0; |
1033 | 0 | int i, j; |
1034 | |
|
1035 | 0 | i = a->wds; |
1036 | 0 | j = b->wds; |
1037 | | #ifdef DEBUG |
1038 | | if (i > 1 && !a->x[i-1]) |
1039 | | Bug("cmp called with a->x[a->wds-1] == 0"); |
1040 | | if (j > 1 && !b->x[j-1]) |
1041 | | Bug("cmp called with b->x[b->wds-1] == 0"); |
1042 | | #endif |
1043 | 0 | if (i -= j) |
1044 | 0 | return i; |
1045 | 0 | xa0 = a->x; |
1046 | 0 | xa = xa0 + j; |
1047 | 0 | xb0 = b->x; |
1048 | 0 | xb = xb0 + j; |
1049 | 0 | for(;;) { |
1050 | 0 | if (*--xa != *--xb) |
1051 | 0 | return *xa < *xb ? -1 : 1; |
1052 | 0 | if (xa <= xa0) |
1053 | 0 | break; |
1054 | 0 | } |
1055 | 0 | return 0; |
1056 | 0 | } |
1057 | | |
1058 | | static Bigint * |
1059 | | diff |
1060 | | #ifdef KR_headers |
1061 | | (a, b) Bigint *a, *b; |
1062 | | #else |
1063 | | (Bigint *a, Bigint *b) |
1064 | | #endif |
1065 | 0 | { |
1066 | 0 | Bigint *c; |
1067 | 0 | int i, wa, wb; |
1068 | 0 | ULong *xa, *xae, *xb, *xbe, *xc; |
1069 | 0 | #ifdef ULLong |
1070 | 0 | ULLong borrow, y; |
1071 | | #else |
1072 | | ULong borrow, y; |
1073 | | #ifdef Pack_32 |
1074 | | ULong z; |
1075 | | #endif |
1076 | | #endif |
1077 | |
|
1078 | 0 | i = cmp(a,b); |
1079 | 0 | if (!i) { |
1080 | 0 | c = Balloc(0); |
1081 | 0 | c->wds = 1; |
1082 | 0 | c->x[0] = 0; |
1083 | 0 | return c; |
1084 | 0 | } |
1085 | 0 | if (i < 0) { |
1086 | 0 | c = a; |
1087 | 0 | a = b; |
1088 | 0 | b = c; |
1089 | 0 | i = 1; |
1090 | 0 | } |
1091 | 0 | else |
1092 | 0 | i = 0; |
1093 | 0 | c = Balloc(a->k); |
1094 | 0 | c->sign = i; |
1095 | 0 | wa = a->wds; |
1096 | 0 | xa = a->x; |
1097 | 0 | xae = xa + wa; |
1098 | 0 | wb = b->wds; |
1099 | 0 | xb = b->x; |
1100 | 0 | xbe = xb + wb; |
1101 | 0 | xc = c->x; |
1102 | 0 | borrow = 0; |
1103 | 0 | #ifdef ULLong |
1104 | 0 | do { |
1105 | 0 | y = (ULLong)*xa++ - *xb++ - borrow; |
1106 | 0 | borrow = y >> 32 & (ULong)1; |
1107 | 0 | *xc++ = y & FFFFFFFF; |
1108 | 0 | } |
1109 | 0 | while(xb < xbe); |
1110 | 0 | while(xa < xae) { |
1111 | 0 | y = *xa++ - borrow; |
1112 | 0 | borrow = y >> 32 & (ULong)1; |
1113 | 0 | *xc++ = y & FFFFFFFF; |
1114 | 0 | } |
1115 | | #else |
1116 | | #ifdef Pack_32 |
1117 | | do { |
1118 | | y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; |
1119 | | borrow = (y & 0x10000) >> 16; |
1120 | | z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; |
1121 | | borrow = (z & 0x10000) >> 16; |
1122 | | Storeinc(xc, z, y); |
1123 | | } |
1124 | | while(xb < xbe); |
1125 | | while(xa < xae) { |
1126 | | y = (*xa & 0xffff) - borrow; |
1127 | | borrow = (y & 0x10000) >> 16; |
1128 | | z = (*xa++ >> 16) - borrow; |
1129 | | borrow = (z & 0x10000) >> 16; |
1130 | | Storeinc(xc, z, y); |
1131 | | } |
1132 | | #else |
1133 | | do { |
1134 | | y = *xa++ - *xb++ - borrow; |
1135 | | borrow = (y & 0x10000) >> 16; |
1136 | | *xc++ = y & 0xffff; |
1137 | | } |
1138 | | while(xb < xbe); |
1139 | | while(xa < xae) { |
1140 | | y = *xa++ - borrow; |
1141 | | borrow = (y & 0x10000) >> 16; |
1142 | | *xc++ = y & 0xffff; |
1143 | | } |
1144 | | #endif |
1145 | | #endif |
1146 | 0 | while(!*--xc) |
1147 | 0 | wa--; |
1148 | 0 | c->wds = wa; |
1149 | 0 | return c; |
1150 | 0 | } |
1151 | | |
1152 | | static double |
1153 | | ulp |
1154 | | #ifdef KR_headers |
1155 | | (x) U *x; |
1156 | | #else |
1157 | | (U *x) |
1158 | | #endif |
1159 | 0 | { |
1160 | 0 | Long L; |
1161 | 0 | U u; |
1162 | |
|
1163 | 0 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; |
1164 | | #ifndef Avoid_Underflow |
1165 | | #ifndef Sudden_Underflow |
1166 | | if (L > 0) { |
1167 | | #endif |
1168 | | #endif |
1169 | | #ifdef IBM |
1170 | | L |= Exp_msk1 >> 4; |
1171 | | #endif |
1172 | 0 | word0(&u) = L; |
1173 | 0 | word1(&u) = 0; |
1174 | | #ifndef Avoid_Underflow |
1175 | | #ifndef Sudden_Underflow |
1176 | | } |
1177 | | else { |
1178 | | L = -L >> Exp_shift; |
1179 | | if (L < Exp_shift) { |
1180 | | word0(&u) = 0x80000 >> L; |
1181 | | word1(&u) = 0; |
1182 | | } |
1183 | | else { |
1184 | | word0(&u) = 0; |
1185 | | L -= Exp_shift; |
1186 | | word1(&u) = L >= 31 ? 1 : 1 << 31 - L; |
1187 | | } |
1188 | | } |
1189 | | #endif |
1190 | | #endif |
1191 | 0 | return dval(&u); |
1192 | 0 | } |
1193 | | |
1194 | | static double |
1195 | | b2d |
1196 | | #ifdef KR_headers |
1197 | | (a, e) Bigint *a; int *e; |
1198 | | #else |
1199 | | (Bigint *a, int *e) |
1200 | | #endif |
1201 | 0 | { |
1202 | 0 | ULong *xa, *xa0, w, y, z; |
1203 | 0 | int k; |
1204 | 0 | U d; |
1205 | | #ifdef VAX |
1206 | | ULong d0, d1; |
1207 | | #else |
1208 | 0 | #define d0 word0(&d) |
1209 | 0 | #define d1 word1(&d) |
1210 | 0 | #endif |
1211 | |
|
1212 | 0 | xa0 = a->x; |
1213 | 0 | xa = xa0 + a->wds; |
1214 | 0 | y = *--xa; |
1215 | | #ifdef DEBUG |
1216 | | if (!y) Bug("zero y in b2d"); |
1217 | | #endif |
1218 | 0 | k = hi0bits(y); |
1219 | 0 | *e = 32 - k; |
1220 | 0 | #ifdef Pack_32 |
1221 | 0 | if (k < Ebits) { |
1222 | 0 | d0 = Exp_1 | y >> (Ebits - k); |
1223 | 0 | w = xa > xa0 ? *--xa : 0; |
1224 | 0 | d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); |
1225 | 0 | goto ret_d; |
1226 | 0 | } |
1227 | 0 | z = xa > xa0 ? *--xa : 0; |
1228 | 0 | if (k -= Ebits) { |
1229 | 0 | d0 = Exp_1 | y << k | z >> (32 - k); |
1230 | 0 | y = xa > xa0 ? *--xa : 0; |
1231 | 0 | d1 = z << k | y >> (32 - k); |
1232 | 0 | } |
1233 | 0 | else { |
1234 | 0 | d0 = Exp_1 | y; |
1235 | 0 | d1 = z; |
1236 | 0 | } |
1237 | | #else |
1238 | | if (k < Ebits + 16) { |
1239 | | z = xa > xa0 ? *--xa : 0; |
1240 | | d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; |
1241 | | w = xa > xa0 ? *--xa : 0; |
1242 | | y = xa > xa0 ? *--xa : 0; |
1243 | | d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; |
1244 | | goto ret_d; |
1245 | | } |
1246 | | z = xa > xa0 ? *--xa : 0; |
1247 | | w = xa > xa0 ? *--xa : 0; |
1248 | | k -= Ebits + 16; |
1249 | | d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; |
1250 | | y = xa > xa0 ? *--xa : 0; |
1251 | | d1 = w << k + 16 | y << k; |
1252 | | #endif |
1253 | 0 | ret_d: |
1254 | | #ifdef VAX |
1255 | | word0(&d) = d0 >> 16 | d0 << 16; |
1256 | | word1(&d) = d1 >> 16 | d1 << 16; |
1257 | | #else |
1258 | 0 | #undef d0 |
1259 | 0 | #undef d1 |
1260 | 0 | #endif |
1261 | 0 | return dval(&d); |
1262 | 0 | } |
1263 | | |
1264 | | static Bigint * |
1265 | | d2b |
1266 | | #ifdef KR_headers |
1267 | | (d, e, bits) U *d; int *e, *bits; |
1268 | | #else |
1269 | | (U *d, int *e, int *bits) |
1270 | | #endif |
1271 | 0 | { |
1272 | 0 | Bigint *b; |
1273 | 0 | int de, k; |
1274 | 0 | ULong *x, y, z; |
1275 | 0 | #ifndef Sudden_Underflow |
1276 | 0 | int i; |
1277 | 0 | #endif |
1278 | | #ifdef VAX |
1279 | | ULong d0, d1; |
1280 | | d0 = word0(d) >> 16 | word0(d) << 16; |
1281 | | d1 = word1(d) >> 16 | word1(d) << 16; |
1282 | | #else |
1283 | 0 | #define d0 word0(d) |
1284 | 0 | #define d1 word1(d) |
1285 | 0 | #endif |
1286 | |
|
1287 | 0 | #ifdef Pack_32 |
1288 | 0 | b = Balloc(1); |
1289 | | #else |
1290 | | b = Balloc(2); |
1291 | | #endif |
1292 | 0 | x = b->x; |
1293 | |
|
1294 | 0 | z = d0 & Frac_mask; |
1295 | 0 | d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ |
1296 | | #ifdef Sudden_Underflow |
1297 | | de = (int)(d0 >> Exp_shift); |
1298 | | #ifndef IBM |
1299 | | z |= Exp_msk11; |
1300 | | #endif |
1301 | | #else |
1302 | 0 | if ((de = (int)(d0 >> Exp_shift))) |
1303 | 0 | z |= Exp_msk1; |
1304 | 0 | #endif |
1305 | 0 | #ifdef Pack_32 |
1306 | 0 | if ((y = d1)) { |
1307 | 0 | if ((k = lo0bits(&y))) { |
1308 | 0 | x[0] = y | z << (32 - k); |
1309 | 0 | z >>= k; |
1310 | 0 | } |
1311 | 0 | else |
1312 | 0 | x[0] = y; |
1313 | 0 | #ifndef Sudden_Underflow |
1314 | 0 | i = |
1315 | 0 | #endif |
1316 | 0 | b->wds = (x[1] = z) ? 2 : 1; |
1317 | 0 | } |
1318 | 0 | else { |
1319 | 0 | k = lo0bits(&z); |
1320 | 0 | x[0] = z; |
1321 | 0 | #ifndef Sudden_Underflow |
1322 | 0 | i = |
1323 | 0 | #endif |
1324 | 0 | b->wds = 1; |
1325 | 0 | k += 32; |
1326 | 0 | } |
1327 | | #else |
1328 | | if (y = d1) { |
1329 | | if (k = lo0bits(&y)) |
1330 | | if (k >= 16) { |
1331 | | x[0] = y | z << 32 - k & 0xffff; |
1332 | | x[1] = z >> k - 16 & 0xffff; |
1333 | | x[2] = z >> k; |
1334 | | i = 2; |
1335 | | } |
1336 | | else { |
1337 | | x[0] = y & 0xffff; |
1338 | | x[1] = y >> 16 | z << 16 - k & 0xffff; |
1339 | | x[2] = z >> k & 0xffff; |
1340 | | x[3] = z >> k+16; |
1341 | | i = 3; |
1342 | | } |
1343 | | else { |
1344 | | x[0] = y & 0xffff; |
1345 | | x[1] = y >> 16; |
1346 | | x[2] = z & 0xffff; |
1347 | | x[3] = z >> 16; |
1348 | | i = 3; |
1349 | | } |
1350 | | } |
1351 | | else { |
1352 | | #ifdef DEBUG |
1353 | | if (!z) |
1354 | | Bug("Zero passed to d2b"); |
1355 | | #endif |
1356 | | k = lo0bits(&z); |
1357 | | if (k >= 16) { |
1358 | | x[0] = z; |
1359 | | i = 0; |
1360 | | } |
1361 | | else { |
1362 | | x[0] = z & 0xffff; |
1363 | | x[1] = z >> 16; |
1364 | | i = 1; |
1365 | | } |
1366 | | k += 32; |
1367 | | } |
1368 | | while(!x[i]) |
1369 | | --i; |
1370 | | b->wds = i + 1; |
1371 | | #endif |
1372 | 0 | #ifndef Sudden_Underflow |
1373 | 0 | if (de) { |
1374 | 0 | #endif |
1375 | | #ifdef IBM |
1376 | | *e = (de - Bias - (P-1) << 2) + k; |
1377 | | *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); |
1378 | | #else |
1379 | 0 | *e = de - Bias - (P-1) + k; |
1380 | 0 | *bits = P - k; |
1381 | 0 | #endif |
1382 | 0 | #ifndef Sudden_Underflow |
1383 | 0 | } |
1384 | 0 | else { |
1385 | 0 | *e = de - Bias - (P-1) + 1 + k; |
1386 | 0 | #ifdef Pack_32 |
1387 | 0 | *bits = 32*i - hi0bits(x[i-1]); |
1388 | | #else |
1389 | | *bits = (i+2)*16 - hi0bits(x[i]); |
1390 | | #endif |
1391 | 0 | } |
1392 | 0 | #endif |
1393 | 0 | return b; |
1394 | 0 | } |
1395 | | #undef d0 |
1396 | | #undef d1 |
1397 | | |
1398 | | static double |
1399 | | ratio |
1400 | | #ifdef KR_headers |
1401 | | (a, b) Bigint *a, *b; |
1402 | | #else |
1403 | | (Bigint *a, Bigint *b) |
1404 | | #endif |
1405 | 0 | { |
1406 | 0 | U da, db; |
1407 | 0 | int k, ka, kb; |
1408 | |
|
1409 | 0 | dval(&da) = b2d(a, &ka); |
1410 | 0 | dval(&db) = b2d(b, &kb); |
1411 | 0 | #ifdef Pack_32 |
1412 | 0 | k = ka - kb + 32*(a->wds - b->wds); |
1413 | | #else |
1414 | | k = ka - kb + 16*(a->wds - b->wds); |
1415 | | #endif |
1416 | | #ifdef IBM |
1417 | | if (k > 0) { |
1418 | | word0(&da) += (k >> 2)*Exp_msk1; |
1419 | | if (k &= 3) |
1420 | | dval(&da) *= 1 << k; |
1421 | | } |
1422 | | else { |
1423 | | k = -k; |
1424 | | word0(&db) += (k >> 2)*Exp_msk1; |
1425 | | if (k &= 3) |
1426 | | dval(&db) *= 1 << k; |
1427 | | } |
1428 | | #else |
1429 | 0 | if (k > 0) |
1430 | 0 | word0(&da) += k*Exp_msk1; |
1431 | 0 | else { |
1432 | 0 | k = -k; |
1433 | 0 | word0(&db) += k*Exp_msk1; |
1434 | 0 | } |
1435 | 0 | #endif |
1436 | 0 | return dval(&da) / dval(&db); |
1437 | 0 | } |
1438 | | |
1439 | | static CONST double |
1440 | | tens[] = { |
1441 | | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
1442 | | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
1443 | | 1e20, 1e21, 1e22 |
1444 | | #ifdef VAX |
1445 | | , 1e23, 1e24 |
1446 | | #endif |
1447 | | }; |
1448 | | |
1449 | | static CONST double |
1450 | | #ifdef IEEE_Arith |
1451 | | bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; |
1452 | | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, |
1453 | | #ifdef Avoid_Underflow |
1454 | | 9007199254740992.*9007199254740992.e-256 |
1455 | | /* = 2^106 * 1e-256 */ |
1456 | | #else |
1457 | | 1e-256 |
1458 | | #endif |
1459 | | }; |
1460 | | /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ |
1461 | | /* flag unnecessarily. It leads to a song and dance at the end of strtod. */ |
1462 | 0 | #define Scale_Bit 0x10 |
1463 | 0 | #define n_bigtens 5 |
1464 | | #else |
1465 | | #ifdef IBM |
1466 | | bigtens[] = { 1e16, 1e32, 1e64 }; |
1467 | | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; |
1468 | | #define n_bigtens 3 |
1469 | | #else |
1470 | | bigtens[] = { 1e16, 1e32 }; |
1471 | | static CONST double tinytens[] = { 1e-16, 1e-32 }; |
1472 | | #define n_bigtens 2 |
1473 | | #endif |
1474 | | #endif |
1475 | | |
1476 | | #undef Need_Hexdig |
1477 | | #ifdef INFNAN_CHECK |
1478 | | #ifndef No_Hex_NaN |
1479 | | #define Need_Hexdig |
1480 | | #endif |
1481 | | #endif |
1482 | | |
1483 | | #ifndef Need_Hexdig |
1484 | | #ifndef NO_HEX_FP |
1485 | | #define Need_Hexdig |
1486 | | #endif |
1487 | | #endif |
1488 | | |
1489 | | #ifdef Need_Hexdig /*{*/ |
1490 | | static unsigned char hexdig[256]; |
1491 | | |
1492 | | static void |
1493 | | #ifdef KR_headers |
1494 | | htinit(h, s, inc) unsigned char *h; unsigned char *s; int inc; |
1495 | | #else |
1496 | | htinit(unsigned char *h, unsigned char *s, int inc) |
1497 | | #endif |
1498 | 0 | { |
1499 | 0 | int i, j; |
1500 | 0 | for(i = 0; (j = s[i]) !=0; i++) |
1501 | 0 | h[j] = i + inc; |
1502 | 0 | } |
1503 | | |
1504 | | static void |
1505 | | #ifdef KR_headers |
1506 | | hexdig_init() |
1507 | | #else |
1508 | | hexdig_init(void) |
1509 | | #endif |
1510 | 0 | { |
1511 | 0 | #define USC (unsigned char *) |
1512 | 0 | htinit(hexdig, USC "0123456789", 0x10); |
1513 | 0 | htinit(hexdig, USC "abcdef", 0x10 + 10); |
1514 | 0 | htinit(hexdig, USC "ABCDEF", 0x10 + 10); |
1515 | 0 | } |
1516 | | #endif /* } Need_Hexdig */ |
1517 | | |
1518 | | #ifdef INFNAN_CHECK |
1519 | | |
1520 | | #ifndef NAN_WORD0 |
1521 | 0 | #define NAN_WORD0 0x7ff80000 |
1522 | | #endif |
1523 | | |
1524 | | #ifndef NAN_WORD1 |
1525 | 0 | #define NAN_WORD1 0 |
1526 | | #endif |
1527 | | |
1528 | | static int |
1529 | | match |
1530 | | #ifdef KR_headers |
1531 | | (sp, t) char **sp, *t; |
1532 | | #else |
1533 | | (CONST char **sp, CONST char *t) |
1534 | | #endif |
1535 | 0 | { |
1536 | 0 | int c, d; |
1537 | 0 | CONST char *s = *sp; |
1538 | |
|
1539 | 0 | while((d = *t++)) { |
1540 | 0 | if ((c = *++s) >= 'A' && c <= 'Z') |
1541 | 0 | c += 'a' - 'A'; |
1542 | 0 | if (c != d) |
1543 | 0 | return 0; |
1544 | 0 | } |
1545 | 0 | *sp = s + 1; |
1546 | 0 | return 1; |
1547 | 0 | } |
1548 | | |
1549 | | #ifndef No_Hex_NaN |
1550 | | static void |
1551 | | hexnan |
1552 | | #ifdef KR_headers |
1553 | | (rvp, sp) U *rvp; CONST char **sp; |
1554 | | #else |
1555 | | (U *rvp, CONST char **sp) |
1556 | | #endif |
1557 | 0 | { |
1558 | 0 | ULong c, x[2]; |
1559 | 0 | CONST char *s; |
1560 | 0 | int c1, havedig, udx0, xshift; |
1561 | |
|
1562 | 0 | if (!hexdig[(int)'0']) |
1563 | 0 | hexdig_init(); |
1564 | 0 | x[0] = x[1] = 0; |
1565 | 0 | havedig = xshift = 0; |
1566 | 0 | udx0 = 1; |
1567 | 0 | s = *sp; |
1568 | | /* allow optional initial 0x or 0X */ |
1569 | 0 | while((c = *(CONST unsigned char*)(s+1)) && c <= ' ') |
1570 | 0 | ++s; |
1571 | 0 | if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')) |
1572 | 0 | s += 2; |
1573 | 0 | while((c = *(CONST unsigned char*)++s)) { |
1574 | 0 | if ((c1 = hexdig[c])) |
1575 | 0 | c = c1 & 0xf; |
1576 | 0 | else if (c <= ' ') { |
1577 | 0 | if (udx0 && havedig) { |
1578 | 0 | udx0 = 0; |
1579 | 0 | xshift = 1; |
1580 | 0 | } |
1581 | 0 | continue; |
1582 | 0 | } |
1583 | | #ifdef GDTOA_NON_PEDANTIC_NANCHECK |
1584 | | else if (/*(*/ c == ')' && havedig) { |
1585 | | *sp = s + 1; |
1586 | | break; |
1587 | | } |
1588 | | else |
1589 | | return; /* invalid form: don't change *sp */ |
1590 | | #else |
1591 | 0 | else { |
1592 | 0 | do { |
1593 | 0 | if (/*(*/ c == ')') { |
1594 | 0 | *sp = s + 1; |
1595 | 0 | break; |
1596 | 0 | } |
1597 | 0 | } while((c = *++s)); |
1598 | 0 | break; |
1599 | 0 | } |
1600 | 0 | #endif |
1601 | 0 | havedig = 1; |
1602 | 0 | if (xshift) { |
1603 | 0 | xshift = 0; |
1604 | 0 | x[0] = x[1]; |
1605 | 0 | x[1] = 0; |
1606 | 0 | } |
1607 | 0 | if (udx0) |
1608 | 0 | x[0] = (x[0] << 4) | (x[1] >> 28); |
1609 | 0 | x[1] = (x[1] << 4) | c; |
1610 | 0 | } |
1611 | 0 | if ((x[0] &= 0xfffff) || x[1]) { |
1612 | 0 | word0(rvp) = Exp_mask | x[0]; |
1613 | 0 | word1(rvp) = x[1]; |
1614 | 0 | } |
1615 | 0 | } |
1616 | | #endif /*No_Hex_NaN*/ |
1617 | | #endif /* INFNAN_CHECK */ |
1618 | | |
1619 | | #ifdef Pack_32 |
1620 | | #define ULbits 32 |
1621 | | #define kshift 5 |
1622 | 0 | #define kmask 31 |
1623 | | #else |
1624 | | #define ULbits 16 |
1625 | | #define kshift 4 |
1626 | | #define kmask 15 |
1627 | | #endif |
1628 | | #ifndef NO_HEX_FP /*{*/ |
1629 | | |
1630 | | static void |
1631 | | #ifdef KR_headers |
1632 | | rshift(b, k) Bigint *b; int k; |
1633 | | #else |
1634 | | rshift(Bigint *b, int k) |
1635 | | #endif |
1636 | | { |
1637 | | ULong *x, *x1, *xe, y; |
1638 | | int n; |
1639 | | |
1640 | | x = x1 = b->x; |
1641 | | n = k >> kshift; |
1642 | | if (n < b->wds) { |
1643 | | xe = x + b->wds; |
1644 | | x += n; |
1645 | | if (k &= kmask) { |
1646 | | n = 32 - k; |
1647 | | y = *x++ >> k; |
1648 | | while(x < xe) { |
1649 | | *x1++ = (y | (*x << n)) & 0xffffffff; |
1650 | | y = *x++ >> k; |
1651 | | } |
1652 | | if ((*x1 = y) !=0) |
1653 | | x1++; |
1654 | | } |
1655 | | else |
1656 | | while(x < xe) |
1657 | | *x1++ = *x++; |
1658 | | } |
1659 | | if ((b->wds = x1 - b->x) == 0) |
1660 | | b->x[0] = 0; |
1661 | | } |
1662 | | |
1663 | | static ULong |
1664 | | #ifdef KR_headers |
1665 | | any_on(b, k) Bigint *b; int k; |
1666 | | #else |
1667 | | any_on(Bigint *b, int k) |
1668 | | #endif |
1669 | | { |
1670 | | int n, nwds; |
1671 | | ULong *x, *x0, x1, x2; |
1672 | | |
1673 | | x = b->x; |
1674 | | nwds = b->wds; |
1675 | | n = k >> kshift; |
1676 | | if (n > nwds) |
1677 | | n = nwds; |
1678 | | else if (n < nwds && (k &= kmask)) { |
1679 | | x1 = x2 = x[n]; |
1680 | | x1 >>= k; |
1681 | | x1 <<= k; |
1682 | | if (x1 != x2) |
1683 | | return 1; |
1684 | | } |
1685 | | x0 = x; |
1686 | | x += n; |
1687 | | while(x > x0) |
1688 | | if (*--x) |
1689 | | return 1; |
1690 | | return 0; |
1691 | | } |
1692 | | |
1693 | | enum { /* rounding values: same as FLT_ROUNDS */ |
1694 | | Round_zero = 0, |
1695 | | Round_near = 1, |
1696 | | Round_up = 2, |
1697 | | Round_down = 3 |
1698 | | }; |
1699 | | |
1700 | | static Bigint * |
1701 | | #ifdef KR_headers |
1702 | | increment(b) Bigint *b; |
1703 | | #else |
1704 | | increment(Bigint *b) |
1705 | | #endif |
1706 | | { |
1707 | | ULong *x, *xe; |
1708 | | Bigint *b1; |
1709 | | |
1710 | | x = b->x; |
1711 | | xe = x + b->wds; |
1712 | | do { |
1713 | | if (*x < (ULong)0xffffffffL) { |
1714 | | ++*x; |
1715 | | return b; |
1716 | | } |
1717 | | *x++ = 0; |
1718 | | } while(x < xe); |
1719 | | { |
1720 | | if (b->wds >= b->maxwds) { |
1721 | | b1 = Balloc(b->k+1); |
1722 | | Bcopy(b1,b); |
1723 | | Bfree(b); |
1724 | | b = b1; |
1725 | | } |
1726 | | b->x[b->wds++] = 1; |
1727 | | } |
1728 | | return b; |
1729 | | } |
1730 | | |
1731 | | void |
1732 | | #ifdef KR_headers |
1733 | | gethex(sp, rvp, rounding, sign) |
1734 | | CONST char **sp; U *rvp; int rounding, sign; |
1735 | | #else |
1736 | | gethex( CONST char **sp, U *rvp, int rounding, int sign) |
1737 | | #endif |
1738 | | { |
1739 | | Bigint *b; |
1740 | | CONST unsigned char *decpt, *s0, *s, *s1; |
1741 | | Long e, e1; |
1742 | | ULong L, lostbits, *x; |
1743 | | int big, denorm, esign, havedig, k, n, nbits, up, zret; |
1744 | | #ifdef IBM |
1745 | | int j; |
1746 | | #endif |
1747 | | enum { |
1748 | | #ifdef IEEE_Arith /*{{*/ |
1749 | | emax = 0x7fe - Bias - P + 1, |
1750 | | emin = Emin - P + 1 |
1751 | | #else /*}{*/ |
1752 | | emin = Emin - P, |
1753 | | #ifdef VAX |
1754 | | emax = 0x7ff - Bias - P + 1 |
1755 | | #endif |
1756 | | #ifdef IBM |
1757 | | emax = 0x7f - Bias - P |
1758 | | #endif |
1759 | | #endif /*}}*/ |
1760 | | }; |
1761 | | #ifdef USE_LOCALE |
1762 | | int i; |
1763 | | #ifdef NO_LOCALE_CACHE |
1764 | | const unsigned char *decimalpoint = (unsigned char*) |
1765 | | localeconv()->decimal_point; |
1766 | | #else |
1767 | | const unsigned char *decimalpoint; |
1768 | | static unsigned char *decimalpoint_cache; |
1769 | | if (!(s0 = decimalpoint_cache)) { |
1770 | | s0 = (unsigned char*)localeconv()->decimal_point; |
1771 | | if ((decimalpoint_cache = (unsigned char*) |
1772 | | MALLOC(strlen((CONST char*)s0) + 1))) { |
1773 | | strcpy((char*)decimalpoint_cache, (CONST char*)s0); |
1774 | | s0 = decimalpoint_cache; |
1775 | | } |
1776 | | } |
1777 | | decimalpoint = s0; |
1778 | | #endif |
1779 | | #endif |
1780 | | |
1781 | | if (!hexdig['0']) |
1782 | | hexdig_init(); |
1783 | | havedig = 0; |
1784 | | s0 = *(CONST unsigned char **)sp + 2; |
1785 | | while(s0[havedig] == '0') |
1786 | | havedig++; |
1787 | | s0 += havedig; |
1788 | | s = s0; |
1789 | | decpt = 0; |
1790 | | zret = 0; |
1791 | | e = 0; |
1792 | | if (hexdig[*s]) |
1793 | | havedig++; |
1794 | | else { |
1795 | | zret = 1; |
1796 | | #ifdef USE_LOCALE |
1797 | | for(i = 0; decimalpoint[i]; ++i) { |
1798 | | if (s[i] != decimalpoint[i]) |
1799 | | goto pcheck; |
1800 | | } |
1801 | | decpt = s += i; |
1802 | | #else |
1803 | | if (*s != '.') |
1804 | | goto pcheck; |
1805 | | decpt = ++s; |
1806 | | #endif |
1807 | | if (!hexdig[*s]) |
1808 | | goto pcheck; |
1809 | | while(*s == '0') |
1810 | | s++; |
1811 | | if (hexdig[*s]) |
1812 | | zret = 0; |
1813 | | havedig = 1; |
1814 | | s0 = s; |
1815 | | } |
1816 | | while(hexdig[*s]) |
1817 | | s++; |
1818 | | #ifdef USE_LOCALE |
1819 | | if (*s == *decimalpoint && !decpt) { |
1820 | | for(i = 1; decimalpoint[i]; ++i) { |
1821 | | if (s[i] != decimalpoint[i]) |
1822 | | goto pcheck; |
1823 | | } |
1824 | | decpt = s += i; |
1825 | | #else |
1826 | | if (*s == '.' && !decpt) { |
1827 | | decpt = ++s; |
1828 | | #endif |
1829 | | while(hexdig[*s]) |
1830 | | s++; |
1831 | | }/*}*/ |
1832 | | if (decpt) |
1833 | | e = -(((Long)(s-decpt)) << 2); |
1834 | | pcheck: |
1835 | | s1 = s; |
1836 | | big = esign = 0; |
1837 | | switch(*s) { |
1838 | | case 'p': |
1839 | | case 'P': |
1840 | | switch(*++s) { |
1841 | | case '-': |
1842 | | esign = 1; |
1843 | | // fall through |
1844 | | case '+': |
1845 | | s++; |
1846 | | } |
1847 | | if ((n = hexdig[*s]) == 0 || n > 0x19) { |
1848 | | s = s1; |
1849 | | break; |
1850 | | } |
1851 | | e1 = n - 0x10; |
1852 | | while((n = hexdig[*++s]) !=0 && n <= 0x19) { |
1853 | | if (e1 & 0xf8000000) |
1854 | | big = 1; |
1855 | | e1 = 10*e1 + n - 0x10; |
1856 | | } |
1857 | | if (esign) |
1858 | | e1 = -e1; |
1859 | | e += e1; |
1860 | | } |
1861 | | *sp = (char*)s; |
1862 | | if (!havedig) |
1863 | | *sp = (char*)s0 - 1; |
1864 | | if (zret) |
1865 | | goto retz1; |
1866 | | if (big) { |
1867 | | if (esign) { |
1868 | | #ifdef IEEE_Arith |
1869 | | switch(rounding) { |
1870 | | case Round_up: |
1871 | | if (sign) |
1872 | | break; |
1873 | | goto ret_tiny; |
1874 | | case Round_down: |
1875 | | if (!sign) |
1876 | | break; |
1877 | | goto ret_tiny; |
1878 | | } |
1879 | | #endif |
1880 | | goto retz; |
1881 | | #ifdef IEEE_Arith |
1882 | | ret_tiny: |
1883 | | #ifndef NO_ERRNO |
1884 | | errno = ERANGE; |
1885 | | #endif |
1886 | | word0(rvp) = 0; |
1887 | | word1(rvp) = 1; |
1888 | | return; |
1889 | | #endif /* IEEE_Arith */ |
1890 | | } |
1891 | | switch(rounding) { |
1892 | | case Round_near: |
1893 | | goto ovfl1; |
1894 | | case Round_up: |
1895 | | if (!sign) |
1896 | | goto ovfl1; |
1897 | | goto ret_big; |
1898 | | case Round_down: |
1899 | | if (sign) |
1900 | | goto ovfl1; |
1901 | | goto ret_big; |
1902 | | } |
1903 | | ret_big: |
1904 | | word0(rvp) = Big0; |
1905 | | word1(rvp) = Big1; |
1906 | | return; |
1907 | | } |
1908 | | n = s1 - s0 - 1; |
1909 | | for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1) |
1910 | | k++; |
1911 | | b = Balloc(k); |
1912 | | x = b->x; |
1913 | | n = 0; |
1914 | | L = 0; |
1915 | | #ifdef USE_LOCALE |
1916 | | for(i = 0; decimalpoint[i+1]; ++i); |
1917 | | #endif |
1918 | | while(s1 > s0) { |
1919 | | #ifdef USE_LOCALE |
1920 | | if (*--s1 == decimalpoint[i]) { |
1921 | | s1 -= i; |
1922 | | continue; |
1923 | | } |
1924 | | #else |
1925 | | if (*--s1 == '.') |
1926 | | continue; |
1927 | | #endif |
1928 | | if (n == ULbits) { |
1929 | | *x++ = L; |
1930 | | L = 0; |
1931 | | n = 0; |
1932 | | } |
1933 | | L |= (hexdig[*s1] & 0x0f) << n; |
1934 | | n += 4; |
1935 | | } |
1936 | | *x++ = L; |
1937 | | b->wds = n = x - b->x; |
1938 | | n = ULbits*n - hi0bits(L); |
1939 | | nbits = Nbits; |
1940 | | lostbits = 0; |
1941 | | x = b->x; |
1942 | | if (n > nbits) { |
1943 | | n -= nbits; |
1944 | | if (any_on(b,n)) { |
1945 | | lostbits = 1; |
1946 | | k = n - 1; |
1947 | | if (x[k>>kshift] & 1 << (k & kmask)) { |
1948 | | lostbits = 2; |
1949 | | if (k > 0 && any_on(b,k)) |
1950 | | lostbits = 3; |
1951 | | } |
1952 | | } |
1953 | | rshift(b, n); |
1954 | | e += n; |
1955 | | } |
1956 | | else if (n < nbits) { |
1957 | | n = nbits - n; |
1958 | | b = lshift(b, n); |
1959 | | e -= n; |
1960 | | x = b->x; |
1961 | | } |
1962 | | if (e > Emax) { |
1963 | | ovfl: |
1964 | | Bfree(b); |
1965 | | ovfl1: |
1966 | | #ifndef NO_ERRNO |
1967 | | errno = ERANGE; |
1968 | | #endif |
1969 | | word0(rvp) = Exp_mask; |
1970 | | word1(rvp) = 0; |
1971 | | return; |
1972 | | } |
1973 | | denorm = 0; |
1974 | | if (e < emin) { |
1975 | | denorm = 1; |
1976 | | n = emin - e; |
1977 | | if (n >= nbits) { |
1978 | | #ifdef IEEE_Arith /*{*/ |
1979 | | switch (rounding) { |
1980 | | case Round_near: |
1981 | | if (n == nbits && (n < 2 || any_on(b,n-1))) |
1982 | | goto ret_tiny; |
1983 | | break; |
1984 | | case Round_up: |
1985 | | if (!sign) |
1986 | | goto ret_tiny; |
1987 | | break; |
1988 | | case Round_down: |
1989 | | if (sign) |
1990 | | goto ret_tiny; |
1991 | | } |
1992 | | #endif /* } IEEE_Arith */ |
1993 | | Bfree(b); |
1994 | | retz: |
1995 | | #ifndef NO_ERRNO |
1996 | | errno = ERANGE; |
1997 | | #endif |
1998 | | retz1: |
1999 | | rvp->d = 0.; |
2000 | | return; |
2001 | | } |
2002 | | k = n - 1; |
2003 | | if (lostbits) |
2004 | | lostbits = 1; |
2005 | | else if (k > 0) |
2006 | | lostbits = any_on(b,k); |
2007 | | if (x[k>>kshift] & 1 << (k & kmask)) |
2008 | | lostbits |= 2; |
2009 | | nbits -= n; |
2010 | | rshift(b,n); |
2011 | | e = emin; |
2012 | | } |
2013 | | if (lostbits) { |
2014 | | up = 0; |
2015 | | switch(rounding) { |
2016 | | case Round_zero: |
2017 | | break; |
2018 | | case Round_near: |
2019 | | if (lostbits & 2 |
2020 | | && (lostbits & 1) | (x[0] & 1)) |
2021 | | up = 1; |
2022 | | break; |
2023 | | case Round_up: |
2024 | | up = 1 - sign; |
2025 | | break; |
2026 | | case Round_down: |
2027 | | up = sign; |
2028 | | } |
2029 | | if (up) { |
2030 | | k = b->wds; |
2031 | | b = increment(b); |
2032 | | x = b->x; |
2033 | | if (denorm) { |
2034 | | #if 0 |
2035 | | if (nbits == Nbits - 1 |
2036 | | && x[nbits >> kshift] & 1 << (nbits & kmask)) |
2037 | | denorm = 0; /* not currently used */ |
2038 | | #endif |
2039 | | } |
2040 | | else if (b->wds > k |
2041 | | || ((n = nbits & kmask) !=0 |
2042 | | && hi0bits(x[k-1]) < 32-n)) { |
2043 | | rshift(b,1); |
2044 | | if (++e > Emax) |
2045 | | goto ovfl; |
2046 | | } |
2047 | | } |
2048 | | } |
2049 | | #ifdef IEEE_Arith |
2050 | | if (denorm) |
2051 | | word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0; |
2052 | | else |
2053 | | word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20); |
2054 | | word1(rvp) = b->x[0]; |
2055 | | #endif |
2056 | | #ifdef IBM |
2057 | | if ((j = e & 3)) { |
2058 | | k = b->x[0] & ((1 << j) - 1); |
2059 | | rshift(b,j); |
2060 | | if (k) { |
2061 | | switch(rounding) { |
2062 | | case Round_up: |
2063 | | if (!sign) |
2064 | | increment(b); |
2065 | | break; |
2066 | | case Round_down: |
2067 | | if (sign) |
2068 | | increment(b); |
2069 | | break; |
2070 | | case Round_near: |
2071 | | j = 1 << (j-1); |
2072 | | if (k & j && ((k & (j-1)) | lostbits)) |
2073 | | increment(b); |
2074 | | } |
2075 | | } |
2076 | | } |
2077 | | e >>= 2; |
2078 | | word0(rvp) = b->x[1] | ((e + 65 + 13) << 24); |
2079 | | word1(rvp) = b->x[0]; |
2080 | | #endif |
2081 | | #ifdef VAX |
2082 | | /* The next two lines ignore swap of low- and high-order 2 bytes. */ |
2083 | | /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */ |
2084 | | /* word1(rvp) = b->x[0]; */ |
2085 | | word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16); |
2086 | | word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16); |
2087 | | #endif |
2088 | | Bfree(b); |
2089 | | } |
2090 | | #endif /*}!NO_HEX_FP*/ |
2091 | | |
2092 | | static int |
2093 | | #ifdef KR_headers |
2094 | | dshift(b, p2) Bigint *b; int p2; |
2095 | | #else |
2096 | | dshift(Bigint *b, int p2) |
2097 | | #endif |
2098 | 0 | { |
2099 | 0 | int rv = hi0bits(b->x[b->wds-1]) - 4; |
2100 | 0 | if (p2 > 0) |
2101 | 0 | rv -= p2; |
2102 | 0 | return rv & kmask; |
2103 | 0 | } |
2104 | | |
2105 | | static int |
2106 | | quorem |
2107 | | #ifdef KR_headers |
2108 | | (b, S) Bigint *b, *S; |
2109 | | #else |
2110 | | (Bigint *b, Bigint *S) |
2111 | | #endif |
2112 | 0 | { |
2113 | 0 | int n; |
2114 | 0 | ULong *bx, *bxe, q, *sx, *sxe; |
2115 | 0 | #ifdef ULLong |
2116 | 0 | ULLong borrow, carry, y, ys; |
2117 | | #else |
2118 | | ULong borrow, carry, y, ys; |
2119 | | #ifdef Pack_32 |
2120 | | ULong si, z, zs; |
2121 | | #endif |
2122 | | #endif |
2123 | |
|
2124 | 0 | n = S->wds; |
2125 | | #ifdef DEBUG |
2126 | | /*debug*/ if (b->wds > n) |
2127 | | /*debug*/ Bug("oversize b in quorem"); |
2128 | | #endif |
2129 | 0 | if (b->wds < n) |
2130 | 0 | return 0; |
2131 | 0 | sx = S->x; |
2132 | 0 | sxe = sx + --n; |
2133 | 0 | bx = b->x; |
2134 | 0 | bxe = bx + n; |
2135 | 0 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ |
2136 | | #ifdef DEBUG |
2137 | | /*debug*/ if (q > 9) |
2138 | | /*debug*/ Bug("oversized quotient in quorem"); |
2139 | | #endif |
2140 | 0 | if (q) { |
2141 | 0 | borrow = 0; |
2142 | 0 | carry = 0; |
2143 | 0 | do { |
2144 | 0 | #ifdef ULLong |
2145 | 0 | ys = *sx++ * (ULLong)q + carry; |
2146 | 0 | carry = ys >> 32; |
2147 | 0 | y = *bx - (ys & FFFFFFFF) - borrow; |
2148 | 0 | borrow = y >> 32 & (ULong)1; |
2149 | 0 | *bx++ = y & FFFFFFFF; |
2150 | | #else |
2151 | | #ifdef Pack_32 |
2152 | | si = *sx++; |
2153 | | ys = (si & 0xffff) * q + carry; |
2154 | | zs = (si >> 16) * q + (ys >> 16); |
2155 | | carry = zs >> 16; |
2156 | | y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
2157 | | borrow = (y & 0x10000) >> 16; |
2158 | | z = (*bx >> 16) - (zs & 0xffff) - borrow; |
2159 | | borrow = (z & 0x10000) >> 16; |
2160 | | Storeinc(bx, z, y); |
2161 | | #else |
2162 | | ys = *sx++ * q + carry; |
2163 | | carry = ys >> 16; |
2164 | | y = *bx - (ys & 0xffff) - borrow; |
2165 | | borrow = (y & 0x10000) >> 16; |
2166 | | *bx++ = y & 0xffff; |
2167 | | #endif |
2168 | | #endif |
2169 | 0 | } |
2170 | 0 | while(sx <= sxe); |
2171 | 0 | if (!*bxe) { |
2172 | 0 | bx = b->x; |
2173 | 0 | while(--bxe > bx && !*bxe) |
2174 | 0 | --n; |
2175 | 0 | b->wds = n; |
2176 | 0 | } |
2177 | 0 | } |
2178 | 0 | if (cmp(b, S) >= 0) { |
2179 | 0 | q++; |
2180 | 0 | borrow = 0; |
2181 | 0 | carry = 0; |
2182 | 0 | bx = b->x; |
2183 | 0 | sx = S->x; |
2184 | 0 | do { |
2185 | 0 | #ifdef ULLong |
2186 | 0 | ys = *sx++ + carry; |
2187 | 0 | carry = ys >> 32; |
2188 | 0 | y = *bx - (ys & FFFFFFFF) - borrow; |
2189 | 0 | borrow = y >> 32 & (ULong)1; |
2190 | 0 | *bx++ = y & FFFFFFFF; |
2191 | | #else |
2192 | | #ifdef Pack_32 |
2193 | | si = *sx++; |
2194 | | ys = (si & 0xffff) + carry; |
2195 | | zs = (si >> 16) + (ys >> 16); |
2196 | | carry = zs >> 16; |
2197 | | y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
2198 | | borrow = (y & 0x10000) >> 16; |
2199 | | z = (*bx >> 16) - (zs & 0xffff) - borrow; |
2200 | | borrow = (z & 0x10000) >> 16; |
2201 | | Storeinc(bx, z, y); |
2202 | | #else |
2203 | | ys = *sx++ + carry; |
2204 | | carry = ys >> 16; |
2205 | | y = *bx - (ys & 0xffff) - borrow; |
2206 | | borrow = (y & 0x10000) >> 16; |
2207 | | *bx++ = y & 0xffff; |
2208 | | #endif |
2209 | | #endif |
2210 | 0 | } |
2211 | 0 | while(sx <= sxe); |
2212 | 0 | bx = b->x; |
2213 | 0 | bxe = bx + n; |
2214 | 0 | if (!*bxe) { |
2215 | 0 | while(--bxe > bx && !*bxe) |
2216 | 0 | --n; |
2217 | 0 | b->wds = n; |
2218 | 0 | } |
2219 | 0 | } |
2220 | 0 | return q; |
2221 | 0 | } |
2222 | | |
2223 | | #ifndef NO_STRTOD_BIGCOMP |
2224 | | |
2225 | | static void |
2226 | | bigcomp |
2227 | | #ifdef KR_headers |
2228 | | (rv, s0, bc) |
2229 | | U *rv; CONST char *s0; BCinfo *bc; |
2230 | | #else |
2231 | | (U *rv, CONST char *s0, BCinfo *bc) |
2232 | | #endif |
2233 | 0 | { |
2234 | 0 | Bigint *b, *d; |
2235 | 0 | int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase; |
2236 | |
|
2237 | 0 | dsign = bc->dsign; |
2238 | 0 | nd = bc->nd; |
2239 | 0 | nd0 = bc->nd0; |
2240 | 0 | p5 = nd + bc->e0 - 1; |
2241 | 0 | dd = speccase = 0; |
2242 | 0 | #ifndef Sudden_Underflow |
2243 | 0 | if (rv->d == 0.) { /* special case: value near underflow-to-zero */ |
2244 | | /* threshold was rounded to zero */ |
2245 | 0 | b = i2b(1); |
2246 | 0 | p2 = Emin - P + 1; |
2247 | 0 | bbits = 1; |
2248 | 0 | #ifdef Avoid_Underflow |
2249 | 0 | word0(rv) = (P+2) << Exp_shift; |
2250 | | #else |
2251 | | word1(rv) = 1; |
2252 | | #endif |
2253 | 0 | i = 0; |
2254 | | #ifdef Honor_FLT_ROUNDS |
2255 | | if (bc->rounding == 1) |
2256 | | #endif |
2257 | 0 | { |
2258 | 0 | speccase = 1; |
2259 | 0 | --p2; |
2260 | 0 | dsign = 0; |
2261 | 0 | goto have_i; |
2262 | 0 | } |
2263 | 0 | } |
2264 | 0 | else |
2265 | 0 | #endif |
2266 | 0 | b = d2b(rv, &p2, &bbits); |
2267 | 0 | #ifdef Avoid_Underflow |
2268 | 0 | p2 -= bc->scale; |
2269 | 0 | #endif |
2270 | | /* floor(log2(rv)) == bbits - 1 + p2 */ |
2271 | | /* Check for denormal case. */ |
2272 | 0 | i = P - bbits; |
2273 | 0 | if (i > (j = P - Emin - 1 + p2)) { |
2274 | | #ifdef Sudden_Underflow |
2275 | | Bfree(b); |
2276 | | b = i2b(1); |
2277 | | p2 = Emin; |
2278 | | i = P - 1; |
2279 | | #ifdef Avoid_Underflow |
2280 | | word0(rv) = (1 + bc->scale) << Exp_shift; |
2281 | | #else |
2282 | | word0(rv) = Exp_msk1; |
2283 | | #endif |
2284 | | word1(rv) = 0; |
2285 | | #else |
2286 | 0 | i = j; |
2287 | 0 | #endif |
2288 | 0 | } |
2289 | | #ifdef Honor_FLT_ROUNDS |
2290 | | if (bc->rounding != 1) { |
2291 | | if (i > 0) |
2292 | | b = lshift(b, i); |
2293 | | if (dsign) |
2294 | | b = increment(b); |
2295 | | } |
2296 | | else |
2297 | | #endif |
2298 | 0 | { |
2299 | 0 | b = lshift(b, ++i); |
2300 | 0 | b->x[0] |= 1; |
2301 | 0 | } |
2302 | 0 | #ifndef Sudden_Underflow |
2303 | 0 | have_i: |
2304 | 0 | #endif |
2305 | 0 | p2 -= p5 + i; |
2306 | 0 | d = i2b(1); |
2307 | | /* Arrange for convenient computation of quotients: |
2308 | | * shift left if necessary so divisor has 4 leading 0 bits. |
2309 | | */ |
2310 | 0 | if (p5 > 0) |
2311 | 0 | d = pow5mult(d, p5); |
2312 | 0 | else if (p5 < 0) |
2313 | 0 | b = pow5mult(b, -p5); |
2314 | 0 | if (p2 > 0) { |
2315 | 0 | b2 = p2; |
2316 | 0 | d2 = 0; |
2317 | 0 | } |
2318 | 0 | else { |
2319 | 0 | b2 = 0; |
2320 | 0 | d2 = -p2; |
2321 | 0 | } |
2322 | 0 | i = dshift(d, d2); |
2323 | 0 | if ((b2 += i) > 0) |
2324 | 0 | b = lshift(b, b2); |
2325 | 0 | if ((d2 += i) > 0) |
2326 | 0 | d = lshift(d, d2); |
2327 | | |
2328 | | /* Now b/d = exactly half-way between the two floating-point values */ |
2329 | | /* on either side of the input string. Compute first digit of b/d. */ |
2330 | |
|
2331 | 0 | if (!(dig = quorem(b,d))) { |
2332 | 0 | b = multadd(b, 10, 0); /* very unlikely */ |
2333 | 0 | dig = quorem(b,d); |
2334 | 0 | } |
2335 | | |
2336 | | /* Compare b/d with s0 */ |
2337 | |
|
2338 | 0 | for(i = 0; i < nd0; ) { |
2339 | 0 | if ((dd = s0[i++] - '0' - dig)) |
2340 | 0 | goto ret; |
2341 | 0 | if (!b->x[0] && b->wds == 1) { |
2342 | 0 | if (i < nd) |
2343 | 0 | dd = 1; |
2344 | 0 | goto ret; |
2345 | 0 | } |
2346 | 0 | b = multadd(b, 10, 0); |
2347 | 0 | dig = quorem(b,d); |
2348 | 0 | } |
2349 | 0 | for(j = bc->dp1; i++ < nd;) { |
2350 | 0 | if ((dd = s0[j++] - '0' - dig)) |
2351 | 0 | goto ret; |
2352 | 0 | if (!b->x[0] && b->wds == 1) { |
2353 | 0 | if (i < nd) |
2354 | 0 | dd = 1; |
2355 | 0 | goto ret; |
2356 | 0 | } |
2357 | 0 | b = multadd(b, 10, 0); |
2358 | 0 | dig = quorem(b,d); |
2359 | 0 | } |
2360 | 0 | if (b->x[0] || b->wds > 1) |
2361 | 0 | dd = -1; |
2362 | 0 | ret: |
2363 | 0 | Bfree(b); |
2364 | 0 | Bfree(d); |
2365 | | #ifdef Honor_FLT_ROUNDS |
2366 | | if (bc->rounding != 1) { |
2367 | | if (dd < 0) { |
2368 | | if (bc->rounding == 0) { |
2369 | | if (!dsign) |
2370 | | goto retlow1; |
2371 | | } |
2372 | | else if (dsign) |
2373 | | goto rethi1; |
2374 | | } |
2375 | | else if (dd > 0) { |
2376 | | if (bc->rounding == 0) { |
2377 | | if (dsign) |
2378 | | goto rethi1; |
2379 | | goto ret1; |
2380 | | } |
2381 | | if (!dsign) |
2382 | | goto rethi1; |
2383 | | dval(rv) += 2.*ulp(rv); |
2384 | | } |
2385 | | else { |
2386 | | bc->inexact = 0; |
2387 | | if (dsign) |
2388 | | goto rethi1; |
2389 | | } |
2390 | | } |
2391 | | else |
2392 | | #endif |
2393 | 0 | if (speccase) { |
2394 | 0 | if (dd <= 0) |
2395 | 0 | rv->d = 0.; |
2396 | 0 | } |
2397 | 0 | else if (dd < 0) { |
2398 | 0 | if (!dsign) /* does not happen for round-near */ |
2399 | 0 | retlow1: |
2400 | 0 | dval(rv) -= ulp(rv); |
2401 | 0 | } |
2402 | 0 | else if (dd > 0) { |
2403 | 0 | if (dsign) { |
2404 | 0 | rethi1: |
2405 | 0 | dval(rv) += ulp(rv); |
2406 | 0 | } |
2407 | 0 | } |
2408 | 0 | else { |
2409 | | /* Exact half-way case: apply round-even rule. */ |
2410 | 0 | if (word1(rv) & 1) { |
2411 | 0 | if (dsign) |
2412 | 0 | goto rethi1; |
2413 | 0 | goto retlow1; |
2414 | 0 | } |
2415 | 0 | } |
2416 | | |
2417 | | #ifdef Honor_FLT_ROUNDS |
2418 | | ret1: |
2419 | | #endif |
2420 | 0 | return; |
2421 | 0 | } |
2422 | | #endif /* NO_STRTOD_BIGCOMP */ |
2423 | | |
2424 | | double |
2425 | | strtod |
2426 | | #ifdef KR_headers |
2427 | | (s00, se) CONST char *s00; char **se; |
2428 | | #else |
2429 | | (CONST char *s00, char **se) |
2430 | | #endif |
2431 | 0 | { |
2432 | 0 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1; |
2433 | 0 | int esign, i, j, k, nd, nd0, nf, nz, nz0, sign; |
2434 | 0 | CONST char *s, *s0, *s1; |
2435 | 0 | double aadj, aadj1; |
2436 | 0 | Long L; |
2437 | 0 | U aadj2, adj, rv, rv0; |
2438 | 0 | ULong y, z; |
2439 | 0 | BCinfo bc; |
2440 | 0 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; |
2441 | | #ifdef SET_INEXACT |
2442 | | int oldinexact; |
2443 | | #endif |
2444 | | #ifdef Honor_FLT_ROUNDS /*{*/ |
2445 | | #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ |
2446 | | bc.rounding = Flt_Rounds; |
2447 | | #else /*}{*/ |
2448 | | bc.rounding = 1; |
2449 | | switch(fegetround()) { |
2450 | | case FE_TOWARDZERO: bc.rounding = 0; break; |
2451 | | case FE_UPWARD: bc.rounding = 2; break; |
2452 | | case FE_DOWNWARD: bc.rounding = 3; |
2453 | | } |
2454 | | #endif /*}}*/ |
2455 | | #endif /*}*/ |
2456 | | #ifdef USE_LOCALE |
2457 | | CONST char *s2; |
2458 | | #endif |
2459 | |
|
2460 | 0 | sign = nz0 = nz = bc.dplen = bc.uflchk = 0; |
2461 | 0 | dval(&rv) = 0.; |
2462 | 0 | for(s = s00;;s++) switch(*s) { |
2463 | 0 | case '-': |
2464 | 0 | sign = 1; |
2465 | | // fall through |
2466 | 0 | case '+': |
2467 | 0 | if (*++s) |
2468 | 0 | goto break2; |
2469 | | // fall through |
2470 | 0 | case 0: |
2471 | 0 | goto ret0; |
2472 | 0 | case '\t': |
2473 | 0 | case '\n': |
2474 | 0 | case '\v': |
2475 | 0 | case '\f': |
2476 | 0 | case '\r': |
2477 | 0 | case ' ': |
2478 | 0 | continue; |
2479 | 0 | default: |
2480 | 0 | goto break2; |
2481 | 0 | } |
2482 | 0 | break2: |
2483 | 0 | if (*s == '0') { |
2484 | | #ifndef NO_HEX_FP /*{*/ |
2485 | | switch(s[1]) { |
2486 | | case 'x': |
2487 | | case 'X': |
2488 | | #ifdef Honor_FLT_ROUNDS |
2489 | | gethex(&s, &rv, bc.rounding, sign); |
2490 | | #else |
2491 | | gethex(&s, &rv, 1, sign); |
2492 | | #endif |
2493 | | goto ret; |
2494 | | } |
2495 | | #endif /*}*/ |
2496 | 0 | nz0 = 1; |
2497 | 0 | while(*++s == '0') ; |
2498 | 0 | if (!*s) |
2499 | 0 | goto ret; |
2500 | 0 | } |
2501 | 0 | s0 = s; |
2502 | 0 | y = z = 0; |
2503 | 0 | for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) |
2504 | 0 | if (nd < 9) |
2505 | 0 | y = 10*y + c - '0'; |
2506 | 0 | else if (nd < 16) |
2507 | 0 | z = 10*z + c - '0'; |
2508 | 0 | nd0 = nd; |
2509 | 0 | bc.dp0 = bc.dp1 = s - s0; |
2510 | | #ifdef USE_LOCALE |
2511 | | s1 = localeconv()->decimal_point; |
2512 | | if (c == *s1) { |
2513 | | c = '.'; |
2514 | | if (*++s1) { |
2515 | | s2 = s; |
2516 | | for(;;) { |
2517 | | if (*++s2 != *s1) { |
2518 | | c = 0; |
2519 | | break; |
2520 | | } |
2521 | | if (!*++s1) { |
2522 | | s = s2; |
2523 | | break; |
2524 | | } |
2525 | | } |
2526 | | } |
2527 | | } |
2528 | | #endif |
2529 | 0 | if (c == '.') { |
2530 | 0 | c = *++s; |
2531 | 0 | bc.dp1 = s - s0; |
2532 | 0 | bc.dplen = bc.dp1 - bc.dp0; |
2533 | 0 | if (!nd) { |
2534 | 0 | for(; c == '0'; c = *++s) |
2535 | 0 | nz++; |
2536 | 0 | if (c > '0' && c <= '9') { |
2537 | 0 | s0 = s; |
2538 | 0 | nf += nz; |
2539 | 0 | nz = 0; |
2540 | 0 | goto have_dig; |
2541 | 0 | } |
2542 | 0 | goto dig_done; |
2543 | 0 | } |
2544 | 0 | for(; c >= '0' && c <= '9'; c = *++s) { |
2545 | 0 | have_dig: |
2546 | 0 | nz++; |
2547 | 0 | if (c -= '0') { |
2548 | 0 | nf += nz; |
2549 | 0 | for(i = 1; i < nz; i++) |
2550 | 0 | if (nd++ < 9) |
2551 | 0 | y *= 10; |
2552 | 0 | else if (nd <= DBL_DIG + 1) |
2553 | 0 | z *= 10; |
2554 | 0 | if (nd++ < 9) |
2555 | 0 | y = 10*y + c; |
2556 | 0 | else if (nd <= DBL_DIG + 1) |
2557 | 0 | z = 10*z + c; |
2558 | 0 | nz = 0; |
2559 | 0 | } |
2560 | 0 | } |
2561 | 0 | } |
2562 | 0 | dig_done: |
2563 | 0 | e = 0; |
2564 | 0 | if (c == 'e' || c == 'E') { |
2565 | 0 | if (!nd && !nz && !nz0) { |
2566 | 0 | goto ret0; |
2567 | 0 | } |
2568 | 0 | s00 = s; |
2569 | 0 | esign = 0; |
2570 | 0 | switch(c = *++s) { |
2571 | 0 | case '-': |
2572 | 0 | esign = 1; |
2573 | | // fall through |
2574 | 0 | case '+': |
2575 | 0 | c = *++s; |
2576 | 0 | } |
2577 | 0 | if (c >= '0' && c <= '9') { |
2578 | 0 | while(c == '0') |
2579 | 0 | c = *++s; |
2580 | 0 | if (c > '0' && c <= '9') { |
2581 | 0 | L = c - '0'; |
2582 | 0 | s1 = s; |
2583 | 0 | while((c = *++s) >= '0' && c <= '9') |
2584 | 0 | L = 10*L + c - '0'; |
2585 | 0 | if (s - s1 > 8 || L > 19999) |
2586 | | /* Avoid confusion from exponents |
2587 | | * so large that e might overflow. |
2588 | | */ |
2589 | 0 | e = 19999; /* safe for 16 bit ints */ |
2590 | 0 | else |
2591 | 0 | e = (int)L; |
2592 | 0 | if (esign) |
2593 | 0 | e = -e; |
2594 | 0 | } |
2595 | 0 | else |
2596 | 0 | e = 0; |
2597 | 0 | } |
2598 | 0 | else |
2599 | 0 | s = s00; |
2600 | 0 | } |
2601 | 0 | if (!nd) { |
2602 | 0 | if (!nz && !nz0) { |
2603 | 0 | #ifdef INFNAN_CHECK |
2604 | | /* Check for Nan and Infinity */ |
2605 | 0 | if (!bc.dplen) |
2606 | 0 | switch(c) { |
2607 | 0 | case 'i': |
2608 | 0 | case 'I': |
2609 | 0 | if (match(&s,"nf")) { |
2610 | 0 | --s; |
2611 | 0 | if (!match(&s,"inity")) |
2612 | 0 | ++s; |
2613 | 0 | word0(&rv) = 0x7ff00000; |
2614 | 0 | word1(&rv) = 0; |
2615 | 0 | goto ret; |
2616 | 0 | } |
2617 | 0 | break; |
2618 | 0 | case 'n': |
2619 | 0 | case 'N': |
2620 | 0 | if (match(&s, "an")) { |
2621 | 0 | word0(&rv) = NAN_WORD0; |
2622 | 0 | word1(&rv) = NAN_WORD1; |
2623 | 0 | #ifndef No_Hex_NaN |
2624 | 0 | if (*s == '(') /*)*/ |
2625 | 0 | hexnan(&rv, &s); |
2626 | 0 | #endif |
2627 | 0 | goto ret; |
2628 | 0 | } |
2629 | 0 | } |
2630 | 0 | #endif /* INFNAN_CHECK */ |
2631 | 0 | ret0: |
2632 | 0 | s = s00; |
2633 | 0 | sign = 0; |
2634 | 0 | } |
2635 | 0 | goto ret; |
2636 | 0 | } |
2637 | 0 | bc.e0 = e1 = e -= nf; |
2638 | | |
2639 | | /* Now we have nd0 digits, starting at s0, followed by a |
2640 | | * decimal point, followed by nd-nd0 digits. The number we're |
2641 | | * after is the integer represented by those digits times |
2642 | | * 10**e */ |
2643 | |
|
2644 | 0 | if (!nd0) |
2645 | 0 | nd0 = nd; |
2646 | 0 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; |
2647 | 0 | dval(&rv) = y; |
2648 | 0 | if (k > 9) { |
2649 | | #ifdef SET_INEXACT |
2650 | | if (k > DBL_DIG) |
2651 | | oldinexact = get_inexact(); |
2652 | | #endif |
2653 | 0 | dval(&rv) = tens[k - 9] * dval(&rv) + z; |
2654 | 0 | } |
2655 | 0 | bd0 = 0; |
2656 | 0 | if (nd <= DBL_DIG |
2657 | 0 | #ifndef RND_PRODQUOT |
2658 | 0 | #ifndef Honor_FLT_ROUNDS |
2659 | 0 | && Flt_Rounds == 1 |
2660 | 0 | #endif |
2661 | 0 | #endif |
2662 | 0 | ) { |
2663 | 0 | if (!e) |
2664 | 0 | goto ret; |
2665 | 0 | if (e > 0) { |
2666 | 0 | if (e <= Ten_pmax) { |
2667 | | #ifdef VAX |
2668 | | goto vax_ovfl_check; |
2669 | | #else |
2670 | | #ifdef Honor_FLT_ROUNDS |
2671 | | /* round correctly FLT_ROUNDS = 2 or 3 */ |
2672 | | if (sign) { |
2673 | | rv.d = -rv.d; |
2674 | | sign = 0; |
2675 | | } |
2676 | | #endif |
2677 | 0 | /* rv = */ rounded_product(dval(&rv), tens[e]); |
2678 | 0 | goto ret; |
2679 | 0 | #endif |
2680 | 0 | } |
2681 | 0 | i = DBL_DIG - nd; |
2682 | 0 | if (e <= Ten_pmax + i) { |
2683 | | /* A fancier test would sometimes let us do |
2684 | | * this for larger i values. |
2685 | | */ |
2686 | | #ifdef Honor_FLT_ROUNDS |
2687 | | /* round correctly FLT_ROUNDS = 2 or 3 */ |
2688 | | if (sign) { |
2689 | | rv.d = -rv.d; |
2690 | | sign = 0; |
2691 | | } |
2692 | | #endif |
2693 | 0 | e -= i; |
2694 | 0 | dval(&rv) *= tens[i]; |
2695 | | #ifdef VAX |
2696 | | /* VAX exponent range is so narrow we must |
2697 | | * worry about overflow here... |
2698 | | */ |
2699 | | vax_ovfl_check: |
2700 | | word0(&rv) -= P*Exp_msk1; |
2701 | | /* rv = */ rounded_product(dval(&rv), tens[e]); |
2702 | | if ((word0(&rv) & Exp_mask) |
2703 | | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) |
2704 | | goto ovfl; |
2705 | | word0(&rv) += P*Exp_msk1; |
2706 | | #else |
2707 | 0 | /* rv = */ rounded_product(dval(&rv), tens[e]); |
2708 | 0 | #endif |
2709 | 0 | goto ret; |
2710 | 0 | } |
2711 | 0 | } |
2712 | 0 | #ifndef Inaccurate_Divide |
2713 | 0 | else if (e >= -Ten_pmax) { |
2714 | | #ifdef Honor_FLT_ROUNDS |
2715 | | /* round correctly FLT_ROUNDS = 2 or 3 */ |
2716 | | if (sign) { |
2717 | | rv.d = -rv.d; |
2718 | | sign = 0; |
2719 | | } |
2720 | | #endif |
2721 | 0 | /* rv = */ rounded_quotient(dval(&rv), tens[-e]); |
2722 | 0 | goto ret; |
2723 | 0 | } |
2724 | 0 | #endif |
2725 | 0 | } |
2726 | 0 | e1 += nd - k; |
2727 | |
|
2728 | 0 | #ifdef IEEE_Arith |
2729 | | #ifdef SET_INEXACT |
2730 | | bc.inexact = 1; |
2731 | | if (k <= DBL_DIG) |
2732 | | oldinexact = get_inexact(); |
2733 | | #endif |
2734 | 0 | #ifdef Avoid_Underflow |
2735 | 0 | bc.scale = 0; |
2736 | 0 | #endif |
2737 | | #ifdef Honor_FLT_ROUNDS |
2738 | | if (bc.rounding >= 2) { |
2739 | | if (sign) |
2740 | | bc.rounding = bc.rounding == 2 ? 0 : 2; |
2741 | | else |
2742 | | if (bc.rounding != 2) |
2743 | | bc.rounding = 0; |
2744 | | } |
2745 | | #endif |
2746 | 0 | #endif /*IEEE_Arith*/ |
2747 | | |
2748 | | /* Get starting approximation = rv * 10**e1 */ |
2749 | |
|
2750 | 0 | if (e1 > 0) { |
2751 | 0 | if ((i = e1 & 15)) |
2752 | 0 | dval(&rv) *= tens[i]; |
2753 | 0 | if (e1 &= ~15) { |
2754 | 0 | if (e1 > DBL_MAX_10_EXP) { |
2755 | 0 | ovfl: |
2756 | 0 | #ifndef NO_ERRNO |
2757 | 0 | errno = ERANGE; |
2758 | 0 | #endif |
2759 | | /* Can't trust HUGE_VAL */ |
2760 | 0 | #ifdef IEEE_Arith |
2761 | | #ifdef Honor_FLT_ROUNDS |
2762 | | switch(bc.rounding) { |
2763 | | case 0: /* toward 0 */ |
2764 | | case 3: /* toward -infinity */ |
2765 | | word0(&rv) = Big0; |
2766 | | word1(&rv) = Big1; |
2767 | | break; |
2768 | | default: |
2769 | | word0(&rv) = Exp_mask; |
2770 | | word1(&rv) = 0; |
2771 | | } |
2772 | | #else /*Honor_FLT_ROUNDS*/ |
2773 | 0 | word0(&rv) = Exp_mask; |
2774 | 0 | word1(&rv) = 0; |
2775 | 0 | #endif /*Honor_FLT_ROUNDS*/ |
2776 | | #ifdef SET_INEXACT |
2777 | | /* set overflow bit */ |
2778 | | dval(&rv0) = 1e300; |
2779 | | dval(&rv0) *= dval(&rv0); |
2780 | | #endif |
2781 | | #else /*IEEE_Arith*/ |
2782 | | word0(&rv) = Big0; |
2783 | | word1(&rv) = Big1; |
2784 | | #endif /*IEEE_Arith*/ |
2785 | 0 | goto ret; |
2786 | 0 | } |
2787 | 0 | e1 >>= 4; |
2788 | 0 | for(j = 0; e1 > 1; j++, e1 >>= 1) |
2789 | 0 | if (e1 & 1) |
2790 | 0 | dval(&rv) *= bigtens[j]; |
2791 | | /* The last multiplication could overflow. */ |
2792 | 0 | word0(&rv) -= P*Exp_msk1; |
2793 | 0 | dval(&rv) *= bigtens[j]; |
2794 | 0 | if ((z = word0(&rv) & Exp_mask) |
2795 | 0 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) |
2796 | 0 | goto ovfl; |
2797 | 0 | if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { |
2798 | | /* set to largest number */ |
2799 | | /* (Can't trust DBL_MAX) */ |
2800 | 0 | word0(&rv) = Big0; |
2801 | 0 | word1(&rv) = Big1; |
2802 | 0 | } |
2803 | 0 | else |
2804 | 0 | word0(&rv) += P*Exp_msk1; |
2805 | 0 | } |
2806 | 0 | } |
2807 | 0 | else if (e1 < 0) { |
2808 | 0 | e1 = -e1; |
2809 | 0 | if ((i = e1 & 15)) |
2810 | 0 | dval(&rv) /= tens[i]; |
2811 | 0 | if (e1 >>= 4) { |
2812 | 0 | if (e1 >= 1 << n_bigtens) |
2813 | 0 | goto undfl; |
2814 | 0 | #ifdef Avoid_Underflow |
2815 | 0 | if (e1 & Scale_Bit) |
2816 | 0 | bc.scale = 2*P; |
2817 | 0 | for(j = 0; e1 > 0; j++, e1 >>= 1) |
2818 | 0 | if (e1 & 1) |
2819 | 0 | dval(&rv) *= tinytens[j]; |
2820 | 0 | if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask) |
2821 | 0 | >> Exp_shift)) > 0) { |
2822 | | /* scaled rv is denormal; clear j low bits */ |
2823 | 0 | if (j >= 32) { |
2824 | 0 | word1(&rv) = 0; |
2825 | 0 | if (j >= 53) |
2826 | 0 | word0(&rv) = (P+2)*Exp_msk1; |
2827 | 0 | else |
2828 | 0 | word0(&rv) &= 0xffffffff << (j-32); |
2829 | 0 | } |
2830 | 0 | else |
2831 | 0 | word1(&rv) &= 0xffffffff << j; |
2832 | 0 | } |
2833 | | #else |
2834 | | for(j = 0; e1 > 1; j++, e1 >>= 1) |
2835 | | if (e1 & 1) |
2836 | | dval(&rv) *= tinytens[j]; |
2837 | | /* The last multiplication could underflow. */ |
2838 | | dval(&rv0) = dval(&rv); |
2839 | | dval(&rv) *= tinytens[j]; |
2840 | | if (!dval(&rv)) { |
2841 | | dval(&rv) = 2.*dval(&rv0); |
2842 | | dval(&rv) *= tinytens[j]; |
2843 | | #endif |
2844 | 0 | if (!dval(&rv)) { |
2845 | 0 | undfl: |
2846 | 0 | dval(&rv) = 0.; |
2847 | 0 | #ifndef NO_ERRNO |
2848 | 0 | errno = ERANGE; |
2849 | 0 | #endif |
2850 | 0 | goto ret; |
2851 | 0 | } |
2852 | | #ifndef Avoid_Underflow |
2853 | | word0(&rv) = Tiny0; |
2854 | | word1(&rv) = Tiny1; |
2855 | | /* The refinement below will clean |
2856 | | * this approximation up. |
2857 | | */ |
2858 | | } |
2859 | | #endif |
2860 | 0 | } |
2861 | 0 | } |
2862 | | |
2863 | | /* Now the hard part -- adjusting rv to the correct value.*/ |
2864 | | |
2865 | | /* Put digits into bd: true value = bd * 10^e */ |
2866 | | |
2867 | 0 | bc.nd = nd; |
2868 | 0 | #ifndef NO_STRTOD_BIGCOMP |
2869 | 0 | bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */ |
2870 | | /* to silence an erroneous warning about bc.nd0 */ |
2871 | | /* possibly not being initialized. */ |
2872 | 0 | if (nd > strtod_diglim) { |
2873 | | /* ASSERT(strtod_diglim >= 18); 18 == one more than the */ |
2874 | | /* minimum number of decimal digits to distinguish double values */ |
2875 | | /* in IEEE arithmetic. */ |
2876 | 0 | i = j = 18; |
2877 | 0 | if (i > nd0) |
2878 | 0 | j += bc.dplen; |
2879 | 0 | for(;;) { |
2880 | 0 | if (--j <= bc.dp1 && j >= bc.dp0) |
2881 | 0 | j = bc.dp0 - 1; |
2882 | 0 | if (s0[j] != '0') |
2883 | 0 | break; |
2884 | 0 | --i; |
2885 | 0 | } |
2886 | 0 | e += nd - i; |
2887 | 0 | nd = i; |
2888 | 0 | if (nd0 > nd) |
2889 | 0 | nd0 = nd; |
2890 | 0 | if (nd < 9) { /* must recompute y */ |
2891 | 0 | y = 0; |
2892 | 0 | for(i = 0; i < nd0; ++i) |
2893 | 0 | y = 10*y + s0[i] - '0'; |
2894 | 0 | for(j = bc.dp1; i < nd; ++i) |
2895 | 0 | y = 10*y + s0[j++] - '0'; |
2896 | 0 | } |
2897 | 0 | } |
2898 | 0 | #endif |
2899 | 0 | bd0 = s2b(s0, nd0, nd, y, bc.dplen); |
2900 | |
|
2901 | 0 | for(;;) { |
2902 | 0 | bd = Balloc(bd0->k); |
2903 | 0 | Bcopy(bd, bd0); |
2904 | 0 | bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ |
2905 | 0 | bs = i2b(1); |
2906 | |
|
2907 | 0 | if (e >= 0) { |
2908 | 0 | bb2 = bb5 = 0; |
2909 | 0 | bd2 = bd5 = e; |
2910 | 0 | } |
2911 | 0 | else { |
2912 | 0 | bb2 = bb5 = -e; |
2913 | 0 | bd2 = bd5 = 0; |
2914 | 0 | } |
2915 | 0 | if (bbe >= 0) |
2916 | 0 | bb2 += bbe; |
2917 | 0 | else |
2918 | 0 | bd2 -= bbe; |
2919 | 0 | bs2 = bb2; |
2920 | | #ifdef Honor_FLT_ROUNDS |
2921 | | if (bc.rounding != 1) |
2922 | | bs2++; |
2923 | | #endif |
2924 | 0 | #ifdef Avoid_Underflow |
2925 | 0 | j = bbe - bc.scale; |
2926 | 0 | i = j + bbbits - 1; /* logb(rv) */ |
2927 | 0 | if (i < Emin) /* denormal */ |
2928 | 0 | j += P - Emin; |
2929 | 0 | else |
2930 | 0 | j = P + 1 - bbbits; |
2931 | | #else /*Avoid_Underflow*/ |
2932 | | #ifdef Sudden_Underflow |
2933 | | #ifdef IBM |
2934 | | j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); |
2935 | | #else |
2936 | | j = P + 1 - bbbits; |
2937 | | #endif |
2938 | | #else /*Sudden_Underflow*/ |
2939 | | j = bbe; |
2940 | | i = j + bbbits - 1; /* logb(rv) */ |
2941 | | if (i < Emin) /* denormal */ |
2942 | | j += P - Emin; |
2943 | | else |
2944 | | j = P + 1 - bbbits; |
2945 | | #endif /*Sudden_Underflow*/ |
2946 | | #endif /*Avoid_Underflow*/ |
2947 | 0 | bb2 += j; |
2948 | 0 | bd2 += j; |
2949 | 0 | #ifdef Avoid_Underflow |
2950 | 0 | bd2 += bc.scale; |
2951 | 0 | #endif |
2952 | 0 | i = bb2 < bd2 ? bb2 : bd2; |
2953 | 0 | if (i > bs2) |
2954 | 0 | i = bs2; |
2955 | 0 | if (i > 0) { |
2956 | 0 | bb2 -= i; |
2957 | 0 | bd2 -= i; |
2958 | 0 | bs2 -= i; |
2959 | 0 | } |
2960 | 0 | if (bb5 > 0) { |
2961 | 0 | bs = pow5mult(bs, bb5); |
2962 | 0 | bb1 = mult(bs, bb); |
2963 | 0 | Bfree(bb); |
2964 | 0 | bb = bb1; |
2965 | 0 | } |
2966 | 0 | if (bb2 > 0) |
2967 | 0 | bb = lshift(bb, bb2); |
2968 | 0 | if (bd5 > 0) |
2969 | 0 | bd = pow5mult(bd, bd5); |
2970 | 0 | if (bd2 > 0) |
2971 | 0 | bd = lshift(bd, bd2); |
2972 | 0 | if (bs2 > 0) |
2973 | 0 | bs = lshift(bs, bs2); |
2974 | 0 | delta = diff(bb, bd); |
2975 | 0 | bc.dsign = delta->sign; |
2976 | 0 | delta->sign = 0; |
2977 | 0 | i = cmp(delta, bs); |
2978 | 0 | #ifndef NO_STRTOD_BIGCOMP |
2979 | 0 | if (bc.nd > nd && i <= 0) { |
2980 | 0 | if (bc.dsign) |
2981 | 0 | break; /* Must use bigcomp(). */ |
2982 | | #ifdef Honor_FLT_ROUNDS |
2983 | | if (bc.rounding != 1) { |
2984 | | if (i < 0) |
2985 | | break; |
2986 | | } |
2987 | | else |
2988 | | #endif |
2989 | 0 | { |
2990 | 0 | bc.nd = nd; |
2991 | 0 | i = -1; /* Discarded digits make delta smaller. */ |
2992 | 0 | } |
2993 | 0 | } |
2994 | 0 | #endif |
2995 | | #ifdef Honor_FLT_ROUNDS |
2996 | | if (bc.rounding != 1) { |
2997 | | if (i < 0) { |
2998 | | /* Error is less than an ulp */ |
2999 | | if (!delta->x[0] && delta->wds <= 1) { |
3000 | | /* exact */ |
3001 | | #ifdef SET_INEXACT |
3002 | | bc.inexact = 0; |
3003 | | #endif |
3004 | | break; |
3005 | | } |
3006 | | if (bc.rounding) { |
3007 | | if (bc.dsign) { |
3008 | | adj.d = 1.; |
3009 | | goto apply_adj; |
3010 | | } |
3011 | | } |
3012 | | else if (!bc.dsign) { |
3013 | | adj.d = -1.; |
3014 | | if (!word1(&rv) |
3015 | | && !(word0(&rv) & Frac_mask)) { |
3016 | | y = word0(&rv) & Exp_mask; |
3017 | | #ifdef Avoid_Underflow |
3018 | | if (!bc.scale || y > 2*P*Exp_msk1) |
3019 | | #else |
3020 | | if (y) |
3021 | | #endif |
3022 | | { |
3023 | | delta = lshift(delta,Log2P); |
3024 | | if (cmp(delta, bs) <= 0) |
3025 | | adj.d = -0.5; |
3026 | | } |
3027 | | } |
3028 | | apply_adj: |
3029 | | #ifdef Avoid_Underflow |
3030 | | if (bc.scale && (y = word0(&rv) & Exp_mask) |
3031 | | <= 2*P*Exp_msk1) |
3032 | | word0(&adj) += (2*P+1)*Exp_msk1 - y; |
3033 | | #else |
3034 | | #ifdef Sudden_Underflow |
3035 | | if ((word0(&rv) & Exp_mask) <= |
3036 | | P*Exp_msk1) { |
3037 | | word0(&rv) += P*Exp_msk1; |
3038 | | dval(&rv) += adj.d*ulp(dval(&rv)); |
3039 | | word0(&rv) -= P*Exp_msk1; |
3040 | | } |
3041 | | else |
3042 | | #endif /*Sudden_Underflow*/ |
3043 | | #endif /*Avoid_Underflow*/ |
3044 | | dval(&rv) += adj.d*ulp(&rv); |
3045 | | } |
3046 | | break; |
3047 | | } |
3048 | | adj.d = ratio(delta, bs); |
3049 | | if (adj.d < 1.) |
3050 | | adj.d = 1.; |
3051 | | if (adj.d <= 0x7ffffffe) { |
3052 | | /* adj = rounding ? ceil(adj) : floor(adj); */ |
3053 | | y = adj.d; |
3054 | | if (y != adj.d) { |
3055 | | if (!((bc.rounding>>1) ^ bc.dsign)) |
3056 | | y++; |
3057 | | adj.d = y; |
3058 | | } |
3059 | | } |
3060 | | #ifdef Avoid_Underflow |
3061 | | if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) |
3062 | | word0(&adj) += (2*P+1)*Exp_msk1 - y; |
3063 | | #else |
3064 | | #ifdef Sudden_Underflow |
3065 | | if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { |
3066 | | word0(&rv) += P*Exp_msk1; |
3067 | | adj.d *= ulp(dval(&rv)); |
3068 | | if (bc.dsign) |
3069 | | dval(&rv) += adj.d; |
3070 | | else |
3071 | | dval(&rv) -= adj.d; |
3072 | | word0(&rv) -= P*Exp_msk1; |
3073 | | goto cont; |
3074 | | } |
3075 | | #endif /*Sudden_Underflow*/ |
3076 | | #endif /*Avoid_Underflow*/ |
3077 | | adj.d *= ulp(&rv); |
3078 | | if (bc.dsign) { |
3079 | | if (word0(&rv) == Big0 && word1(&rv) == Big1) |
3080 | | goto ovfl; |
3081 | | dval(&rv) += adj.d; |
3082 | | } |
3083 | | else |
3084 | | dval(&rv) -= adj.d; |
3085 | | goto cont; |
3086 | | } |
3087 | | #endif /*Honor_FLT_ROUNDS*/ |
3088 | | |
3089 | 0 | if (i < 0) { |
3090 | | /* Error is less than half an ulp -- check for |
3091 | | * special case of mantissa a power of two. |
3092 | | */ |
3093 | 0 | if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask |
3094 | 0 | #ifdef IEEE_Arith |
3095 | 0 | #ifdef Avoid_Underflow |
3096 | 0 | || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1 |
3097 | | #else |
3098 | | || (word0(&rv) & Exp_mask) <= Exp_msk1 |
3099 | | #endif |
3100 | 0 | #endif |
3101 | 0 | ) { |
3102 | | #ifdef SET_INEXACT |
3103 | | if (!delta->x[0] && delta->wds <= 1) |
3104 | | bc.inexact = 0; |
3105 | | #endif |
3106 | 0 | break; |
3107 | 0 | } |
3108 | 0 | if (!delta->x[0] && delta->wds <= 1) { |
3109 | | /* exact result */ |
3110 | | #ifdef SET_INEXACT |
3111 | | bc.inexact = 0; |
3112 | | #endif |
3113 | 0 | break; |
3114 | 0 | } |
3115 | 0 | delta = lshift(delta,Log2P); |
3116 | 0 | if (cmp(delta, bs) > 0) |
3117 | 0 | goto drop_down; |
3118 | 0 | break; |
3119 | 0 | } |
3120 | 0 | if (i == 0) { |
3121 | | /* exactly half-way between */ |
3122 | 0 | if (bc.dsign) { |
3123 | 0 | if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 |
3124 | 0 | && word1(&rv) == ( |
3125 | 0 | #ifdef Avoid_Underflow |
3126 | 0 | (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) |
3127 | 0 | ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : |
3128 | 0 | #endif |
3129 | 0 | 0xffffffff)) { |
3130 | | /*boundary case -- increment exponent*/ |
3131 | 0 | word0(&rv) = (word0(&rv) & Exp_mask) |
3132 | 0 | + Exp_msk1 |
3133 | | #ifdef IBM |
3134 | | | Exp_msk1 >> 4 |
3135 | | #endif |
3136 | 0 | ; |
3137 | 0 | word1(&rv) = 0; |
3138 | 0 | #ifdef Avoid_Underflow |
3139 | 0 | bc.dsign = 0; |
3140 | 0 | #endif |
3141 | 0 | break; |
3142 | 0 | } |
3143 | 0 | } |
3144 | 0 | else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) { |
3145 | 0 | drop_down: |
3146 | | /* boundary case -- decrement exponent */ |
3147 | | #ifdef Sudden_Underflow /*{{*/ |
3148 | | L = word0(&rv) & Exp_mask; |
3149 | | #ifdef IBM |
3150 | | if (L < Exp_msk1) |
3151 | | #else |
3152 | | #ifdef Avoid_Underflow |
3153 | | if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) |
3154 | | #else |
3155 | | if (L <= Exp_msk1) |
3156 | | #endif /*Avoid_Underflow*/ |
3157 | | #endif /*IBM*/ |
3158 | | { |
3159 | | if (bc.nd >nd) { |
3160 | | bc.uflchk = 1; |
3161 | | break; |
3162 | | } |
3163 | | goto undfl; |
3164 | | } |
3165 | | L -= Exp_msk1; |
3166 | | #else /*Sudden_Underflow}{*/ |
3167 | 0 | #ifdef Avoid_Underflow |
3168 | 0 | if (bc.scale) { |
3169 | 0 | L = word0(&rv) & Exp_mask; |
3170 | 0 | if (L <= (2*P+1)*Exp_msk1) { |
3171 | 0 | if (L > (P+2)*Exp_msk1) |
3172 | | /* round even ==> */ |
3173 | | /* accept rv */ |
3174 | 0 | break; |
3175 | | /* rv = smallest denormal */ |
3176 | 0 | if (bc.nd >nd) { |
3177 | 0 | bc.uflchk = 1; |
3178 | 0 | break; |
3179 | 0 | } |
3180 | 0 | goto undfl; |
3181 | 0 | } |
3182 | 0 | } |
3183 | 0 | #endif /*Avoid_Underflow*/ |
3184 | 0 | L = (word0(&rv) & Exp_mask) - Exp_msk1; |
3185 | 0 | #endif /*Sudden_Underflow}}*/ |
3186 | 0 | word0(&rv) = L | Bndry_mask1; |
3187 | 0 | word1(&rv) = 0xffffffff; |
3188 | | #ifdef IBM |
3189 | | goto cont; |
3190 | | #else |
3191 | 0 | break; |
3192 | 0 | #endif |
3193 | 0 | } |
3194 | 0 | #ifndef ROUND_BIASED |
3195 | 0 | if (!(word1(&rv) & LSB)) |
3196 | 0 | break; |
3197 | 0 | #endif |
3198 | 0 | if (bc.dsign) |
3199 | 0 | dval(&rv) += ulp(&rv); |
3200 | 0 | #ifndef ROUND_BIASED |
3201 | 0 | else { |
3202 | 0 | dval(&rv) -= ulp(&rv); |
3203 | 0 | #ifndef Sudden_Underflow |
3204 | 0 | if (!dval(&rv)) { |
3205 | 0 | if (bc.nd >nd) { |
3206 | 0 | bc.uflchk = 1; |
3207 | 0 | break; |
3208 | 0 | } |
3209 | 0 | goto undfl; |
3210 | 0 | } |
3211 | 0 | #endif |
3212 | 0 | } |
3213 | 0 | #ifdef Avoid_Underflow |
3214 | 0 | bc.dsign = 1 - bc.dsign; |
3215 | 0 | #endif |
3216 | 0 | #endif |
3217 | 0 | break; |
3218 | 0 | } |
3219 | 0 | if ((aadj = ratio(delta, bs)) <= 2.) { |
3220 | 0 | if (bc.dsign) |
3221 | 0 | aadj = aadj1 = 1.; |
3222 | 0 | else if (word1(&rv) || word0(&rv) & Bndry_mask) { |
3223 | 0 | #ifndef Sudden_Underflow |
3224 | 0 | if (word1(&rv) == Tiny1 && !word0(&rv)) { |
3225 | 0 | if (bc.nd >nd) { |
3226 | 0 | bc.uflchk = 1; |
3227 | 0 | break; |
3228 | 0 | } |
3229 | 0 | goto undfl; |
3230 | 0 | } |
3231 | 0 | #endif |
3232 | 0 | aadj = 1.; |
3233 | 0 | aadj1 = -1.; |
3234 | 0 | } |
3235 | 0 | else { |
3236 | | /* special case -- power of FLT_RADIX to be */ |
3237 | | /* rounded down... */ |
3238 | |
|
3239 | 0 | if (aadj < 2./FLT_RADIX) |
3240 | 0 | aadj = 1./FLT_RADIX; |
3241 | 0 | else |
3242 | 0 | aadj *= 0.5; |
3243 | 0 | aadj1 = -aadj; |
3244 | 0 | } |
3245 | 0 | } |
3246 | 0 | else { |
3247 | 0 | aadj *= 0.5; |
3248 | 0 | aadj1 = bc.dsign ? aadj : -aadj; |
3249 | | #ifdef Check_FLT_ROUNDS |
3250 | | switch(bc.rounding) { |
3251 | | case 2: /* towards +infinity */ |
3252 | | aadj1 -= 0.5; |
3253 | | break; |
3254 | | case 0: /* towards 0 */ |
3255 | | case 3: /* towards -infinity */ |
3256 | | aadj1 += 0.5; |
3257 | | } |
3258 | | #else |
3259 | 0 | if (Flt_Rounds == 0) |
3260 | 0 | aadj1 += 0.5; |
3261 | 0 | #endif /*Check_FLT_ROUNDS*/ |
3262 | 0 | } |
3263 | 0 | y = word0(&rv) & Exp_mask; |
3264 | | |
3265 | | /* Check for overflow */ |
3266 | |
|
3267 | 0 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { |
3268 | 0 | dval(&rv0) = dval(&rv); |
3269 | 0 | word0(&rv) -= P*Exp_msk1; |
3270 | 0 | adj.d = aadj1 * ulp(&rv); |
3271 | 0 | dval(&rv) += adj.d; |
3272 | 0 | if ((word0(&rv) & Exp_mask) >= |
3273 | 0 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { |
3274 | 0 | if (word0(&rv0) == Big0 && word1(&rv0) == Big1) |
3275 | 0 | goto ovfl; |
3276 | 0 | word0(&rv) = Big0; |
3277 | 0 | word1(&rv) = Big1; |
3278 | 0 | goto cont; |
3279 | 0 | } |
3280 | 0 | else |
3281 | 0 | word0(&rv) += P*Exp_msk1; |
3282 | 0 | } |
3283 | 0 | else { |
3284 | 0 | #ifdef Avoid_Underflow |
3285 | 0 | if (bc.scale && y <= 2*P*Exp_msk1) { |
3286 | 0 | if (aadj <= 0x7fffffff) { |
3287 | 0 | if ((z = (ULong)aadj) <= 0) |
3288 | 0 | z = 1; |
3289 | 0 | aadj = z; |
3290 | 0 | aadj1 = bc.dsign ? aadj : -aadj; |
3291 | 0 | } |
3292 | 0 | dval(&aadj2) = aadj1; |
3293 | 0 | word0(&aadj2) += (2*P+1)*Exp_msk1 - y; |
3294 | 0 | aadj1 = dval(&aadj2); |
3295 | 0 | } |
3296 | 0 | adj.d = aadj1 * ulp(&rv); |
3297 | 0 | dval(&rv) += adj.d; |
3298 | | #else |
3299 | | #ifdef Sudden_Underflow |
3300 | | if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { |
3301 | | dval(&rv0) = dval(&rv); |
3302 | | word0(&rv) += P*Exp_msk1; |
3303 | | adj.d = aadj1 * ulp(&rv); |
3304 | | dval(&rv) += adj.d; |
3305 | | #ifdef IBM |
3306 | | if ((word0(&rv) & Exp_mask) < P*Exp_msk1) |
3307 | | #else |
3308 | | if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) |
3309 | | #endif |
3310 | | { |
3311 | | if (word0(&rv0) == Tiny0 |
3312 | | && word1(&rv0) == Tiny1) { |
3313 | | if (bc.nd >nd) { |
3314 | | bc.uflchk = 1; |
3315 | | break; |
3316 | | } |
3317 | | goto undfl; |
3318 | | } |
3319 | | word0(&rv) = Tiny0; |
3320 | | word1(&rv) = Tiny1; |
3321 | | goto cont; |
3322 | | } |
3323 | | else |
3324 | | word0(&rv) -= P*Exp_msk1; |
3325 | | } |
3326 | | else { |
3327 | | adj.d = aadj1 * ulp(&rv); |
3328 | | dval(&rv) += adj.d; |
3329 | | } |
3330 | | #else /*Sudden_Underflow*/ |
3331 | | /* Compute adj so that the IEEE rounding rules will |
3332 | | * correctly round rv + adj in some half-way cases. |
3333 | | * If rv * ulp(rv) is denormalized (i.e., |
3334 | | * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid |
3335 | | * trouble from bits lost to denormalization; |
3336 | | * example: 1.2e-307 . |
3337 | | */ |
3338 | | if (y <= (P-1)*Exp_msk1 && aadj > 1.) { |
3339 | | aadj1 = (double)(int)(aadj + 0.5); |
3340 | | if (!bc.dsign) |
3341 | | aadj1 = -aadj1; |
3342 | | } |
3343 | | adj.d = aadj1 * ulp(&rv); |
3344 | | dval(&rv) += adj.d; |
3345 | | #endif /*Sudden_Underflow*/ |
3346 | | #endif /*Avoid_Underflow*/ |
3347 | 0 | } |
3348 | 0 | z = word0(&rv) & Exp_mask; |
3349 | 0 | #ifndef SET_INEXACT |
3350 | 0 | if (bc.nd == nd) { |
3351 | 0 | #ifdef Avoid_Underflow |
3352 | 0 | if (!bc.scale) |
3353 | 0 | #endif |
3354 | 0 | if (y == z) { |
3355 | | /* Can we stop now? */ |
3356 | 0 | L = (Long)aadj; |
3357 | 0 | aadj -= L; |
3358 | | /* The tolerances below are conservative. */ |
3359 | 0 | if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) { |
3360 | 0 | if (aadj < .4999999 || aadj > .5000001) |
3361 | 0 | break; |
3362 | 0 | } |
3363 | 0 | else if (aadj < .4999999/FLT_RADIX) |
3364 | 0 | break; |
3365 | 0 | } |
3366 | 0 | } |
3367 | 0 | #endif |
3368 | 0 | cont: |
3369 | 0 | Bfree(bb); |
3370 | 0 | Bfree(bd); |
3371 | 0 | Bfree(bs); |
3372 | 0 | Bfree(delta); |
3373 | 0 | } |
3374 | 0 | Bfree(bb); |
3375 | 0 | Bfree(bd); |
3376 | 0 | Bfree(bs); |
3377 | 0 | Bfree(bd0); |
3378 | 0 | Bfree(delta); |
3379 | 0 | #ifndef NO_STRTOD_BIGCOMP |
3380 | 0 | if (bc.nd > nd) |
3381 | 0 | bigcomp(&rv, s0, &bc); |
3382 | 0 | #endif |
3383 | | #ifdef SET_INEXACT |
3384 | | if (bc.inexact) { |
3385 | | if (!oldinexact) { |
3386 | | word0(&rv0) = Exp_1 + (70 << Exp_shift); |
3387 | | word1(&rv0) = 0; |
3388 | | dval(&rv0) += 1.; |
3389 | | } |
3390 | | } |
3391 | | else if (!oldinexact) |
3392 | | clear_inexact(); |
3393 | | #endif |
3394 | 0 | #ifdef Avoid_Underflow |
3395 | 0 | if (bc.scale) { |
3396 | 0 | word0(&rv0) = Exp_1 - 2*P*Exp_msk1; |
3397 | 0 | word1(&rv0) = 0; |
3398 | 0 | dval(&rv) *= dval(&rv0); |
3399 | 0 | #ifndef NO_ERRNO |
3400 | | /* try to avoid the bug of testing an 8087 register value */ |
3401 | 0 | #ifdef IEEE_Arith |
3402 | 0 | if (!(word0(&rv) & Exp_mask)) |
3403 | | #else |
3404 | | if (word0(&rv) == 0 && word1(&rv) == 0) |
3405 | | #endif |
3406 | 0 | errno = ERANGE; |
3407 | 0 | #endif |
3408 | 0 | } |
3409 | 0 | #endif /* Avoid_Underflow */ |
3410 | | #ifdef SET_INEXACT |
3411 | | if (bc.inexact && !(word0(&rv) & Exp_mask)) { |
3412 | | /* set underflow bit */ |
3413 | | dval(&rv0) = 1e-300; |
3414 | | dval(&rv0) *= dval(&rv0); |
3415 | | } |
3416 | | #endif |
3417 | 0 | ret: |
3418 | 0 | if (se) |
3419 | 0 | *se = (char *)s; |
3420 | 0 | return sign ? -dval(&rv) : dval(&rv); |
3421 | 0 | } |
3422 | | |
3423 | | #ifndef MULTIPLE_THREADS |
3424 | | static char *dtoa_result; |
3425 | | #endif |
3426 | | |
3427 | | static char * |
3428 | | #ifdef KR_headers |
3429 | | rv_alloc(i) int i; |
3430 | | #else |
3431 | | rv_alloc(int i) |
3432 | | #endif |
3433 | 0 | { |
3434 | 0 | int j, k, *r; |
3435 | |
|
3436 | 0 | j = sizeof(ULong); |
3437 | 0 | for(k = 0; |
3438 | 0 | sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (size_t)i; |
3439 | 0 | j <<= 1) |
3440 | 0 | k++; |
3441 | 0 | r = (int*)Balloc(k); |
3442 | 0 | *r = k; |
3443 | 0 | return |
3444 | | #ifndef MULTIPLE_THREADS |
3445 | | dtoa_result = |
3446 | | #endif |
3447 | 0 | (char *)(r+1); |
3448 | 0 | } |
3449 | | |
3450 | | static char * |
3451 | | #ifdef KR_headers |
3452 | | nrv_alloc(s, rve, n) char *s, **rve; int n; |
3453 | | #else |
3454 | | nrv_alloc(CONST char *s, char **rve, int n) |
3455 | | #endif |
3456 | 0 | { |
3457 | 0 | char *rv, *t; |
3458 | |
|
3459 | 0 | t = rv = rv_alloc(n); |
3460 | 0 | while((*t = *s++)) t++; |
3461 | 0 | if (rve) |
3462 | 0 | *rve = t; |
3463 | 0 | return rv; |
3464 | 0 | } |
3465 | | |
3466 | | /* freedtoa(s) must be used to free values s returned by dtoa |
3467 | | * when MULTIPLE_THREADS is #defined. It should be used in all cases, |
3468 | | * but for consistency with earlier versions of dtoa, it is optional |
3469 | | * when MULTIPLE_THREADS is not defined. |
3470 | | */ |
3471 | | |
3472 | | void |
3473 | | #ifdef KR_headers |
3474 | | freedtoa(s) char *s; |
3475 | | #else |
3476 | | freedtoa(char *s) |
3477 | | #endif |
3478 | 0 | { |
3479 | 0 | Bigint *b = (Bigint *)((int *)s - 1); |
3480 | 0 | b->maxwds = 1 << (b->k = *(int*)b); |
3481 | 0 | Bfree(b); |
3482 | | #ifndef MULTIPLE_THREADS |
3483 | | if (s == dtoa_result) |
3484 | | dtoa_result = 0; |
3485 | | #endif |
3486 | 0 | } |
3487 | | |
3488 | | /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. |
3489 | | * |
3490 | | * Inspired by "How to Print Floating-Point Numbers Accurately" by |
3491 | | * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. |
3492 | | * |
3493 | | * Modifications: |
3494 | | * 1. Rather than iterating, we use a simple numeric overestimate |
3495 | | * to determine k = floor(log10(d)). We scale relevant |
3496 | | * quantities using O(log2(k)) rather than O(k) multiplications. |
3497 | | * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't |
3498 | | * try to generate digits strictly left to right. Instead, we |
3499 | | * compute with fewer bits and propagate the carry if necessary |
3500 | | * when rounding the final digit up. This is often faster. |
3501 | | * 3. Under the assumption that input will be rounded nearest, |
3502 | | * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. |
3503 | | * That is, we allow equality in stopping tests when the |
3504 | | * round-nearest rule will give the same floating-point value |
3505 | | * as would satisfaction of the stopping test with strict |
3506 | | * inequality. |
3507 | | * 4. We remove common factors of powers of 2 from relevant |
3508 | | * quantities. |
3509 | | * 5. When converting floating-point integers less than 1e16, |
3510 | | * we use floating-point arithmetic rather than resorting |
3511 | | * to multiple-precision integers. |
3512 | | * 6. When asked to produce fewer than 15 digits, we first try |
3513 | | * to get by with floating-point arithmetic; we resort to |
3514 | | * multiple-precision integer arithmetic only if we cannot |
3515 | | * guarantee that the floating-point calculation has given |
3516 | | * the correctly rounded result. For k requested digits and |
3517 | | * "uniformly" distributed input, the probability is |
3518 | | * something like 10^(k-15) that we must resort to the Long |
3519 | | * calculation. |
3520 | | */ |
3521 | | |
3522 | | char * |
3523 | | dtoa |
3524 | | #ifdef KR_headers |
3525 | | (dd, mode, ndigits, decpt, sign, rve) |
3526 | | double dd; int mode, ndigits, *decpt, *sign; char **rve; |
3527 | | #else |
3528 | | (double dd, int mode, int ndigits, int *decpt, int *sign, char **rve) |
3529 | | #endif |
3530 | 0 | { |
3531 | | /* Arguments ndigits, decpt, sign are similar to those |
3532 | | of ecvt and fcvt; trailing zeros are suppressed from |
3533 | | the returned string. If not null, *rve is set to point |
3534 | | to the end of the return value. If d is +-Infinity or NaN, |
3535 | | then *decpt is set to 9999. |
3536 | | |
3537 | | mode: |
3538 | | 0 ==> shortest string that yields d when read in |
3539 | | and rounded to nearest. |
3540 | | 1 ==> like 0, but with Steele & White stopping rule; |
3541 | | e.g. with IEEE P754 arithmetic , mode 0 gives |
3542 | | 1e23 whereas mode 1 gives 9.999999999999999e22. |
3543 | | 2 ==> max(1,ndigits) significant digits. This gives a |
3544 | | return value similar to that of ecvt, except |
3545 | | that trailing zeros are suppressed. |
3546 | | 3 ==> through ndigits past the decimal point. This |
3547 | | gives a return value similar to that from fcvt, |
3548 | | except that trailing zeros are suppressed, and |
3549 | | ndigits can be negative. |
3550 | | 4,5 ==> similar to 2 and 3, respectively, but (in |
3551 | | round-nearest mode) with the tests of mode 0 to |
3552 | | possibly return a shorter string that rounds to d. |
3553 | | With IEEE arithmetic and compilation with |
3554 | | -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same |
3555 | | as modes 2 and 3 when FLT_ROUNDS != 1. |
3556 | | 6-9 ==> Debugging modes similar to mode - 4: don't try |
3557 | | fast floating-point estimate (if applicable). |
3558 | | |
3559 | | Values of mode other than 0-9 are treated as mode 0. |
3560 | | |
3561 | | Sufficient space is allocated to the return value |
3562 | | to hold the suppressed trailing zeros. |
3563 | | */ |
3564 | |
|
3565 | 0 | int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, |
3566 | 0 | j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, |
3567 | 0 | spec_case, try_quick; |
3568 | 0 | Long L; |
3569 | 0 | #ifndef Sudden_Underflow |
3570 | 0 | int denorm; |
3571 | 0 | ULong x; |
3572 | 0 | #endif |
3573 | 0 | Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S; |
3574 | 0 | U d2, eps, u; |
3575 | 0 | double ds; |
3576 | 0 | char *s, *s0; |
3577 | | #ifdef SET_INEXACT |
3578 | | int inexact, oldinexact; |
3579 | | #endif |
3580 | | #ifdef Honor_FLT_ROUNDS /*{*/ |
3581 | | int Rounding; |
3582 | | #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ |
3583 | | Rounding = Flt_Rounds; |
3584 | | #else /*}{*/ |
3585 | | Rounding = 1; |
3586 | | switch(fegetround()) { |
3587 | | case FE_TOWARDZERO: Rounding = 0; break; |
3588 | | case FE_UPWARD: Rounding = 2; break; |
3589 | | case FE_DOWNWARD: Rounding = 3; |
3590 | | } |
3591 | | #endif /*}}*/ |
3592 | | #endif /*}*/ |
3593 | |
|
3594 | | #ifndef MULTIPLE_THREADS |
3595 | | if (dtoa_result) { |
3596 | | freedtoa(dtoa_result); |
3597 | | dtoa_result = 0; |
3598 | | } |
3599 | | #endif |
3600 | |
|
3601 | 0 | u.d = dd; |
3602 | 0 | if (word0(&u) & Sign_bit) { |
3603 | | /* set sign for everything, including 0's and NaNs */ |
3604 | 0 | *sign = 1; |
3605 | 0 | word0(&u) &= ~Sign_bit; /* clear sign bit */ |
3606 | 0 | } |
3607 | 0 | else |
3608 | 0 | *sign = 0; |
3609 | |
|
3610 | 0 | #if defined(IEEE_Arith) + defined(VAX) |
3611 | 0 | #ifdef IEEE_Arith |
3612 | 0 | if ((word0(&u) & Exp_mask) == Exp_mask) |
3613 | | #else |
3614 | | if (word0(&u) == 0x8000) |
3615 | | #endif |
3616 | 0 | { |
3617 | | /* Infinity or NaN */ |
3618 | 0 | *decpt = 9999; |
3619 | 0 | #ifdef IEEE_Arith |
3620 | 0 | if (!word1(&u) && !(word0(&u) & 0xfffff)) |
3621 | 0 | return nrv_alloc("Infinity", rve, 8); |
3622 | 0 | #endif |
3623 | 0 | return nrv_alloc("NaN", rve, 3); |
3624 | 0 | } |
3625 | 0 | #endif |
3626 | | #ifdef IBM |
3627 | | dval(&u) += 0; /* normalize */ |
3628 | | #endif |
3629 | 0 | if (!dval(&u)) { |
3630 | 0 | *decpt = 1; |
3631 | 0 | return nrv_alloc("0", rve, 1); |
3632 | 0 | } |
3633 | | |
3634 | | #ifdef SET_INEXACT |
3635 | | try_quick = oldinexact = get_inexact(); |
3636 | | inexact = 1; |
3637 | | #endif |
3638 | | #ifdef Honor_FLT_ROUNDS |
3639 | | if (Rounding >= 2) { |
3640 | | if (*sign) |
3641 | | Rounding = Rounding == 2 ? 0 : 2; |
3642 | | else |
3643 | | if (Rounding != 2) |
3644 | | Rounding = 0; |
3645 | | } |
3646 | | #endif |
3647 | | |
3648 | 0 | b = d2b(&u, &be, &bbits); |
3649 | | #ifdef Sudden_Underflow |
3650 | | i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); |
3651 | | #else |
3652 | 0 | if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) { |
3653 | 0 | #endif |
3654 | 0 | dval(&d2) = dval(&u); |
3655 | 0 | word0(&d2) &= Frac_mask1; |
3656 | 0 | word0(&d2) |= Exp_11; |
3657 | | #ifdef IBM |
3658 | | if (j = 11 - hi0bits(word0(&d2) & Frac_mask)) |
3659 | | dval(&d2) /= 1 << j; |
3660 | | #endif |
3661 | | |
3662 | | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 |
3663 | | * log10(x) = log(x) / log(10) |
3664 | | * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) |
3665 | | * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) |
3666 | | * |
3667 | | * This suggests computing an approximation k to log10(d) by |
3668 | | * |
3669 | | * k = (i - Bias)*0.301029995663981 |
3670 | | * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); |
3671 | | * |
3672 | | * We want k to be too large rather than too small. |
3673 | | * The error in the first-order Taylor series approximation |
3674 | | * is in our favor, so we just round up the constant enough |
3675 | | * to compensate for any error in the multiplication of |
3676 | | * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, |
3677 | | * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, |
3678 | | * adding 1e-13 to the constant term more than suffices. |
3679 | | * Hence we adjust the constant term to 0.1760912590558. |
3680 | | * (We could get a more accurate k by invoking log10, |
3681 | | * but this is probably not worthwhile.) |
3682 | | */ |
3683 | |
|
3684 | 0 | i -= Bias; |
3685 | | #ifdef IBM |
3686 | | i <<= 2; |
3687 | | i += j; |
3688 | | #endif |
3689 | 0 | #ifndef Sudden_Underflow |
3690 | 0 | denorm = 0; |
3691 | 0 | } |
3692 | 0 | else { |
3693 | | /* d is denormalized */ |
3694 | |
|
3695 | 0 | i = bbits + be + (Bias + (P-1) - 1); |
3696 | 0 | x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32) |
3697 | 0 | : word1(&u) << (32 - i); |
3698 | 0 | dval(&d2) = x; |
3699 | 0 | word0(&d2) -= 31*Exp_msk1; /* adjust exponent */ |
3700 | 0 | i -= (Bias + (P-1) - 1) + 1; |
3701 | 0 | denorm = 1; |
3702 | 0 | } |
3703 | 0 | #endif |
3704 | 0 | ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; |
3705 | 0 | k = (int)ds; |
3706 | 0 | if (ds < 0. && ds != k) |
3707 | 0 | k--; /* want k = floor(ds) */ |
3708 | 0 | k_check = 1; |
3709 | 0 | if (k >= 0 && k <= Ten_pmax) { |
3710 | 0 | if (dval(&u) < tens[k]) |
3711 | 0 | k--; |
3712 | 0 | k_check = 0; |
3713 | 0 | } |
3714 | 0 | j = bbits - i - 1; |
3715 | 0 | if (j >= 0) { |
3716 | 0 | b2 = 0; |
3717 | 0 | s2 = j; |
3718 | 0 | } |
3719 | 0 | else { |
3720 | 0 | b2 = -j; |
3721 | 0 | s2 = 0; |
3722 | 0 | } |
3723 | 0 | if (k >= 0) { |
3724 | 0 | b5 = 0; |
3725 | 0 | s5 = k; |
3726 | 0 | s2 += k; |
3727 | 0 | } |
3728 | 0 | else { |
3729 | 0 | b2 -= k; |
3730 | 0 | b5 = -k; |
3731 | 0 | s5 = 0; |
3732 | 0 | } |
3733 | 0 | if (mode < 0 || mode > 9) |
3734 | 0 | mode = 0; |
3735 | |
|
3736 | 0 | #ifndef SET_INEXACT |
3737 | | #ifdef Check_FLT_ROUNDS |
3738 | | try_quick = Rounding == 1; |
3739 | | #else |
3740 | 0 | try_quick = 1; |
3741 | 0 | #endif |
3742 | 0 | #endif /*SET_INEXACT*/ |
3743 | |
|
3744 | 0 | if (mode > 5) { |
3745 | 0 | mode -= 4; |
3746 | 0 | try_quick = 0; |
3747 | 0 | } |
3748 | 0 | leftright = 1; |
3749 | 0 | ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */ |
3750 | | /* silence erroneous "gcc -Wall" warning. */ |
3751 | 0 | switch(mode) { |
3752 | 0 | case 0: |
3753 | 0 | case 1: |
3754 | 0 | i = 18; |
3755 | 0 | ndigits = 0; |
3756 | 0 | break; |
3757 | 0 | case 2: |
3758 | 0 | leftright = 0; |
3759 | | // fall through |
3760 | 0 | case 4: |
3761 | 0 | if (ndigits <= 0) |
3762 | 0 | ndigits = 1; |
3763 | 0 | ilim = ilim1 = i = ndigits; |
3764 | 0 | break; |
3765 | 0 | case 3: |
3766 | 0 | leftright = 0; |
3767 | | // fall through |
3768 | 0 | case 5: |
3769 | 0 | i = ndigits + k + 1; |
3770 | 0 | ilim = i; |
3771 | 0 | ilim1 = i - 1; |
3772 | 0 | if (i <= 0) |
3773 | 0 | i = 1; |
3774 | 0 | } |
3775 | 0 | s = s0 = rv_alloc(i); |
3776 | |
|
3777 | | #ifdef Honor_FLT_ROUNDS |
3778 | | if (mode > 1 && Rounding != 1) |
3779 | | leftright = 0; |
3780 | | #endif |
3781 | |
|
3782 | 0 | if (ilim >= 0 && ilim <= Quick_max && try_quick) { |
3783 | | |
3784 | | /* Try to get by with floating-point arithmetic. */ |
3785 | |
|
3786 | 0 | i = 0; |
3787 | 0 | dval(&d2) = dval(&u); |
3788 | 0 | k0 = k; |
3789 | 0 | ilim0 = ilim; |
3790 | 0 | ieps = 2; /* conservative */ |
3791 | 0 | if (k > 0) { |
3792 | 0 | ds = tens[k&0xf]; |
3793 | 0 | j = k >> 4; |
3794 | 0 | if (j & Bletch) { |
3795 | | /* prevent overflows */ |
3796 | 0 | j &= Bletch - 1; |
3797 | 0 | dval(&u) /= bigtens[n_bigtens-1]; |
3798 | 0 | ieps++; |
3799 | 0 | } |
3800 | 0 | for(; j; j >>= 1, i++) |
3801 | 0 | if (j & 1) { |
3802 | 0 | ieps++; |
3803 | 0 | ds *= bigtens[i]; |
3804 | 0 | } |
3805 | 0 | dval(&u) /= ds; |
3806 | 0 | } |
3807 | 0 | else if ((j1 = -k)) { |
3808 | 0 | dval(&u) *= tens[j1 & 0xf]; |
3809 | 0 | for(j = j1 >> 4; j; j >>= 1, i++) |
3810 | 0 | if (j & 1) { |
3811 | 0 | ieps++; |
3812 | 0 | dval(&u) *= bigtens[i]; |
3813 | 0 | } |
3814 | 0 | } |
3815 | 0 | if (k_check && dval(&u) < 1. && ilim > 0) { |
3816 | 0 | if (ilim1 <= 0) |
3817 | 0 | goto fast_failed; |
3818 | 0 | ilim = ilim1; |
3819 | 0 | k--; |
3820 | 0 | dval(&u) *= 10.; |
3821 | 0 | ieps++; |
3822 | 0 | } |
3823 | 0 | dval(&eps) = ieps*dval(&u) + 7.; |
3824 | 0 | word0(&eps) -= (P-1)*Exp_msk1; |
3825 | 0 | if (ilim == 0) { |
3826 | 0 | S = mhi = 0; |
3827 | 0 | dval(&u) -= 5.; |
3828 | 0 | if (dval(&u) > dval(&eps)) |
3829 | 0 | goto one_digit; |
3830 | 0 | if (dval(&u) < -dval(&eps)) |
3831 | 0 | goto no_digits; |
3832 | 0 | goto fast_failed; |
3833 | 0 | } |
3834 | 0 | #ifndef No_leftright |
3835 | 0 | if (leftright) { |
3836 | | /* Use Steele & White method of only |
3837 | | * generating digits needed. |
3838 | | */ |
3839 | 0 | dval(&eps) = 0.5/tens[ilim-1] - dval(&eps); |
3840 | 0 | for(i = 0;;) { |
3841 | 0 | L = (Long)dval(&u); |
3842 | 0 | dval(&u) -= L; |
3843 | 0 | *s++ = '0' + (int)L; |
3844 | 0 | if (dval(&u) < dval(&eps)) |
3845 | 0 | goto ret1; |
3846 | 0 | if (1. - dval(&u) < dval(&eps)) |
3847 | 0 | goto bump_up; |
3848 | 0 | if (++i >= ilim) |
3849 | 0 | break; |
3850 | 0 | dval(&eps) *= 10.; |
3851 | 0 | dval(&u) *= 10.; |
3852 | 0 | } |
3853 | 0 | } |
3854 | 0 | else { |
3855 | 0 | #endif |
3856 | | /* Generate ilim digits, then fix them up. */ |
3857 | 0 | dval(&eps) *= tens[ilim-1]; |
3858 | 0 | for(i = 1;; i++, dval(&u) *= 10.) { |
3859 | 0 | L = (Long)(dval(&u)); |
3860 | 0 | if (!(dval(&u) -= L)) |
3861 | 0 | ilim = i; |
3862 | 0 | *s++ = '0' + (int)L; |
3863 | 0 | if (i == ilim) { |
3864 | 0 | if (dval(&u) > 0.5 + dval(&eps)) |
3865 | 0 | goto bump_up; |
3866 | 0 | else if (dval(&u) < 0.5 - dval(&eps)) { |
3867 | 0 | while(*--s == '0') {} |
3868 | 0 | s++; |
3869 | 0 | goto ret1; |
3870 | 0 | } |
3871 | 0 | break; |
3872 | 0 | } |
3873 | 0 | } |
3874 | 0 | #ifndef No_leftright |
3875 | 0 | } |
3876 | 0 | #endif |
3877 | 0 | fast_failed: |
3878 | 0 | s = s0; |
3879 | 0 | dval(&u) = dval(&d2); |
3880 | 0 | k = k0; |
3881 | 0 | ilim = ilim0; |
3882 | 0 | } |
3883 | | |
3884 | | /* Do we have a "small" integer? */ |
3885 | | |
3886 | 0 | if (be >= 0 && k <= Int_max) { |
3887 | | /* Yes. */ |
3888 | 0 | ds = tens[k]; |
3889 | 0 | if (ndigits < 0 && ilim <= 0) { |
3890 | 0 | S = mhi = 0; |
3891 | 0 | if (ilim < 0 || dval(&u) <= 5*ds) |
3892 | 0 | goto no_digits; |
3893 | 0 | goto one_digit; |
3894 | 0 | } |
3895 | 0 | for(i = 1; i <= k + 1; i++, dval(&u) *= 10.) { |
3896 | 0 | L = (Long)(dval(&u) / ds); |
3897 | 0 | dval(&u) -= L*ds; |
3898 | | #ifdef Check_FLT_ROUNDS |
3899 | | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ |
3900 | | if (dval(&u) < 0) { |
3901 | | L--; |
3902 | | dval(&u) += ds; |
3903 | | } |
3904 | | #endif |
3905 | 0 | *s++ = '0' + (int)L; |
3906 | 0 | if (!dval(&u)) { |
3907 | | #ifdef SET_INEXACT |
3908 | | inexact = 0; |
3909 | | #endif |
3910 | 0 | break; |
3911 | 0 | } |
3912 | 0 | if (i == ilim) { |
3913 | | #ifdef Honor_FLT_ROUNDS |
3914 | | if (mode > 1) |
3915 | | switch(Rounding) { |
3916 | | case 0: goto ret1; |
3917 | | case 2: goto bump_up; |
3918 | | } |
3919 | | #endif |
3920 | 0 | dval(&u) += dval(&u); |
3921 | 0 | if (dval(&u) > ds || (dval(&u) == ds && L & 1)) { |
3922 | 0 | bump_up: |
3923 | 0 | while(*--s == '9') |
3924 | 0 | if (s == s0) { |
3925 | 0 | k++; |
3926 | 0 | *s = '0'; |
3927 | 0 | break; |
3928 | 0 | } |
3929 | 0 | ++*s++; |
3930 | 0 | } |
3931 | 0 | break; |
3932 | 0 | } |
3933 | 0 | } |
3934 | 0 | goto ret1; |
3935 | 0 | } |
3936 | | |
3937 | 0 | m2 = b2; |
3938 | 0 | m5 = b5; |
3939 | 0 | mhi = mlo = 0; |
3940 | 0 | if (leftright) { |
3941 | 0 | i = |
3942 | 0 | #ifndef Sudden_Underflow |
3943 | 0 | denorm ? be + (Bias + (P-1) - 1 + 1) : |
3944 | 0 | #endif |
3945 | | #ifdef IBM |
3946 | | 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); |
3947 | | #else |
3948 | 0 | 1 + P - bbits; |
3949 | 0 | #endif |
3950 | 0 | b2 += i; |
3951 | 0 | s2 += i; |
3952 | 0 | mhi = i2b(1); |
3953 | 0 | } |
3954 | 0 | if (m2 > 0 && s2 > 0) { |
3955 | 0 | i = m2 < s2 ? m2 : s2; |
3956 | 0 | b2 -= i; |
3957 | 0 | m2 -= i; |
3958 | 0 | s2 -= i; |
3959 | 0 | } |
3960 | 0 | if (b5 > 0) { |
3961 | 0 | if (leftright) { |
3962 | 0 | if (m5 > 0) { |
3963 | 0 | mhi = pow5mult(mhi, m5); |
3964 | 0 | b1 = mult(mhi, b); |
3965 | 0 | Bfree(b); |
3966 | 0 | b = b1; |
3967 | 0 | } |
3968 | 0 | if ((j = b5 - m5)) |
3969 | 0 | b = pow5mult(b, j); |
3970 | 0 | } |
3971 | 0 | else |
3972 | 0 | b = pow5mult(b, b5); |
3973 | 0 | } |
3974 | 0 | S = i2b(1); |
3975 | 0 | if (s5 > 0) |
3976 | 0 | S = pow5mult(S, s5); |
3977 | | |
3978 | | /* Check for special case that d is a normalized power of 2. */ |
3979 | |
|
3980 | 0 | spec_case = 0; |
3981 | 0 | if ((mode < 2 || leftright) |
3982 | | #ifdef Honor_FLT_ROUNDS |
3983 | | && Rounding == 1 |
3984 | | #endif |
3985 | 0 | ) { |
3986 | 0 | if (!word1(&u) && !(word0(&u) & Bndry_mask) |
3987 | 0 | #ifndef Sudden_Underflow |
3988 | 0 | && word0(&u) & (Exp_mask & ~Exp_msk1) |
3989 | 0 | #endif |
3990 | 0 | ) { |
3991 | | /* The special case */ |
3992 | 0 | b2 += Log2P; |
3993 | 0 | s2 += Log2P; |
3994 | 0 | spec_case = 1; |
3995 | 0 | } |
3996 | 0 | } |
3997 | | |
3998 | | /* Arrange for convenient computation of quotients: |
3999 | | * shift left if necessary so divisor has 4 leading 0 bits. |
4000 | | * |
4001 | | * Perhaps we should just compute leading 28 bits of S once |
4002 | | * and for all and pass them and a shift to quorem, so it |
4003 | | * can do shifts and ors to compute the numerator for q. |
4004 | | */ |
4005 | 0 | #ifdef Pack_32 |
4006 | 0 | if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)) |
4007 | 0 | i = 32 - i; |
4008 | 0 | #define iInc 28 |
4009 | | #else |
4010 | | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) |
4011 | | i = 16 - i; |
4012 | | #define iInc 12 |
4013 | | #endif |
4014 | 0 | i = dshift(S, s2); |
4015 | 0 | b2 += i; |
4016 | 0 | m2 += i; |
4017 | 0 | s2 += i; |
4018 | 0 | if (b2 > 0) |
4019 | 0 | b = lshift(b, b2); |
4020 | 0 | if (s2 > 0) |
4021 | 0 | S = lshift(S, s2); |
4022 | 0 | if (k_check) { |
4023 | 0 | if (cmp(b,S) < 0) { |
4024 | 0 | k--; |
4025 | 0 | b = multadd(b, 10, 0); /* we botched the k estimate */ |
4026 | 0 | if (leftright) |
4027 | 0 | mhi = multadd(mhi, 10, 0); |
4028 | 0 | ilim = ilim1; |
4029 | 0 | } |
4030 | 0 | } |
4031 | 0 | if (ilim <= 0 && (mode == 3 || mode == 5)) { |
4032 | 0 | if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { |
4033 | | /* no digits, fcvt style */ |
4034 | 0 | no_digits: |
4035 | 0 | k = -1 - ndigits; |
4036 | 0 | goto ret; |
4037 | 0 | } |
4038 | 0 | one_digit: |
4039 | 0 | *s++ = '1'; |
4040 | 0 | k++; |
4041 | 0 | goto ret; |
4042 | 0 | } |
4043 | 0 | if (leftright) { |
4044 | 0 | if (m2 > 0) |
4045 | 0 | mhi = lshift(mhi, m2); |
4046 | | |
4047 | | /* Compute mlo -- check for special case |
4048 | | * that d is a normalized power of 2. |
4049 | | */ |
4050 | |
|
4051 | 0 | mlo = mhi; |
4052 | 0 | if (spec_case) { |
4053 | 0 | mhi = Balloc(mhi->k); |
4054 | 0 | Bcopy(mhi, mlo); |
4055 | 0 | mhi = lshift(mhi, Log2P); |
4056 | 0 | } |
4057 | |
|
4058 | 0 | for(i = 1;;i++) { |
4059 | 0 | dig = quorem(b,S) + '0'; |
4060 | | /* Do we yet have the shortest decimal string |
4061 | | * that will round to d? |
4062 | | */ |
4063 | 0 | j = cmp(b, mlo); |
4064 | 0 | delta = diff(S, mhi); |
4065 | 0 | j1 = delta->sign ? 1 : cmp(b, delta); |
4066 | 0 | Bfree(delta); |
4067 | 0 | #ifndef ROUND_BIASED |
4068 | 0 | if (j1 == 0 && mode != 1 && !(word1(&u) & 1) |
4069 | | #ifdef Honor_FLT_ROUNDS |
4070 | | && Rounding >= 1 |
4071 | | #endif |
4072 | 0 | ) { |
4073 | 0 | if (dig == '9') |
4074 | 0 | goto round_9_up; |
4075 | 0 | if (j > 0) |
4076 | 0 | dig++; |
4077 | | #ifdef SET_INEXACT |
4078 | | else if (!b->x[0] && b->wds <= 1) |
4079 | | inexact = 0; |
4080 | | #endif |
4081 | 0 | *s++ = dig; |
4082 | 0 | goto ret; |
4083 | 0 | } |
4084 | 0 | #endif |
4085 | 0 | if (j < 0 || (j == 0 && mode != 1 |
4086 | 0 | #ifndef ROUND_BIASED |
4087 | 0 | && !(word1(&u) & 1) |
4088 | 0 | #endif |
4089 | 0 | )) { |
4090 | 0 | if (!b->x[0] && b->wds <= 1) { |
4091 | | #ifdef SET_INEXACT |
4092 | | inexact = 0; |
4093 | | #endif |
4094 | 0 | goto accept_dig; |
4095 | 0 | } |
4096 | | #ifdef Honor_FLT_ROUNDS |
4097 | | if (mode > 1) |
4098 | | switch(Rounding) { |
4099 | | case 0: goto accept_dig; |
4100 | | case 2: goto keep_dig; |
4101 | | } |
4102 | | #endif /*Honor_FLT_ROUNDS*/ |
4103 | 0 | if (j1 > 0) { |
4104 | 0 | b = lshift(b, 1); |
4105 | 0 | j1 = cmp(b, S); |
4106 | 0 | if ((j1 > 0 || (j1 == 0 && dig & 1)) |
4107 | 0 | && dig++ == '9') |
4108 | 0 | goto round_9_up; |
4109 | 0 | } |
4110 | 0 | accept_dig: |
4111 | 0 | *s++ = dig; |
4112 | 0 | goto ret; |
4113 | 0 | } |
4114 | 0 | if (j1 > 0) { |
4115 | | #ifdef Honor_FLT_ROUNDS |
4116 | | if (!Rounding) |
4117 | | goto accept_dig; |
4118 | | #endif |
4119 | 0 | if (dig == '9') { /* possible if i == 1 */ |
4120 | 0 | round_9_up: |
4121 | 0 | *s++ = '9'; |
4122 | 0 | goto roundoff; |
4123 | 0 | } |
4124 | 0 | *s++ = dig + 1; |
4125 | 0 | goto ret; |
4126 | 0 | } |
4127 | | #ifdef Honor_FLT_ROUNDS |
4128 | | keep_dig: |
4129 | | #endif |
4130 | 0 | *s++ = dig; |
4131 | 0 | if (i == ilim) |
4132 | 0 | break; |
4133 | 0 | b = multadd(b, 10, 0); |
4134 | 0 | if (mlo == mhi) |
4135 | 0 | mlo = mhi = multadd(mhi, 10, 0); |
4136 | 0 | else { |
4137 | 0 | mlo = multadd(mlo, 10, 0); |
4138 | 0 | mhi = multadd(mhi, 10, 0); |
4139 | 0 | } |
4140 | 0 | } |
4141 | 0 | } |
4142 | 0 | else |
4143 | 0 | for(i = 1;; i++) { |
4144 | 0 | *s++ = dig = quorem(b,S) + '0'; |
4145 | 0 | if (!b->x[0] && b->wds <= 1) { |
4146 | | #ifdef SET_INEXACT |
4147 | | inexact = 0; |
4148 | | #endif |
4149 | 0 | goto ret; |
4150 | 0 | } |
4151 | 0 | if (i >= ilim) |
4152 | 0 | break; |
4153 | 0 | b = multadd(b, 10, 0); |
4154 | 0 | } |
4155 | | |
4156 | | /* Round off last digit */ |
4157 | | |
4158 | | #ifdef Honor_FLT_ROUNDS |
4159 | | switch(Rounding) { |
4160 | | case 0: goto trimzeros; |
4161 | | case 2: goto roundoff; |
4162 | | } |
4163 | | #endif |
4164 | 0 | b = lshift(b, 1); |
4165 | 0 | j = cmp(b, S); |
4166 | 0 | if (j > 0 || (j == 0 && dig & 1)) { |
4167 | 0 | roundoff: |
4168 | 0 | while(*--s == '9') |
4169 | 0 | if (s == s0) { |
4170 | 0 | k++; |
4171 | 0 | *s++ = '1'; |
4172 | 0 | goto ret; |
4173 | 0 | } |
4174 | 0 | ++*s++; |
4175 | 0 | } |
4176 | 0 | else { |
4177 | | #ifdef Honor_FLT_ROUNDS |
4178 | | trimzeros: |
4179 | | #endif |
4180 | 0 | while(*--s == '0') {} |
4181 | 0 | s++; |
4182 | 0 | } |
4183 | 0 | ret: |
4184 | 0 | Bfree(S); |
4185 | 0 | if (mhi) { |
4186 | 0 | if (mlo && mlo != mhi) |
4187 | 0 | Bfree(mlo); |
4188 | 0 | Bfree(mhi); |
4189 | 0 | } |
4190 | 0 | ret1: |
4191 | | #ifdef SET_INEXACT |
4192 | | if (inexact) { |
4193 | | if (!oldinexact) { |
4194 | | word0(&u) = Exp_1 + (70 << Exp_shift); |
4195 | | word1(&u) = 0; |
4196 | | dval(&u) += 1.; |
4197 | | } |
4198 | | } |
4199 | | else if (!oldinexact) |
4200 | | clear_inexact(); |
4201 | | #endif |
4202 | 0 | Bfree(b); |
4203 | 0 | *s = 0; |
4204 | 0 | *decpt = k + 1; |
4205 | 0 | if (rve) |
4206 | 0 | *rve = s; |
4207 | 0 | return s0; |
4208 | 0 | } |
4209 | | |
4210 | | } // namespace dmg_fp |