11195Srgrimes/****************************************************************
250472Speter
337SrgrimesThe author of this software is David M. Gay.
4156813Sru
5156813SruCopyright (C) 1998 by Lucent Technologies
6156813SruAll Rights Reserved
738103Speter
873251SgshapiroPermission to use, copy, modify, and distribute this software and
938103Speterits documentation for any purpose and without fee is hereby
10183242Ssamgranted, provided that the above copyright notice appear in all
11184343Ssamcopies and that both that the copyright notice and this
12183242Ssampermission notice and warranty disclaimer appear in supporting
13161748Scpercivadocumentation, and that the name of Lucent or any of its entities
14183242Ssamnot be used in advertising or publicity pertaining to
15162674Spisodistribution of the software without specific, written prior
16158266Sumepermission.
17183242Ssam
18145693SbrooksLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19145693SbrooksINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20183242SsamIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21183242SsamSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22183242SsamWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23155210SrwatsonIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24155210SrwatsonARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25155210SrwatsonTHIS SOFTWARE.
26155210Srwatson
27155210Srwatson****************************************************************/
28155210Srwatson
29155210Srwatson/* Please send bug reports to David M. Gay (dmg at acm dot org,
30155210Srwatson * with " at " changed at "@" and " dot " changed to ".").	*/
31155210Srwatson
32183242Ssam#include "gdtoaimp.h"
33183242Ssam
34183242Ssam char*
35183242Ssam#ifdef KR_headers
36183242Ssamg_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; size_t bufsize;
37183242Ssam#else
38183242Ssamg_ffmt(char *buf, float *f, int ndig, size_t bufsize)
39183242Ssam#endif
40183242Ssam{
41183242Ssam	static FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, 0 };
42183242Ssam	char *b, *s, *se;
43183242Ssam	ULong bits[1], *L, sign;
44183242Ssam	int decpt, ex, i, mode;
45183242Ssam#ifdef Honor_FLT_ROUNDS
46183242Ssam#include "gdtoa_fltrnds.h"
47183242Ssam#else
48183242Ssam#define fpi &fpi0
49183242Ssam#endif
50183242Ssam
51183242Ssam	if (ndig < 0)
52183242Ssam		ndig = 0;
53183242Ssam	if (bufsize < ndig + 10)
54156813Sru		return 0;
55183242Ssam
56121911Smarkm	L = (ULong*)f;
5737Srgrimes	sign = L[0] & 0x80000000L;
58183242Ssam	if ((L[0] & 0x7f800000) == 0x7f800000) {
59183242Ssam		/* Infinity or NaN */
60158115Sume		if (L[0] & 0x7fffff) {
61158115Sume			return strcp(buf, "NaN");
62183242Ssam			}
63183242Ssam		b = buf;
64183242Ssam		if (sign)
65183242Ssam			*b++ = '-';
66193635Sedwin		return strcp(b, "Infinity");
67193635Sedwin		}
68193635Sedwin	if (*f == 0.) {
69193635Sedwin		b = buf;
70156813Sru#ifndef IGNORE_ZERO_SIGN
7157488Speter		if (L[0] & 0x80000000L)
7274837Sgreen			*b++ = '-';
73124214Sdes#endif
7457459Smarkm		*b++ = '0';
75156813Sru		*b = 0;
7660677Skris		return b;
7760677Skris		}
7860677Skris	bits[0] = L[0] & 0x7fffff;
79183242Ssam	if ( (ex = (L[0] >> 23) & 0xff) !=0)
80183242Ssam		bits[0] |= 0x800000;
81183242Ssam	else
82183242Ssam		ex = 1;
83183242Ssam	ex -= 0x7f + 23;
84183242Ssam	mode = 2;
85183242Ssam	if (ndig <= 0) {
86183242Ssam		if (bufsize < 16)
87183242Ssam			return 0;
88183242Ssam		mode = 0;
89183242Ssam		}
90183242Ssam	i = STRTOG_Normal;
91184343Ssam	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
92184343Ssam	return g__fmt(buf, s, se, decpt, sign, bufsize);
93184343Ssam	}
94184343Ssam