blob: b1f5868c3ecf31e7bdc52986a7ff1514468a2d06 [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
94#ifdef ANDROID_CHANGES
95#include <pthread.h>
96#define mutex_lock(x) pthread_mutex_lock(x)
97#define mutex_unlock(x) pthread_mutex_unlock(x)
98#endif
99
100#include <sys/cdefs.h>
101#if defined(LIBC_SCCS) && !defined(lint)
102__RCSID("$NetBSD: strtod.c,v 1.45.2.1 2005/04/19 13:35:54 tron Exp $");
103#endif /* LIBC_SCCS and not lint */
104
105#define Unsigned_Shifts
106#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
107 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
108 defined(__powerpc__) || defined(__sh__) || defined(__x86_64__) || \
109 defined(__hppa__) || \
110 (defined(__arm__) && defined(__VFP_FP__))
111#include <endian.h>
112#if BYTE_ORDER == BIG_ENDIAN
113#define IEEE_BIG_ENDIAN
114#else
115#define IEEE_LITTLE_ENDIAN
116#endif
117#endif
118
119#if defined(__arm__) && !defined(__VFP_FP__)
120/*
121 * Although the CPU is little endian the FP has different
122 * byte and word endianness. The byte order is still little endian
123 * but the word order is big endian.
124 */
125#define IEEE_BIG_ENDIAN
126#endif
127
128#ifdef __vax__
129#define VAX
130#endif
131
132#if defined(__hppa__) || defined(__mips__) || defined(__sh__)
133#define NAN_WORD0 0x7ff40000
134#else
135#define NAN_WORD0 0x7ff80000
136#endif
137#define NAN_WORD1 0
138
139#define Long int32_t
140#define ULong u_int32_t
141
142#ifdef DEBUG
143#include "stdio.h"
144#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
145#endif
146
147#ifdef __cplusplus
148#include "malloc.h"
149#include "memory.h"
150#else
151#ifndef KR_headers
152#include "stdlib.h"
153#include "string.h"
154#ifndef ANDROID_CHANGES
155#include "locale.h"
156#endif /* ANDROID_CHANGES */
157#else
158#include "malloc.h"
159#include "memory.h"
160#endif
161#endif
162#ifndef ANDROID_CHANGES
163#include "extern.h"
164#include "reentrant.h"
165#endif /* ANDROID_CHANGES */
166
167#ifdef MALLOC
168#ifdef KR_headers
169extern char *MALLOC();
170#else
171extern void *MALLOC(size_t);
172#endif
173#else
174#define MALLOC malloc
175#endif
176
177#include "ctype.h"
178#include "errno.h"
179#include "float.h"
180
181#ifndef __MATH_H__
182#include "math.h"
183#endif
184
185#ifdef __cplusplus
186extern "C" {
187#endif
188
189#ifndef CONST
190#ifdef KR_headers
191#define CONST /* blank */
192#else
193#define CONST const
194#endif
195#endif
196
197#ifdef Unsigned_Shifts
198#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
199#else
200#define Sign_Extend(a,b) /*no-op*/
201#endif
202
203#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
204 defined(IBM) != 1
205Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
206IBM should be defined.
207#endif
208
209typedef union {
210 double d;
211 ULong ul[2];
212} _double;
213#define value(x) ((x).d)
214#ifdef IEEE_LITTLE_ENDIAN
215#define word0(x) ((x).ul[1])
216#define word1(x) ((x).ul[0])
217#else
218#define word0(x) ((x).ul[0])
219#define word1(x) ((x).ul[1])
220#endif
221
222/* The following definition of Storeinc is appropriate for MIPS processors.
223 * An alternative that might be better on some machines is
224 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
225 */
226#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
227#define Storeinc(a,b,c) \
228 (((u_short *)(void *)a)[1] = \
229 (u_short)b, ((u_short *)(void *)a)[0] = (u_short)c, a++)
230#else
231#define Storeinc(a,b,c) \
232 (((u_short *)(void *)a)[0] = \
233 (u_short)b, ((u_short *)(void *)a)[1] = (u_short)c, a++)
234#endif
235
236/* #define P DBL_MANT_DIG */
237/* Ten_pmax = floor(P*log(2)/log(5)) */
238/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
239/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
240/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
241
242#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
243#define Exp_shift 20
244#define Exp_shift1 20
245#define Exp_msk1 0x100000
246#define Exp_msk11 0x100000
247#define Exp_mask 0x7ff00000
248#define P 53
249#define Bias 1023
250#define IEEE_Arith
251#define Emin (-1022)
252#define Exp_1 0x3ff00000
253#define Exp_11 0x3ff00000
254#define Ebits 11
255#define Frac_mask 0xfffff
256#define Frac_mask1 0xfffff
257#define Ten_pmax 22
258#define Bletch 0x10
259#define Bndry_mask 0xfffff
260#define Bndry_mask1 0xfffff
261#define LSB 1
262#define Sign_bit 0x80000000
263#define Log2P 1
264#define Tiny0 0
265#define Tiny1 1
266#define Quick_max 14
267#define Int_max 14
268#define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
269#else
270#undef Sudden_Underflow
271#define Sudden_Underflow
272#ifdef IBM
273#define Exp_shift 24
274#define Exp_shift1 24
275#define Exp_msk1 0x1000000
276#define Exp_msk11 0x1000000
277#define Exp_mask 0x7f000000
278#define P 14
279#define Bias 65
280#define Exp_1 0x41000000
281#define Exp_11 0x41000000
282#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
283#define Frac_mask 0xffffff
284#define Frac_mask1 0xffffff
285#define Bletch 4
286#define Ten_pmax 22
287#define Bndry_mask 0xefffff
288#define Bndry_mask1 0xffffff
289#define LSB 1
290#define Sign_bit 0x80000000
291#define Log2P 4
292#define Tiny0 0x100000
293#define Tiny1 0
294#define Quick_max 14
295#define Int_max 15
296#else /* VAX */
297#define Exp_shift 23
298#define Exp_shift1 7
299#define Exp_msk1 0x80
300#define Exp_msk11 0x800000
301#define Exp_mask 0x7f80
302#define P 56
303#define Bias 129
304#define Exp_1 0x40800000
305#define Exp_11 0x4080
306#define Ebits 8
307#define Frac_mask 0x7fffff
308#define Frac_mask1 0xffff007f
309#define Ten_pmax 24
310#define Bletch 2
311#define Bndry_mask 0xffff007f
312#define Bndry_mask1 0xffff007f
313#define LSB 0x10000
314#define Sign_bit 0x8000
315#define Log2P 1
316#define Tiny0 0x80
317#define Tiny1 0
318#define Quick_max 15
319#define Int_max 15
320#endif
321#endif
322
323#ifndef IEEE_Arith
324#define ROUND_BIASED
325#endif
326
327#ifdef RND_PRODQUOT
328#define rounded_product(a,b) a = rnd_prod(a, b)
329#define rounded_quotient(a,b) a = rnd_quot(a, b)
330#ifdef KR_headers
331extern double rnd_prod(), rnd_quot();
332#else
333extern double rnd_prod(double, double), rnd_quot(double, double);
334#endif
335#else
336#define rounded_product(a,b) a *= b
337#define rounded_quotient(a,b) a /= b
338#endif
339
340#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
341#define Big1 0xffffffff
342
343#ifndef Just_16
344/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
345 * This makes some inner loops simpler and sometimes saves work
346 * during multiplications, but it often seems to make things slightly
347 * slower. Hence the default is now to store 32 bits per Long.
348 */
349#ifndef Pack_32
350#define Pack_32
351#endif
352#endif
353
354#define Kmax 15
355
356#ifdef __cplusplus
357extern "C" double strtod(const char *s00, char **se);
358extern "C" char *__dtoa(double d, int mode, int ndigits,
359 int *decpt, int *sign, char **rve);
360#endif
361
362 struct
363Bigint {
364 struct Bigint *next;
365 int k, maxwds, sign, wds;
366 ULong x[1];
367 };
368
369 typedef struct Bigint Bigint;
370
371 static Bigint *freelist[Kmax+1];
372
373#ifdef ANDROID_CHANGES
374 static pthread_mutex_t freelist_mutex = PTHREAD_MUTEX_INITIALIZER;
375#else
376#ifdef _REENTRANT
377 static mutex_t freelist_mutex = MUTEX_INITIALIZER;
378#endif
379#endif
380
David 'Digit' Turner81326262010-03-04 11:51:42 -0800381/* Special value used to indicate an invalid Bigint value,
382 * e.g. when a memory allocation fails. The idea is that we
383 * want to avoid introducing NULL checks everytime a bigint
384 * computation is performed. Also the NULL value can also be
385 * already used to indicate "value not initialized yet" and
386 * returning NULL might alter the execution code path in
387 * case of OOM.
388 */
389#define BIGINT_INVALID ((Bigint *)&bigint_invalid_value)
390
391static const Bigint bigint_invalid_value;
392
393
394/* Return BIGINT_INVALID on allocation failure.
395 *
396 * Most of the code here depends on the fact that this function
397 * never returns NULL.
398 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399 static Bigint *
400Balloc
401#ifdef KR_headers
402 (k) int k;
403#else
404 (int k)
405#endif
406{
407 int x;
408 Bigint *rv;
409
410 mutex_lock(&freelist_mutex);
411
412 if ((rv = freelist[k]) != NULL) {
413 freelist[k] = rv->next;
414 }
415 else {
416 x = 1 << k;
417 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
David 'Digit' Turner81326262010-03-04 11:51:42 -0800418 if (rv == NULL) {
419 rv = BIGINT_INVALID;
420 goto EXIT;
421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422 rv->k = k;
423 rv->maxwds = x;
424 }
425 rv->sign = rv->wds = 0;
David 'Digit' Turner81326262010-03-04 11:51:42 -0800426EXIT:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427 mutex_unlock(&freelist_mutex);
428
429 return rv;
430 }
431
432 static void
433Bfree
434#ifdef KR_headers
435 (v) Bigint *v;
436#else
437 (Bigint *v)
438#endif
439{
David 'Digit' Turner81326262010-03-04 11:51:42 -0800440 if (v && v != BIGINT_INVALID) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441 mutex_lock(&freelist_mutex);
442
443 v->next = freelist[v->k];
444 freelist[v->k] = v;
445
446 mutex_unlock(&freelist_mutex);
447 }
448 }
449
David 'Digit' Turner81326262010-03-04 11:51:42 -0800450#define Bcopy_valid(x,y) memcpy(&(x)->sign, &(y)->sign, \
451 (y)->wds*sizeof(Long) + 2*sizeof(int))
452
453#define Bcopy(x,y) Bcopy_ptr(&(x),(y))
454
455 static void
456Bcopy_ptr(Bigint **px, Bigint *y)
457{
458 if (*px == BIGINT_INVALID)
459 return; /* no space to store copy */
460 if (y == BIGINT_INVALID) {
461 Bfree(*px); /* invalid input */
462 *px = BIGINT_INVALID;
463 } else {
464 Bcopy_valid(*px,y);
465 }
466}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467
468 static Bigint *
469multadd
470#ifdef KR_headers
471 (b, m, a) Bigint *b; int m, a;
472#else
473 (Bigint *b, int m, int a) /* multiply by m and add a */
474#endif
475{
476 int i, wds;
477 ULong *x, y;
478#ifdef Pack_32
479 ULong xi, z;
480#endif
481 Bigint *b1;
482
David 'Digit' Turner81326262010-03-04 11:51:42 -0800483 if (b == BIGINT_INVALID)
484 return b;
485
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800486 wds = b->wds;
487 x = b->x;
488 i = 0;
489 do {
490#ifdef Pack_32
491 xi = *x;
492 y = (xi & 0xffff) * m + a;
493 z = (xi >> 16) * m + (y >> 16);
494 a = (int)(z >> 16);
495 *x++ = (z << 16) + (y & 0xffff);
496#else
497 y = *x * m + a;
498 a = (int)(y >> 16);
499 *x++ = y & 0xffff;
500#endif
501 }
502 while(++i < wds);
503 if (a) {
504 if (wds >= b->maxwds) {
505 b1 = Balloc(b->k+1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800506 if (b1 == BIGINT_INVALID) {
507 Bfree(b);
508 return b1;
509 }
510 Bcopy_valid(b1, b);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800511 Bfree(b);
512 b = b1;
513 }
514 b->x[wds++] = a;
515 b->wds = wds;
516 }
517 return b;
518 }
519
520 static Bigint *
521s2b
522#ifdef KR_headers
523 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
524#else
525 (CONST char *s, int nd0, int nd, ULong y9)
526#endif
527{
528 Bigint *b;
529 int i, k;
530 Long x, y;
531
532 x = (nd + 8) / 9;
533 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
534#ifdef Pack_32
535 b = Balloc(k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800536 if (b == BIGINT_INVALID)
537 return b;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538 b->x[0] = y9;
539 b->wds = 1;
540#else
541 b = Balloc(k+1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800542 if (b == BIGINT_INVALID)
543 return b;
544
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800545 b->x[0] = y9 & 0xffff;
546 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
547#endif
548
549 i = 9;
550 if (9 < nd0) {
551 s += 9;
552 do b = multadd(b, 10, *s++ - '0');
553 while(++i < nd0);
554 s++;
555 }
556 else
557 s += 10;
558 for(; i < nd; i++)
559 b = multadd(b, 10, *s++ - '0');
560 return b;
561 }
562
563 static int
564hi0bits
565#ifdef KR_headers
566 (x) ULong x;
567#else
568 (ULong x)
569#endif
570{
571 int k = 0;
572
573 if (!(x & 0xffff0000)) {
574 k = 16;
575 x <<= 16;
576 }
577 if (!(x & 0xff000000)) {
578 k += 8;
579 x <<= 8;
580 }
581 if (!(x & 0xf0000000)) {
582 k += 4;
583 x <<= 4;
584 }
585 if (!(x & 0xc0000000)) {
586 k += 2;
587 x <<= 2;
588 }
589 if (!(x & 0x80000000)) {
590 k++;
591 if (!(x & 0x40000000))
592 return 32;
593 }
594 return k;
595 }
596
597 static int
598lo0bits
599#ifdef KR_headers
600 (y) ULong *y;
601#else
602 (ULong *y)
603#endif
604{
605 int k;
606 ULong x = *y;
607
608 if (x & 7) {
609 if (x & 1)
610 return 0;
611 if (x & 2) {
612 *y = x >> 1;
613 return 1;
614 }
615 *y = x >> 2;
616 return 2;
617 }
618 k = 0;
619 if (!(x & 0xffff)) {
620 k = 16;
621 x >>= 16;
622 }
623 if (!(x & 0xff)) {
624 k += 8;
625 x >>= 8;
626 }
627 if (!(x & 0xf)) {
628 k += 4;
629 x >>= 4;
630 }
631 if (!(x & 0x3)) {
632 k += 2;
633 x >>= 2;
634 }
635 if (!(x & 1)) {
636 k++;
637 x >>= 1;
638 if (!x & 1)
639 return 32;
640 }
641 *y = x;
642 return k;
643 }
644
645 static Bigint *
646i2b
647#ifdef KR_headers
648 (i) int i;
649#else
650 (int i)
651#endif
652{
653 Bigint *b;
654
655 b = Balloc(1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800656 if (b != BIGINT_INVALID) {
657 b->x[0] = i;
658 b->wds = 1;
659 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660 return b;
661 }
662
663 static Bigint *
664mult
665#ifdef KR_headers
666 (a, b) Bigint *a, *b;
667#else
668 (Bigint *a, Bigint *b)
669#endif
670{
671 Bigint *c;
672 int k, wa, wb, wc;
673 ULong carry, y, z;
674 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
675#ifdef Pack_32
676 ULong z2;
677#endif
678
David 'Digit' Turner81326262010-03-04 11:51:42 -0800679 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
680 return BIGINT_INVALID;
681
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682 if (a->wds < b->wds) {
683 c = a;
684 a = b;
685 b = c;
686 }
687 k = a->k;
688 wa = a->wds;
689 wb = b->wds;
690 wc = wa + wb;
691 if (wc > a->maxwds)
692 k++;
693 c = Balloc(k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800694 if (c == BIGINT_INVALID)
695 return c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696 for(x = c->x, xa = x + wc; x < xa; x++)
697 *x = 0;
698 xa = a->x;
699 xae = xa + wa;
700 xb = b->x;
701 xbe = xb + wb;
702 xc0 = c->x;
703#ifdef Pack_32
704 for(; xb < xbe; xb++, xc0++) {
705 if ((y = *xb & 0xffff) != 0) {
706 x = xa;
707 xc = xc0;
708 carry = 0;
709 do {
710 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
711 carry = z >> 16;
712 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
713 carry = z2 >> 16;
714 Storeinc(xc, z2, z);
715 }
716 while(x < xae);
717 *xc = carry;
718 }
719 if ((y = *xb >> 16) != 0) {
720 x = xa;
721 xc = xc0;
722 carry = 0;
723 z2 = *xc;
724 do {
725 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
726 carry = z >> 16;
727 Storeinc(xc, z, z2);
728 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
729 carry = z2 >> 16;
730 }
731 while(x < xae);
732 *xc = z2;
733 }
734 }
735#else
736 for(; xb < xbe; xc0++) {
737 if (y = *xb++) {
738 x = xa;
739 xc = xc0;
740 carry = 0;
741 do {
742 z = *x++ * y + *xc + carry;
743 carry = z >> 16;
744 *xc++ = z & 0xffff;
745 }
746 while(x < xae);
747 *xc = carry;
748 }
749 }
750#endif
751 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
752 c->wds = wc;
753 return c;
754 }
755
756 static Bigint *p5s;
757
758 static Bigint *
759pow5mult
760#ifdef KR_headers
761 (b, k) Bigint *b; int k;
762#else
763 (Bigint *b, int k)
764#endif
765{
766 Bigint *b1, *p5, *p51;
767 int i;
768 static const int p05[3] = { 5, 25, 125 };
769
David 'Digit' Turner81326262010-03-04 11:51:42 -0800770 if (b == BIGINT_INVALID)
771 return b;
772
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800773 if ((i = k & 3) != 0)
774 b = multadd(b, p05[i-1], 0);
775
776 if (!(k = (unsigned int) k >> 2))
777 return b;
778 if (!(p5 = p5s)) {
779 /* first time */
David 'Digit' Turner81326262010-03-04 11:51:42 -0800780 p5 = i2b(625);
781 if (p5 == BIGINT_INVALID) {
782 Bfree(b);
783 return p5;
784 }
785 p5s = p5;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786 p5->next = 0;
787 }
788 for(;;) {
789 if (k & 1) {
790 b1 = mult(b, p5);
791 Bfree(b);
792 b = b1;
793 }
794 if (!(k = (unsigned int) k >> 1))
795 break;
796 if (!(p51 = p5->next)) {
David 'Digit' Turner81326262010-03-04 11:51:42 -0800797 p51 = mult(p5,p5);
798 if (p51 == BIGINT_INVALID) {
799 Bfree(b);
800 return p51;
801 }
802 p5->next = p51;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803 p51->next = 0;
804 }
805 p5 = p51;
806 }
807 return b;
808 }
809
810 static Bigint *
811lshift
812#ifdef KR_headers
813 (b, k) Bigint *b; int k;
814#else
815 (Bigint *b, int k)
816#endif
817{
818 int i, k1, n, n1;
819 Bigint *b1;
820 ULong *x, *x1, *xe, z;
821
David 'Digit' Turner81326262010-03-04 11:51:42 -0800822 if (b == BIGINT_INVALID)
823 return b;
824
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800825#ifdef Pack_32
826 n = (unsigned int)k >> 5;
827#else
828 n = (unsigned int)k >> 4;
829#endif
830 k1 = b->k;
831 n1 = n + b->wds + 1;
832 for(i = b->maxwds; n1 > i; i <<= 1)
833 k1++;
834 b1 = Balloc(k1);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800835 if (b1 == BIGINT_INVALID) {
836 Bfree(b);
837 return b1;
838 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800839 x1 = b1->x;
840 for(i = 0; i < n; i++)
841 *x1++ = 0;
842 x = b->x;
843 xe = x + b->wds;
844#ifdef Pack_32
845 if (k &= 0x1f) {
846 k1 = 32 - k;
847 z = 0;
848 do {
849 *x1++ = *x << k | z;
850 z = *x++ >> k1;
851 }
852 while(x < xe);
853 if ((*x1 = z) != 0)
854 ++n1;
855 }
856#else
857 if (k &= 0xf) {
858 k1 = 16 - k;
859 z = 0;
860 do {
861 *x1++ = *x << k & 0xffff | z;
862 z = *x++ >> k1;
863 }
864 while(x < xe);
865 if (*x1 = z)
866 ++n1;
867 }
868#endif
869 else do
870 *x1++ = *x++;
871 while(x < xe);
872 b1->wds = n1 - 1;
873 Bfree(b);
874 return b1;
875 }
876
877 static int
878cmp
879#ifdef KR_headers
880 (a, b) Bigint *a, *b;
881#else
882 (Bigint *a, Bigint *b)
883#endif
884{
885 ULong *xa, *xa0, *xb, *xb0;
886 int i, j;
887
David 'Digit' Turner81326262010-03-04 11:51:42 -0800888 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
889#ifdef DEBUG
890 Bug("cmp called with a or b invalid");
891#else
892 return 0; /* equal - the best we can do right now */
893#endif
894
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895 i = a->wds;
896 j = b->wds;
897#ifdef DEBUG
898 if (i > 1 && !a->x[i-1])
899 Bug("cmp called with a->x[a->wds-1] == 0");
900 if (j > 1 && !b->x[j-1])
901 Bug("cmp called with b->x[b->wds-1] == 0");
902#endif
903 if (i -= j)
904 return i;
905 xa0 = a->x;
906 xa = xa0 + j;
907 xb0 = b->x;
908 xb = xb0 + j;
909 for(;;) {
910 if (*--xa != *--xb)
911 return *xa < *xb ? -1 : 1;
912 if (xa <= xa0)
913 break;
914 }
915 return 0;
916 }
917
918 static Bigint *
919diff
920#ifdef KR_headers
921 (a, b) Bigint *a, *b;
922#else
923 (Bigint *a, Bigint *b)
924#endif
925{
926 Bigint *c;
927 int i, wa, wb;
928 Long borrow, y; /* We need signed shifts here. */
929 ULong *xa, *xae, *xb, *xbe, *xc;
930#ifdef Pack_32
931 Long z;
932#endif
933
David 'Digit' Turner81326262010-03-04 11:51:42 -0800934 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
935 return BIGINT_INVALID;
936
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937 i = cmp(a,b);
938 if (!i) {
939 c = Balloc(0);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800940 if (c != BIGINT_INVALID) {
941 c->wds = 1;
942 c->x[0] = 0;
943 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944 return c;
945 }
946 if (i < 0) {
947 c = a;
948 a = b;
949 b = c;
950 i = 1;
951 }
952 else
953 i = 0;
954 c = Balloc(a->k);
David 'Digit' Turner81326262010-03-04 11:51:42 -0800955 if (c == BIGINT_INVALID)
956 return c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800957 c->sign = i;
958 wa = a->wds;
959 xa = a->x;
960 xae = xa + wa;
961 wb = b->wds;
962 xb = b->x;
963 xbe = xb + wb;
964 xc = c->x;
965 borrow = 0;
966#ifdef Pack_32
967 do {
968 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
969 borrow = (ULong)y >> 16;
970 Sign_Extend(borrow, y);
971 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
972 borrow = (ULong)z >> 16;
973 Sign_Extend(borrow, z);
974 Storeinc(xc, z, y);
975 }
976 while(xb < xbe);
977 while(xa < xae) {
978 y = (*xa & 0xffff) + borrow;
979 borrow = (ULong)y >> 16;
980 Sign_Extend(borrow, y);
981 z = (*xa++ >> 16) + borrow;
982 borrow = (ULong)z >> 16;
983 Sign_Extend(borrow, z);
984 Storeinc(xc, z, y);
985 }
986#else
987 do {
988 y = *xa++ - *xb++ + borrow;
989 borrow = y >> 16;
990 Sign_Extend(borrow, y);
991 *xc++ = y & 0xffff;
992 }
993 while(xb < xbe);
994 while(xa < xae) {
995 y = *xa++ + borrow;
996 borrow = y >> 16;
997 Sign_Extend(borrow, y);
998 *xc++ = y & 0xffff;
999 }
1000#endif
1001 while(!*--xc)
1002 wa--;
1003 c->wds = wa;
1004 return c;
1005 }
1006
1007 static double
1008ulp
1009#ifdef KR_headers
1010 (_x) double _x;
1011#else
1012 (double _x)
1013#endif
1014{
1015 _double x;
1016 Long L;
1017 _double a;
1018
1019 value(x) = _x;
1020 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1021#ifndef Sudden_Underflow
1022 if (L > 0) {
1023#endif
1024#ifdef IBM
1025 L |= Exp_msk1 >> 4;
1026#endif
1027 word0(a) = L;
1028 word1(a) = 0;
1029#ifndef Sudden_Underflow
1030 }
1031 else {
1032 L = (ULong)-L >> Exp_shift;
1033 if (L < Exp_shift) {
1034 word0(a) = 0x80000 >> L;
1035 word1(a) = 0;
1036 }
1037 else {
1038 word0(a) = 0;
1039 L -= Exp_shift;
1040 word1(a) = L >= 31 ? 1 : 1 << (31 - L);
1041 }
1042 }
1043#endif
1044 return value(a);
1045 }
1046
1047 static double
1048b2d
1049#ifdef KR_headers
1050 (a, e) Bigint *a; int *e;
1051#else
1052 (Bigint *a, int *e)
1053#endif
1054{
1055 ULong *xa, *xa0, w, y, z;
1056 int k;
1057 _double d;
1058#ifdef VAX
1059 ULong d0, d1;
1060#else
1061#define d0 word0(d)
1062#define d1 word1(d)
1063#endif
1064
David 'Digit' Turner81326262010-03-04 11:51:42 -08001065 if (a == BIGINT_INVALID)
1066 return NAN;
1067
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068 xa0 = a->x;
1069 xa = xa0 + a->wds;
1070 y = *--xa;
1071#ifdef DEBUG
1072 if (!y) Bug("zero y in b2d");
1073#endif
1074 k = hi0bits(y);
1075 *e = 32 - k;
1076#ifdef Pack_32
1077 if (k < Ebits) {
1078 d0 = Exp_1 | y >> (Ebits - k);
1079 w = xa > xa0 ? *--xa : 0;
1080 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1081 goto ret_d;
1082 }
1083 z = xa > xa0 ? *--xa : 0;
1084 if (k -= Ebits) {
1085 d0 = Exp_1 | y << k | z >> (32 - k);
1086 y = xa > xa0 ? *--xa : 0;
1087 d1 = z << k | y >> (32 - k);
1088 }
1089 else {
1090 d0 = Exp_1 | y;
1091 d1 = z;
1092 }
1093#else
1094 if (k < Ebits + 16) {
1095 z = xa > xa0 ? *--xa : 0;
1096 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1097 w = xa > xa0 ? *--xa : 0;
1098 y = xa > xa0 ? *--xa : 0;
1099 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1100 goto ret_d;
1101 }
1102 z = xa > xa0 ? *--xa : 0;
1103 w = xa > xa0 ? *--xa : 0;
1104 k -= Ebits + 16;
1105 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1106 y = xa > xa0 ? *--xa : 0;
1107 d1 = w << k + 16 | y << k;
1108#endif
1109 ret_d:
1110#ifdef VAX
1111 word0(d) = d0 >> 16 | d0 << 16;
1112 word1(d) = d1 >> 16 | d1 << 16;
1113#else
1114#undef d0
1115#undef d1
1116#endif
1117 return value(d);
1118 }
1119
1120 static Bigint *
1121d2b
1122#ifdef KR_headers
1123 (_d, e, bits) double d; int *e, *bits;
1124#else
1125 (double _d, int *e, int *bits)
1126#endif
1127{
1128 Bigint *b;
1129 int de, i, k;
1130 ULong *x, y, z;
1131 _double d;
1132#ifdef VAX
1133 ULong d0, d1;
1134#endif
1135
1136 value(d) = _d;
1137#ifdef VAX
1138 d0 = word0(d) >> 16 | word0(d) << 16;
1139 d1 = word1(d) >> 16 | word1(d) << 16;
1140#else
1141#define d0 word0(d)
1142#define d1 word1(d)
1143#endif
1144
1145#ifdef Pack_32
1146 b = Balloc(1);
1147#else
1148 b = Balloc(2);
1149#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08001150 if (b == BIGINT_INVALID)
1151 return b;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001152 x = b->x;
1153
1154 z = d0 & Frac_mask;
1155 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1156#ifdef Sudden_Underflow
1157 de = (int)(d0 >> Exp_shift);
1158#ifndef IBM
1159 z |= Exp_msk11;
1160#endif
1161#else
1162 if ((de = (int)(d0 >> Exp_shift)) != 0)
1163 z |= Exp_msk1;
1164#endif
1165#ifdef Pack_32
1166 if ((y = d1) != 0) {
1167 if ((k = lo0bits(&y)) != 0) {
1168 x[0] = y | z << (32 - k);
1169 z >>= k;
1170 }
1171 else
1172 x[0] = y;
1173 i = b->wds = (x[1] = z) ? 2 : 1;
1174 }
1175 else {
1176#ifdef DEBUG
1177 if (!z)
1178 Bug("Zero passed to d2b");
1179#endif
1180 k = lo0bits(&z);
1181 x[0] = z;
1182 i = b->wds = 1;
1183 k += 32;
1184 }
1185#else
1186 if (y = d1) {
1187 if (k = lo0bits(&y))
1188 if (k >= 16) {
1189 x[0] = y | z << 32 - k & 0xffff;
1190 x[1] = z >> k - 16 & 0xffff;
1191 x[2] = z >> k;
1192 i = 2;
1193 }
1194 else {
1195 x[0] = y & 0xffff;
1196 x[1] = y >> 16 | z << 16 - k & 0xffff;
1197 x[2] = z >> k & 0xffff;
1198 x[3] = z >> k+16;
1199 i = 3;
1200 }
1201 else {
1202 x[0] = y & 0xffff;
1203 x[1] = y >> 16;
1204 x[2] = z & 0xffff;
1205 x[3] = z >> 16;
1206 i = 3;
1207 }
1208 }
1209 else {
1210#ifdef DEBUG
1211 if (!z)
1212 Bug("Zero passed to d2b");
1213#endif
1214 k = lo0bits(&z);
1215 if (k >= 16) {
1216 x[0] = z;
1217 i = 0;
1218 }
1219 else {
1220 x[0] = z & 0xffff;
1221 x[1] = z >> 16;
1222 i = 1;
1223 }
1224 k += 32;
1225 }
1226 while(!x[i])
1227 --i;
1228 b->wds = i + 1;
1229#endif
1230#ifndef Sudden_Underflow
1231 if (de) {
1232#endif
1233#ifdef IBM
1234 *e = (de - Bias - (P-1) << 2) + k;
1235 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1236#else
1237 *e = de - Bias - (P-1) + k;
1238 *bits = P - k;
1239#endif
1240#ifndef Sudden_Underflow
1241 }
1242 else {
1243 *e = de - Bias - (P-1) + 1 + k;
1244#ifdef Pack_32
1245 *bits = 32*i - hi0bits(x[i-1]);
1246#else
1247 *bits = (i+2)*16 - hi0bits(x[i]);
1248#endif
1249 }
1250#endif
1251 return b;
1252 }
1253#undef d0
1254#undef d1
1255
1256 static double
1257ratio
1258#ifdef KR_headers
1259 (a, b) Bigint *a, *b;
1260#else
1261 (Bigint *a, Bigint *b)
1262#endif
1263{
1264 _double da, db;
1265 int k, ka, kb;
1266
David 'Digit' Turner81326262010-03-04 11:51:42 -08001267 if (a == BIGINT_INVALID || b == BIGINT_INVALID)
1268 return NAN; /* for lack of better value ? */
1269
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001270 value(da) = b2d(a, &ka);
1271 value(db) = b2d(b, &kb);
1272#ifdef Pack_32
1273 k = ka - kb + 32*(a->wds - b->wds);
1274#else
1275 k = ka - kb + 16*(a->wds - b->wds);
1276#endif
1277#ifdef IBM
1278 if (k > 0) {
1279 word0(da) += (k >> 2)*Exp_msk1;
1280 if (k &= 3)
1281 da *= 1 << k;
1282 }
1283 else {
1284 k = -k;
1285 word0(db) += (k >> 2)*Exp_msk1;
1286 if (k &= 3)
1287 db *= 1 << k;
1288 }
1289#else
1290 if (k > 0)
1291 word0(da) += k*Exp_msk1;
1292 else {
1293 k = -k;
1294 word0(db) += k*Exp_msk1;
1295 }
1296#endif
1297 return value(da) / value(db);
1298 }
1299
1300static CONST double
1301tens[] = {
1302 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1303 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1304 1e20, 1e21, 1e22
1305#ifdef VAX
1306 , 1e23, 1e24
1307#endif
1308 };
1309
1310#ifdef IEEE_Arith
1311static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1312static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1313#define n_bigtens 5
1314#else
1315#ifdef IBM
1316static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1317static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1318#define n_bigtens 3
1319#else
1320static CONST double bigtens[] = { 1e16, 1e32 };
1321static CONST double tinytens[] = { 1e-16, 1e-32 };
1322#define n_bigtens 2
1323#endif
1324#endif
1325
1326 double
1327strtod
1328#ifdef KR_headers
1329 (s00, se) CONST char *s00; char **se;
1330#else
1331 (CONST char *s00, char **se)
1332#endif
1333{
1334 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1335 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1336 CONST char *s, *s0, *s1;
1337 double aadj, aadj1, adj;
1338 _double rv, rv0;
1339 Long L;
1340 ULong y, z;
1341 Bigint *bb1, *bd0;
1342 Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
1343
1344#ifdef ANDROID_CHANGES
1345 CONST char decimal_point = '.';
1346#else /* ANDROID_CHANGES */
1347#ifndef KR_headers
1348 CONST char decimal_point = localeconv()->decimal_point[0];
1349#else
1350 CONST char decimal_point = '.';
1351#endif
1352
1353#endif /* ANDROID_CHANGES */
1354
1355 sign = nz0 = nz = 0;
1356 value(rv) = 0.;
1357
1358
1359 for(s = s00; isspace((unsigned char) *s); s++)
1360 ;
1361
1362 if (*s == '-') {
1363 sign = 1;
1364 s++;
1365 } else if (*s == '+') {
1366 s++;
1367 }
1368
1369 if (*s == '\0') {
1370 s = s00;
1371 goto ret;
1372 }
1373
1374 /* "INF" or "INFINITY" */
1375 if (tolower((unsigned char)*s) == 'i' && strncasecmp(s, "inf", 3) == 0) {
1376 if (strncasecmp(s + 3, "inity", 5) == 0)
1377 s += 8;
1378 else
1379 s += 3;
1380
1381 value(rv) = HUGE_VAL;
1382 goto ret;
1383 }
1384
1385#ifdef IEEE_Arith
1386 /* "NAN" or "NAN(n-char-sequence-opt)" */
1387 if (tolower((unsigned char)*s) == 'n' && strncasecmp(s, "nan", 3) == 0) {
1388 /* Build a quiet NaN. */
1389 word0(rv) = NAN_WORD0;
1390 word1(rv) = NAN_WORD1;
1391 s+= 3;
1392
1393 /* Don't interpret (n-char-sequence-opt), for now. */
1394 if (*s == '(') {
1395 s0 = s;
1396 for (s++; *s != ')' && *s != '\0'; s++)
1397 ;
1398 if (*s == ')')
1399 s++; /* Skip over closing paren ... */
1400 else
1401 s = s0; /* ... otherwise go back. */
1402 }
1403
1404 goto ret;
1405 }
1406#endif
1407
1408 if (*s == '0') {
1409 nz0 = 1;
1410 while(*++s == '0') ;
1411 if (!*s)
1412 goto ret;
1413 }
1414 s0 = s;
1415 y = z = 0;
1416 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1417 if (nd < 9)
1418 y = 10*y + c - '0';
1419 else if (nd < 16)
1420 z = 10*z + c - '0';
1421 nd0 = nd;
1422 if (c == decimal_point) {
1423 c = *++s;
1424 if (!nd) {
1425 for(; c == '0'; c = *++s)
1426 nz++;
1427 if (c > '0' && c <= '9') {
1428 s0 = s;
1429 nf += nz;
1430 nz = 0;
1431 goto have_dig;
1432 }
1433 goto dig_done;
1434 }
1435 for(; c >= '0' && c <= '9'; c = *++s) {
1436 have_dig:
1437 nz++;
1438 if (c -= '0') {
1439 nf += nz;
1440 for(i = 1; i < nz; i++)
1441 if (nd++ < 9)
1442 y *= 10;
1443 else if (nd <= DBL_DIG + 1)
1444 z *= 10;
1445 if (nd++ < 9)
1446 y = 10*y + c;
1447 else if (nd <= DBL_DIG + 1)
1448 z = 10*z + c;
1449 nz = 0;
1450 }
1451 }
1452 }
1453 dig_done:
1454 e = 0;
1455 if (c == 'e' || c == 'E') {
1456 if (!nd && !nz && !nz0) {
1457 s = s00;
1458 goto ret;
1459 }
1460 s00 = s;
1461 esign = 0;
1462 switch(c = *++s) {
1463 case '-':
1464 esign = 1;
1465 /* FALLTHROUGH */
1466 case '+':
1467 c = *++s;
1468 }
1469 if (c >= '0' && c <= '9') {
1470 while(c == '0')
1471 c = *++s;
1472 if (c > '0' && c <= '9') {
1473 L = c - '0';
1474 s1 = s;
1475 while((c = *++s) >= '0' && c <= '9')
1476 L = 10*L + c - '0';
1477 if (s - s1 > 8 || L > 19999)
1478 /* Avoid confusion from exponents
1479 * so large that e might overflow.
1480 */
1481 e = 19999; /* safe for 16 bit ints */
1482 else
1483 e = (int)L;
1484 if (esign)
1485 e = -e;
1486 }
1487 else
1488 e = 0;
1489 }
1490 else
1491 s = s00;
1492 }
1493 if (!nd) {
1494 if (!nz && !nz0)
1495 s = s00;
1496 goto ret;
1497 }
1498 e1 = e -= nf;
1499
1500 /* Now we have nd0 digits, starting at s0, followed by a
1501 * decimal point, followed by nd-nd0 digits. The number we're
1502 * after is the integer represented by those digits times
1503 * 10**e */
1504
1505 if (!nd0)
1506 nd0 = nd;
1507 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1508 value(rv) = y;
1509 if (k > 9)
1510 value(rv) = tens[k - 9] * value(rv) + z;
1511 bd0 = 0;
1512 if (nd <= DBL_DIG
1513#ifndef RND_PRODQUOT
1514 && FLT_ROUNDS == 1
1515#endif
1516 ) {
1517 if (!e)
1518 goto ret;
1519 if (e > 0) {
1520 if (e <= Ten_pmax) {
1521#ifdef VAX
1522 goto vax_ovfl_check;
1523#else
1524 /* value(rv) = */ rounded_product(value(rv),
1525 tens[e]);
1526 goto ret;
1527#endif
1528 }
1529 i = DBL_DIG - nd;
1530 if (e <= Ten_pmax + i) {
1531 /* A fancier test would sometimes let us do
1532 * this for larger i values.
1533 */
1534 e -= i;
1535 value(rv) *= tens[i];
1536#ifdef VAX
1537 /* VAX exponent range is so narrow we must
1538 * worry about overflow here...
1539 */
1540 vax_ovfl_check:
1541 word0(rv) -= P*Exp_msk1;
1542 /* value(rv) = */ rounded_product(value(rv),
1543 tens[e]);
1544 if ((word0(rv) & Exp_mask)
1545 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1546 goto ovfl;
1547 word0(rv) += P*Exp_msk1;
1548#else
1549 /* value(rv) = */ rounded_product(value(rv),
1550 tens[e]);
1551#endif
1552 goto ret;
1553 }
1554 }
1555#ifndef Inaccurate_Divide
1556 else if (e >= -Ten_pmax) {
1557 /* value(rv) = */ rounded_quotient(value(rv),
1558 tens[-e]);
1559 goto ret;
1560 }
1561#endif
1562 }
1563 e1 += nd - k;
1564
1565 /* Get starting approximation = rv * 10**e1 */
1566
1567 if (e1 > 0) {
1568 if ((i = e1 & 15) != 0)
1569 value(rv) *= tens[i];
1570 if (e1 &= ~15) {
1571 if (e1 > DBL_MAX_10_EXP) {
1572 ovfl:
1573 errno = ERANGE;
1574 value(rv) = HUGE_VAL;
1575 if (bd0)
1576 goto retfree;
1577 goto ret;
1578 }
1579 if ((e1 = (unsigned int)e1 >> 4) != 0) {
1580 for(j = 0; e1 > 1; j++,
1581 e1 = (unsigned int)e1 >> 1)
1582 if (e1 & 1)
1583 value(rv) *= bigtens[j];
1584 /* The last multiplication could overflow. */
1585 word0(rv) -= P*Exp_msk1;
1586 value(rv) *= bigtens[j];
1587 if ((z = word0(rv) & Exp_mask)
1588 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1589 goto ovfl;
1590 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1591 /* set to largest number */
1592 /* (Can't trust DBL_MAX) */
1593 word0(rv) = Big0;
1594 word1(rv) = Big1;
1595 }
1596 else
1597 word0(rv) += P*Exp_msk1;
1598 }
1599
1600 }
1601 }
1602 else if (e1 < 0) {
1603 e1 = -e1;
1604 if ((i = e1 & 15) != 0)
1605 value(rv) /= tens[i];
1606 if (e1 &= ~15) {
1607 e1 = (unsigned int)e1 >> 4;
1608 if (e1 >= 1 << n_bigtens)
1609 goto undfl;
1610 for(j = 0; e1 > 1; j++,
1611 e1 = (unsigned int)e1 >> 1)
1612 if (e1 & 1)
1613 value(rv) *= tinytens[j];
1614 /* The last multiplication could underflow. */
1615 value(rv0) = value(rv);
1616 value(rv) *= tinytens[j];
1617 if (!value(rv)) {
1618 value(rv) = 2.*value(rv0);
1619 value(rv) *= tinytens[j];
1620 if (!value(rv)) {
1621 undfl:
1622 value(rv) = 0.;
1623 errno = ERANGE;
1624 if (bd0)
1625 goto retfree;
1626 goto ret;
1627 }
1628 word0(rv) = Tiny0;
1629 word1(rv) = Tiny1;
1630 /* The refinement below will clean
1631 * this approximation up.
1632 */
1633 }
1634 }
1635 }
1636
1637 /* Now the hard part -- adjusting rv to the correct value.*/
1638
1639 /* Put digits into bd: true value = bd * 10^e */
1640
1641 bd0 = s2b(s0, nd0, nd, y);
1642
1643 for(;;) {
1644 bd = Balloc(bd0->k);
1645 Bcopy(bd, bd0);
1646 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1647 bs = i2b(1);
1648
1649 if (e >= 0) {
1650 bb2 = bb5 = 0;
1651 bd2 = bd5 = e;
1652 }
1653 else {
1654 bb2 = bb5 = -e;
1655 bd2 = bd5 = 0;
1656 }
1657 if (bbe >= 0)
1658 bb2 += bbe;
1659 else
1660 bd2 -= bbe;
1661 bs2 = bb2;
1662#ifdef Sudden_Underflow
1663#ifdef IBM
1664 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1665#else
1666 j = P + 1 - bbbits;
1667#endif
1668#else
1669 i = bbe + bbbits - 1; /* logb(rv) */
1670 if (i < Emin) /* denormal */
1671 j = bbe + (P-Emin);
1672 else
1673 j = P + 1 - bbbits;
1674#endif
1675 bb2 += j;
1676 bd2 += j;
1677 i = bb2 < bd2 ? bb2 : bd2;
1678 if (i > bs2)
1679 i = bs2;
1680 if (i > 0) {
1681 bb2 -= i;
1682 bd2 -= i;
1683 bs2 -= i;
1684 }
1685 if (bb5 > 0) {
1686 bs = pow5mult(bs, bb5);
1687 bb1 = mult(bs, bb);
1688 Bfree(bb);
1689 bb = bb1;
1690 }
1691 if (bb2 > 0)
1692 bb = lshift(bb, bb2);
1693 if (bd5 > 0)
1694 bd = pow5mult(bd, bd5);
1695 if (bd2 > 0)
1696 bd = lshift(bd, bd2);
1697 if (bs2 > 0)
1698 bs = lshift(bs, bs2);
1699 delta = diff(bb, bd);
1700 dsign = delta->sign;
1701 delta->sign = 0;
1702 i = cmp(delta, bs);
1703 if (i < 0) {
1704 /* Error is less than half an ulp -- check for
1705 * special case of mantissa a power of two.
1706 */
1707 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1708 break;
1709 delta = lshift(delta,Log2P);
1710 if (cmp(delta, bs) > 0)
1711 goto drop_down;
1712 break;
1713 }
1714 if (i == 0) {
1715 /* exactly half-way between */
1716 if (dsign) {
1717 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1718 && word1(rv) == 0xffffffff) {
1719 /*boundary case -- increment exponent*/
1720 word0(rv) = (word0(rv) & Exp_mask)
1721 + Exp_msk1
1722#ifdef IBM
1723 | Exp_msk1 >> 4
1724#endif
1725 ;
1726 word1(rv) = 0;
1727 break;
1728 }
1729 }
1730 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1731 drop_down:
1732 /* boundary case -- decrement exponent */
1733#ifdef Sudden_Underflow
1734 L = word0(rv) & Exp_mask;
1735#ifdef IBM
1736 if (L < Exp_msk1)
1737#else
1738 if (L <= Exp_msk1)
1739#endif
1740 goto undfl;
1741 L -= Exp_msk1;
1742#else
1743 L = (word0(rv) & Exp_mask) - Exp_msk1;
1744#endif
1745 word0(rv) = L | Bndry_mask1;
1746 word1(rv) = 0xffffffff;
1747#ifdef IBM
1748 goto cont;
1749#else
1750 break;
1751#endif
1752 }
1753#ifndef ROUND_BIASED
1754 if (!(word1(rv) & LSB))
1755 break;
1756#endif
1757 if (dsign)
1758 value(rv) += ulp(value(rv));
1759#ifndef ROUND_BIASED
1760 else {
1761 value(rv) -= ulp(value(rv));
1762#ifndef Sudden_Underflow
1763 if (!value(rv))
1764 goto undfl;
1765#endif
1766 }
1767#endif
1768 break;
1769 }
1770 if ((aadj = ratio(delta, bs)) <= 2.) {
1771 if (dsign)
1772 aadj = aadj1 = 1.;
1773 else if (word1(rv) || word0(rv) & Bndry_mask) {
1774#ifndef Sudden_Underflow
1775 if (word1(rv) == Tiny1 && !word0(rv))
1776 goto undfl;
1777#endif
1778 aadj = 1.;
1779 aadj1 = -1.;
1780 }
1781 else {
1782 /* special case -- power of FLT_RADIX to be */
1783 /* rounded down... */
1784
1785 if (aadj < 2./FLT_RADIX)
1786 aadj = 1./FLT_RADIX;
1787 else
1788 aadj *= 0.5;
1789 aadj1 = -aadj;
1790 }
1791 }
1792 else {
1793 aadj *= 0.5;
1794 aadj1 = dsign ? aadj : -aadj;
1795#ifdef Check_FLT_ROUNDS
1796 switch(FLT_ROUNDS) {
1797 case 2: /* towards +infinity */
1798 aadj1 -= 0.5;
1799 break;
1800 case 0: /* towards 0 */
1801 case 3: /* towards -infinity */
1802 aadj1 += 0.5;
1803 }
1804#else
1805 if (FLT_ROUNDS == 0)
1806 aadj1 += 0.5;
1807#endif
1808 }
1809 y = word0(rv) & Exp_mask;
1810
1811 /* Check for overflow */
1812
1813 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1814 value(rv0) = value(rv);
1815 word0(rv) -= P*Exp_msk1;
1816 adj = aadj1 * ulp(value(rv));
1817 value(rv) += adj;
1818 if ((word0(rv) & Exp_mask) >=
1819 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1820 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1821 goto ovfl;
1822 word0(rv) = Big0;
1823 word1(rv) = Big1;
1824 goto cont;
1825 }
1826 else
1827 word0(rv) += P*Exp_msk1;
1828 }
1829 else {
1830#ifdef Sudden_Underflow
1831 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1832 value(rv0) = value(rv);
1833 word0(rv) += P*Exp_msk1;
1834 adj = aadj1 * ulp(value(rv));
1835 value(rv) += adj;
1836#ifdef IBM
1837 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1838#else
1839 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1840#endif
1841 {
1842 if (word0(rv0) == Tiny0
1843 && word1(rv0) == Tiny1)
1844 goto undfl;
1845 word0(rv) = Tiny0;
1846 word1(rv) = Tiny1;
1847 goto cont;
1848 }
1849 else
1850 word0(rv) -= P*Exp_msk1;
1851 }
1852 else {
1853 adj = aadj1 * ulp(value(rv));
1854 value(rv) += adj;
1855 }
1856#else
1857 /* Compute adj so that the IEEE rounding rules will
1858 * correctly round rv + adj in some half-way cases.
1859 * If rv * ulp(rv) is denormalized (i.e.,
1860 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1861 * trouble from bits lost to denormalization;
1862 * example: 1.2e-307 .
1863 */
1864 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1865 aadj1 = (double)(int)(aadj + 0.5);
1866 if (!dsign)
1867 aadj1 = -aadj1;
1868 }
1869 adj = aadj1 * ulp(value(rv));
1870 value(rv) += adj;
1871#endif
1872 }
1873 z = word0(rv) & Exp_mask;
1874 if (y == z) {
1875 /* Can we stop now? */
1876 L = aadj;
1877 aadj -= L;
1878 /* The tolerances below are conservative. */
1879 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1880 if (aadj < .4999999 || aadj > .5000001)
1881 break;
1882 }
1883 else if (aadj < .4999999/FLT_RADIX)
1884 break;
1885 }
1886 cont:
1887 Bfree(bb);
1888 Bfree(bd);
1889 Bfree(bs);
1890 Bfree(delta);
1891 }
1892 retfree:
1893 Bfree(bb);
1894 Bfree(bd);
1895 Bfree(bs);
1896 Bfree(bd0);
1897 Bfree(delta);
1898 ret:
1899 if (se)
1900 /* LINTED interface specification */
1901 *se = (char *)s;
1902 return sign ? -value(rv) : value(rv);
1903 }
1904
1905 static int
1906quorem
1907#ifdef KR_headers
1908 (b, S) Bigint *b, *S;
1909#else
1910 (Bigint *b, Bigint *S)
1911#endif
1912{
1913 int n;
1914 Long borrow, y;
1915 ULong carry, q, ys;
1916 ULong *bx, *bxe, *sx, *sxe;
1917#ifdef Pack_32
1918 Long z;
1919 ULong si, zs;
1920#endif
1921
David 'Digit' Turner81326262010-03-04 11:51:42 -08001922 if (b == BIGINT_INVALID || S == BIGINT_INVALID)
1923 return 0;
1924
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001925 n = S->wds;
1926#ifdef DEBUG
1927 /*debug*/ if (b->wds > n)
1928 /*debug*/ Bug("oversize b in quorem");
1929#endif
1930 if (b->wds < n)
1931 return 0;
1932 sx = S->x;
1933 sxe = sx + --n;
1934 bx = b->x;
1935 bxe = bx + n;
1936 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1937#ifdef DEBUG
1938 /*debug*/ if (q > 9)
1939 /*debug*/ Bug("oversized quotient in quorem");
1940#endif
1941 if (q) {
1942 borrow = 0;
1943 carry = 0;
1944 do {
1945#ifdef Pack_32
1946 si = *sx++;
1947 ys = (si & 0xffff) * q + carry;
1948 zs = (si >> 16) * q + (ys >> 16);
1949 carry = zs >> 16;
1950 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1951 borrow = (ULong)y >> 16;
1952 Sign_Extend(borrow, y);
1953 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1954 borrow = (ULong)z >> 16;
1955 Sign_Extend(borrow, z);
1956 Storeinc(bx, z, y);
1957#else
1958 ys = *sx++ * q + carry;
1959 carry = ys >> 16;
1960 y = *bx - (ys & 0xffff) + borrow;
1961 borrow = y >> 16;
1962 Sign_Extend(borrow, y);
1963 *bx++ = y & 0xffff;
1964#endif
1965 }
1966 while(sx <= sxe);
1967 if (!*bxe) {
1968 bx = b->x;
1969 while(--bxe > bx && !*bxe)
1970 --n;
1971 b->wds = n;
1972 }
1973 }
1974 if (cmp(b, S) >= 0) {
1975 q++;
1976 borrow = 0;
1977 carry = 0;
1978 bx = b->x;
1979 sx = S->x;
1980 do {
1981#ifdef Pack_32
1982 si = *sx++;
1983 ys = (si & 0xffff) + carry;
1984 zs = (si >> 16) + (ys >> 16);
1985 carry = zs >> 16;
1986 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1987 borrow = (ULong)y >> 16;
1988 Sign_Extend(borrow, y);
1989 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1990 borrow = (ULong)z >> 16;
1991 Sign_Extend(borrow, z);
1992 Storeinc(bx, z, y);
1993#else
1994 ys = *sx++ + carry;
1995 carry = ys >> 16;
1996 y = *bx - (ys & 0xffff) + borrow;
1997 borrow = y >> 16;
1998 Sign_Extend(borrow, y);
1999 *bx++ = y & 0xffff;
2000#endif
2001 }
2002 while(sx <= sxe);
2003 bx = b->x;
2004 bxe = bx + n;
2005 if (!*bxe) {
2006 while(--bxe > bx && !*bxe)
2007 --n;
2008 b->wds = n;
2009 }
2010 }
2011 return q;
2012 }
2013
2014/* freedtoa(s) must be used to free values s returned by dtoa
2015 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2016 * but for consistency with earlier versions of dtoa, it is optional
2017 * when MULTIPLE_THREADS is not defined.
2018 */
2019
2020void
2021#ifdef KR_headers
2022freedtoa(s) char *s;
2023#else
2024freedtoa(char *s)
2025#endif
2026{
2027 free(s);
2028}
2029
2030
2031
2032/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2033 *
2034 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2035 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
2036 *
2037 * Modifications:
2038 * 1. Rather than iterating, we use a simple numeric overestimate
2039 * to determine k = floor(log10(d)). We scale relevant
2040 * quantities using O(log2(k)) rather than O(k) multiplications.
2041 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2042 * try to generate digits strictly left to right. Instead, we
2043 * compute with fewer bits and propagate the carry if necessary
2044 * when rounding the final digit up. This is often faster.
2045 * 3. Under the assumption that input will be rounded nearest,
2046 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2047 * That is, we allow equality in stopping tests when the
2048 * round-nearest rule will give the same floating-point value
2049 * as would satisfaction of the stopping test with strict
2050 * inequality.
2051 * 4. We remove common factors of powers of 2 from relevant
2052 * quantities.
2053 * 5. When converting floating-point integers less than 1e16,
2054 * we use floating-point arithmetic rather than resorting
2055 * to multiple-precision integers.
2056 * 6. When asked to produce fewer than 15 digits, we first try
2057 * to get by with floating-point arithmetic; we resort to
2058 * multiple-precision integer arithmetic only if we cannot
2059 * guarantee that the floating-point calculation has given
2060 * the correctly rounded result. For k requested digits and
2061 * "uniformly" distributed input, the probability is
2062 * something like 10^(k-15) that we must resort to the Long
2063 * calculation.
2064 */
2065
2066 char *
2067__dtoa
2068#ifdef KR_headers
2069 (_d, mode, ndigits, decpt, sign, rve)
2070 double _d; int mode, ndigits, *decpt, *sign; char **rve;
2071#else
2072 (double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
2073#endif
2074{
2075 /* Arguments ndigits, decpt, sign are similar to those
2076 of ecvt and fcvt; trailing zeros are suppressed from
2077 the returned string. If not null, *rve is set to point
2078 to the end of the return value. If d is +-Infinity or NaN,
2079 then *decpt is set to 9999.
2080
2081 mode:
2082 0 ==> shortest string that yields d when read in
2083 and rounded to nearest.
2084 1 ==> like 0, but with Steele & White stopping rule;
2085 e.g. with IEEE P754 arithmetic , mode 0 gives
2086 1e23 whereas mode 1 gives 9.999999999999999e22.
2087 2 ==> max(1,ndigits) significant digits. This gives a
2088 return value similar to that of ecvt, except
2089 that trailing zeros are suppressed.
2090 3 ==> through ndigits past the decimal point. This
2091 gives a return value similar to that from fcvt,
2092 except that trailing zeros are suppressed, and
2093 ndigits can be negative.
2094 4-9 should give the same return values as 2-3, i.e.,
2095 4 <= mode <= 9 ==> same return as mode
2096 2 + (mode & 1). These modes are mainly for
2097 debugging; often they run slower but sometimes
2098 faster than modes 2-3.
2099 4,5,8,9 ==> left-to-right digit generation.
2100 6-9 ==> don't try fast floating-point estimate
2101 (if applicable).
2102
2103 Values of mode other than 0-9 are treated as mode 0.
2104
2105 Sufficient space is allocated to the return value
2106 to hold the suppressed trailing zeros.
2107 */
2108
2109 int bbits, b2, b5, be, dig, i, ieps, ilim0,
2110 j, jj1, k, k0, k_check, leftright, m2, m5, s2, s5,
2111 try_quick;
2112 int ilim = 0, ilim1 = 0, spec_case = 0; /* pacify gcc */
2113 Long L;
2114#ifndef Sudden_Underflow
2115 int denorm;
2116 ULong x;
2117#endif
2118 Bigint *b, *b1, *delta, *mhi, *S;
2119 Bigint *mlo = NULL; /* pacify gcc */
2120 double ds;
2121 char *s, *s0;
2122 Bigint *result = NULL;
2123 int result_k = 0;
2124 _double d, d2, eps;
2125
2126 value(d) = _d;
2127
2128 if (word0(d) & Sign_bit) {
2129 /* set sign for everything, including 0's and NaNs */
2130 *sign = 1;
2131 word0(d) &= ~Sign_bit; /* clear sign bit */
2132 }
2133 else
2134 *sign = 0;
2135
2136#if defined(IEEE_Arith) + defined(VAX)
2137#ifdef IEEE_Arith
2138 if ((word0(d) & Exp_mask) == Exp_mask)
2139#else
2140 if (word0(d) == 0x8000)
2141#endif
2142 {
2143 /* Infinity or NaN */
2144 *decpt = 9999;
2145 s =
2146#ifdef IEEE_Arith
2147 !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
2148#endif
2149 "NaN";
David 'Digit' Turner81326262010-03-04 11:51:42 -08002150 result = Balloc(strlen(s)+1);
2151 if (result == BIGINT_INVALID)
2152 return NULL;
2153 s0 = (char *)(void *)result;
2154 strcpy(s0, s);
2155 if (rve)
2156 *rve =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002157#ifdef IEEE_Arith
David 'Digit' Turner81326262010-03-04 11:51:42 -08002158 s0[3] ? s0 + 8 :
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002159#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08002160 s0 + 3;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002161 return s0;
2162 }
2163#endif
2164#ifdef IBM
2165 value(d) += 0; /* normalize */
2166#endif
2167 if (!value(d)) {
2168 *decpt = 1;
David 'Digit' Turner81326262010-03-04 11:51:42 -08002169 result = Balloc(2);
2170 if (result == BIGINT_INVALID)
2171 return NULL;
2172 s0 = (char *)(void *)result;
2173 strcpy(s0, "0");
2174 if (rve)
2175 *rve = s0 + 1;
2176 return s0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002177 }
2178
2179 b = d2b(value(d), &be, &bbits);
2180#ifdef Sudden_Underflow
2181 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2182#else
2183 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
2184#endif
2185 value(d2) = value(d);
2186 word0(d2) &= Frac_mask1;
2187 word0(d2) |= Exp_11;
2188#ifdef IBM
2189 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2190 value(d2) /= 1 << j;
2191#endif
2192
2193 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2194 * log10(x) = log(x) / log(10)
2195 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2196 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2197 *
2198 * This suggests computing an approximation k to log10(d) by
2199 *
2200 * k = (i - Bias)*0.301029995663981
2201 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2202 *
2203 * We want k to be too large rather than too small.
2204 * The error in the first-order Taylor series approximation
2205 * is in our favor, so we just round up the constant enough
2206 * to compensate for any error in the multiplication of
2207 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2208 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2209 * adding 1e-13 to the constant term more than suffices.
2210 * Hence we adjust the constant term to 0.1760912590558.
2211 * (We could get a more accurate k by invoking log10,
2212 * but this is probably not worthwhile.)
2213 */
2214
2215 i -= Bias;
2216#ifdef IBM
2217 i <<= 2;
2218 i += j;
2219#endif
2220#ifndef Sudden_Underflow
2221 denorm = 0;
2222 }
2223 else {
2224 /* d is denormalized */
2225
2226 i = bbits + be + (Bias + (P-1) - 1);
2227 x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
2228 : word1(d) << (32 - i);
2229 value(d2) = x;
2230 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2231 i -= (Bias + (P-1) - 1) + 1;
2232 denorm = 1;
2233 }
2234#endif
2235 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2236 i*0.301029995663981;
2237 k = (int)ds;
2238 if (ds < 0. && ds != k)
2239 k--; /* want k = floor(ds) */
2240 k_check = 1;
2241 if (k >= 0 && k <= Ten_pmax) {
2242 if (value(d) < tens[k])
2243 k--;
2244 k_check = 0;
2245 }
2246 j = bbits - i - 1;
2247 if (j >= 0) {
2248 b2 = 0;
2249 s2 = j;
2250 }
2251 else {
2252 b2 = -j;
2253 s2 = 0;
2254 }
2255 if (k >= 0) {
2256 b5 = 0;
2257 s5 = k;
2258 s2 += k;
2259 }
2260 else {
2261 b2 -= k;
2262 b5 = -k;
2263 s5 = 0;
2264 }
2265 if (mode < 0 || mode > 9)
2266 mode = 0;
2267 try_quick = 1;
2268 if (mode > 5) {
2269 mode -= 4;
2270 try_quick = 0;
2271 }
2272 leftright = 1;
2273 switch(mode) {
2274 case 0:
2275 case 1:
2276 ilim = ilim1 = -1;
2277 i = 18;
2278 ndigits = 0;
2279 break;
2280 case 2:
2281 leftright = 0;
2282 /* FALLTHROUGH */
2283 case 4:
2284 if (ndigits <= 0)
2285 ndigits = 1;
2286 ilim = ilim1 = i = ndigits;
2287 break;
2288 case 3:
2289 leftright = 0;
2290 /* FALLTHROUGH */
2291 case 5:
2292 i = ndigits + k + 1;
2293 ilim = i;
2294 ilim1 = i - 1;
2295 if (i <= 0)
2296 i = 1;
2297 }
2298 j = sizeof(ULong);
2299 for(result_k = 0; (int)(sizeof(Bigint) - sizeof(ULong)) + j <= i;
2300 j <<= 1) result_k++;
2301 // this is really a ugly hack, the code uses Balloc
2302 // instead of malloc, but casts the result into a char*
2303 // it seems the only reason to do that is due to the
2304 // complicated way the block size need to be computed
2305 // buuurk....
2306 result = Balloc(result_k);
David 'Digit' Turner81326262010-03-04 11:51:42 -08002307 if (result == BIGINT_INVALID) {
2308 Bfree(b);
2309 return NULL;
2310 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002311 s = s0 = (char *)(void *)result;
2312
2313 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2314
2315 /* Try to get by with floating-point arithmetic. */
2316
2317 i = 0;
2318 value(d2) = value(d);
2319 k0 = k;
2320 ilim0 = ilim;
2321 ieps = 2; /* conservative */
2322 if (k > 0) {
2323 ds = tens[k&0xf];
2324 j = (unsigned int)k >> 4;
2325 if (j & Bletch) {
2326 /* prevent overflows */
2327 j &= Bletch - 1;
2328 value(d) /= bigtens[n_bigtens-1];
2329 ieps++;
2330 }
2331 for(; j; j = (unsigned int)j >> 1, i++)
2332 if (j & 1) {
2333 ieps++;
2334 ds *= bigtens[i];
2335 }
2336 value(d) /= ds;
2337 }
2338 else if ((jj1 = -k) != 0) {
2339 value(d) *= tens[jj1 & 0xf];
2340 for(j = (unsigned int)jj1 >> 4; j;
2341 j = (unsigned int)j >> 1, i++)
2342 if (j & 1) {
2343 ieps++;
2344 value(d) *= bigtens[i];
2345 }
2346 }
2347 if (k_check && value(d) < 1. && ilim > 0) {
2348 if (ilim1 <= 0)
2349 goto fast_failed;
2350 ilim = ilim1;
2351 k--;
2352 value(d) *= 10.;
2353 ieps++;
2354 }
2355 value(eps) = ieps*value(d) + 7.;
2356 word0(eps) -= (P-1)*Exp_msk1;
2357 if (ilim == 0) {
2358 S = mhi = 0;
2359 value(d) -= 5.;
2360 if (value(d) > value(eps))
2361 goto one_digit;
2362 if (value(d) < -value(eps))
2363 goto no_digits;
2364 goto fast_failed;
2365 }
2366#ifndef No_leftright
2367 if (leftright) {
2368 /* Use Steele & White method of only
2369 * generating digits needed.
2370 */
2371 value(eps) = 0.5/tens[ilim-1] - value(eps);
2372 for(i = 0;;) {
2373 L = value(d);
2374 value(d) -= L;
2375 *s++ = '0' + (int)L;
2376 if (value(d) < value(eps))
2377 goto ret1;
2378 if (1. - value(d) < value(eps))
2379 goto bump_up;
2380 if (++i >= ilim)
2381 break;
2382 value(eps) *= 10.;
2383 value(d) *= 10.;
2384 }
2385 }
2386 else {
2387#endif
2388 /* Generate ilim digits, then fix them up. */
2389 value(eps) *= tens[ilim-1];
2390 for(i = 1;; i++, value(d) *= 10.) {
2391 L = value(d);
2392 value(d) -= L;
2393 *s++ = '0' + (int)L;
2394 if (i == ilim) {
2395 if (value(d) > 0.5 + value(eps))
2396 goto bump_up;
2397 else if (value(d) < 0.5 - value(eps)) {
2398 while(*--s == '0');
2399 s++;
2400 goto ret1;
2401 }
2402 break;
2403 }
2404 }
2405#ifndef No_leftright
2406 }
2407#endif
2408 fast_failed:
2409 s = s0;
2410 value(d) = value(d2);
2411 k = k0;
2412 ilim = ilim0;
2413 }
2414
2415 /* Do we have a "small" integer? */
2416
2417 if (be >= 0 && k <= Int_max) {
2418 /* Yes. */
2419 ds = tens[k];
2420 if (ndigits < 0 && ilim <= 0) {
2421 S = mhi = 0;
2422 if (ilim < 0 || value(d) <= 5*ds)
2423 goto no_digits;
2424 goto one_digit;
2425 }
2426 for(i = 1;; i++) {
2427 L = value(d) / ds;
2428 value(d) -= L*ds;
2429#ifdef Check_FLT_ROUNDS
2430 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2431 if (value(d) < 0) {
2432 L--;
2433 value(d) += ds;
2434 }
2435#endif
2436 *s++ = '0' + (int)L;
2437 if (i == ilim) {
2438 value(d) += value(d);
2439 if (value(d) > ds || (value(d) == ds && L & 1)) {
2440 bump_up:
2441 while(*--s == '9')
2442 if (s == s0) {
2443 k++;
2444 *s = '0';
2445 break;
2446 }
2447 ++*s++;
2448 }
2449 break;
2450 }
2451 if (!(value(d) *= 10.))
2452 break;
2453 }
2454 goto ret1;
2455 }
2456
2457 m2 = b2;
2458 m5 = b5;
2459 mhi = mlo = 0;
2460 if (leftright) {
2461 if (mode < 2) {
2462 i =
2463#ifndef Sudden_Underflow
2464 denorm ? be + (Bias + (P-1) - 1 + 1) :
2465#endif
2466#ifdef IBM
2467 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2468#else
2469 1 + P - bbits;
2470#endif
2471 }
2472 else {
2473 j = ilim - 1;
2474 if (m5 >= j)
2475 m5 -= j;
2476 else {
2477 s5 += j -= m5;
2478 b5 += j;
2479 m5 = 0;
2480 }
2481 if ((i = ilim) < 0) {
2482 m2 -= i;
2483 i = 0;
2484 }
2485 }
2486 b2 += i;
2487 s2 += i;
2488 mhi = i2b(1);
2489 }
2490 if (m2 > 0 && s2 > 0) {
2491 i = m2 < s2 ? m2 : s2;
2492 b2 -= i;
2493 m2 -= i;
2494 s2 -= i;
2495 }
2496 if (b5 > 0) {
2497 if (leftright) {
2498 if (m5 > 0) {
2499 mhi = pow5mult(mhi, m5);
2500 b1 = mult(mhi, b);
2501 Bfree(b);
2502 b = b1;
2503 }
2504 if ((j = b5 - m5) != 0)
2505 b = pow5mult(b, j);
2506 }
2507 else
2508 b = pow5mult(b, b5);
2509 }
2510 S = i2b(1);
2511 if (s5 > 0)
2512 S = pow5mult(S, s5);
2513
2514 /* Check for special case that d is a normalized power of 2. */
2515
2516 if (mode < 2) {
2517 if (!word1(d) && !(word0(d) & Bndry_mask)
2518#ifndef Sudden_Underflow
2519 && word0(d) & Exp_mask
2520#endif
2521 ) {
2522 /* The special case */
2523 b2 += Log2P;
2524 s2 += Log2P;
2525 spec_case = 1;
2526 }
2527 else
2528 spec_case = 0;
2529 }
2530
2531 /* Arrange for convenient computation of quotients:
2532 * shift left if necessary so divisor has 4 leading 0 bits.
2533 *
2534 * Perhaps we should just compute leading 28 bits of S once
2535 * and for all and pass them and a shift to quorem, so it
2536 * can do shifts and ors to compute the numerator for q.
2537 */
David 'Digit' Turner81326262010-03-04 11:51:42 -08002538 if (S == BIGINT_INVALID) {
2539 i = 0;
2540 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002541#ifdef Pack_32
David 'Digit' Turner81326262010-03-04 11:51:42 -08002542 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
2543 i = 32 - i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002544#else
David 'Digit' Turner81326262010-03-04 11:51:42 -08002545 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2546 i = 16 - i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002547#endif
David 'Digit' Turner81326262010-03-04 11:51:42 -08002548 }
2549
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002550 if (i > 4) {
2551 i -= 4;
2552 b2 += i;
2553 m2 += i;
2554 s2 += i;
2555 }
2556 else if (i < 4) {
2557 i += 28;
2558 b2 += i;
2559 m2 += i;
2560 s2 += i;
2561 }
2562 if (b2 > 0)
2563 b = lshift(b, b2);
2564 if (s2 > 0)
2565 S = lshift(S, s2);
2566 if (k_check) {
2567 if (cmp(b,S) < 0) {
2568 k--;
2569 b = multadd(b, 10, 0); /* we botched the k estimate */
2570 if (leftright)
2571 mhi = multadd(mhi, 10, 0);
2572 ilim = ilim1;
2573 }
2574 }
2575 if (ilim <= 0 && mode > 2) {
2576 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2577 /* no digits, fcvt style */
2578 no_digits:
2579 k = -1 - ndigits;
2580 goto ret;
2581 }
2582 one_digit:
2583 *s++ = '1';
2584 k++;
2585 goto ret;
2586 }
2587 if (leftright) {
2588 if (m2 > 0)
2589 mhi = lshift(mhi, m2);
2590
2591 /* Compute mlo -- check for special case
2592 * that d is a normalized power of 2.
2593 */
2594
2595 mlo = mhi;
2596 if (spec_case) {
2597 mhi = Balloc(mhi->k);
2598 Bcopy(mhi, mlo);
2599 mhi = lshift(mhi, Log2P);
2600 }
2601
2602 for(i = 1;;i++) {
2603 dig = quorem(b,S) + '0';
2604 /* Do we yet have the shortest decimal string
2605 * that will round to d?
2606 */
2607 j = cmp(b, mlo);
2608 delta = diff(S, mhi);
2609 jj1 = delta->sign ? 1 : cmp(b, delta);
2610 Bfree(delta);
2611#ifndef ROUND_BIASED
2612 if (jj1 == 0 && !mode && !(word1(d) & 1)) {
2613 if (dig == '9')
2614 goto round_9_up;
2615 if (j > 0)
2616 dig++;
2617 *s++ = dig;
2618 goto ret;
2619 }
2620#endif
2621 if (j < 0 || (j == 0 && !mode
2622#ifndef ROUND_BIASED
2623 && !(word1(d) & 1)
2624#endif
2625 )) {
2626 if (jj1 > 0) {
2627 b = lshift(b, 1);
2628 jj1 = cmp(b, S);
2629 if ((jj1 > 0 || (jj1 == 0 && dig & 1))
2630 && dig++ == '9')
2631 goto round_9_up;
2632 }
2633 *s++ = dig;
2634 goto ret;
2635 }
2636 if (jj1 > 0) {
2637 if (dig == '9') { /* possible if i == 1 */
2638 round_9_up:
2639 *s++ = '9';
2640 goto roundoff;
2641 }
2642 *s++ = dig + 1;
2643 goto ret;
2644 }
2645 *s++ = dig;
2646 if (i == ilim)
2647 break;
2648 b = multadd(b, 10, 0);
2649 if (mlo == mhi)
2650 mlo = mhi = multadd(mhi, 10, 0);
2651 else {
2652 mlo = multadd(mlo, 10, 0);
2653 mhi = multadd(mhi, 10, 0);
2654 }
2655 }
2656 }
2657 else
2658 for(i = 1;; i++) {
2659 *s++ = dig = quorem(b,S) + '0';
2660 if (i >= ilim)
2661 break;
2662 b = multadd(b, 10, 0);
2663 }
2664
2665 /* Round off last digit */
2666
2667 b = lshift(b, 1);
2668 j = cmp(b, S);
2669 if (j > 0 || (j == 0 && dig & 1)) {
2670 roundoff:
2671 while(*--s == '9')
2672 if (s == s0) {
2673 k++;
2674 *s++ = '1';
2675 goto ret;
2676 }
2677 ++*s++;
2678 }
2679 else {
2680 while(*--s == '0');
2681 s++;
2682 }
2683 ret:
2684 Bfree(S);
2685 if (mhi) {
2686 if (mlo && mlo != mhi)
2687 Bfree(mlo);
2688 Bfree(mhi);
2689 }
2690 ret1:
2691 Bfree(b);
2692 if (s == s0) { /* don't return empty string */
2693 *s++ = '0';
2694 k = 0;
2695 }
2696 *s = 0;
2697 *decpt = k + 1;
2698 if (rve)
2699 *rve = s;
2700 return s0;
2701 }
2702#ifdef __cplusplus
2703}
2704#endif