2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "build/build_config.h"
24 FILE_COMPILE_FOR_SPEED
26 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
28 void uli2a(unsigned long int num
, unsigned int base
, int uc
, char *bf
)
32 while (num
/ d
>= base
)
38 if (n
|| dgt
> 0 || d
== 0) {
39 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
46 void li2a(long num
, char *bf
)
52 uli2a(num
, 10, 0, bf
);
57 void ui2a(unsigned int num
, unsigned int base
, int uc
, char *bf
)
61 while (num
/ d
>= base
)
67 if (n
|| dgt
> 0 || d
== 0) {
68 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
75 void i2a(int num
, char *bf
)
86 if (ch
>= '0' && ch
<= '9')
88 else if (ch
>= 'a' && ch
<= 'f')
90 else if (ch
>= 'A' && ch
<= 'F')
96 char a2i(char ch
, const char **src
, int base
, int *nump
)
101 while ((digit
= a2d(ch
)) >= 0) {
104 num
= num
* base
+ digit
;
112 #ifndef HAVE_ITOA_FUNCTION
115 ** The following two functions together make up an itoa()
116 ** implementation. Function i2a() is a 'private' function
117 ** called by the public itoa() function.
119 ** itoa() takes three arguments:
120 ** 1) the integer to be converted,
121 ** 2) a pointer to a character conversion buffer,
122 ** 3) the radix for the conversion
123 ** which can range between 2 and 36 inclusive
124 ** range errors on the radix default it to base10
125 ** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
128 static char *_i2a(unsigned i
, char *a
, unsigned base
)
131 a
= _i2a(i
/ base
, a
, base
);
132 *a
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i
% base
];
136 char *itoa(int i
, char *a
, int base
)
138 if ((base
< 2) || (base
> 36))
142 *_i2a(-(unsigned) i
, a
+ 1, base
) = 0;
144 *_i2a(i
, a
, base
) = 0;
150 char *ftoa(float x
, char *floatString
)
154 char intString2
[12] = { 0, };
155 char *decimalPoint
= ".";
158 if (x
> 0) // Rounding for x.xxx display format
163 value
= (int32_t)(x
* 1000.0f
); // Convert float * 1000 to an integer
165 itoa(ABS(value
), intString1
, 10); // Create string from abs of integer value
168 intString2
[0] = ' '; // Positive number, add a pad space
170 intString2
[0] = '-'; // Negative number, add a negative sign
172 if (strlen(intString1
) == 1) {
176 strcat(intString2
, intString1
);
177 } else if (strlen(intString1
) == 2) {
180 strcat(intString2
, intString1
);
181 } else if (strlen(intString1
) == 3) {
183 strcat(intString2
, intString1
);
185 strcat(intString2
, intString1
);
188 dpLocation
= strlen(intString2
) - 3;
190 memcpy(floatString
, intString2
, dpLocation
);
191 floatString
[dpLocation
] = '\0';
192 strcat(floatString
, decimalPoint
);
193 strcat(floatString
, intString2
+ dpLocation
);
198 // Simple and fast atof (ascii to float) function.
200 // - Executes about 5x faster than standard MSCRT library atof().
201 // - An attractive alternative if the number of calls is in the millions.
202 // - Assumes input is a proper integer, fraction, or scientific format.
203 // - Matches library atof() to 15 digits (except at extreme exponents).
204 // - Follows atof() precedent of essentially no error checking.
206 // 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
207 #define white_space(c) ((c) == ' ' || (c) == '\t')
208 #define valid_digit(c) ((c) >= '0' && (c) <= '9')
209 float fastA2F(const char *p
)
212 float sign
, value
, scale
;
214 // Skip leading white space, if any.
215 while (white_space(*p
)) {
225 } else if (*p
== '+') {
229 // Get digits before decimal point or exponent, if any.
231 while (valid_digit(*p
)) {
232 value
= value
* 10.0f
+ (*p
- '0');
236 // Get digits after decimal point, if any.
241 while (valid_digit(*p
)) {
242 value
+= (*p
- '0') / pow10
;
248 // Handle exponent, if any.
250 if ((*p
== 'e') || (*p
== 'E')) {
254 // Get sign of exponent, if any.
260 } else if (*p
== '+') {
264 // Get digits of exponent, if any.
266 while (valid_digit(*p
)) {
267 expon
= expon
* 10 + (*p
- '0');
273 // Calculate scaling factor.
274 // while (expon >= 50) { scale *= 1E50f; expon -= 50; }
285 // Return signed and scaled floating point result.
286 return sign
* (frac
? (value
/ scale
) : (value
* scale
));
289 unsigned long int fastA2UL(const char *p
)
291 unsigned long int result
= 0;
294 while (white_space(*p
)) {
309 int fastA2I(const char *s
)
315 while (white_space(*s
)) {
324 while ((digit
= a2d(*s
)) >= 0) {
327 num
= num
* 10 + digit
;