xref: /freebsd/contrib/gdtoa/g_Qfmt.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, 2000 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 #undef _0
35cc36ccd1SDavid Schultz #undef _1
36cc36ccd1SDavid Schultz 
37cc36ccd1SDavid Schultz /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
38cc36ccd1SDavid Schultz 
39cc36ccd1SDavid Schultz #ifdef IEEE_MC68k
40cc36ccd1SDavid Schultz #define _0 0
41cc36ccd1SDavid Schultz #define _1 1
42cc36ccd1SDavid Schultz #define _2 2
43cc36ccd1SDavid Schultz #define _3 3
44cc36ccd1SDavid Schultz #endif
45cc36ccd1SDavid Schultz #ifdef IEEE_8087
46cc36ccd1SDavid Schultz #define _0 3
47cc36ccd1SDavid Schultz #define _1 2
48cc36ccd1SDavid Schultz #define _2 1
49cc36ccd1SDavid Schultz #define _3 0
50cc36ccd1SDavid Schultz #endif
51cc36ccd1SDavid Schultz 
52cc36ccd1SDavid Schultz  char*
53cc36ccd1SDavid Schultz #ifdef KR_headers
g_Qfmt(buf,V,ndig,bufsize)544848dd08SDavid Schultz g_Qfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; size_t bufsize;
55cc36ccd1SDavid Schultz #else
564848dd08SDavid Schultz g_Qfmt(char *buf, void *V, int ndig, size_t bufsize)
57cc36ccd1SDavid Schultz #endif
58cc36ccd1SDavid Schultz {
594848dd08SDavid Schultz 	static FPI fpi0 = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, 0 };
60cc36ccd1SDavid Schultz 	char *b, *s, *se;
61cc36ccd1SDavid Schultz 	ULong bits[4], *L, sign;
62cc36ccd1SDavid Schultz 	int decpt, ex, i, mode;
634848dd08SDavid Schultz #ifdef Honor_FLT_ROUNDS
644848dd08SDavid Schultz #include "gdtoa_fltrnds.h"
654848dd08SDavid Schultz #else
664848dd08SDavid Schultz #define fpi &fpi0
674848dd08SDavid Schultz #endif
68cc36ccd1SDavid Schultz 
69cc36ccd1SDavid Schultz 	if (ndig < 0)
70cc36ccd1SDavid Schultz 		ndig = 0;
71cc36ccd1SDavid Schultz 	if (bufsize < ndig + 10)
72cc36ccd1SDavid Schultz 		return 0;
73cc36ccd1SDavid Schultz 
74cc36ccd1SDavid Schultz 	L = (ULong*)V;
75cc36ccd1SDavid Schultz 	sign = L[_0] & 0x80000000L;
76cc36ccd1SDavid Schultz 	bits[3] = L[_0] & 0xffff;
77cc36ccd1SDavid Schultz 	bits[2] = L[_1];
78cc36ccd1SDavid Schultz 	bits[1] = L[_2];
79cc36ccd1SDavid Schultz 	bits[0] = L[_3];
80cc36ccd1SDavid Schultz 	b = buf;
81cc36ccd1SDavid Schultz 	if ( (ex = (L[_0] & 0x7fff0000L) >> 16) !=0) {
82cc36ccd1SDavid Schultz 		if (ex == 0x7fff) {
83cc36ccd1SDavid Schultz 			/* Infinity or NaN */
84cc36ccd1SDavid Schultz 			if (bits[0] | bits[1] | bits[2] | bits[3])
85c88250a5SDavid Schultz 				b = strcp(b, "NaN");
86cc36ccd1SDavid Schultz 			else {
87cc36ccd1SDavid Schultz 				b = buf;
88cc36ccd1SDavid Schultz 				if (sign)
89cc36ccd1SDavid Schultz 					*b++ = '-';
90cc36ccd1SDavid Schultz 				b = strcp(b, "Infinity");
91cc36ccd1SDavid Schultz 				}
92cc36ccd1SDavid Schultz 			return b;
93cc36ccd1SDavid Schultz 			}
94cc36ccd1SDavid Schultz 		i = STRTOG_Normal;
95cc36ccd1SDavid Schultz 		bits[3] |= 0x10000;
96cc36ccd1SDavid Schultz 		}
97cc36ccd1SDavid Schultz 	else if (bits[0] | bits[1] | bits[2] | bits[3]) {
98cc36ccd1SDavid Schultz 		i = STRTOG_Denormal;
99cc36ccd1SDavid Schultz 		ex = 1;
100cc36ccd1SDavid Schultz 		}
101cc36ccd1SDavid Schultz 	else {
102cc36ccd1SDavid Schultz #ifndef IGNORE_ZERO_SIGN
103cc36ccd1SDavid Schultz 		if (sign)
104cc36ccd1SDavid Schultz 			*b++ = '-';
105cc36ccd1SDavid Schultz #endif
106cc36ccd1SDavid Schultz 		*b++ = '0';
107cc36ccd1SDavid Schultz 		*b = 0;
108cc36ccd1SDavid Schultz 		return b;
109cc36ccd1SDavid Schultz 		}
110cc36ccd1SDavid Schultz 	ex -= 0x3fff + 112;
111cc36ccd1SDavid Schultz 	mode = 2;
112cc36ccd1SDavid Schultz 	if (ndig <= 0) {
113cc36ccd1SDavid Schultz 		if (bufsize < 48)
114cc36ccd1SDavid Schultz 			return 0;
115cc36ccd1SDavid Schultz 		mode = 0;
116cc36ccd1SDavid Schultz 		}
1174848dd08SDavid Schultz 	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
1184848dd08SDavid Schultz 	return g__fmt(buf, s, se, decpt, sign, bufsize);
119cc36ccd1SDavid Schultz 	}
120