blob: 95d0e19219531717301933f3285e85ba7c7038c0 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $NetBSD: strtod.c,v 1.45.2.1 2005/04/19 13:35:54 tron Exp $ */
2
3/****************************************************************
4 *
5 * The author of this software is David M. Gay.
6 *
7 * Copyright (c) 1991 by AT&T.
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose without fee is hereby granted, provided that this entire notice
11 * is included in all copies of any software which is or includes a copy
12 * or modification of this software and in all copies of the supporting
13 * documentation for such software.
14 *
15 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
17 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
18 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
19 *
20 ***************************************************************/
21
22/* Please send bug reports to
23 David M. Gay
24 AT&T Bell Laboratories, Room 2C-463
25 600 Mountain Avenue
26 Murray Hill, NJ 07974-2070
27 U.S.A.
28 dmg@research.att.com or research!dmg
29 */
30
31/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
32 *
33 * This strtod returns a nearest machine number to the input decimal
34 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
35 * broken by the IEEE round-even rule. Otherwise ties are broken by
36 * biased rounding (add half and chop).
37 *
38 * Inspired loosely by William D. Clinger's paper "How to Read Floating
39 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
40 *
41 * Modifications:
42 *
43 * 1. We only require IEEE, IBM, or VAX double-precision
44 * arithmetic (not IEEE double-extended).
45 * 2. We get by with floating-point arithmetic in a case that
46 * Clinger missed -- when we're computing d * 10^n
47 * for a small integer d and the integer n is not too
48 * much larger than 22 (the maximum integer k for which
49 * we can represent 10^k exactly), we may be able to
50 * compute (d*10^k) * 10^(e-k) with just one roundoff.
51 * 3. Rather than a bit-at-a-time adjustment of the binary
52 * result in the hard case, we use floating-point
53 * arithmetic to determine the adjustment to within
54 * one bit; only in really hard cases do we need to
55 * compute a second residual.
56 * 4. Because of 3., we don't need a large table of powers of 10
57 * for ten-to-e (just some small tables, e.g. of 10^k
58 * for 0 <= k <= 22).
59 */
60
61/*
62 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
63 * significant byte has the lowest address.
64 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
65 * significant byte has the lowest address.
66 * #define Long int on machines with 32-bit ints and 64-bit longs.
67 * #define Sudden_Underflow for IEEE-format machines without gradual
68 * underflow (i.e., that flush to zero on underflow).
69 * #define IBM for IBM mainframe-style floating-point arithmetic.
70 * #define VAX for VAX-style floating-point arithmetic.
71 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
72 * #define No_leftright to omit left-right logic in fast floating-point
73 * computation of dtoa.
74 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
75 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
76 * that use extended-precision instructions to compute rounded
77 * products and quotients) with IBM.
78 * #define ROUND_BIASED for IEEE-format with biased rounding.
79 * #define Inaccurate_Divide for IEEE-format with correctly rounded
80 * products but inaccurate quotients, e.g., for Intel i860.
81 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
82 * integer arithmetic. Whether this speeds things up or slows things
83 * down depends on the machine and the number being converted.
84 * #define KR_headers for old-style C function headers.
85 * #define Bad_float_h if your system lacks a float.h or if it does not
86 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
87 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
88 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
89 * if memory is available and otherwise does something you deem
90 * appropriate. If MALLOC is undefined, malloc will be invoked
91 * directly -- and assumed always to succeed.
92 */
93
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094#include <sys/cdefs.h>
95#if defined(LIBC_SCCS) && !defined(lint)
96__RCSID("$NetBSD: strtod.c,v 1.45.2.1 2005/04/19 13:35:54 tron Exp $");
97#endif /* LIBC_SCCS and not lint */
98
99#define Unsigned_Shifts
100#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
101 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
102 defined(__powerpc__) || defined(__sh__) || defined(__x86_64__) || \
103 defined(__hppa__) || \
Serban Constantinescu65ca2822013-10-08 19:32:36 +0100104 (defined(__arm__) && defined(__VFP_FP__)) || defined(__aarch64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#include <endian.h>
106#if BYTE_ORDER == BIG_ENDIAN
107#define IEEE_BIG_ENDIAN
108#else
109#define IEEE_LITTLE_ENDIAN
110#endif
111#endif
112
113#if defined(__arm__) && !defined(__VFP_FP__)
114/*
115 * Although the CPU is little endian the FP has different
116 * byte and word endianness. The byte order is still little endian
117 * but the word order is big endian.
118 */
119#define IEEE_BIG_ENDIAN
120#endif
121
122#ifdef __vax__
123#define VAX
124#endif
125
126#if defined(__hppa__) || defined(__mips__) || defined(__sh__)
127#define NAN_WORD0 0x7ff40000
128#else
129#define NAN_WORD0 0x7ff80000
130#endif
131#define NAN_WORD1 0
132
133#define Long int32_t
134#define ULong u_int32_t
135
136#ifdef DEBUG
137#include "stdio.h"
138#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
139#endif
140
141#ifdef __cplusplus
142#include "malloc.h"
143#include "memory.h"
144#else
145#ifndef KR_headers
146#include "stdlib.h"
147#include "string.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148#include "locale.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149#else
150#include "malloc.h"
151#include "memory.h"
152#endif
153#endif
Elliott Hughes205c7882014-03-13 16:17:43 -0700154#include "../upstream-netbsd/extern.h" /* Android-changed. */
155#include "../upstream-netbsd/reentrant.h" /* Android-changed. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
157#ifdef MALLOC
158#ifdef KR_headers
159extern char *MALLOC();
160#else
161extern void *MALLOC(size_t);
162#endif
163#else
164#define MALLOC malloc
165#endif
166
167#include "ctype.h"
168#include "errno.h"
169#include "float.h"
170
171#ifndef __MATH_H__
172#include "math.h"
173#endif
174
175#ifdef __cplusplus
176extern "C" {
177#endif
178
179#ifndef CONST
180#ifdef KR_headers
181#define CONST /* blank */
182#else
183#define CONST const
184#endif
185#endif
186
187#ifdef Unsigned_Shifts
188#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
189#else
190#define Sign_Extend(a,b) /*no-op*/
191#endif
192
193#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
194 defined(IBM) != 1
195Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
196IBM should be defined.
197#endif
198
199typedef union {
200 double d;
201 ULong ul[2];
202} _double;
203#define value(x) ((x).d)
204#ifdef IEEE_LITTLE_ENDIAN
205#define word0(x) ((x).ul[1])
206#define word1(x) ((x).ul[0])
207#else
208#define word0(x) ((x).ul[0])
209#define word1(x) ((x).ul[1])
210#endif
211
212/* The following definition of Storeinc is appropriate for MIPS processors.
213 * An alternative that might be better on some machines is
214 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
215 */
216#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
217#define Storeinc(a,b,c) \
218 (((u_short *)(void *)a)[1] = \
219 (u_short)b, ((u_short *)(void *)a)[0] = (u_short)c, a++)
220#else
221#define Storeinc(a,b,c) \
222 (((u_short *)(void *)a)[0] = \
223 (u_short)b, ((u_short *)(void *)a)[1] = (u_short)c, a++)
224#endif
225
226/* #define P DBL_MANT_DIG */
227/* Ten_pmax = floor(P*log(2)/log(5)) */
228/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
229/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
230/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
231
232#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
233#define Exp_shift 20
234#define Exp_shift1 20
235#define Exp_msk1 0x100000
236#define Exp_msk11 0x100000
237#define Exp_mask 0x7ff00000
238#define P 53
239#define Bias 1023
240#define IEEE_Arith
241#define Emin (-1022)
242#define Exp_1 0x3ff00000
243#define Exp_11 0x3ff00000
244#define Ebits 11
245#define Frac_mask 0xfffff
246#define Frac_mask1 0xfffff
247#define Ten_pmax 22
248#define Bletch 0x10
249#define Bndry_mask 0xfffff
250#define Bndry_mask1 0xfffff
251#define LSB 1
252#define Sign_bit 0x80000000
253#define Log2P 1
254#define Tiny0 0
255#define Tiny1 1
256#define Quick_max 14
257#define Int_max 14
258#define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
259#else
260#undef Sudden_Underflow
261#define Sudden_Underflow
262#ifdef IBM
263#define Exp_shift 24
264#define Exp_shift1 24
265#define Exp_msk1 0x1000000
266#define Exp_msk11 0x1000000
267#define Exp_mask 0x7f000000
268#define P 14
269#define Bias 65
270#define Exp_1 0x41000000
271#define Exp_11 0x41000000
272#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
273#define Frac_mask 0xffffff
274#define Frac_mask1 0xffffff
275#define Bletch 4
276#define Ten_pmax 22
277#define Bndry_mask 0xefffff
278#define Bndry_mask1 0xffffff
279#define LSB 1
280#define Sign_bit 0x80000000
281#define Log2P 4
282#define Tiny0 0x100000
283#define Tiny1 0
284#define Quick_max 14
285#define Int_max 15
286#else /* VAX */
287#define Exp_shift 23
288#define Exp_shift1 7
289#define Exp_msk1 0x80
290#define Exp_msk11 0x800000
291#define Exp_mask 0x7f80
292#define P 56
293#define Bias 129
294#define Exp_1 0x40800000
295#define Exp_11 0x4080
296#define Ebits 8
297#define Frac_mask 0x7fffff
298#define Frac_mask1 0xffff007f
299#define Ten_pmax 24
300#define Bletch 2
301#define Bndry_mask 0xffff007f
302#define Bndry_mask1 0xffff007f
303#define LSB 0x10000
304#define Sign_bit 0x8000
305#define Log2P 1
306#define Tiny0 0x80
307#define Tiny1 0
308#define Quick_max 15
309#define Int_max 15
310#endif
311#endif
312
313#ifndef IEEE_Arith
314#define ROUND_BIASED
315#endif
316
317#ifdef RND_PRODQUOT
318#define rounded_product(a,b) a = rnd_prod(a, b)
319#define rounded_quotient(a,b) a = rnd_quot(a, b)
320#ifdef KR_headers
321extern double rnd_prod(), rnd_quot();
322#else
323extern double rnd_prod(double, double), rnd_quot(double, double);
324#endif
325#else
326#define rounded_product(a,b) a *= b
327#define rounded_quotient(a,b) a /= b
328#endif
329
330#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
331#define Big1 0xffffffff
332
333#ifndef Just_16
334/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
335 * This makes some inner loops simpler and sometimes saves work
336 * during multiplications, but it often seems to make things slightly
337 * slower. Hence the default is now to store 32 bits per Long.
338 */
339#ifndef Pack_32
340#define Pack_32
341#endif
342#endif
343
344#define Kmax 15
345
346#ifdef __cplusplus
347extern "C" double strtod(const char *s00, char **se);
348extern "C" char *__dtoa(double d, int mode, int ndigits,
349 int *decpt, int *sign, char **rve);
350#endif
351
352 struct
353Bigint {
354 struct Bigint *next;
355 int k, maxwds, sign, wds;
356 ULong x[1];
André Goddard Rosae7347692010-02-05 18:32:52 -0200357};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358
359 typedef struct Bigint Bigint;
360
361 static Bigint *freelist[Kmax+1];
362
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363#ifdef _REENTRANT
364 static mutex_t freelist_mutex = MUTEX_INITIALIZER;
365#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366
David 'Digit' Turner81326262010-03-04 11:51:42 -0800367/* Special value used to indicate an invalid Bigint value,
368 * e.g. when a memory allocation fails. The idea is that we
369 * want to avoid introducing NULL checks everytime a bigint
370 * computation is performed. Also the NULL value can also be
371 * already used to indicate "value not initialized yet" and
372 * returning NULL might alter the execution code path in
373 * case of OOM.
374 */
375#define BIGINT_INVALID ((Bigint *)&bigint_invalid_value)
376
377static const Bigint bigint_invalid_value;
378
379
380/* Return BIGINT_INVALID on allocation failure.
381 *
382 * Most of the code here depends on the fact that this function
383 * never returns NULL.
384 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 static Bigint *
386Balloc
387#ifdef KR_headers
388 (k) int k;
389#else
390 (int k)
391#endif
392{
393 int x;
394 Bigint *rv;
395
396 mutex_lock(&freelist_mutex);
397
398 if ((rv = freelist[k]) != NULL) {
399 freelist[k] = rv->next;
André Goddard Rosae7347692010-02-05 18:32:52 -0200400 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 else {
402 x = 1 << k;
403 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
David 'Digit' Turner81326262010-03-04 11:51:42 -0800404 if (rv == NULL) {
405 rv = BIGINT_INVALID;
406 goto EXIT;
407 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408 rv->k = k;
409 rv->maxwds = x;
André Goddard Rosae7347692010-02-05 18:32:52 -0200410 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 rv->sign = rv->wds = 0;
David 'Digit' Turner81326262010-03-04 11:51:42 -0800412EXIT:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413 mutex_unlock(&freelist_mutex);
414
415 return rv;
André Goddard Rosae7347692010-02-05 18:32:52 -0200416}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417
418 static void
419Bfree
420#ifdef KR_headers
421 (v) Bigint *v;
422#else
423 (Bigint *v)
424#endif
425{
David 'Digit' Turner81326262010-03-04 11:51:42 -0800426 if (v && v != BIGINT_INVALID) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427 mutex_lock(&freelist_mutex);
428
429 v->next = freelist[v->k];
430 freelist[v->k] = v;
431
432 mutex_unlock(&freelist_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200434}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435
David 'Digit' Turner81326262010-03-04 11:51:42 -0800436#define Bcopy_valid(x,y) memcpy(&(x)->sign, &(y)->sign, \
437 (y)->wds*sizeof(Long) + 2*sizeof(int))
438
439#define Bcopy(x,y) Bcopy_ptr(&(x),(y))
440
441 static void
442Bcopy_ptr(Bigint **px, Bigint *y)
443{
444 if (*px == BIGINT_INVALID)
445 return; /* no space to store copy */
446 if (y == BIGINT_INVALID) {
447 Bfree(*px); /* invalid input */
448 *px = BIGINT_INVALID;
449 } else {
450 Bcopy_valid(*px,y);
451 }
452}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453
454 static Bigint *
455multadd
456#ifdef KR_headers
457 (b, m, a) Bigint *b; int m, a;
458#else
459 (Bigint *b, int m, int a) /* multiply by m and add a */
460#endif
461{
462 int i, wds;
463 ULong *x, y;
464#ifdef Pack_32
465 ULong xi, z;
466#endif
467 Bigint *b1;
468
David 'Digit' Turner81326262010-03-04 11:51:42 -0800469 if (b == BIGINT_INVALID)
470 return b;
471
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800472 wds = b->wds;
473 x = b->x;
474 i = 0;
475 do {
476#ifdef Pack_32
477 xi = *x;
478 y = (xi & 0xffff) * m + a;
479 z = (xi >> 16) * m + (y >> 16);
480 a = (int)(z >> 16);
481 *x++ = (z << 16) + (y & 0xffff);
482#else
483 y = *x * m + a;
484 a = (int)(y >> 16);
485 *x++ = y & 0xffff;
486#endif
André Goddard Rosae7347692010-02-05 18:32:52 -0200487 }
488 while(++i < wds);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800489 if (a) {
490 if (wds >= b->maxwds) {
491 b1 = Balloc(b->k+1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800492 if (b1 == BIGINT_INVALID) {
493 Bfree(b);
494 return b1;
495 }
496 Bcopy_valid(b1, b);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800497 Bfree(b);
498 b = b1;
499 }
500 b->x[wds++] = a;
501 b->wds = wds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200503 return b;
504}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800505
506 static Bigint *
507s2b
508#ifdef KR_headers
509 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
510#else
511 (CONST char *s, int nd0, int nd, ULong y9)
512#endif
513{
514 Bigint *b;
515 int i, k;
516 Long x, y;
517
518 x = (nd + 8) / 9;
519 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
520#ifdef Pack_32
521 b = Balloc(k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800522 if (b == BIGINT_INVALID)
523 return b;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524 b->x[0] = y9;
525 b->wds = 1;
526#else
527 b = Balloc(k+1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800528 if (b == BIGINT_INVALID)
529 return b;
530
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800531 b->x[0] = y9 & 0xffff;
532 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
533#endif
534
535 i = 9;
536 if (9 < nd0) {
537 s += 9;
538 do b = multadd(b, 10, *s++ - '0');
539 while(++i < nd0);
540 s++;
André Goddard Rosae7347692010-02-05 18:32:52 -0200541 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800542 else
543 s += 10;
544 for(; i < nd; i++)
545 b = multadd(b, 10, *s++ - '0');
546 return b;
André Goddard Rosae7347692010-02-05 18:32:52 -0200547}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800548
549 static int
550hi0bits
551#ifdef KR_headers
552 (x) ULong x;
553#else
554 (ULong x)
555#endif
556{
557 int k = 0;
558
559 if (!(x & 0xffff0000)) {
560 k = 16;
561 x <<= 16;
André Goddard Rosae7347692010-02-05 18:32:52 -0200562 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800563 if (!(x & 0xff000000)) {
564 k += 8;
565 x <<= 8;
André Goddard Rosae7347692010-02-05 18:32:52 -0200566 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800567 if (!(x & 0xf0000000)) {
568 k += 4;
569 x <<= 4;
André Goddard Rosae7347692010-02-05 18:32:52 -0200570 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800571 if (!(x & 0xc0000000)) {
572 k += 2;
573 x <<= 2;
André Goddard Rosae7347692010-02-05 18:32:52 -0200574 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800575 if (!(x & 0x80000000)) {
576 k++;
577 if (!(x & 0x40000000))
578 return 32;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800579 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200580 return k;
581}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582
583 static int
584lo0bits
585#ifdef KR_headers
586 (y) ULong *y;
587#else
588 (ULong *y)
589#endif
590{
591 int k;
592 ULong x = *y;
593
594 if (x & 7) {
595 if (x & 1)
596 return 0;
597 if (x & 2) {
598 *y = x >> 1;
599 return 1;
600 }
601 *y = x >> 2;
602 return 2;
André Goddard Rosae7347692010-02-05 18:32:52 -0200603 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604 k = 0;
605 if (!(x & 0xffff)) {
606 k = 16;
607 x >>= 16;
André Goddard Rosae7347692010-02-05 18:32:52 -0200608 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800609 if (!(x & 0xff)) {
610 k += 8;
611 x >>= 8;
André Goddard Rosae7347692010-02-05 18:32:52 -0200612 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800613 if (!(x & 0xf)) {
614 k += 4;
615 x >>= 4;
André Goddard Rosae7347692010-02-05 18:32:52 -0200616 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617 if (!(x & 0x3)) {
618 k += 2;
619 x >>= 2;
André Goddard Rosae7347692010-02-05 18:32:52 -0200620 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800621 if (!(x & 1)) {
622 k++;
623 x >>= 1;
624 if (!x & 1)
625 return 32;
André Goddard Rosae7347692010-02-05 18:32:52 -0200626 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627 *y = x;
628 return k;
André Goddard Rosae7347692010-02-05 18:32:52 -0200629}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630
631 static Bigint *
632i2b
633#ifdef KR_headers
634 (i) int i;
635#else
636 (int i)
637#endif
638{
639 Bigint *b;
640
641 b = Balloc(1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800642 if (b != BIGINT_INVALID) {
643 b->x[0] = i;
644 b->wds = 1;
645 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646 return b;
André Goddard Rosae7347692010-02-05 18:32:52 -0200647}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648
649 static Bigint *
650mult
651#ifdef KR_headers
652 (a, b) Bigint *a, *b;
653#else
654 (Bigint *a, Bigint *b)
655#endif
656{
657 Bigint *c;
658 int k, wa, wb, wc;
659 ULong carry, y, z;
660 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
661#ifdef Pack_32
662 ULong z2;
663#endif
664
David 'Digit' Turner81326262010-03-04 11:51:42 -0800665 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
666 return BIGINT_INVALID;
667
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668 if (a->wds < b->wds) {
669 c = a;
670 a = b;
671 b = c;
André Goddard Rosae7347692010-02-05 18:32:52 -0200672 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800673 k = a->k;
674 wa = a->wds;
675 wb = b->wds;
676 wc = wa + wb;
677 if (wc > a->maxwds)
678 k++;
679 c = Balloc(k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800680 if (c == BIGINT_INVALID)
681 return c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682 for(x = c->x, xa = x + wc; x < xa; x++)
683 *x = 0;
684 xa = a->x;
685 xae = xa + wa;
686 xb = b->x;
687 xbe = xb + wb;
688 xc0 = c->x;
689#ifdef Pack_32
690 for(; xb < xbe; xb++, xc0++) {
691 if ((y = *xb & 0xffff) != 0) {
692 x = xa;
693 xc = xc0;
694 carry = 0;
695 do {
696 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
697 carry = z >> 16;
698 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
699 carry = z2 >> 16;
700 Storeinc(xc, z2, z);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200702 while(x < xae);
703 *xc = carry;
704 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705 if ((y = *xb >> 16) != 0) {
706 x = xa;
707 xc = xc0;
708 carry = 0;
709 z2 = *xc;
710 do {
711 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
712 carry = z >> 16;
713 Storeinc(xc, z, z2);
714 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
715 carry = z2 >> 16;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200717 while(x < xae);
718 *xc = z2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800719 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200720 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721#else
722 for(; xb < xbe; xc0++) {
723 if (y = *xb++) {
724 x = xa;
725 xc = xc0;
726 carry = 0;
727 do {
728 z = *x++ * y + *xc + carry;
729 carry = z >> 16;
730 *xc++ = z & 0xffff;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800731 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200732 while(x < xae);
733 *xc = carry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200735 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736#endif
737 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
738 c->wds = wc;
739 return c;
André Goddard Rosae7347692010-02-05 18:32:52 -0200740}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741
742 static Bigint *p5s;
Glenn Kasten144a5d32011-01-09 09:50:10 -0800743 static pthread_mutex_t p5s_mutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744
745 static Bigint *
746pow5mult
747#ifdef KR_headers
748 (b, k) Bigint *b; int k;
749#else
750 (Bigint *b, int k)
751#endif
752{
753 Bigint *b1, *p5, *p51;
754 int i;
755 static const int p05[3] = { 5, 25, 125 };
756
David 'Digit' Turner81326262010-03-04 11:51:42 -0800757 if (b == BIGINT_INVALID)
758 return b;
759
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760 if ((i = k & 3) != 0)
761 b = multadd(b, p05[i-1], 0);
762
763 if (!(k = (unsigned int) k >> 2))
764 return b;
Glenn Kasten144a5d32011-01-09 09:50:10 -0800765 mutex_lock(&p5s_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766 if (!(p5 = p5s)) {
767 /* first time */
David 'Digit' Turner81326262010-03-04 11:51:42 -0800768 p5 = i2b(625);
769 if (p5 == BIGINT_INVALID) {
770 Bfree(b);
Glenn Kasten144a5d32011-01-09 09:50:10 -0800771 mutex_unlock(&p5s_mutex);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800772 return p5;
773 }
774 p5s = p5;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800775 p5->next = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -0200776 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777 for(;;) {
778 if (k & 1) {
779 b1 = mult(b, p5);
780 Bfree(b);
781 b = b1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200782 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783 if (!(k = (unsigned int) k >> 1))
784 break;
785 if (!(p51 = p5->next)) {
David 'Digit' Turner81326262010-03-04 11:51:42 -0800786 p51 = mult(p5,p5);
787 if (p51 == BIGINT_INVALID) {
788 Bfree(b);
Glenn Kasten144a5d32011-01-09 09:50:10 -0800789 mutex_unlock(&p5s_mutex);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800790 return p51;
791 }
792 p5->next = p51;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793 p51->next = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200795 p5 = p51;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796 }
Glenn Kasten144a5d32011-01-09 09:50:10 -0800797 mutex_unlock(&p5s_mutex);
André Goddard Rosae7347692010-02-05 18:32:52 -0200798 return b;
799}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800
801 static Bigint *
802lshift
803#ifdef KR_headers
804 (b, k) Bigint *b; int k;
805#else
806 (Bigint *b, int k)
807#endif
808{
809 int i, k1, n, n1;
810 Bigint *b1;
811 ULong *x, *x1, *xe, z;
812
David 'Digit' Turner81326262010-03-04 11:51:42 -0800813 if (b == BIGINT_INVALID)
814 return b;
815
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800816#ifdef Pack_32
817 n = (unsigned int)k >> 5;
818#else
819 n = (unsigned int)k >> 4;
820#endif
821 k1 = b->k;
822 n1 = n + b->wds + 1;
823 for(i = b->maxwds; n1 > i; i <<= 1)
824 k1++;
825 b1 = Balloc(k1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800826 if (b1 == BIGINT_INVALID) {
827 Bfree(b);
828 return b1;
829 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800830 x1 = b1->x;
831 for(i = 0; i < n; i++)
832 *x1++ = 0;
833 x = b->x;
834 xe = x + b->wds;
835#ifdef Pack_32
836 if (k &= 0x1f) {
837 k1 = 32 - k;
838 z = 0;
839 do {
840 *x1++ = *x << k | z;
841 z = *x++ >> k1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200842 }
843 while(x < xe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844 if ((*x1 = z) != 0)
845 ++n1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200846 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847#else
848 if (k &= 0xf) {
849 k1 = 16 - k;
850 z = 0;
851 do {
852 *x1++ = *x << k & 0xffff | z;
853 z = *x++ >> k1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200854 }
855 while(x < xe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800856 if (*x1 = z)
857 ++n1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200858 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859#endif
860 else do
861 *x1++ = *x++;
862 while(x < xe);
863 b1->wds = n1 - 1;
864 Bfree(b);
865 return b1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200866}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867
868 static int
869cmp
870#ifdef KR_headers
871 (a, b) Bigint *a, *b;
872#else
873 (Bigint *a, Bigint *b)
874#endif
875{
876 ULong *xa, *xa0, *xb, *xb0;
877 int i, j;
878
David 'Digit' Turner81326262010-03-04 11:51:42 -0800879 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
880#ifdef DEBUG
881 Bug("cmp called with a or b invalid");
882#else
883 return 0; /* equal - the best we can do right now */
884#endif
885
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800886 i = a->wds;
887 j = b->wds;
888#ifdef DEBUG
889 if (i > 1 && !a->x[i-1])
890 Bug("cmp called with a->x[a->wds-1] == 0");
891 if (j > 1 && !b->x[j-1])
892 Bug("cmp called with b->x[b->wds-1] == 0");
893#endif
894 if (i -= j)
895 return i;
896 xa0 = a->x;
897 xa = xa0 + j;
898 xb0 = b->x;
899 xb = xb0 + j;
900 for(;;) {
901 if (*--xa != *--xb)
902 return *xa < *xb ? -1 : 1;
903 if (xa <= xa0)
904 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800905 }
André Goddard Rosae7347692010-02-05 18:32:52 -0200906 return 0;
907}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800908
909 static Bigint *
910diff
911#ifdef KR_headers
912 (a, b) Bigint *a, *b;
913#else
914 (Bigint *a, Bigint *b)
915#endif
916{
917 Bigint *c;
918 int i, wa, wb;
919 Long borrow, y; /* We need signed shifts here. */
920 ULong *xa, *xae, *xb, *xbe, *xc;
921#ifdef Pack_32
922 Long z;
923#endif
924
David 'Digit' Turner81326262010-03-04 11:51:42 -0800925 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
926 return BIGINT_INVALID;
927
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800928 i = cmp(a,b);
929 if (!i) {
930 c = Balloc(0);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800931 if (c != BIGINT_INVALID) {
932 c->wds = 1;
933 c->x[0] = 0;
934 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800935 return c;
André Goddard Rosae7347692010-02-05 18:32:52 -0200936 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937 if (i < 0) {
938 c = a;
939 a = b;
940 b = c;
941 i = 1;
André Goddard Rosae7347692010-02-05 18:32:52 -0200942 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943 else
944 i = 0;
945 c = Balloc(a->k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800946 if (c == BIGINT_INVALID)
947 return c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948 c->sign = i;
949 wa = a->wds;
950 xa = a->x;
951 xae = xa + wa;
952 wb = b->wds;
953 xb = b->x;
954 xbe = xb + wb;
955 xc = c->x;
956 borrow = 0;
957#ifdef Pack_32
958 do {
959 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
960 borrow = (ULong)y >> 16;
961 Sign_Extend(borrow, y);
962 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
963 borrow = (ULong)z >> 16;
964 Sign_Extend(borrow, z);
965 Storeinc(xc, z, y);
André Goddard Rosae7347692010-02-05 18:32:52 -0200966 }
967 while(xb < xbe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800968 while(xa < xae) {
969 y = (*xa & 0xffff) + borrow;
970 borrow = (ULong)y >> 16;
971 Sign_Extend(borrow, y);
972 z = (*xa++ >> 16) + borrow;
973 borrow = (ULong)z >> 16;
974 Sign_Extend(borrow, z);
975 Storeinc(xc, z, y);
André Goddard Rosae7347692010-02-05 18:32:52 -0200976 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977#else
978 do {
979 y = *xa++ - *xb++ + borrow;
980 borrow = y >> 16;
981 Sign_Extend(borrow, y);
982 *xc++ = y & 0xffff;
André Goddard Rosae7347692010-02-05 18:32:52 -0200983 }
984 while(xb < xbe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985 while(xa < xae) {
986 y = *xa++ + borrow;
987 borrow = y >> 16;
988 Sign_Extend(borrow, y);
989 *xc++ = y & 0xffff;
André Goddard Rosae7347692010-02-05 18:32:52 -0200990 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800991#endif
992 while(!*--xc)
993 wa--;
994 c->wds = wa;
995 return c;
André Goddard Rosae7347692010-02-05 18:32:52 -0200996}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800997
998 static double
999ulp
1000#ifdef KR_headers
1001 (_x) double _x;
1002#else
1003 (double _x)
1004#endif
1005{
1006 _double x;
1007 Long L;
1008 _double a;
1009
1010 value(x) = _x;
1011 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1012#ifndef Sudden_Underflow
1013 if (L > 0) {
1014#endif
1015#ifdef IBM
1016 L |= Exp_msk1 >> 4;
1017#endif
1018 word0(a) = L;
1019 word1(a) = 0;
1020#ifndef Sudden_Underflow
André Goddard Rosae7347692010-02-05 18:32:52 -02001021 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022 else {
1023 L = (ULong)-L >> Exp_shift;
1024 if (L < Exp_shift) {
1025 word0(a) = 0x80000 >> L;
1026 word1(a) = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02001027 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001028 else {
1029 word0(a) = 0;
1030 L -= Exp_shift;
1031 word1(a) = L >= 31 ? 1 : 1 << (31 - L);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001033 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034#endif
1035 return value(a);
André Goddard Rosae7347692010-02-05 18:32:52 -02001036}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001037
1038 static double
1039b2d
1040#ifdef KR_headers
1041 (a, e) Bigint *a; int *e;
1042#else
1043 (Bigint *a, int *e)
1044#endif
1045{
1046 ULong *xa, *xa0, w, y, z;
1047 int k;
1048 _double d;
1049#ifdef VAX
1050 ULong d0, d1;
1051#else
1052#define d0 word0(d)
1053#define d1 word1(d)
1054#endif
1055
David 'Digit' Turner81326262010-03-04 11:51:42 -08001056 if (a == BIGINT_INVALID)
1057 return NAN;
1058
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059 xa0 = a->x;
1060 xa = xa0 + a->wds;
1061 y = *--xa;
1062#ifdef DEBUG
1063 if (!y) Bug("zero y in b2d");
1064#endif
1065 k = hi0bits(y);
1066 *e = 32 - k;
1067#ifdef Pack_32
1068 if (k < Ebits) {
1069 d0 = Exp_1 | y >> (Ebits - k);
1070 w = xa > xa0 ? *--xa : 0;
1071 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1072 goto ret_d;
André Goddard Rosae7347692010-02-05 18:32:52 -02001073 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001074 z = xa > xa0 ? *--xa : 0;
1075 if (k -= Ebits) {
1076 d0 = Exp_1 | y << k | z >> (32 - k);
1077 y = xa > xa0 ? *--xa : 0;
1078 d1 = z << k | y >> (32 - k);
André Goddard Rosae7347692010-02-05 18:32:52 -02001079 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001080 else {
1081 d0 = Exp_1 | y;
1082 d1 = z;
André Goddard Rosae7347692010-02-05 18:32:52 -02001083 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001084#else
1085 if (k < Ebits + 16) {
1086 z = xa > xa0 ? *--xa : 0;
1087 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1088 w = xa > xa0 ? *--xa : 0;
1089 y = xa > xa0 ? *--xa : 0;
1090 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1091 goto ret_d;
André Goddard Rosae7347692010-02-05 18:32:52 -02001092 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001093 z = xa > xa0 ? *--xa : 0;
1094 w = xa > xa0 ? *--xa : 0;
1095 k -= Ebits + 16;
1096 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1097 y = xa > xa0 ? *--xa : 0;
1098 d1 = w << k + 16 | y << k;
1099#endif
1100 ret_d:
1101#ifdef VAX
1102 word0(d) = d0 >> 16 | d0 << 16;
1103 word1(d) = d1 >> 16 | d1 << 16;
1104#else
1105#undef d0
1106#undef d1
1107#endif
1108 return value(d);
André Goddard Rosae7347692010-02-05 18:32:52 -02001109}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001110
1111 static Bigint *
1112d2b
1113#ifdef KR_headers
1114 (_d, e, bits) double d; int *e, *bits;
1115#else
1116 (double _d, int *e, int *bits)
1117#endif
1118{
1119 Bigint *b;
1120 int de, i, k;
1121 ULong *x, y, z;
1122 _double d;
1123#ifdef VAX
1124 ULong d0, d1;
1125#endif
1126
1127 value(d) = _d;
1128#ifdef VAX
1129 d0 = word0(d) >> 16 | word0(d) << 16;
1130 d1 = word1(d) >> 16 | word1(d) << 16;
1131#else
1132#define d0 word0(d)
1133#define d1 word1(d)
1134#endif
1135
1136#ifdef Pack_32
1137 b = Balloc(1);
1138#else
1139 b = Balloc(2);
1140#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08001141 if (b == BIGINT_INVALID)
1142 return b;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001143 x = b->x;
1144
1145 z = d0 & Frac_mask;
1146 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1147#ifdef Sudden_Underflow
1148 de = (int)(d0 >> Exp_shift);
1149#ifndef IBM
1150 z |= Exp_msk11;
1151#endif
1152#else
1153 if ((de = (int)(d0 >> Exp_shift)) != 0)
1154 z |= Exp_msk1;
1155#endif
1156#ifdef Pack_32
1157 if ((y = d1) != 0) {
1158 if ((k = lo0bits(&y)) != 0) {
1159 x[0] = y | z << (32 - k);
1160 z >>= k;
André Goddard Rosae7347692010-02-05 18:32:52 -02001161 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001162 else
1163 x[0] = y;
1164 i = b->wds = (x[1] = z) ? 2 : 1;
André Goddard Rosae7347692010-02-05 18:32:52 -02001165 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001166 else {
1167#ifdef DEBUG
1168 if (!z)
1169 Bug("Zero passed to d2b");
1170#endif
1171 k = lo0bits(&z);
1172 x[0] = z;
1173 i = b->wds = 1;
1174 k += 32;
André Goddard Rosae7347692010-02-05 18:32:52 -02001175 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001176#else
1177 if (y = d1) {
1178 if (k = lo0bits(&y))
1179 if (k >= 16) {
1180 x[0] = y | z << 32 - k & 0xffff;
1181 x[1] = z >> k - 16 & 0xffff;
1182 x[2] = z >> k;
1183 i = 2;
André Goddard Rosae7347692010-02-05 18:32:52 -02001184 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185 else {
1186 x[0] = y & 0xffff;
1187 x[1] = y >> 16 | z << 16 - k & 0xffff;
1188 x[2] = z >> k & 0xffff;
1189 x[3] = z >> k+16;
1190 i = 3;
André Goddard Rosae7347692010-02-05 18:32:52 -02001191 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192 else {
1193 x[0] = y & 0xffff;
1194 x[1] = y >> 16;
1195 x[2] = z & 0xffff;
1196 x[3] = z >> 16;
1197 i = 3;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001199 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200 else {
1201#ifdef DEBUG
1202 if (!z)
1203 Bug("Zero passed to d2b");
1204#endif
1205 k = lo0bits(&z);
1206 if (k >= 16) {
1207 x[0] = z;
1208 i = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02001209 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001210 else {
1211 x[0] = z & 0xffff;
1212 x[1] = z >> 16;
1213 i = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001214 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001215 k += 32;
1216 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001217 while(!x[i])
1218 --i;
1219 b->wds = i + 1;
1220#endif
1221#ifndef Sudden_Underflow
1222 if (de) {
1223#endif
1224#ifdef IBM
1225 *e = (de - Bias - (P-1) << 2) + k;
1226 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1227#else
1228 *e = de - Bias - (P-1) + k;
1229 *bits = P - k;
1230#endif
1231#ifndef Sudden_Underflow
André Goddard Rosae7347692010-02-05 18:32:52 -02001232 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001233 else {
1234 *e = de - Bias - (P-1) + 1 + k;
1235#ifdef Pack_32
1236 *bits = 32*i - hi0bits(x[i-1]);
1237#else
1238 *bits = (i+2)*16 - hi0bits(x[i]);
1239#endif
1240 }
1241#endif
1242 return b;
André Goddard Rosae7347692010-02-05 18:32:52 -02001243}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001244#undef d0
1245#undef d1
1246
1247 static double
1248ratio
1249#ifdef KR_headers
1250 (a, b) Bigint *a, *b;
1251#else
1252 (Bigint *a, Bigint *b)
1253#endif
1254{
1255 _double da, db;
1256 int k, ka, kb;
1257
David 'Digit' Turner81326262010-03-04 11:51:42 -08001258 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
1259 return NAN; /* for lack of better value ? */
1260
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001261 value(da) = b2d(a, &ka);
1262 value(db) = b2d(b, &kb);
1263#ifdef Pack_32
1264 k = ka - kb + 32*(a->wds - b->wds);
1265#else
1266 k = ka - kb + 16*(a->wds - b->wds);
1267#endif
1268#ifdef IBM
1269 if (k > 0) {
1270 word0(da) += (k >> 2)*Exp_msk1;
1271 if (k &= 3)
1272 da *= 1 << k;
André Goddard Rosae7347692010-02-05 18:32:52 -02001273 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001274 else {
1275 k = -k;
1276 word0(db) += (k >> 2)*Exp_msk1;
1277 if (k &= 3)
1278 db *= 1 << k;
André Goddard Rosae7347692010-02-05 18:32:52 -02001279 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001280#else
1281 if (k > 0)
1282 word0(da) += k*Exp_msk1;
1283 else {
1284 k = -k;
1285 word0(db) += k*Exp_msk1;
André Goddard Rosae7347692010-02-05 18:32:52 -02001286 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001287#endif
1288 return value(da) / value(db);
André Goddard Rosae7347692010-02-05 18:32:52 -02001289}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001290
1291static CONST double
1292tens[] = {
1293 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1294 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1295 1e20, 1e21, 1e22
1296#ifdef VAX
1297 , 1e23, 1e24
1298#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001299};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001300
1301#ifdef IEEE_Arith
1302static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1303static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1304#define n_bigtens 5
1305#else
1306#ifdef IBM
1307static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1308static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1309#define n_bigtens 3
1310#else
1311static CONST double bigtens[] = { 1e16, 1e32 };
1312static CONST double tinytens[] = { 1e-16, 1e-32 };
1313#define n_bigtens 2
1314#endif
1315#endif
1316
1317 double
1318strtod
1319#ifdef KR_headers
1320 (s00, se) CONST char *s00; char **se;
1321#else
1322 (CONST char *s00, char **se)
1323#endif
1324{
1325 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1326 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1327 CONST char *s, *s0, *s1;
1328 double aadj, aadj1, adj;
1329 _double rv, rv0;
1330 Long L;
1331 ULong y, z;
1332 Bigint *bb1, *bd0;
1333 Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
1334
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001335#ifndef KR_headers
1336 CONST char decimal_point = localeconv()->decimal_point[0];
1337#else
1338 CONST char decimal_point = '.';
1339#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001340
1341 sign = nz0 = nz = 0;
1342 value(rv) = 0.;
1343
1344
1345 for(s = s00; isspace((unsigned char) *s); s++)
1346 ;
1347
1348 if (*s == '-') {
1349 sign = 1;
1350 s++;
1351 } else if (*s == '+') {
1352 s++;
1353 }
1354
1355 if (*s == '\0') {
1356 s = s00;
1357 goto ret;
1358 }
1359
1360 /* "INF" or "INFINITY" */
1361 if (tolower((unsigned char)*s) == 'i' && strncasecmp(s, "inf", 3) == 0) {
1362 if (strncasecmp(s + 3, "inity", 5) == 0)
1363 s += 8;
1364 else
1365 s += 3;
1366
1367 value(rv) = HUGE_VAL;
1368 goto ret;
1369 }
1370
1371#ifdef IEEE_Arith
1372 /* "NAN" or "NAN(n-char-sequence-opt)" */
1373 if (tolower((unsigned char)*s) == 'n' && strncasecmp(s, "nan", 3) == 0) {
1374 /* Build a quiet NaN. */
1375 word0(rv) = NAN_WORD0;
1376 word1(rv) = NAN_WORD1;
1377 s+= 3;
1378
1379 /* Don't interpret (n-char-sequence-opt), for now. */
1380 if (*s == '(') {
1381 s0 = s;
1382 for (s++; *s != ')' && *s != '\0'; s++)
1383 ;
1384 if (*s == ')')
1385 s++; /* Skip over closing paren ... */
1386 else
1387 s = s0; /* ... otherwise go back. */
1388 }
1389
1390 goto ret;
1391 }
1392#endif
1393
1394 if (*s == '0') {
1395 nz0 = 1;
1396 while(*++s == '0') ;
1397 if (!*s)
1398 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02001399 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400 s0 = s;
1401 y = z = 0;
1402 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1403 if (nd < 9)
1404 y = 10*y + c - '0';
1405 else if (nd < 16)
1406 z = 10*z + c - '0';
1407 nd0 = nd;
1408 if (c == decimal_point) {
1409 c = *++s;
1410 if (!nd) {
1411 for(; c == '0'; c = *++s)
1412 nz++;
1413 if (c > '0' && c <= '9') {
1414 s0 = s;
1415 nf += nz;
1416 nz = 0;
1417 goto have_dig;
1418 }
1419 goto dig_done;
André Goddard Rosae7347692010-02-05 18:32:52 -02001420 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421 for(; c >= '0' && c <= '9'; c = *++s) {
1422 have_dig:
1423 nz++;
1424 if (c -= '0') {
1425 nf += nz;
1426 for(i = 1; i < nz; i++)
1427 if (nd++ < 9)
1428 y *= 10;
1429 else if (nd <= DBL_DIG + 1)
1430 z *= 10;
1431 if (nd++ < 9)
1432 y = 10*y + c;
1433 else if (nd <= DBL_DIG + 1)
1434 z = 10*z + c;
1435 nz = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436 }
1437 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001438 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001439 dig_done:
1440 e = 0;
1441 if (c == 'e' || c == 'E') {
1442 if (!nd && !nz && !nz0) {
1443 s = s00;
1444 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02001445 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001446 s00 = s;
1447 esign = 0;
1448 switch(c = *++s) {
1449 case '-':
1450 esign = 1;
1451 /* FALLTHROUGH */
1452 case '+':
1453 c = *++s;
André Goddard Rosae7347692010-02-05 18:32:52 -02001454 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455 if (c >= '0' && c <= '9') {
1456 while(c == '0')
1457 c = *++s;
1458 if (c > '0' && c <= '9') {
1459 L = c - '0';
1460 s1 = s;
1461 while((c = *++s) >= '0' && c <= '9')
1462 L = 10*L + c - '0';
1463 if (s - s1 > 8 || L > 19999)
1464 /* Avoid confusion from exponents
1465 * so large that e might overflow.
1466 */
1467 e = 19999; /* safe for 16 bit ints */
1468 else
1469 e = (int)L;
1470 if (esign)
1471 e = -e;
André Goddard Rosae7347692010-02-05 18:32:52 -02001472 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473 else
1474 e = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02001475 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476 else
1477 s = s00;
André Goddard Rosae7347692010-02-05 18:32:52 -02001478 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479 if (!nd) {
1480 if (!nz && !nz0)
1481 s = s00;
1482 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02001483 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001484 e1 = e -= nf;
1485
1486 /* Now we have nd0 digits, starting at s0, followed by a
1487 * decimal point, followed by nd-nd0 digits. The number we're
1488 * after is the integer represented by those digits times
1489 * 10**e */
1490
1491 if (!nd0)
1492 nd0 = nd;
1493 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1494 value(rv) = y;
1495 if (k > 9)
1496 value(rv) = tens[k - 9] * value(rv) + z;
1497 bd0 = 0;
1498 if (nd <= DBL_DIG
1499#ifndef RND_PRODQUOT
1500 && FLT_ROUNDS == 1
1501#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001502 ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001503 if (!e)
1504 goto ret;
1505 if (e > 0) {
1506 if (e <= Ten_pmax) {
1507#ifdef VAX
1508 goto vax_ovfl_check;
1509#else
1510 /* value(rv) = */ rounded_product(value(rv),
1511 tens[e]);
1512 goto ret;
1513#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001514 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001515 i = DBL_DIG - nd;
1516 if (e <= Ten_pmax + i) {
1517 /* A fancier test would sometimes let us do
1518 * this for larger i values.
1519 */
1520 e -= i;
1521 value(rv) *= tens[i];
1522#ifdef VAX
1523 /* VAX exponent range is so narrow we must
1524 * worry about overflow here...
1525 */
1526 vax_ovfl_check:
1527 word0(rv) -= P*Exp_msk1;
1528 /* value(rv) = */ rounded_product(value(rv),
1529 tens[e]);
1530 if ((word0(rv) & Exp_mask)
1531 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1532 goto ovfl;
1533 word0(rv) += P*Exp_msk1;
1534#else
1535 /* value(rv) = */ rounded_product(value(rv),
1536 tens[e]);
1537#endif
1538 goto ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001540 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541#ifndef Inaccurate_Divide
1542 else if (e >= -Ten_pmax) {
1543 /* value(rv) = */ rounded_quotient(value(rv),
1544 tens[-e]);
1545 goto ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001546 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001547#endif
1548 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549 e1 += nd - k;
1550
1551 /* Get starting approximation = rv * 10**e1 */
1552
1553 if (e1 > 0) {
1554 if ((i = e1 & 15) != 0)
1555 value(rv) *= tens[i];
1556 if (e1 &= ~15) {
1557 if (e1 > DBL_MAX_10_EXP) {
1558 ovfl:
1559 errno = ERANGE;
1560 value(rv) = HUGE_VAL;
1561 if (bd0)
1562 goto retfree;
1563 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02001564 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565 if ((e1 = (unsigned int)e1 >> 4) != 0) {
1566 for(j = 0; e1 > 1; j++,
1567 e1 = (unsigned int)e1 >> 1)
1568 if (e1 & 1)
1569 value(rv) *= bigtens[j];
1570 /* The last multiplication could overflow. */
1571 word0(rv) -= P*Exp_msk1;
1572 value(rv) *= bigtens[j];
1573 if ((z = word0(rv) & Exp_mask)
1574 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1575 goto ovfl;
1576 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1577 /* set to largest number */
1578 /* (Can't trust DBL_MAX) */
1579 word0(rv) = Big0;
1580 word1(rv) = Big1;
1581 }
1582 else
1583 word0(rv) += P*Exp_msk1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001584 }
1585 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001586 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587 else if (e1 < 0) {
1588 e1 = -e1;
1589 if ((i = e1 & 15) != 0)
1590 value(rv) /= tens[i];
1591 if (e1 &= ~15) {
1592 e1 = (unsigned int)e1 >> 4;
1593 if (e1 >= 1 << n_bigtens)
1594 goto undfl;
1595 for(j = 0; e1 > 1; j++,
1596 e1 = (unsigned int)e1 >> 1)
1597 if (e1 & 1)
1598 value(rv) *= tinytens[j];
1599 /* The last multiplication could underflow. */
1600 value(rv0) = value(rv);
1601 value(rv) *= tinytens[j];
1602 if (!value(rv)) {
1603 value(rv) = 2.*value(rv0);
1604 value(rv) *= tinytens[j];
1605 if (!value(rv)) {
1606 undfl:
1607 value(rv) = 0.;
1608 errno = ERANGE;
1609 if (bd0)
1610 goto retfree;
1611 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02001612 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001613 word0(rv) = Tiny0;
1614 word1(rv) = Tiny1;
1615 /* The refinement below will clean
1616 * this approximation up.
1617 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001618 }
1619 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001620 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001621
1622 /* Now the hard part -- adjusting rv to the correct value.*/
1623
1624 /* Put digits into bd: true value = bd * 10^e */
1625
1626 bd0 = s2b(s0, nd0, nd, y);
1627
1628 for(;;) {
1629 bd = Balloc(bd0->k);
1630 Bcopy(bd, bd0);
1631 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1632 bs = i2b(1);
1633
1634 if (e >= 0) {
1635 bb2 = bb5 = 0;
1636 bd2 = bd5 = e;
André Goddard Rosae7347692010-02-05 18:32:52 -02001637 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001638 else {
1639 bb2 = bb5 = -e;
1640 bd2 = bd5 = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02001641 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001642 if (bbe >= 0)
1643 bb2 += bbe;
1644 else
1645 bd2 -= bbe;
1646 bs2 = bb2;
1647#ifdef Sudden_Underflow
1648#ifdef IBM
1649 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1650#else
1651 j = P + 1 - bbbits;
1652#endif
1653#else
1654 i = bbe + bbbits - 1; /* logb(rv) */
1655 if (i < Emin) /* denormal */
1656 j = bbe + (P-Emin);
1657 else
1658 j = P + 1 - bbbits;
1659#endif
1660 bb2 += j;
1661 bd2 += j;
1662 i = bb2 < bd2 ? bb2 : bd2;
1663 if (i > bs2)
1664 i = bs2;
1665 if (i > 0) {
1666 bb2 -= i;
1667 bd2 -= i;
1668 bs2 -= i;
André Goddard Rosae7347692010-02-05 18:32:52 -02001669 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001670 if (bb5 > 0) {
1671 bs = pow5mult(bs, bb5);
1672 bb1 = mult(bs, bb);
1673 Bfree(bb);
1674 bb = bb1;
André Goddard Rosae7347692010-02-05 18:32:52 -02001675 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001676 if (bb2 > 0)
1677 bb = lshift(bb, bb2);
1678 if (bd5 > 0)
1679 bd = pow5mult(bd, bd5);
1680 if (bd2 > 0)
1681 bd = lshift(bd, bd2);
1682 if (bs2 > 0)
1683 bs = lshift(bs, bs2);
1684 delta = diff(bb, bd);
1685 dsign = delta->sign;
1686 delta->sign = 0;
1687 i = cmp(delta, bs);
1688 if (i < 0) {
1689 /* Error is less than half an ulp -- check for
1690 * special case of mantissa a power of two.
1691 */
1692 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1693 break;
1694 delta = lshift(delta,Log2P);
1695 if (cmp(delta, bs) > 0)
1696 goto drop_down;
1697 break;
André Goddard Rosae7347692010-02-05 18:32:52 -02001698 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699 if (i == 0) {
1700 /* exactly half-way between */
1701 if (dsign) {
1702 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1703 && word1(rv) == 0xffffffff) {
1704 /*boundary case -- increment exponent*/
1705 word0(rv) = (word0(rv) & Exp_mask)
1706 + Exp_msk1
1707#ifdef IBM
1708 | Exp_msk1 >> 4
1709#endif
1710 ;
1711 word1(rv) = 0;
1712 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001714 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001715 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1716 drop_down:
1717 /* boundary case -- decrement exponent */
1718#ifdef Sudden_Underflow
1719 L = word0(rv) & Exp_mask;
1720#ifdef IBM
1721 if (L < Exp_msk1)
1722#else
1723 if (L <= Exp_msk1)
1724#endif
1725 goto undfl;
1726 L -= Exp_msk1;
1727#else
1728 L = (word0(rv) & Exp_mask) - Exp_msk1;
1729#endif
1730 word0(rv) = L | Bndry_mask1;
1731 word1(rv) = 0xffffffff;
1732#ifdef IBM
1733 goto cont;
1734#else
1735 break;
1736#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001737 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001738#ifndef ROUND_BIASED
1739 if (!(word1(rv) & LSB))
1740 break;
1741#endif
1742 if (dsign)
1743 value(rv) += ulp(value(rv));
1744#ifndef ROUND_BIASED
1745 else {
1746 value(rv) -= ulp(value(rv));
1747#ifndef Sudden_Underflow
1748 if (!value(rv))
1749 goto undfl;
1750#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001751 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752#endif
1753 break;
André Goddard Rosae7347692010-02-05 18:32:52 -02001754 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001755 if ((aadj = ratio(delta, bs)) <= 2.) {
1756 if (dsign)
1757 aadj = aadj1 = 1.;
1758 else if (word1(rv) || word0(rv) & Bndry_mask) {
1759#ifndef Sudden_Underflow
1760 if (word1(rv) == Tiny1 && !word0(rv))
1761 goto undfl;
1762#endif
1763 aadj = 1.;
1764 aadj1 = -1.;
André Goddard Rosae7347692010-02-05 18:32:52 -02001765 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 else {
1767 /* special case -- power of FLT_RADIX to be */
1768 /* rounded down... */
1769
1770 if (aadj < 2./FLT_RADIX)
1771 aadj = 1./FLT_RADIX;
1772 else
1773 aadj *= 0.5;
1774 aadj1 = -aadj;
1775 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001776 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001777 else {
1778 aadj *= 0.5;
1779 aadj1 = dsign ? aadj : -aadj;
1780#ifdef Check_FLT_ROUNDS
1781 switch(FLT_ROUNDS) {
1782 case 2: /* towards +infinity */
1783 aadj1 -= 0.5;
1784 break;
1785 case 0: /* towards 0 */
1786 case 3: /* towards -infinity */
1787 aadj1 += 0.5;
André Goddard Rosae7347692010-02-05 18:32:52 -02001788 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001789#else
1790 if (FLT_ROUNDS == 0)
1791 aadj1 += 0.5;
1792#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001793 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001794 y = word0(rv) & Exp_mask;
1795
1796 /* Check for overflow */
1797
1798 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1799 value(rv0) = value(rv);
1800 word0(rv) -= P*Exp_msk1;
1801 adj = aadj1 * ulp(value(rv));
1802 value(rv) += adj;
1803 if ((word0(rv) & Exp_mask) >=
1804 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1805 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1806 goto ovfl;
1807 word0(rv) = Big0;
1808 word1(rv) = Big1;
1809 goto cont;
André Goddard Rosae7347692010-02-05 18:32:52 -02001810 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001811 else
1812 word0(rv) += P*Exp_msk1;
André Goddard Rosae7347692010-02-05 18:32:52 -02001813 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001814 else {
1815#ifdef Sudden_Underflow
1816 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1817 value(rv0) = value(rv);
1818 word0(rv) += P*Exp_msk1;
1819 adj = aadj1 * ulp(value(rv));
1820 value(rv) += adj;
1821#ifdef IBM
1822 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1823#else
1824 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1825#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001826 {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001827 if (word0(rv0) == Tiny0
1828 && word1(rv0) == Tiny1)
1829 goto undfl;
1830 word0(rv) = Tiny0;
1831 word1(rv) = Tiny1;
1832 goto cont;
André Goddard Rosae7347692010-02-05 18:32:52 -02001833 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834 else
1835 word0(rv) -= P*Exp_msk1;
1836 }
1837 else {
1838 adj = aadj1 * ulp(value(rv));
1839 value(rv) += adj;
André Goddard Rosae7347692010-02-05 18:32:52 -02001840 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001841#else
1842 /* Compute adj so that the IEEE rounding rules will
1843 * correctly round rv + adj in some half-way cases.
1844 * If rv * ulp(rv) is denormalized (i.e.,
1845 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1846 * trouble from bits lost to denormalization;
1847 * example: 1.2e-307 .
1848 */
1849 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1850 aadj1 = (double)(int)(aadj + 0.5);
1851 if (!dsign)
1852 aadj1 = -aadj1;
André Goddard Rosae7347692010-02-05 18:32:52 -02001853 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001854 adj = aadj1 * ulp(value(rv));
1855 value(rv) += adj;
1856#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001857 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858 z = word0(rv) & Exp_mask;
1859 if (y == z) {
1860 /* Can we stop now? */
1861 L = aadj;
1862 aadj -= L;
1863 /* The tolerances below are conservative. */
1864 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1865 if (aadj < .4999999 || aadj > .5000001)
1866 break;
André Goddard Rosae7347692010-02-05 18:32:52 -02001867 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001868 else if (aadj < .4999999/FLT_RADIX)
1869 break;
André Goddard Rosae7347692010-02-05 18:32:52 -02001870 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001871 cont:
1872 Bfree(bb);
1873 Bfree(bd);
1874 Bfree(bs);
1875 Bfree(delta);
André Goddard Rosae7347692010-02-05 18:32:52 -02001876 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001877 retfree:
1878 Bfree(bb);
1879 Bfree(bd);
1880 Bfree(bs);
1881 Bfree(bd0);
1882 Bfree(delta);
1883 ret:
1884 if (se)
1885 /* LINTED interface specification */
1886 *se = (char *)s;
1887 return sign ? -value(rv) : value(rv);
André Goddard Rosae7347692010-02-05 18:32:52 -02001888}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001889
1890 static int
1891quorem
1892#ifdef KR_headers
1893 (b, S) Bigint *b, *S;
1894#else
1895 (Bigint *b, Bigint *S)
1896#endif
1897{
1898 int n;
1899 Long borrow, y;
1900 ULong carry, q, ys;
1901 ULong *bx, *bxe, *sx, *sxe;
1902#ifdef Pack_32
1903 Long z;
1904 ULong si, zs;
1905#endif
1906
David 'Digit' Turner81326262010-03-04 11:51:42 -08001907 if (b == BIGINT_INVALID || S == BIGINT_INVALID)
1908 return 0;
1909
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001910 n = S->wds;
1911#ifdef DEBUG
1912 /*debug*/ if (b->wds > n)
1913 /*debug*/ Bug("oversize b in quorem");
1914#endif
1915 if (b->wds < n)
1916 return 0;
1917 sx = S->x;
1918 sxe = sx + --n;
1919 bx = b->x;
1920 bxe = bx + n;
1921 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1922#ifdef DEBUG
1923 /*debug*/ if (q > 9)
1924 /*debug*/ Bug("oversized quotient in quorem");
1925#endif
1926 if (q) {
1927 borrow = 0;
1928 carry = 0;
1929 do {
1930#ifdef Pack_32
1931 si = *sx++;
1932 ys = (si & 0xffff) * q + carry;
1933 zs = (si >> 16) * q + (ys >> 16);
1934 carry = zs >> 16;
1935 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1936 borrow = (ULong)y >> 16;
1937 Sign_Extend(borrow, y);
1938 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1939 borrow = (ULong)z >> 16;
1940 Sign_Extend(borrow, z);
1941 Storeinc(bx, z, y);
1942#else
1943 ys = *sx++ * q + carry;
1944 carry = ys >> 16;
1945 y = *bx - (ys & 0xffff) + borrow;
1946 borrow = y >> 16;
1947 Sign_Extend(borrow, y);
1948 *bx++ = y & 0xffff;
1949#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001950 }
1951 while(sx <= sxe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001952 if (!*bxe) {
1953 bx = b->x;
1954 while(--bxe > bx && !*bxe)
1955 --n;
1956 b->wds = n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001957 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001958 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001959 if (cmp(b, S) >= 0) {
1960 q++;
1961 borrow = 0;
1962 carry = 0;
1963 bx = b->x;
1964 sx = S->x;
1965 do {
1966#ifdef Pack_32
1967 si = *sx++;
1968 ys = (si & 0xffff) + carry;
1969 zs = (si >> 16) + (ys >> 16);
1970 carry = zs >> 16;
1971 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1972 borrow = (ULong)y >> 16;
1973 Sign_Extend(borrow, y);
1974 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1975 borrow = (ULong)z >> 16;
1976 Sign_Extend(borrow, z);
1977 Storeinc(bx, z, y);
1978#else
1979 ys = *sx++ + carry;
1980 carry = ys >> 16;
1981 y = *bx - (ys & 0xffff) + borrow;
1982 borrow = y >> 16;
1983 Sign_Extend(borrow, y);
1984 *bx++ = y & 0xffff;
1985#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02001986 }
1987 while(sx <= sxe);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001988 bx = b->x;
1989 bxe = bx + n;
1990 if (!*bxe) {
1991 while(--bxe > bx && !*bxe)
1992 --n;
1993 b->wds = n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001994 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001995 }
André Goddard Rosae7347692010-02-05 18:32:52 -02001996 return q;
1997}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001998
1999/* freedtoa(s) must be used to free values s returned by dtoa
2000 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2001 * but for consistency with earlier versions of dtoa, it is optional
2002 * when MULTIPLE_THREADS is not defined.
2003 */
2004
2005void
2006#ifdef KR_headers
2007freedtoa(s) char *s;
2008#else
2009freedtoa(char *s)
2010#endif
2011{
2012 free(s);
2013}
2014
2015
2016
2017/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2018 *
2019 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2020 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
2021 *
2022 * Modifications:
2023 * 1. Rather than iterating, we use a simple numeric overestimate
2024 * to determine k = floor(log10(d)). We scale relevant
2025 * quantities using O(log2(k)) rather than O(k) multiplications.
2026 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2027 * try to generate digits strictly left to right. Instead, we
2028 * compute with fewer bits and propagate the carry if necessary
2029 * when rounding the final digit up. This is often faster.
2030 * 3. Under the assumption that input will be rounded nearest,
2031 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2032 * That is, we allow equality in stopping tests when the
2033 * round-nearest rule will give the same floating-point value
2034 * as would satisfaction of the stopping test with strict
2035 * inequality.
2036 * 4. We remove common factors of powers of 2 from relevant
2037 * quantities.
2038 * 5. When converting floating-point integers less than 1e16,
2039 * we use floating-point arithmetic rather than resorting
2040 * to multiple-precision integers.
2041 * 6. When asked to produce fewer than 15 digits, we first try
2042 * to get by with floating-point arithmetic; we resort to
2043 * multiple-precision integer arithmetic only if we cannot
2044 * guarantee that the floating-point calculation has given
2045 * the correctly rounded result. For k requested digits and
2046 * "uniformly" distributed input, the probability is
2047 * something like 10^(k-15) that we must resort to the Long
2048 * calculation.
2049 */
2050
Jim Huangcec75a72010-10-15 01:35:08 +08002051__LIBC_HIDDEN__ char *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002052__dtoa
2053#ifdef KR_headers
2054 (_d, mode, ndigits, decpt, sign, rve)
2055 double _d; int mode, ndigits, *decpt, *sign; char **rve;
2056#else
2057 (double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
2058#endif
2059{
2060 /* Arguments ndigits, decpt, sign are similar to those
2061 of ecvt and fcvt; trailing zeros are suppressed from
2062 the returned string. If not null, *rve is set to point
2063 to the end of the return value. If d is +-Infinity or NaN,
2064 then *decpt is set to 9999.
2065
2066 mode:
2067 0 ==> shortest string that yields d when read in
2068 and rounded to nearest.
2069 1 ==> like 0, but with Steele & White stopping rule;
2070 e.g. with IEEE P754 arithmetic , mode 0 gives
2071 1e23 whereas mode 1 gives 9.999999999999999e22.
2072 2 ==> max(1,ndigits) significant digits. This gives a
2073 return value similar to that of ecvt, except
2074 that trailing zeros are suppressed.
2075 3 ==> through ndigits past the decimal point. This
2076 gives a return value similar to that from fcvt,
2077 except that trailing zeros are suppressed, and
2078 ndigits can be negative.
2079 4-9 should give the same return values as 2-3, i.e.,
2080 4 <= mode <= 9 ==> same return as mode
2081 2 + (mode & 1). These modes are mainly for
2082 debugging; often they run slower but sometimes
2083 faster than modes 2-3.
2084 4,5,8,9 ==> left-to-right digit generation.
2085 6-9 ==> don't try fast floating-point estimate
2086 (if applicable).
2087
2088 Values of mode other than 0-9 are treated as mode 0.
2089
2090 Sufficient space is allocated to the return value
2091 to hold the suppressed trailing zeros.
2092 */
2093
2094 int bbits, b2, b5, be, dig, i, ieps, ilim0,
2095 j, jj1, k, k0, k_check, leftright, m2, m5, s2, s5,
2096 try_quick;
2097 int ilim = 0, ilim1 = 0, spec_case = 0; /* pacify gcc */
2098 Long L;
2099#ifndef Sudden_Underflow
2100 int denorm;
2101 ULong x;
2102#endif
2103 Bigint *b, *b1, *delta, *mhi, *S;
2104 Bigint *mlo = NULL; /* pacify gcc */
2105 double ds;
2106 char *s, *s0;
2107 Bigint *result = NULL;
2108 int result_k = 0;
2109 _double d, d2, eps;
2110
2111 value(d) = _d;
2112
2113 if (word0(d) & Sign_bit) {
2114 /* set sign for everything, including 0's and NaNs */
2115 *sign = 1;
2116 word0(d) &= ~Sign_bit; /* clear sign bit */
André Goddard Rosae7347692010-02-05 18:32:52 -02002117 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002118 else
2119 *sign = 0;
2120
2121#if defined(IEEE_Arith) + defined(VAX)
2122#ifdef IEEE_Arith
2123 if ((word0(d) & Exp_mask) == Exp_mask)
2124#else
2125 if (word0(d) == 0x8000)
2126#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02002127 {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002128 /* Infinity or NaN */
2129 *decpt = 9999;
2130 s =
2131#ifdef IEEE_Arith
2132 !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
2133#endif
2134 "NaN";
David 'Digit' Turner81326262010-03-04 11:51:42 -08002135 result = Balloc(strlen(s)+1);
2136 if (result == BIGINT_INVALID)
2137 return NULL;
2138 s0 = (char *)(void *)result;
2139 strcpy(s0, s);
2140 if (rve)
2141 *rve =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002142#ifdef IEEE_Arith
David 'Digit' Turner81326262010-03-04 11:51:42 -08002143 s0[3] ? s0 + 8 :
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002144#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08002145 s0 + 3;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002146 return s0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002147 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002148#endif
2149#ifdef IBM
2150 value(d) += 0; /* normalize */
2151#endif
2152 if (!value(d)) {
2153 *decpt = 1;
David 'Digit' Turner81326262010-03-04 11:51:42 -08002154 result = Balloc(2);
2155 if (result == BIGINT_INVALID)
2156 return NULL;
André Goddard Rosae7347692010-02-05 18:32:52 -02002157 s0 = (char *)(void *)result;
2158 strcpy(s0, "0");
2159 if (rve)
David 'Digit' Turner81326262010-03-04 11:51:42 -08002160 *rve = s0 + 1;
André Goddard Rosae7347692010-02-05 18:32:52 -02002161 return s0;
2162 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002163
2164 b = d2b(value(d), &be, &bbits);
2165#ifdef Sudden_Underflow
2166 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2167#else
2168 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
2169#endif
2170 value(d2) = value(d);
2171 word0(d2) &= Frac_mask1;
2172 word0(d2) |= Exp_11;
2173#ifdef IBM
2174 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2175 value(d2) /= 1 << j;
2176#endif
2177
2178 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2179 * log10(x) = log(x) / log(10)
2180 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2181 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2182 *
2183 * This suggests computing an approximation k to log10(d) by
2184 *
2185 * k = (i - Bias)*0.301029995663981
2186 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2187 *
2188 * We want k to be too large rather than too small.
2189 * The error in the first-order Taylor series approximation
2190 * is in our favor, so we just round up the constant enough
2191 * to compensate for any error in the multiplication of
2192 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2193 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2194 * adding 1e-13 to the constant term more than suffices.
2195 * Hence we adjust the constant term to 0.1760912590558.
2196 * (We could get a more accurate k by invoking log10,
2197 * but this is probably not worthwhile.)
2198 */
2199
2200 i -= Bias;
2201#ifdef IBM
2202 i <<= 2;
2203 i += j;
2204#endif
2205#ifndef Sudden_Underflow
2206 denorm = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002207 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002208 else {
2209 /* d is denormalized */
2210
2211 i = bbits + be + (Bias + (P-1) - 1);
2212 x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
2213 : word1(d) << (32 - i);
2214 value(d2) = x;
2215 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2216 i -= (Bias + (P-1) - 1) + 1;
2217 denorm = 1;
André Goddard Rosae7347692010-02-05 18:32:52 -02002218 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002219#endif
2220 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2221 i*0.301029995663981;
2222 k = (int)ds;
2223 if (ds < 0. && ds != k)
2224 k--; /* want k = floor(ds) */
2225 k_check = 1;
2226 if (k >= 0 && k <= Ten_pmax) {
2227 if (value(d) < tens[k])
2228 k--;
2229 k_check = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002230 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002231 j = bbits - i - 1;
2232 if (j >= 0) {
2233 b2 = 0;
2234 s2 = j;
André Goddard Rosae7347692010-02-05 18:32:52 -02002235 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002236 else {
2237 b2 = -j;
2238 s2 = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002239 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002240 if (k >= 0) {
2241 b5 = 0;
2242 s5 = k;
2243 s2 += k;
André Goddard Rosae7347692010-02-05 18:32:52 -02002244 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002245 else {
2246 b2 -= k;
2247 b5 = -k;
2248 s5 = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002249 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002250 if (mode < 0 || mode > 9)
2251 mode = 0;
2252 try_quick = 1;
2253 if (mode > 5) {
2254 mode -= 4;
2255 try_quick = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002256 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002257 leftright = 1;
2258 switch(mode) {
2259 case 0:
2260 case 1:
2261 ilim = ilim1 = -1;
2262 i = 18;
2263 ndigits = 0;
2264 break;
2265 case 2:
2266 leftright = 0;
2267 /* FALLTHROUGH */
2268 case 4:
2269 if (ndigits <= 0)
2270 ndigits = 1;
2271 ilim = ilim1 = i = ndigits;
2272 break;
2273 case 3:
2274 leftright = 0;
2275 /* FALLTHROUGH */
2276 case 5:
2277 i = ndigits + k + 1;
2278 ilim = i;
2279 ilim1 = i - 1;
2280 if (i <= 0)
2281 i = 1;
André Goddard Rosae7347692010-02-05 18:32:52 -02002282 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002283 j = sizeof(ULong);
2284 for(result_k = 0; (int)(sizeof(Bigint) - sizeof(ULong)) + j <= i;
2285 j <<= 1) result_k++;
2286 // this is really a ugly hack, the code uses Balloc
2287 // instead of malloc, but casts the result into a char*
2288 // it seems the only reason to do that is due to the
2289 // complicated way the block size need to be computed
2290 // buuurk....
2291 result = Balloc(result_k);
David 'Digit' Turner81326262010-03-04 11:51:42 -08002292 if (result == BIGINT_INVALID) {
2293 Bfree(b);
2294 return NULL;
2295 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002296 s = s0 = (char *)(void *)result;
2297
2298 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2299
2300 /* Try to get by with floating-point arithmetic. */
2301
2302 i = 0;
2303 value(d2) = value(d);
2304 k0 = k;
2305 ilim0 = ilim;
2306 ieps = 2; /* conservative */
2307 if (k > 0) {
2308 ds = tens[k&0xf];
2309 j = (unsigned int)k >> 4;
2310 if (j & Bletch) {
2311 /* prevent overflows */
2312 j &= Bletch - 1;
2313 value(d) /= bigtens[n_bigtens-1];
2314 ieps++;
2315 }
2316 for(; j; j = (unsigned int)j >> 1, i++)
2317 if (j & 1) {
2318 ieps++;
2319 ds *= bigtens[i];
2320 }
2321 value(d) /= ds;
André Goddard Rosae7347692010-02-05 18:32:52 -02002322 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002323 else if ((jj1 = -k) != 0) {
2324 value(d) *= tens[jj1 & 0xf];
2325 for(j = (unsigned int)jj1 >> 4; j;
2326 j = (unsigned int)j >> 1, i++)
2327 if (j & 1) {
2328 ieps++;
2329 value(d) *= bigtens[i];
André Goddard Rosae7347692010-02-05 18:32:52 -02002330 }
2331 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002332 if (k_check && value(d) < 1. && ilim > 0) {
2333 if (ilim1 <= 0)
2334 goto fast_failed;
2335 ilim = ilim1;
2336 k--;
2337 value(d) *= 10.;
2338 ieps++;
André Goddard Rosae7347692010-02-05 18:32:52 -02002339 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002340 value(eps) = ieps*value(d) + 7.;
2341 word0(eps) -= (P-1)*Exp_msk1;
2342 if (ilim == 0) {
2343 S = mhi = 0;
2344 value(d) -= 5.;
2345 if (value(d) > value(eps))
2346 goto one_digit;
2347 if (value(d) < -value(eps))
2348 goto no_digits;
2349 goto fast_failed;
André Goddard Rosae7347692010-02-05 18:32:52 -02002350 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002351#ifndef No_leftright
2352 if (leftright) {
2353 /* Use Steele & White method of only
2354 * generating digits needed.
2355 */
2356 value(eps) = 0.5/tens[ilim-1] - value(eps);
2357 for(i = 0;;) {
2358 L = value(d);
2359 value(d) -= L;
2360 *s++ = '0' + (int)L;
2361 if (value(d) < value(eps))
2362 goto ret1;
2363 if (1. - value(d) < value(eps))
2364 goto bump_up;
2365 if (++i >= ilim)
2366 break;
2367 value(eps) *= 10.;
2368 value(d) *= 10.;
2369 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002370 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002371 else {
2372#endif
2373 /* Generate ilim digits, then fix them up. */
2374 value(eps) *= tens[ilim-1];
2375 for(i = 1;; i++, value(d) *= 10.) {
2376 L = value(d);
2377 value(d) -= L;
2378 *s++ = '0' + (int)L;
2379 if (i == ilim) {
2380 if (value(d) > 0.5 + value(eps))
2381 goto bump_up;
2382 else if (value(d) < 0.5 - value(eps)) {
2383 while(*--s == '0');
2384 s++;
2385 goto ret1;
2386 }
2387 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002388 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002389 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002390#ifndef No_leftright
2391 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002392#endif
2393 fast_failed:
2394 s = s0;
2395 value(d) = value(d2);
2396 k = k0;
2397 ilim = ilim0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002398 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002399
2400 /* Do we have a "small" integer? */
2401
2402 if (be >= 0 && k <= Int_max) {
2403 /* Yes. */
2404 ds = tens[k];
2405 if (ndigits < 0 && ilim <= 0) {
2406 S = mhi = 0;
2407 if (ilim < 0 || value(d) <= 5*ds)
2408 goto no_digits;
2409 goto one_digit;
André Goddard Rosae7347692010-02-05 18:32:52 -02002410 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002411 for(i = 1;; i++) {
2412 L = value(d) / ds;
2413 value(d) -= L*ds;
2414#ifdef Check_FLT_ROUNDS
2415 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2416 if (value(d) < 0) {
2417 L--;
2418 value(d) += ds;
André Goddard Rosae7347692010-02-05 18:32:52 -02002419 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002420#endif
2421 *s++ = '0' + (int)L;
2422 if (i == ilim) {
2423 value(d) += value(d);
2424 if (value(d) > ds || (value(d) == ds && L & 1)) {
2425 bump_up:
2426 while(*--s == '9')
2427 if (s == s0) {
2428 k++;
2429 *s = '0';
2430 break;
André Goddard Rosae7347692010-02-05 18:32:52 -02002431 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002432 ++*s++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002433 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002434 break;
2435 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002436 if (!(value(d) *= 10.))
2437 break;
2438 }
2439 goto ret1;
André Goddard Rosae7347692010-02-05 18:32:52 -02002440 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002441
2442 m2 = b2;
2443 m5 = b5;
2444 mhi = mlo = 0;
2445 if (leftright) {
2446 if (mode < 2) {
2447 i =
2448#ifndef Sudden_Underflow
2449 denorm ? be + (Bias + (P-1) - 1 + 1) :
2450#endif
2451#ifdef IBM
2452 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2453#else
2454 1 + P - bbits;
2455#endif
André Goddard Rosae7347692010-02-05 18:32:52 -02002456 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002457 else {
2458 j = ilim - 1;
2459 if (m5 >= j)
2460 m5 -= j;
2461 else {
2462 s5 += j -= m5;
2463 b5 += j;
2464 m5 = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002465 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002466 if ((i = ilim) < 0) {
2467 m2 -= i;
2468 i = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002469 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002470 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002471 b2 += i;
2472 s2 += i;
2473 mhi = i2b(1);
André Goddard Rosae7347692010-02-05 18:32:52 -02002474 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002475 if (m2 > 0 && s2 > 0) {
2476 i = m2 < s2 ? m2 : s2;
2477 b2 -= i;
2478 m2 -= i;
2479 s2 -= i;
André Goddard Rosae7347692010-02-05 18:32:52 -02002480 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002481 if (b5 > 0) {
2482 if (leftright) {
2483 if (m5 > 0) {
2484 mhi = pow5mult(mhi, m5);
2485 b1 = mult(mhi, b);
2486 Bfree(b);
2487 b = b1;
André Goddard Rosae7347692010-02-05 18:32:52 -02002488 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002489 if ((j = b5 - m5) != 0)
2490 b = pow5mult(b, j);
2491 }
2492 else
2493 b = pow5mult(b, b5);
André Goddard Rosae7347692010-02-05 18:32:52 -02002494 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002495 S = i2b(1);
2496 if (s5 > 0)
2497 S = pow5mult(S, s5);
2498
2499 /* Check for special case that d is a normalized power of 2. */
2500
2501 if (mode < 2) {
2502 if (!word1(d) && !(word0(d) & Bndry_mask)
2503#ifndef Sudden_Underflow
2504 && word0(d) & Exp_mask
2505#endif
2506 ) {
2507 /* The special case */
2508 b2 += Log2P;
2509 s2 += Log2P;
2510 spec_case = 1;
2511 }
2512 else
2513 spec_case = 0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002514 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002515
2516 /* Arrange for convenient computation of quotients:
2517 * shift left if necessary so divisor has 4 leading 0 bits.
2518 *
2519 * Perhaps we should just compute leading 28 bits of S once
2520 * and for all and pass them and a shift to quorem, so it
2521 * can do shifts and ors to compute the numerator for q.
2522 */
David 'Digit' Turner81326262010-03-04 11:51:42 -08002523 if (S == BIGINT_INVALID) {
2524 i = 0;
2525 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002526#ifdef Pack_32
David 'Digit' Turner81326262010-03-04 11:51:42 -08002527 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
2528 i = 32 - i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002529#else
David 'Digit' Turner81326262010-03-04 11:51:42 -08002530 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2531 i = 16 - i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002532#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08002533 }
2534
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002535 if (i > 4) {
2536 i -= 4;
2537 b2 += i;
2538 m2 += i;
2539 s2 += i;
André Goddard Rosae7347692010-02-05 18:32:52 -02002540 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002541 else if (i < 4) {
2542 i += 28;
2543 b2 += i;
2544 m2 += i;
2545 s2 += i;
André Goddard Rosae7347692010-02-05 18:32:52 -02002546 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002547 if (b2 > 0)
2548 b = lshift(b, b2);
2549 if (s2 > 0)
2550 S = lshift(S, s2);
2551 if (k_check) {
2552 if (cmp(b,S) < 0) {
2553 k--;
2554 b = multadd(b, 10, 0); /* we botched the k estimate */
2555 if (leftright)
2556 mhi = multadd(mhi, 10, 0);
2557 ilim = ilim1;
2558 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002559 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002560 if (ilim <= 0 && mode > 2) {
2561 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2562 /* no digits, fcvt style */
2563 no_digits:
2564 k = -1 - ndigits;
2565 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02002566 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002567 one_digit:
2568 *s++ = '1';
2569 k++;
2570 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02002571 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002572 if (leftright) {
2573 if (m2 > 0)
2574 mhi = lshift(mhi, m2);
2575
2576 /* Compute mlo -- check for special case
2577 * that d is a normalized power of 2.
2578 */
2579
2580 mlo = mhi;
2581 if (spec_case) {
2582 mhi = Balloc(mhi->k);
2583 Bcopy(mhi, mlo);
2584 mhi = lshift(mhi, Log2P);
André Goddard Rosae7347692010-02-05 18:32:52 -02002585 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002586
2587 for(i = 1;;i++) {
2588 dig = quorem(b,S) + '0';
2589 /* Do we yet have the shortest decimal string
2590 * that will round to d?
2591 */
2592 j = cmp(b, mlo);
2593 delta = diff(S, mhi);
2594 jj1 = delta->sign ? 1 : cmp(b, delta);
2595 Bfree(delta);
2596#ifndef ROUND_BIASED
2597 if (jj1 == 0 && !mode && !(word1(d) & 1)) {
2598 if (dig == '9')
2599 goto round_9_up;
2600 if (j > 0)
2601 dig++;
2602 *s++ = dig;
2603 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02002604 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002605#endif
2606 if (j < 0 || (j == 0 && !mode
2607#ifndef ROUND_BIASED
2608 && !(word1(d) & 1)
2609#endif
2610 )) {
2611 if (jj1 > 0) {
2612 b = lshift(b, 1);
2613 jj1 = cmp(b, S);
2614 if ((jj1 > 0 || (jj1 == 0 && dig & 1))
2615 && dig++ == '9')
2616 goto round_9_up;
2617 }
2618 *s++ = dig;
2619 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02002620 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002621 if (jj1 > 0) {
2622 if (dig == '9') { /* possible if i == 1 */
2623 round_9_up:
2624 *s++ = '9';
2625 goto roundoff;
2626 }
2627 *s++ = dig + 1;
2628 goto ret;
André Goddard Rosae7347692010-02-05 18:32:52 -02002629 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002630 *s++ = dig;
2631 if (i == ilim)
2632 break;
2633 b = multadd(b, 10, 0);
2634 if (mlo == mhi)
2635 mlo = mhi = multadd(mhi, 10, 0);
2636 else {
2637 mlo = multadd(mlo, 10, 0);
2638 mhi = multadd(mhi, 10, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002639 }
2640 }
André Goddard Rosae7347692010-02-05 18:32:52 -02002641 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002642 else
2643 for(i = 1;; i++) {
2644 *s++ = dig = quorem(b,S) + '0';
2645 if (i >= ilim)
2646 break;
2647 b = multadd(b, 10, 0);
André Goddard Rosae7347692010-02-05 18:32:52 -02002648 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002649
2650 /* Round off last digit */
2651
2652 b = lshift(b, 1);
2653 j = cmp(b, S);
2654 if (j > 0 || (j == 0 && dig & 1)) {
2655 roundoff:
2656 while(*--s == '9')
2657 if (s == s0) {
2658 k++;
2659 *s++ = '1';
2660 goto ret;
2661 }
2662 ++*s++;
André Goddard Rosae7347692010-02-05 18:32:52 -02002663 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002664 else {
2665 while(*--s == '0');
2666 s++;
André Goddard Rosae7347692010-02-05 18:32:52 -02002667 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002668 ret:
2669 Bfree(S);
2670 if (mhi) {
2671 if (mlo && mlo != mhi)
2672 Bfree(mlo);
2673 Bfree(mhi);
André Goddard Rosae7347692010-02-05 18:32:52 -02002674 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002675 ret1:
2676 Bfree(b);
2677 if (s == s0) { /* don't return empty string */
2678 *s++ = '0';
2679 k = 0;
2680 }
2681 *s = 0;
2682 *decpt = k + 1;
2683 if (rve)
2684 *rve = s;
2685 return s0;
André Goddard Rosae7347692010-02-05 18:32:52 -02002686}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002687#ifdef __cplusplus
2688}
2689#endif