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
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,
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
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)
61 static void _mul2 (void)
66 static void _div5 (void)
71 static void _mul5 (void)
76 static void _div10 (void)
81 static void _mul10 (void)
83 M
= ((M
+ (M
<< 2)) << 1);
86 static void _round (int x
)
98 if (M
>= 10 * MANT_UNIT
)
106 static void _put_char (char c
)
111 static void _put_digit (void)
113 _put_char( '0' + ((M
>> 28) & 0x0f));
118 void _ftoa (float value
, char *str
, unsigned char prec
)
130 /* infinity or not a number */
151 /* zero or denormal number */
177 /* denormal number: value = (M - HIDDEN) * pow (2, - 149) */
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
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 */
203 if (M
< 5 * MANT_UNIT
)
216 if (M
< 2 * MANT_UNIT
)
227 while (M
< MANT_UNIT
)
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
)
243 /* Format E: [-]d.dddE+-dd rounded to 'prec' fractional digits (prec >= 0) */
246 _round(prec
& PREC_P
);
270 /* Format F: [-]ddd.ddd rounded to 'prec' fractional digits (prec >= 0) */
273 _round(E10
+ (prec
& PREC_P
));
274 /* Print the integer part */
290 /* Print the fractional part */