Debian Codegraph @

common/strtodg.c

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