Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/src/fmt_fp.c
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/src/fmt_fp.c
r331 r439 1 #ifndef MRB_WITHOUT_FLOAT 2 #if defined(MRB_DISABLE_STDIO) || defined(_WIN32) || defined(_WIN64) 1 3 /* 2 4 … … 29 31 #include <limits.h> 30 32 #include <string.h> 31 #include <stdint.h>32 33 #include <math.h> 33 34 #include <float.h> … … 62 63 #define PAD_SIZE 256 63 64 static void 64 pad(struct fmt_args *f, char c, int w, int l, int fl)65 pad(struct fmt_args *f, char c, ptrdiff_t w, ptrdiff_t l, uint8_t fl) 65 66 { 66 67 char pad[PAD_SIZE]; … … 92 93 93 94 static int 94 fmt_fp(struct fmt_args *f, long double y, int w, int p, int fl, int t)95 fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) 95 96 { 96 97 uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion … … 98 99 uint32_t *a, *d, *r, *z; 99 100 uint32_t i; 100 int e2=0, e, j, l; 101 int e2=0, e, j; 102 ptrdiff_t l; 101 103 char buf[9+LDBL_MANT_DIG/4], *s; 102 104 const char *prefix="-0X+0X 0X-0x+0x 0x"; 103 int pl;105 ptrdiff_t pl; 104 106 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr; 105 107 … … 116 118 const char *ss = (t&32)?"inf":"INF"; 117 119 if (y!=y) ss=(t&32)?"nan":"NAN"; 118 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);120 pad(f, ' ', 0, 3+pl, fl&~ZERO_PAD); 119 121 out(f, prefix, pl); 120 122 out(f, ss, 3); 121 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);122 return MAX(w, 3+pl);123 pad(f, ' ', 0, 3+pl, fl^LEFT_ADJ); 124 return 3+(int)pl; 123 125 } 124 126 … … 128 130 if ((t|32)=='a') { 129 131 long double round = 8.0; 130 int re;132 ptrdiff_t re; 131 133 132 134 if (t&32) prefix += 9; … … 168 170 l = (s-buf) + (ebuf-estr); 169 171 170 pad(f, ' ', w, pl+l, fl);172 pad(f, ' ', 0, pl+l, fl); 171 173 out(f, prefix, pl); 172 pad(f, '0', w, pl+l, fl^ZERO_PAD);174 pad(f, '0', 0, pl+l, fl^ZERO_PAD); 173 175 out(f, buf, s-buf); 174 176 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0); 175 177 out(f, estr, ebuf-estr); 176 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);177 return MAX(w, pl+l);178 pad(f, ' ', 0, pl+l, fl^LEFT_ADJ); 179 return (int)pl+(int)l; 178 180 } 179 181 if (p<0) p=6; … … 203 205 while (e2<0) { 204 206 uint32_t carry=0, *b; 205 int sh=MIN(9,-e2), need=1+( p+LDBL_MANT_DIG/3+8)/9;207 int sh=MIN(9,-e2), need=1+((int)p+LDBL_MANT_DIG/3+8)/9; 206 208 for (d=a; d<z; d++) { 207 209 uint32_t rm = *d & ((1<<sh)-1); … … 217 219 } 218 220 219 if (a<z) for (i=10, e=9*( r-a); *a>=i; i*=10, e++);221 if (a<z) for (i=10, e=9*(int)(r-a); *a>=i; i*=10, e++); 220 222 else e=0; 221 223 222 224 /* Perform rounding: j is precision after the radix (possibly neg) */ 223 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);225 j = (int)p - ((t|32)!='f')*e - ((t|32)=='g' && p); 224 226 if (j < 9*(z-r-1)) { 225 227 uint32_t x; … … 248 250 (*d)++; 249 251 } 250 for (i=10, e=9*( r-a); *a>=i; i*=10, e++);252 for (i=10, e=9*(int)(r-a); *a>=i; i*=10, e++); 251 253 } 252 254 } … … 287 289 } 288 290 289 pad(f, ' ', w, pl+l, fl);291 pad(f, ' ', 0, pl+l, fl); 290 292 out(f, prefix, pl); 291 pad(f, '0', w, pl+l, fl^ZERO_PAD);293 pad(f, '0', 0, pl+l, fl^ZERO_PAD); 292 294 293 295 if ((t|32)=='f') { … … 318 320 } 319 321 out(f, ss, MIN(buf+9-ss, p)); 320 p -= buf+9-ss;322 p -= (int)(buf+9-ss); 321 323 } 322 324 pad(f, '0', p+18, 18, 0); … … 324 326 } 325 327 326 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);327 328 return MAX(w, pl+l);328 pad(f, ' ', 0, pl+l, fl^LEFT_ADJ); 329 330 return (int)pl+(int)l; 329 331 } 330 332 … … 332 334 fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo) 333 335 { 334 int p;336 ptrdiff_t p; 335 337 336 338 if (*fmt != '%') { … … 352 354 case 'e': case 'f': case 'g': case 'a': 353 355 case 'E': case 'F': case 'G': case 'A': 354 return fmt_fp(f, flo, 0,p, 0, *fmt);356 return fmt_fp(f, flo, p, 0, *fmt); 355 357 default: 356 358 return -1; … … 364 366 365 367 f.mrb = mrb; 366 f.str = mrb_str_ buf_new(mrb, 24);368 f.str = mrb_str_new_capa(mrb, 24); 367 369 if (fmt_core(&f, fmt, mrb_float(flo)) < 0) { 368 370 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid format string"); … … 370 372 return f.str; 371 373 } 374 #else /* MRB_DISABLE_STDIO || _WIN32 || _WIN64 */ 375 #include <mruby.h> 376 #include <stdio.h> 377 378 mrb_value 379 mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt) 380 { 381 char buf[25]; 382 383 snprintf(buf, sizeof(buf), fmt, mrb_float(flo)); 384 return mrb_str_new_cstr(mrb, buf); 385 } 386 #endif /* MRB_DISABLE_STDIO || _WIN32 || _WIN64 */ 387 #endif
Note:
See TracChangeset
for help on using the changeset viewer.