g_xfmt.c revision 225736
1192595Sdes/****************************************************************
2180750Sdes
3180750SdesThe author of this software is David M. Gay.
4180750Sdes
5180750SdesCopyright (C) 1998 by Lucent Technologies
6180750SdesAll Rights Reserved
7180750Sdes
8180750SdesPermission to use, copy, modify, and distribute this software and
9180750Sdesits documentation for any purpose and without fee is hereby
10180750Sdesgranted, provided that the above copyright notice appear in all
11180750Sdescopies and that both that the copyright notice and this
12180750Sdespermission notice and warranty disclaimer appear in supporting
13180750Sdesdocumentation, and that the name of Lucent or any of its entities
14180750Sdesnot be used in advertising or publicity pertaining to
15180750Sdesdistribution of the software without specific, written prior
16180750Sdespermission.
17180750Sdes
18180750SdesLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19180750SdesINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20180750SdesIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21180750SdesSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22180750SdesWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23180750SdesIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24180750SdesARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25180750SdesTHIS SOFTWARE.
26180750Sdes
27180750Sdes****************************************************************/
28180750Sdes
29180750Sdes/* Please send bug reports to David M. Gay (dmg at acm dot org,
30180750Sdes * with " at " changed at "@" and " dot " changed to ".").	*/
31180750Sdes
32180750Sdes#include "gdtoaimp.h"
33180750Sdes
34192595Sdes#undef _0
35180750Sdes#undef _1
36180750Sdes
37180750Sdes/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
38180750Sdes
39180750Sdes#ifdef IEEE_MC68k
40180750Sdes#define _0 0
41180750Sdes#define _1 1
42180750Sdes#define _2 2
43180750Sdes#define _3 3
44180750Sdes#define _4 4
45180750Sdes#endif
46180750Sdes#ifdef IEEE_8087
47180750Sdes#define _0 4
48180750Sdes#define _1 3
49180750Sdes#define _2 2
50180750Sdes#define _3 1
51180750Sdes#define _4 0
52180750Sdes#endif
53180750Sdes
54180750Sdes char*
55180750Sdes#ifdef KR_headers
56180750Sdesg_xfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; size_t bufsize;
57180750Sdes#else
58180750Sdesg_xfmt(char *buf, void *V, int ndig, size_t bufsize)
59180750Sdes#endif
60180750Sdes{
61180750Sdes	static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 };
62180750Sdes	char *b, *s, *se;
63180750Sdes	ULong bits[2], sign;
64180750Sdes	UShort *L;
65180750Sdes	int decpt, ex, i, mode;
66180750Sdes#ifdef Honor_FLT_ROUNDS
67180750Sdes#include "gdtoa_fltrnds.h"
68180750Sdes#else
69180750Sdes#define fpi &fpi0
70180750Sdes#endif
71180750Sdes
72180750Sdes	if (ndig < 0)
73180750Sdes		ndig = 0;
74180750Sdes	if (bufsize < ndig + 10)
75180750Sdes		return 0;
76180750Sdes
77180750Sdes	L = (UShort *)V;
78180750Sdes	sign = L[_0] & 0x8000;
79180750Sdes	bits[1] = (L[_1] << 16) | L[_2];
80180750Sdes	bits[0] = (L[_3] << 16) | L[_4];
81180750Sdes	if ( (ex = L[_0] & 0x7fff) !=0) {
82180750Sdes		if (ex == 0x7fff) {
83180750Sdes			/* Infinity or NaN */
84180750Sdes			if (bits[0] | bits[1])
85180750Sdes				b = strcp(buf, "NaN");
86180750Sdes			else {
87180750Sdes				b = buf;
88180750Sdes				if (sign)
89180750Sdes					*b++ = '-';
90180750Sdes				b = strcp(b, "Infinity");
91180750Sdes				}
92180750Sdes			return b;
93180750Sdes			}
94180750Sdes		i = STRTOG_Normal;
95180750Sdes		}
96180750Sdes	else if (bits[0] | bits[1]) {
97180750Sdes		i = STRTOG_Denormal;
98180750Sdes		ex = 1;
99180750Sdes		}
100180750Sdes	else {
101192595Sdes		b = buf;
102180750Sdes#ifndef IGNORE_ZERO_SIGN
103192595Sdes		if (sign)
104180750Sdes			*b++ = '-';
105180750Sdes#endif
106180750Sdes		*b++ = '0';
107180750Sdes		*b = 0;
108180750Sdes		return b;
109180750Sdes		}
110180750Sdes	ex -= 0x3fff + 63;
111180750Sdes	mode = 2;
112180750Sdes	if (ndig <= 0) {
113180750Sdes		if (bufsize < 32)
114180750Sdes			return 0;
115180750Sdes		mode = 0;
116180750Sdes		}
117180750Sdes	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
118180750Sdes	return g__fmt(buf, s, se, decpt, sign, bufsize);
119180750Sdes	}
120180750Sdes