1112158Sdas/****************************************************************
2112158Sdas
3112158SdasThe author of this software is David M. Gay.
4112158Sdas
5112158SdasCopyright (C) 1998 by Lucent Technologies
6112158SdasAll Rights Reserved
7112158Sdas
8112158SdasPermission to use, copy, modify, and distribute this software and
9112158Sdasits documentation for any purpose and without fee is hereby
10112158Sdasgranted, provided that the above copyright notice appear in all
11112158Sdascopies and that both that the copyright notice and this
12112158Sdaspermission notice and warranty disclaimer appear in supporting
13112158Sdasdocumentation, and that the name of Lucent or any of its entities
14112158Sdasnot be used in advertising or publicity pertaining to
15112158Sdasdistribution of the software without specific, written prior
16112158Sdaspermission.
17112158Sdas
18112158SdasLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19112158SdasINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20112158SdasIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21112158SdasSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22112158SdasWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23112158SdasIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24112158SdasARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25112158SdasTHIS SOFTWARE.
26112158Sdas
27112158Sdas****************************************************************/
28112158Sdas
29165743Sdas/* Please send bug reports to David M. Gay (dmg at acm dot org,
30165743Sdas * with " at " changed at "@" and " dot " changed to ".").	*/
31112158Sdas
32112158Sdas#include "gdtoaimp.h"
33112158Sdas
34112158Sdas#undef _0
35112158Sdas#undef _1
36112158Sdas
37112158Sdas/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
38112158Sdas
39112158Sdas#ifdef IEEE_MC68k
40112158Sdas#define _0 0
41112158Sdas#define _1 1
42112158Sdas#define _2 2
43112158Sdas#define _3 3
44112158Sdas#define _4 4
45112158Sdas#endif
46112158Sdas#ifdef IEEE_8087
47112158Sdas#define _0 4
48112158Sdas#define _1 3
49112158Sdas#define _2 2
50112158Sdas#define _3 1
51112158Sdas#define _4 0
52112158Sdas#endif
53112158Sdas
54112158Sdas char*
55112158Sdas#ifdef KR_headers
56187808Sdasg_xfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; size_t bufsize;
57112158Sdas#else
58187808Sdasg_xfmt(char *buf, void *V, int ndig, size_t bufsize)
59112158Sdas#endif
60112158Sdas{
61187808Sdas	static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 };
62112158Sdas	char *b, *s, *se;
63112158Sdas	ULong bits[2], sign;
64112158Sdas	UShort *L;
65112158Sdas	int decpt, ex, i, mode;
66187808Sdas#ifdef Honor_FLT_ROUNDS
67187808Sdas#include "gdtoa_fltrnds.h"
68187808Sdas#else
69187808Sdas#define fpi &fpi0
70187808Sdas#endif
71112158Sdas
72112158Sdas	if (ndig < 0)
73112158Sdas		ndig = 0;
74112158Sdas	if (bufsize < ndig + 10)
75112158Sdas		return 0;
76112158Sdas
77112158Sdas	L = (UShort *)V;
78112158Sdas	sign = L[_0] & 0x8000;
79112158Sdas	bits[1] = (L[_1] << 16) | L[_2];
80112158Sdas	bits[0] = (L[_3] << 16) | L[_4];
81112158Sdas	if ( (ex = L[_0] & 0x7fff) !=0) {
82112158Sdas		if (ex == 0x7fff) {
83112158Sdas			/* Infinity or NaN */
84112158Sdas			if (bits[0] | bits[1])
85112158Sdas				b = strcp(buf, "NaN");
86112158Sdas			else {
87112158Sdas				b = buf;
88112158Sdas				if (sign)
89112158Sdas					*b++ = '-';
90112158Sdas				b = strcp(b, "Infinity");
91112158Sdas				}
92112158Sdas			return b;
93112158Sdas			}
94112158Sdas		i = STRTOG_Normal;
95112158Sdas		}
96112158Sdas	else if (bits[0] | bits[1]) {
97112158Sdas		i = STRTOG_Denormal;
98165743Sdas		ex = 1;
99112158Sdas		}
100112158Sdas	else {
101112158Sdas		b = buf;
102112158Sdas#ifndef IGNORE_ZERO_SIGN
103112158Sdas		if (sign)
104112158Sdas			*b++ = '-';
105112158Sdas#endif
106112158Sdas		*b++ = '0';
107112158Sdas		*b = 0;
108112158Sdas		return b;
109112158Sdas		}
110112158Sdas	ex -= 0x3fff + 63;
111112158Sdas	mode = 2;
112112158Sdas	if (ndig <= 0) {
113112158Sdas		if (bufsize < 32)
114112158Sdas			return 0;
115112158Sdas		mode = 0;
116112158Sdas		}
117187808Sdas	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
118187808Sdas	return g__fmt(buf, s, se, decpt, sign, bufsize);
119112158Sdas	}
120