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 David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to ".").	*/
31 
32 #include "gdtoaimp.h"
33 
34 #ifdef USE_LOCALE
35 #include "locale.h"
36 #endif
37 
38 #ifndef ldus_QNAN0
39 #define ldus_QNAN0 0x7fff
40 #endif
41 #ifndef ldus_QNAN1
42 #define ldus_QNAN1 0xc000
43 #endif
44 #ifndef ldus_QNAN2
45 #define ldus_QNAN2 0
46 #endif
47 #ifndef ldus_QNAN3
48 #define ldus_QNAN3 0
49 #endif
50 #ifndef ldus_QNAN4
51 #define ldus_QNAN4 0
52 #endif
53 
54  const char *InfName[6] = { "Infinity", "infinity", "INFINITY", "Inf", "inf", "INF" };
55  const char *NanName[3] = { "NaN", "nan", "NAN" };
56  ULong NanDflt_Q_D2A[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0x7fffffff };
57  ULong NanDflt_d_D2A[2] = { d_QNAN1, d_QNAN0 };
58  ULong NanDflt_f_D2A[1] = { f_QNAN };
59  ULong NanDflt_xL_D2A[3] = { 1, 0x80000000, 0x7fff0000 };
60  UShort NanDflt_ldus_D2A[5] = { ldus_QNAN4, ldus_QNAN3, ldus_QNAN2, ldus_QNAN1, ldus_QNAN0 };
61 
62  char *
63 #ifdef KR_headers
g__fmt(b,s,se,decpt,sign,blen)64 g__fmt(b, s, se, decpt, sign, blen) char *b; char *s; char *se; int decpt; ULong sign; size_t blen;
65 #else
66 g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
67 #endif
68 {
69 	int i, j, k;
70 	char *be, *s0;
71 	size_t len;
72 #ifdef USE_LOCALE
73 #ifdef NO_LOCALE_CACHE
74 	char *decimalpoint = localeconv()->decimal_point;
75 	size_t dlen = strlen(decimalpoint);
76 #else
77 	char *decimalpoint;
78 	static char *decimalpoint_cache;
79 	static size_t dlen;
80 	if (!(s0 = decimalpoint_cache)) {
81 		s0 = localeconv()->decimal_point;
82 		dlen = strlen(s0);
83 		if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {
84 			strcpy(decimalpoint_cache, s0);
85 			s0 = decimalpoint_cache;
86 			}
87 		}
88 	decimalpoint = s0;
89 #endif
90 #else
91 #define dlen 0
92 #endif
93 	s0 = s;
94 	len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */
95 	if (blen < len)
96 		goto ret0;
97 	be = b + blen - 1;
98 	if (sign)
99 		*b++ = '-';
100 	if (decpt <= -4 || decpt > se - s + 5) {
101 		*b++ = *s++;
102 		if (*s) {
103 #ifdef USE_LOCALE
104 			while((*b = *decimalpoint++))
105 				++b;
106 #else
107 			*b++ = '.';
108 #endif
109 			while((*b = *s++) !=0)
110 				b++;
111 			}
112 		*b++ = 'e';
113 		/* sprintf(b, "%+.2d", decpt - 1); */
114 		if (--decpt < 0) {
115 			*b++ = '-';
116 			decpt = -decpt;
117 			}
118 		else
119 			*b++ = '+';
120 		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
121 		for(;;) {
122 			i = decpt / k;
123 			if (b >= be)
124 				goto ret0;
125 			*b++ = i + '0';
126 			if (--j <= 0)
127 				break;
128 			decpt -= i*k;
129 			decpt *= 10;
130 			}
131 		*b = 0;
132 		}
133 	else if (decpt <= 0) {
134 #ifdef USE_LOCALE
135 		while((*b = *decimalpoint++))
136 			++b;
137 #else
138 		*b++ = '.';
139 #endif
140 		if (be < b - decpt + (se - s))
141 			goto ret0;
142 		for(; decpt < 0; decpt++)
143 			*b++ = '0';
144 		while((*b = *s++) != 0)
145 			b++;
146 		}
147 	else {
148 		while((*b = *s++) != 0) {
149 			b++;
150 			if (--decpt == 0 && *s) {
151 #ifdef USE_LOCALE
152 				while(*b = *decimalpoint++)
153 					++b;
154 #else
155 				*b++ = '.';
156 #endif
157 				}
158 			}
159 		if (b + decpt > be) {
160  ret0:
161 			b = 0;
162 			goto ret;
163 			}
164 		for(; decpt > 0; decpt--)
165 			*b++ = '0';
166 		*b = 0;
167 		}
168  ret:
169 	freedtoa(s0);
170 	return b;
171  	}
172 
173  char *
add_nanbits_D2A(char * b,size_t blen,ULong * bits,int nb)174 add_nanbits_D2A(char *b, size_t blen, ULong *bits, int nb)
175 {
176 	ULong t;
177 	char *rv;
178 	int i, j;
179 	size_t L;
180 	static char Hexdig[16] = "0123456789abcdef";
181 
182 	while(!bits[--nb])
183 		if (!nb)
184 			return b;
185 	L = 8*nb + 3;
186 	t = bits[nb];
187 	do ++L; while((t >>= 4));
188 	if (L > blen)
189 		return b;
190 	b += L;
191 	*--b = 0;
192 	rv = b;
193 	*--b = /*(*/ ')';
194 	for(i = 0; i < nb; ++i) {
195 		t = bits[i];
196 		for(j = 0; j < 8; ++j, t >>= 4)
197 			*--b = Hexdig[t & 0xf];
198 		}
199 	t = bits[nb];
200 	do *--b = Hexdig[t & 0xf]; while(t >>= 4);
201 	*--b = '('; /*)*/
202 	return rv;
203 	}
204