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