Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / lib / pic14 / libc / _ftoa.c
blob4baf176275aa5cd00bd8e04cb0f5097115eface4
1 /*-------------------------------------------------------------------------
2 x_ftoa.c - wrapper function to use _convert_float
4 Copyright (C) 2004, Vangelis Rokas <vrokas at otenet.gr>
6 Modifications for PIC14 by
7 Copyright (C) 2019 Gonzalo Pérez de Olaguer Córdoba <salo@gpoc.es>
9 This library is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this library; see the file COPYING. If not, write to the
21 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22 MA 02110-1301, USA.
24 As a special exception, if you link this library with other files,
25 some of which are compiled with SDCC, to produce an executable,
26 this library does not by itself cause the resulting executable to
27 be covered by the GNU General Public License. This exception does
28 not however invalidate any other reasons why the executable file
29 might be covered by the GNU General Public License.
30 -------------------------------------------------------------------------*/
32 #define __SDCC_FLOAT_LIB
33 #include <float.h>
34 #include <stdlib.h>
36 union {
37 unsigned long l;
38 float f;
39 } lf;
40 int e2;
41 int e10;
43 #define M lf.l /* mantissa */
44 #define F lf.f /* float */
45 #define E2 e2 /* exponent base 2 */
46 #define E10 e10 /* exponent base 10 */
48 /* While processing decimal output we consider the mantissa as a
49 * fixed point number with the integer part in bits 31:28 and the
50 * fractional part in bits 27:0.
52 #define MANT_UNIT (1UL << 28)
54 static char *S; /* the output string */
56 static void _div2 (void)
58 M = ((M + 1) >> 1);
61 static void _mul2 (void)
63 M = (M << 1);
66 static void _div5 (void)
68 M = ((M + 2) / 5);
71 static void _mul5 (void)
73 M = (M + (M << 2));
76 static void _div10 (void)
78 M = ((M + 5) / 10);
81 static void _mul10 (void)
83 M = ((M + (M << 2)) << 1);
86 static void _round (int x)
88 if (x >= 0)
90 unsigned long r = M;
91 M = (MANT_UNIT >> 1);
92 while (x > 0)
94 _div10();
95 --x;
97 M += r;
98 if (M >= 10 * MANT_UNIT)
100 _div10();
101 ++E10;
106 static void _put_char (char c)
108 *S++ = c;
111 static void _put_digit (void)
113 _put_char( '0' + ((M >> 28) & 0x0f));
114 M &= 0x0fffffffUL;
115 _mul10();
118 void _ftoa (float value, char *str, unsigned char prec)
120 F = value;
121 S = str;
123 /* output sign */
124 if (SIGN(M))
125 _put_char('-');
127 E2 = EXP(M);
128 M = MANT(M);
130 /* infinity or not a number */
131 if (E2 == 255)
133 if (M == HIDDEN)
135 /* infinity */
136 _put_char('i');
137 _put_char('n');
138 _put_char('F');
140 else
142 /* not a number */
143 _put_char('n');
144 _put_char('a');
145 _put_char('n');
147 _put_char('\0');
148 return;
151 /* zero or denormal number */
152 if (E2 == 0)
154 if (M == HIDDEN)
156 /* zero */
157 _put_char('0');
158 E10 = prec & PREC_P;
159 if (E10)
161 _put_char('.');
162 while (E10--)
163 _put_char('0');
165 if (prec & PREC_E)
167 _put_char('E2');
168 _put_char('+');
169 _put_char('0');
170 _put_char('0');
172 _put_char('\0');
173 return;
175 else
177 /* denormal number: value = (M - HIDDEN) * pow (2, - 149) */
178 M &= ~HIDDEN;
179 E2 = 1;
180 while (M < HIDDEN)
182 _mul2();
183 --E2;
188 /* Now we have a normalized number with value = M * pow (2, E2 - 150)
189 * where (1 << 23) <= M < (1 << 24) and -22 <= E2 < 255
191 * We adjust it to make M a base2 fixed point value with the implicit
192 * decimal point at bit 28 (so bits 31:28 are the integer part and
193 * bits 27:0 are the fractional part) and E2 is the unbiased base2
194 * exponent.
196 * Then we convert it to M * pow (10, E10)
198 M <<= (28-23); /* now MANT_UNIT <= M < 2 * MANT_UNIT */
199 E2 -= EXCESS + 1; /* now -149 <= E2 < 128 */
200 E10 = 0; /* base 10 exponent */
201 while (E2 > 0)
203 if (M < 5 * MANT_UNIT)
205 _mul2();
207 else
209 _div5();
210 ++E10;
212 --E2;
214 while (E2 < 0)
216 if (M < 2 * MANT_UNIT)
218 _mul5();
219 --E10;
221 else
223 _div2();
225 ++E2;
227 while (M < MANT_UNIT)
229 _mul10();
230 --E10;
232 /* Now we have a number with value = M * pow (10, E10)
233 * where 1 <= M < 10 with fixed point after bit 28.
235 /* Format G: use format E10 if exp < -4 or exp >= prec; format F otherwise. */
236 if ((prec & (PREC_E|PREC_F)) == 0)
238 if (E10 < -4 || E10 >= prec)
239 prec |= PREC_E;
240 else
241 prec |= PREC_F;
243 /* Format E: [-]d.dddE+-dd rounded to 'prec' fractional digits (prec >= 0) */
244 if (prec & PREC_E)
246 _round(prec & PREC_P);
247 _put_digit();
248 E2 = prec & PREC_P;
249 if (E2 > 0)
251 _put_char('.');
252 while (E2 > 0)
254 _put_digit();
255 --E2;
258 _put_char('E');
259 if (E10 < 0)
261 _put_char('-');
262 E10 = - E10;
264 else
266 _put_char('+');
268 _uitoa (E10, S, 10);
270 /* Format F: [-]ddd.ddd rounded to 'prec' fractional digits (prec >= 0) */
271 else
273 _round(E10 + (prec & PREC_P));
274 /* Print the integer part */
275 if (E10 >= 0)
277 while (E10 >= 0)
279 _put_digit();
280 --E10;
283 else
285 _put_char('0');
287 E2 = prec & PREC_P;
288 if (E2 > 0)
290 /* Print the fractional part */
291 _put_char('.');
292 while (E2 > 0)
294 if (E10 < -1)
296 _put_char('0');
297 ++E10;
299 else
301 _put_digit();
303 --E2;
306 _put_char('\0');