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 char*
35112158Sdas#ifdef KR_headers
36187808Sdasg_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; size_t bufsize;
37112158Sdas#else
38187808Sdasg_ffmt(char *buf, float *f, int ndig, size_t bufsize)
39112158Sdas#endif
40112158Sdas{
41187808Sdas	static FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, 0 };
42112158Sdas	char *b, *s, *se;
43112158Sdas	ULong bits[1], *L, sign;
44112158Sdas	int decpt, ex, i, mode;
45187808Sdas#ifdef Honor_FLT_ROUNDS
46187808Sdas#include "gdtoa_fltrnds.h"
47187808Sdas#else
48187808Sdas#define fpi &fpi0
49187808Sdas#endif
50112158Sdas
51112158Sdas	if (ndig < 0)
52112158Sdas		ndig = 0;
53112158Sdas	if (bufsize < ndig + 10)
54112158Sdas		return 0;
55112158Sdas
56112158Sdas	L = (ULong*)f;
57112158Sdas	sign = L[0] & 0x80000000L;
58112158Sdas	if ((L[0] & 0x7f800000) == 0x7f800000) {
59112158Sdas		/* Infinity or NaN */
60112158Sdas		if (L[0] & 0x7fffff) {
61112158Sdas			return strcp(buf, "NaN");
62112158Sdas			}
63112158Sdas		b = buf;
64112158Sdas		if (sign)
65112158Sdas			*b++ = '-';
66112158Sdas		return strcp(b, "Infinity");
67112158Sdas		}
68112158Sdas	if (*f == 0.) {
69112158Sdas		b = buf;
70112158Sdas#ifndef IGNORE_ZERO_SIGN
71112158Sdas		if (L[0] & 0x80000000L)
72112158Sdas			*b++ = '-';
73112158Sdas#endif
74112158Sdas		*b++ = '0';
75112158Sdas		*b = 0;
76112158Sdas		return b;
77112158Sdas		}
78112158Sdas	bits[0] = L[0] & 0x7fffff;
79112158Sdas	if ( (ex = (L[0] >> 23) & 0xff) !=0)
80112158Sdas		bits[0] |= 0x800000;
81112158Sdas	else
82112158Sdas		ex = 1;
83112158Sdas	ex -= 0x7f + 23;
84112158Sdas	mode = 2;
85112158Sdas	if (ndig <= 0) {
86112158Sdas		if (bufsize < 16)
87112158Sdas			return 0;
88112158Sdas		mode = 0;
89112158Sdas		}
90112158Sdas	i = STRTOG_Normal;
91187808Sdas	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
92187808Sdas	return g__fmt(buf, s, se, decpt, sign, bufsize);
93112158Sdas	}
94