28 #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_NONE 51 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 58 #define FPCONST(x) x##F 59 #define FPROUND_TO_ONE 0.9999995F 61 #define FPMIN_BUF_SIZE 6 // +9e+99 63 #define FLT_SIGN_MASK 0x80000000 64 #define FLT_EXP_MASK 0x7F800000 65 #define FLT_MAN_MASK 0x007FFFFF 71 static inline int fp_signbit(
float x) {
union floatbits fb = {x};
return fb.u & FLT_SIGN_MASK; }
72 #define fp_isnan(x) isnan(x) 73 #define fp_isinf(x) isinf(x) 74 static inline int fp_iszero(
float x) {
union floatbits fb = {x};
return fb.u == 0; }
75 static inline int fp_isless1(
float x) {
union floatbits fb = {x};
return fb.u < 0x3f800000; }
77 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 81 #define FPROUND_TO_ONE 0.999999999995 83 #define FPMIN_BUF_SIZE 7 // +9e+199 84 #define fp_signbit(x) signbit(x) 85 #define fp_isnan(x) isnan(x) 86 #define fp_isinf(x) isinf(x) 87 #define fp_iszero(x) (x == 0) 88 #define fp_isless1(x) (x < 1.0) 92 static const FPTYPE g_pos_pow[] = {
96 1e32, 1e16, 1e8, 1e4, 1e2, 1e1
98 static const FPTYPE g_neg_pow[] = {
100 1e-256, 1e-128, 1e-64,
102 1e-32, 1e-16, 1e-8, 1e-4, 1e-2, 1e-1
105 int mp_format_float(FPTYPE f,
char *buf,
size_t buf_size,
char fmt,
int prec,
char sign) {
109 if (buf_size <= FPMIN_BUF_SIZE) {
120 return buf_size >= 2;
122 if (fp_signbit(f) && !fp_isnan(f)) {
133 int buf_remaining = buf_size - 1 - (s - buf);
136 char uc = fmt & 0x20;
142 }
else if (fp_isnan(f)) {
155 char e_char =
'E' | (fmt & 0x20);
158 if (fmt ==
'g' && prec == 0) {
165 const FPTYPE *pos_pow = g_pos_pow;
166 const FPTYPE *neg_pow = g_neg_pow;
172 if (prec + 2 > buf_remaining) {
173 prec = buf_remaining - 2;
175 num_digits = prec + 1;
178 if (prec + 6 > buf_remaining) {
179 prec = buf_remaining - 6;
185 }
else if (fp_isless1(f)) {
190 char first_dig =
'0';
191 if (f >= FPROUND_TO_ONE) {
196 for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) {
202 char e_sign_char =
'-';
203 if (fp_isless1(f) && f >= FPROUND_TO_ONE) {
208 }
else if (fp_isless1(f)) {
216 if (fmt ==
'f' || (fmt ==
'g' && e <= 4)) {
221 if (org_fmt ==
'g') {
226 if (prec + 2 > buf_remaining) {
227 prec = buf_remaining - 2;
233 while (--e && num_digits) {
241 e_sign = e_sign_char;
244 if (prec > (buf_remaining - FPMIN_BUF_SIZE)) {
245 prec = buf_remaining - FPMIN_BUF_SIZE;
253 for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) {
261 if (f >= FPCONST(10.0)) {
271 if (e >= buf_remaining) {
273 }
else if ((e + prec + 2) > buf_remaining) {
274 prec = buf_remaining - e - 2;
282 if (fmt ==
'e' && prec > (buf_remaining - FPMIN_BUF_SIZE)) {
283 prec = buf_remaining - FPMIN_BUF_SIZE;
287 if (prec + (FPMIN_BUF_SIZE - 1) > buf_remaining) {
288 prec = buf_remaining - (FPMIN_BUF_SIZE - 1);
294 if (fmt ==
'g' && e < prec) {
300 num_digits = prec + e + 1;
322 num_digits = prec + 1;
323 }
else if (fmt ==
'g') {
331 for (
int i = 0; i < num_digits; ++i, --dec) {
334 if (dec == 0 && prec > 0) {
344 if ((org_fmt !=
'f' || e <= 1) && f >= FPCONST(5.0)) {
352 if (*rs < '0' || *rs >
'9') {
369 if (rs[1] ==
'.' && fmt !=
'f') {
396 assert((
size_t)(s + 1 - buf) <= buf_size);
398 if (org_fmt ==
'g' && prec > 0) {
400 while (s[-1] ==
'0') {
411 if (FPMIN_BUF_SIZE == 7 && e >= 100) {
412 *s++ =
'0' + (e / 100);
414 *s++ =
'0' + ((e / 10) % 10);
415 *s++ =
'0' + (e % 10);
420 assert((
size_t)(s + 1 - buf) <= buf_size);
425 #endif // MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_NONE