g_ddfmt.c revision 187808
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 David M. Gay (dmg@acm.org). */
30
31#include "gdtoaimp.h"
32#include <string.h>
33
34 char *
35#ifdef KR_headers
36g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; size_t bufsize;
37#else
38g_ddfmt(char *buf, double *dd, int ndig, size_t bufsize)
39#endif
40{
41	FPI fpi;
42	char *b, *s, *se;
43	ULong *L, bits0[4], *bits, *zx;
44	int bx, by, decpt, ex, ey, i, j, mode;
45	Bigint *x, *y, *z;
46	double ddx[2];
47#ifdef Honor_FLT_ROUNDS /*{{*/
48	int Rounding;
49#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
50	Rounding = Flt_Rounds;
51#else /*}{*/
52	Rounding = 1;
53	switch(fegetround()) {
54	  case FE_TOWARDZERO:	Rounding = 0; break;
55	  case FE_UPWARD:	Rounding = 2; break;
56	  case FE_DOWNWARD:	Rounding = 3;
57	  }
58#endif /*}}*/
59#else /*}{*/
60#define Rounding FPI_Round_near
61#endif /*}}*/
62
63	if (bufsize < 10 || bufsize < ndig + 8)
64		return 0;
65
66	L = (ULong*)dd;
67	if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) {
68		/* Infinity or NaN */
69		if (L[_0] & 0xfffff || L[_1]) {
70 nanret:
71			return strcp(buf, "NaN");
72			}
73		if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
74			if (L[2+_0] & 0xfffff || L[2+_1])
75				goto nanret;
76			if ((L[_0] ^ L[2+_0]) & 0x80000000L)
77				goto nanret;	/* Infinity - Infinity */
78			}
79 infret:
80		b = buf;
81		if (L[_0] & 0x80000000L)
82			*b++ = '-';
83		return strcp(b, "Infinity");
84		}
85	if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
86		L += 2;
87		if (L[_0] & 0xfffff || L[_1])
88			goto nanret;
89		goto infret;
90		}
91	if (dd[0] + dd[1] == 0.) {
92		b = buf;
93#ifndef IGNORE_ZERO_SIGN
94		if (L[_0] & L[2+_0] & 0x80000000L)
95			*b++ = '-';
96#endif
97		*b++ = '0';
98		*b = 0;
99		return b;
100		}
101	if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) {
102		ddx[1] = dd[0];
103		ddx[0] = dd[1];
104		dd = ddx;
105		L = (ULong*)dd;
106		}
107	z = d2b(dd[0], &ex, &bx);
108	if (dd[1] == 0.)
109		goto no_y;
110	x = z;
111	y = d2b(dd[1], &ey, &by);
112	if ( (i = ex - ey) !=0) {
113		if (i > 0) {
114			x = lshift(x, i);
115			ex = ey;
116			}
117		else
118			y = lshift(y, -i);
119		}
120	if ((L[_0] ^ L[2+_0]) & 0x80000000L) {
121		z = diff(x, y);
122		if (L[_0] & 0x80000000L)
123			z->sign = 1 - z->sign;
124		}
125	else {
126		z = sum(x, y);
127		if (L[_0] & 0x80000000L)
128			z->sign = 1;
129		}
130	Bfree(x);
131	Bfree(y);
132 no_y:
133	bits = zx = z->x;
134	for(i = 0; !*zx; zx++)
135		i += 32;
136	i += lo0bits(zx);
137	if (i) {
138		rshift(z, i);
139		ex += i;
140		}
141	fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
142	if (fpi.nbits < 106) {
143		fpi.nbits = 106;
144		if (j < 3) {
145			for(i = 0; i <= j; i++)
146				bits0[i] = bits[i];
147			while(i < 4)
148				bits0[i++] = 0;
149			bits = bits0;
150			}
151		}
152	mode = 2;
153	if (ndig <= 0) {
154		if (bufsize < (int)(fpi.nbits * .301029995664) + 10) {
155			Bfree(z);
156			return 0;
157			}
158		mode = 0;
159		}
160	fpi.emin = 1-1023-53+1;
161	fpi.emax = 2046-1023-106+1;
162	fpi.rounding = Rounding;
163	fpi.sudden_underflow = 0;
164	i = STRTOG_Normal;
165	s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
166	b = g__fmt(buf, s, se, decpt, z->sign, bufsize);
167	Bfree(z);
168	return b;
169	}
170