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