xref: /freebsd/contrib/gdtoa/g_ffmt.c (revision 7d4b968b0fa6afc6bca132c27e2041c979ee6101)
1cc36ccd1SDavid Schultz /****************************************************************
2cc36ccd1SDavid Schultz 
3cc36ccd1SDavid Schultz The author of this software is David M. Gay.
4cc36ccd1SDavid Schultz 
5cc36ccd1SDavid Schultz Copyright (C) 1998 by Lucent Technologies
6cc36ccd1SDavid Schultz All Rights Reserved
7cc36ccd1SDavid Schultz 
8cc36ccd1SDavid Schultz Permission to use, copy, modify, and distribute this software and
9cc36ccd1SDavid Schultz its documentation for any purpose and without fee is hereby
10cc36ccd1SDavid Schultz granted, provided that the above copyright notice appear in all
11cc36ccd1SDavid Schultz copies and that both that the copyright notice and this
12cc36ccd1SDavid Schultz permission notice and warranty disclaimer appear in supporting
13cc36ccd1SDavid Schultz documentation, and that the name of Lucent or any of its entities
14cc36ccd1SDavid Schultz not be used in advertising or publicity pertaining to
15cc36ccd1SDavid Schultz distribution of the software without specific, written prior
16cc36ccd1SDavid Schultz permission.
17cc36ccd1SDavid Schultz 
18cc36ccd1SDavid Schultz LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19cc36ccd1SDavid Schultz INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20cc36ccd1SDavid Schultz IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21cc36ccd1SDavid Schultz SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22cc36ccd1SDavid Schultz WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23cc36ccd1SDavid Schultz IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24cc36ccd1SDavid Schultz ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25cc36ccd1SDavid Schultz THIS SOFTWARE.
26cc36ccd1SDavid Schultz 
27cc36ccd1SDavid Schultz ****************************************************************/
28cc36ccd1SDavid Schultz 
29c88250a5SDavid Schultz /* Please send bug reports to David M. Gay (dmg at acm dot org,
30c88250a5SDavid Schultz  * with " at " changed at "@" and " dot " changed to ".").	*/
31cc36ccd1SDavid Schultz 
32cc36ccd1SDavid Schultz #include "gdtoaimp.h"
33cc36ccd1SDavid Schultz 
34cc36ccd1SDavid Schultz  char*
35cc36ccd1SDavid Schultz #ifdef KR_headers
g_ffmt(buf,f,ndig,bufsize)364848dd08SDavid Schultz g_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; size_t bufsize;
37cc36ccd1SDavid Schultz #else
384848dd08SDavid Schultz g_ffmt(char *buf, float *f, int ndig, size_t bufsize)
39cc36ccd1SDavid Schultz #endif
40cc36ccd1SDavid Schultz {
414848dd08SDavid Schultz 	static FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, 0 };
42cc36ccd1SDavid Schultz 	char *b, *s, *se;
43cc36ccd1SDavid Schultz 	ULong bits[1], *L, sign;
44cc36ccd1SDavid Schultz 	int decpt, ex, i, mode;
454848dd08SDavid Schultz #ifdef Honor_FLT_ROUNDS
464848dd08SDavid Schultz #include "gdtoa_fltrnds.h"
474848dd08SDavid Schultz #else
484848dd08SDavid Schultz #define fpi &fpi0
494848dd08SDavid Schultz #endif
50cc36ccd1SDavid Schultz 
51cc36ccd1SDavid Schultz 	if (ndig < 0)
52cc36ccd1SDavid Schultz 		ndig = 0;
53cc36ccd1SDavid Schultz 	if (bufsize < ndig + 10)
54cc36ccd1SDavid Schultz 		return 0;
55cc36ccd1SDavid Schultz 
56cc36ccd1SDavid Schultz 	L = (ULong*)f;
57cc36ccd1SDavid Schultz 	sign = L[0] & 0x80000000L;
58cc36ccd1SDavid Schultz 	if ((L[0] & 0x7f800000) == 0x7f800000) {
59cc36ccd1SDavid Schultz 		/* Infinity or NaN */
60cc36ccd1SDavid Schultz 		if (L[0] & 0x7fffff) {
61cc36ccd1SDavid Schultz 			return strcp(buf, "NaN");
62cc36ccd1SDavid Schultz 			}
63cc36ccd1SDavid Schultz 		b = buf;
64cc36ccd1SDavid Schultz 		if (sign)
65cc36ccd1SDavid Schultz 			*b++ = '-';
66cc36ccd1SDavid Schultz 		return strcp(b, "Infinity");
67cc36ccd1SDavid Schultz 		}
68cc36ccd1SDavid Schultz 	if (*f == 0.) {
69cc36ccd1SDavid Schultz 		b = buf;
70cc36ccd1SDavid Schultz #ifndef IGNORE_ZERO_SIGN
71cc36ccd1SDavid Schultz 		if (L[0] & 0x80000000L)
72cc36ccd1SDavid Schultz 			*b++ = '-';
73cc36ccd1SDavid Schultz #endif
74cc36ccd1SDavid Schultz 		*b++ = '0';
75cc36ccd1SDavid Schultz 		*b = 0;
76cc36ccd1SDavid Schultz 		return b;
77cc36ccd1SDavid Schultz 		}
78cc36ccd1SDavid Schultz 	bits[0] = L[0] & 0x7fffff;
79cc36ccd1SDavid Schultz 	if ( (ex = (L[0] >> 23) & 0xff) !=0)
80cc36ccd1SDavid Schultz 		bits[0] |= 0x800000;
81cc36ccd1SDavid Schultz 	else
82cc36ccd1SDavid Schultz 		ex = 1;
83cc36ccd1SDavid Schultz 	ex -= 0x7f + 23;
84cc36ccd1SDavid Schultz 	mode = 2;
85cc36ccd1SDavid Schultz 	if (ndig <= 0) {
86cc36ccd1SDavid Schultz 		if (bufsize < 16)
87cc36ccd1SDavid Schultz 			return 0;
88cc36ccd1SDavid Schultz 		mode = 0;
89cc36ccd1SDavid Schultz 		}
90cc36ccd1SDavid Schultz 	i = STRTOG_Normal;
914848dd08SDavid Schultz 	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
924848dd08SDavid Schultz 	return g__fmt(buf, s, se, decpt, sign, bufsize);
93cc36ccd1SDavid Schultz 	}
94