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"
25 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
27 void uli2a(unsigned long int num
, unsigned int base
, int uc
, char *bf
)
31 while (num
/ d
>= base
)
37 if (n
|| dgt
> 0 || d
== 0) {
38 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
45 void li2a(long num
, char *bf
)
51 uli2a(num
, 10, 0, bf
);
56 void ui2a(unsigned int num
, unsigned int base
, int uc
, char *bf
)
60 while (num
/ d
>= base
)
66 if (n
|| dgt
> 0 || d
== 0) {
67 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
74 void i2a(int num
, char *bf
)
85 if (ch
>= '0' && ch
<= '9')
87 else if (ch
>= 'a' && ch
<= 'f')
89 else if (ch
>= 'A' && ch
<= 'F')
95 char a2i(char ch
, const char **src
, int base
, int *nump
)
100 while ((digit
= a2d(ch
)) >= 0) {
103 num
= num
* base
+ digit
;
111 #ifndef HAVE_ITOA_FUNCTION
114 ** The following two functions together make up an itoa()
115 ** implementation. Function i2a() is a 'private' function
116 ** called by the public itoa() function.
118 ** itoa() takes three arguments:
119 ** 1) the integer to be converted,
120 ** 2) a pointer to a character conversion buffer,
121 ** 3) the radix for the conversion
122 ** which can range between 2 and 36 inclusive
123 ** range errors on the radix default it to base10
124 ** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
127 static char *_i2a(unsigned i
, char *a
, unsigned base
)
130 a
= _i2a(i
/ base
, a
, base
);
131 *a
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i
% base
];
135 char *itoa(int i
, char *a
, int base
)
137 if ((base
< 2) || (base
> 36))
141 *_i2a(-(unsigned) i
, a
+ 1, base
) = 0;
143 *_i2a(i
, a
, base
) = 0;
149 char *ftoa(float x
, char *floatString
)
153 char intString2
[12] = { 0, };
154 char *decimalPoint
= ".";
157 if (x
> 0) // Rounding for x.xxx display format
162 value
= (int32_t)(x
* 1000.0f
); // Convert float * 1000 to an integer
164 itoa(ABS(value
), intString1
, 10); // Create string from abs of integer value
167 intString2
[0] = ' '; // Positive number, add a pad space
169 intString2
[0] = '-'; // Negative number, add a negative sign
171 if (strlen(intString1
) == 1) {
175 strcat(intString2
, intString1
);
176 } else if (strlen(intString1
) == 2) {
179 strcat(intString2
, intString1
);
180 } else if (strlen(intString1
) == 3) {
182 strcat(intString2
, intString1
);
184 strcat(intString2
, intString1
);
187 dpLocation
= strlen(intString2
) - 3;
189 memcpy(floatString
, intString2
, dpLocation
);
190 floatString
[dpLocation
] = '\0';
191 strcat(floatString
, decimalPoint
);
192 strcat(floatString
, intString2
+ dpLocation
);
197 // Simple and fast atof (ascii to float) function.
199 // - Executes about 5x faster than standard MSCRT library atof().
200 // - An attractive alternative if the number of calls is in the millions.
201 // - Assumes input is a proper integer, fraction, or scientific format.
202 // - Matches library atof() to 15 digits (except at extreme exponents).
203 // - Follows atof() precedent of essentially no error checking.
205 // 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
206 #define white_space(c) ((c) == ' ' || (c) == '\t')
207 #define valid_digit(c) ((c) >= '0' && (c) <= '9')
208 float fastA2F(const char *p
)
211 float sign
, value
, scale
;
213 // Skip leading white space, if any.
214 while (white_space(*p
)) {
224 } else if (*p
== '+') {
228 // Get digits before decimal point or exponent, if any.
230 while (valid_digit(*p
)) {
231 value
= value
* 10.0f
+ (*p
- '0');
235 // Get digits after decimal point, if any.
240 while (valid_digit(*p
)) {
241 value
+= (*p
- '0') / pow10
;
247 // Handle exponent, if any.
249 if ((*p
== 'e') || (*p
== 'E')) {
253 // Get sign of exponent, if any.
259 } else if (*p
== '+') {
263 // Get digits of exponent, if any.
265 while (valid_digit(*p
)) {
266 expon
= expon
* 10 + (*p
- '0');
272 // Calculate scaling factor.
273 // while (expon >= 50) { scale *= 1E50f; expon -= 50; }
284 // Return signed and scaled floating point result.
285 return sign
* (frac
? (value
/ scale
) : (value
* scale
));
288 unsigned long int fastA2UL(const char *p
)
290 unsigned long int result
= 0;
293 while (white_space(*p
)) {
308 int fastA2I(const char *s
)
314 while (white_space(*s
)) {
323 while ((digit
= a2d(*s
)) >= 0) {
326 num
= num
* 10 + digit
;