g_ddfmt.c revision 112158
1/****************************************************************
2
3The author of this software is David M. Gay.
4
5Copyright (C) 1998 by Lucent Technologies
6All Rights Reserved
7
8Permission to use, copy, modify, and distribute this software and
9its documentation for any purpose and without fee is hereby
10granted, provided that the above copyright notice appear in all
11copies and that both that the copyright notice and this
12permission notice and warranty disclaimer appear in supporting
13documentation, and that the name of Lucent or any of its entities
14not be used in advertising or publicity pertaining to
15distribution of the software without specific, written prior
16permission.
17
18LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25THIS SOFTWARE.
26
27****************************************************************/
28
29/* Please send bug reports to
30	David M. Gay
31	Bell Laboratories, Room 2C-463
32	600 Mountain Avenue
33	Murray Hill, NJ 07974-0636
34	U.S.A.
35	dmg@bell-labs.com
36 */
37
38#include "gdtoaimp.h"
39#include <string.h>
40
41 char *
42#ifdef KR_headers
43g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; unsigned bufsize;
44#else
45g_ddfmt(char *buf, double *dd, int ndig, unsigned bufsize)
46#endif
47{
48	FPI fpi;
49	char *b, *s, *se;
50	ULong *L, bits0[4], *bits, *zx;
51	int bx, by, decpt, ex, ey, i, j, mode;
52	Bigint *x, *y, *z;
53	double ddx[2];
54
55	if (bufsize < 10 || bufsize < ndig + 8)
56		return 0;
57
58	L = (ULong*)dd;
59	if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) {
60		/* Infinity or NaN */
61		if (L[_0] & 0xfffff || L[_1]) {
62 nanret:
63			return strcp(buf, "NaN");
64			}
65		if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
66			if (L[2+_0] & 0xfffff || L[2+_1])
67				goto nanret;
68			if ((L[_0] ^ L[2+_0]) & 0x80000000L)
69				goto nanret;	/* Infinity - Infinity */
70			}
71 infret:
72		b = buf;
73		if (L[_0] & 0x80000000L)
74			*b++ = '-';
75		return strcp(b, "Infinity");
76		}
77	if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
78		L += 2;
79		if (L[_0] & 0xfffff || L[_1])
80			goto nanret;
81		goto infret;
82		}
83	if (dd[0] + dd[1] == 0.) {
84		b = buf;
85#ifndef IGNORE_ZERO_SIGN
86		if (L[_0] & L[2+_0] & 0x80000000L)
87			*b++ = '-';
88#endif
89		*b++ = '0';
90		*b = 0;
91		return b;
92		}
93	if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) {
94		ddx[1] = dd[0];
95		ddx[0] = dd[1];
96		dd = ddx;
97		L = (ULong*)dd;
98		}
99	z = d2b(dd[0], &ex, &bx);
100	if (dd[1] == 0.)
101		goto no_y;
102	x = z;
103	y = d2b(dd[1], &ey, &by);
104	if ( (i = ex - ey) !=0) {
105		if (i > 0) {
106			x = lshift(x, i);
107			ex = ey;
108			}
109		else
110			y = lshift(y, -i);
111		}
112	if ((L[_0] ^ L[2+_0]) & 0x80000000L) {
113		z = diff(x, y);
114		if (L[_0] & 0x80000000L)
115			z->sign = 1 - z->sign;
116		}
117	else {
118		z = sum(x, y);
119		if (L[_0] & 0x80000000L)
120			z->sign = 1;
121		}
122	Bfree(x);
123	Bfree(y);
124 no_y:
125	bits = zx = z->x;
126	for(i = 0; !*zx; zx++)
127		i += 32;
128	i += lo0bits(zx);
129	if (i) {
130		rshift(z, i);
131		ex += i;
132		}
133	fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
134	if (fpi.nbits < 106) {
135		fpi.nbits = 106;
136		if (j < 3) {
137			for(i = 0; i <= j; i++)
138				bits0[i] = bits[i];
139			while(i < 4)
140				bits0[i++] = 0;
141			bits = bits0;
142			}
143		}
144	mode = 2;
145	if (ndig <= 0) {
146		if (bufsize < (int)(fpi.nbits * .301029995664) + 10) {
147			Bfree(z);
148			return 0;
149			}
150		mode = 0;
151		}
152	fpi.emin = 1-1023-53+1;
153	fpi.emax = 2046-1023-106+1;
154	fpi.rounding = FPI_Round_near;
155	fpi.sudden_underflow = 0;
156	i = STRTOG_Normal;
157	s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
158	b = g__fmt(buf, s, se, decpt, z->sign);
159	Bfree(z);
160	return b;
161	}
162