]> git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/internal/floatscan.c
Fix floatscan no long double usage (#21)
[wasi-libc.git] / libc-top-half / musl / src / internal / floatscan.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <math.h>
4 #include <float.h>
5 #include <limits.h>
6 #include <errno.h>
7 #include <ctype.h>
8 #ifdef __wasilibc_unmodified_upstream // Changes to optimize printf/scanf when long double isn't needed
9 #else
10 #include "printscan.h"
11 #endif
12
13 #include "shgetc.h"
14 #include "floatscan.h"
15
16 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
17
18 #define LD_B1B_DIG 2
19 #define LD_B1B_MAX 9007199, 254740991
20 #define KMAX 128
21
22 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
23
24 #define LD_B1B_DIG 3
25 #define LD_B1B_MAX 18, 446744073, 709551615
26 #define KMAX 2048
27
28 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
29
30 #define LD_B1B_DIG 4
31 #define LD_B1B_MAX 10384593, 717069655, 257060992, 658440191
32 #define KMAX 2048
33
34 #else
35 #error Unsupported long double representation
36 #endif
37
38 #define MASK (KMAX-1)
39
40 #define CONCAT2(x,y) x ## y
41 #define CONCAT(x,y) CONCAT2(x,y)
42
43 static long long scanexp(FILE *f, int pok)
44 {
45 int c;
46 int x;
47 long long y;
48 int neg = 0;
49
50 c = shgetc(f);
51 if (c=='+' || c=='-') {
52 neg = (c=='-');
53 c = shgetc(f);
54 if (c-'0'>=10U && pok) shunget(f);
55 }
56 if (c-'0'>=10U) {
57 shunget(f);
58 return LLONG_MIN;
59 }
60 for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
61 x = 10*x + c-'0';
62 for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
63 y = 10*y + c-'0';
64 for (; c-'0'<10U; c = shgetc(f));
65 shunget(f);
66 return neg ? -y : y;
67 }
68
69
70 #if defined(__wasilibc_printscan_no_long_double)
71 static long_double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
72 #else
73 static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
74 #endif
75 {
76 uint32_t x[KMAX];
77 static const uint32_t th[] = { LD_B1B_MAX };
78 int i, j, k, a, z;
79 long long lrp=0, dc=0;
80 long long e10=0;
81 int lnz = 0;
82 int gotdig = 0, gotrad = 0;
83 int rp;
84 int e2;
85 int emax = -emin-bits+3;
86 int denormal = 0;
87 #if defined(__wasilibc_printscan_no_long_double)
88 long_double y;
89 long_double frac=0;
90 long_double bias=0;
91 #else
92 long double y;
93 long double frac=0;
94 long double bias=0;
95 #endif
96 static const int p10s[] = { 10, 100, 1000, 10000,
97 100000, 1000000, 10000000, 100000000 };
98
99 j=0;
100 k=0;
101
102 /* Don't let leading zeros consume buffer space */
103 for (; c=='0'; c = shgetc(f)) gotdig=1;
104 if (c=='.') {
105 gotrad = 1;
106 for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
107 }
108
109 x[0] = 0;
110 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
111 if (c == '.') {
112 if (gotrad) break;
113 gotrad = 1;
114 lrp = dc;
115 } else if (k < KMAX-3) {
116 dc++;
117 if (c!='0') lnz = dc;
118 if (j) x[k] = x[k]*10 + c-'0';
119 else x[k] = c-'0';
120 if (++j==9) {
121 k++;
122 j=0;
123 }
124 gotdig=1;
125 } else {
126 dc++;
127 if (c!='0') {
128 lnz = (KMAX-4)*9;
129 x[KMAX-4] |= 1;
130 }
131 }
132 }
133 if (!gotrad) lrp=dc;
134
135 if (gotdig && (c|32)=='e') {
136 e10 = scanexp(f, pok);
137 if (e10 == LLONG_MIN) {
138 if (pok) {
139 shunget(f);
140 } else {
141 shlim(f, 0);
142 return 0;
143 }
144 e10 = 0;
145 }
146 lrp += e10;
147 } else if (c>=0) {
148 shunget(f);
149 }
150 if (!gotdig) {
151 errno = EINVAL;
152 shlim(f, 0);
153 return 0;
154 }
155
156 /* Handle zero specially to avoid nasty special cases later */
157 if (!x[0]) return sign * 0.0;
158
159 /* Optimize small integers (w/no exponent) and over/under-flow */
160 if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
161 #if defined(__wasilibc_printscan_no_long_double)
162 return sign * (long_double)x[0];
163 #else
164 return sign * (long double)x[0];
165 #endif
166 if (lrp > -emin/2) {
167 errno = ERANGE;
168 return sign * LDBL_MAX * LDBL_MAX;
169 }
170 if (lrp < emin-2*LDBL_MANT_DIG) {
171 errno = ERANGE;
172 return sign * LDBL_MIN * LDBL_MIN;
173 }
174
175 /* Align incomplete final B1B digit */
176 if (j) {
177 for (; j<9; j++) x[k]*=10;
178 k++;
179 j=0;
180 }
181
182 a = 0;
183 z = k;
184 e2 = 0;
185 rp = lrp;
186
187 /* Optimize small to mid-size integers (even in exp. notation) */
188 if (lnz<9 && lnz<=rp && rp < 18) {
189 #if defined(__wasilibc_printscan_no_long_double)
190 if (rp == 9) return sign * (long_double)x[0];
191 if (rp < 9) return sign * (long_double)x[0] / p10s[8-rp];
192 #else
193 if (rp == 9) return sign * (long double)x[0];
194 if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
195 #endif
196 int bitlim = bits-3*(int)(rp-9);
197 if (bitlim>30 || x[0]>>bitlim==0)
198 #if defined(__wasilibc_printscan_no_long_double)
199 return sign * (long_double)x[0] * p10s[rp-10];
200 #else
201 return sign * (long double)x[0] * p10s[rp-10];
202 #endif
203 }
204
205 /* Drop trailing zeros */
206 for (; !x[z-1]; z--);
207
208 /* Align radix point to B1B digit boundary */
209 if (rp % 9) {
210 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
211 int p10 = p10s[8-rpm9];
212 uint32_t carry = 0;
213 for (k=a; k!=z; k++) {
214 uint32_t tmp = x[k] % p10;
215 x[k] = x[k]/p10 + carry;
216 carry = 1000000000/p10 * tmp;
217 if (k==a && !x[k]) {
218 a = (a+1 & MASK);
219 rp -= 9;
220 }
221 }
222 if (carry) x[z++] = carry;
223 rp += 9-rpm9;
224 }
225
226 /* Upscale until desired number of bits are left of radix point */
227 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
228 uint32_t carry = 0;
229 e2 -= 29;
230 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
231 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
232 if (tmp > 1000000000) {
233 carry = tmp / 1000000000;
234 x[k] = tmp % 1000000000;
235 } else {
236 carry = 0;
237 x[k] = tmp;
238 }
239 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
240 if (k==a) break;
241 }
242 if (carry) {
243 rp += 9;
244 a = (a-1 & MASK);
245 if (a == z) {
246 z = (z-1 & MASK);
247 x[z-1 & MASK] |= x[z];
248 }
249 x[a] = carry;
250 }
251 }
252
253 /* Downscale until exactly number of bits are left of radix point */
254 for (;;) {
255 uint32_t carry = 0;
256 int sh = 1;
257 for (i=0; i<LD_B1B_DIG; i++) {
258 k = (a+i & MASK);
259 if (k == z || x[k] < th[i]) {
260 i=LD_B1B_DIG;
261 break;
262 }
263 if (x[a+i & MASK] > th[i]) break;
264 }
265 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
266 /* FIXME: find a way to compute optimal sh */
267 if (rp > 9+9*LD_B1B_DIG) sh = 9;
268 e2 += sh;
269 for (k=a; k!=z; k=(k+1 & MASK)) {
270 uint32_t tmp = x[k] & (1<<sh)-1;
271 x[k] = (x[k]>>sh) + carry;
272 carry = (1000000000>>sh) * tmp;
273 if (k==a && !x[k]) {
274 a = (a+1 & MASK);
275 i--;
276 rp -= 9;
277 }
278 }
279 if (carry) {
280 if ((z+1 & MASK) != a) {
281 x[z] = carry;
282 z = (z+1 & MASK);
283 } else x[z-1 & MASK] |= 1;
284 }
285 }
286
287 /* Assemble desired bits into floating point variable */
288 for (y=i=0; i<LD_B1B_DIG; i++) {
289 if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
290 #if defined(__wasilibc_printscan_no_long_double)
291 y = 1000000000.0 * y + x[a+i & MASK];
292 #else
293 y = 1000000000.0L * y + x[a+i & MASK];
294 #endif
295 }
296
297 y *= sign;
298
299 /* Limit precision for denormal results */
300 if (bits > LDBL_MANT_DIG+e2-emin) {
301 bits = LDBL_MANT_DIG+e2-emin;
302 if (bits<0) bits=0;
303 denormal = 1;
304 }
305
306 /* Calculate bias term to force rounding, move out lower bits */
307 if (bits < LDBL_MANT_DIG) {
308 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
309 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
310 y -= frac;
311 y += bias;
312 }
313
314 /* Process tail of decimal input so it can affect rounding */
315 if ((a+i & MASK) != z) {
316 uint32_t t = x[a+i & MASK];
317 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
318 frac += 0.25*sign;
319 else if (t > 500000000)
320 frac += 0.75*sign;
321 else if (t == 500000000) {
322 if ((a+i+1 & MASK) == z)
323 frac += 0.5*sign;
324 else
325 frac += 0.75*sign;
326 }
327 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
328 frac++;
329 }
330
331 y += frac;
332 y -= bias;
333
334 if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) {
335 if (fabs(y) >= CONCAT(0x1p, LDBL_MANT_DIG)) {
336 if (denormal && bits==LDBL_MANT_DIG+e2-emin)
337 denormal = 0;
338 y *= 0.5;
339 e2++;
340 }
341 if (e2+LDBL_MANT_DIG>emax || (denormal && frac))
342 errno = ERANGE;
343 }
344
345 return scalbnl(y, e2);
346 }
347
348 #if defined(__wasilibc_printscan_no_long_double)
349 static long_double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
350 #else
351 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
352 #endif
353 {
354 uint32_t x = 0;
355 #if defined(__wasilibc_printscan_no_long_double)
356 long_double y = 0;
357 long_double scale = 1;
358 long_double bias = 0;
359 #else
360 long double y = 0;
361 long double scale = 1;
362 long double bias = 0;
363 #endif
364 int gottail = 0, gotrad = 0, gotdig = 0;
365 long long rp = 0;
366 long long dc = 0;
367 long long e2 = 0;
368 int d;
369 int c;
370
371 c = shgetc(f);
372
373 /* Skip leading zeros */
374 for (; c=='0'; c = shgetc(f)) gotdig = 1;
375
376 if (c=='.') {
377 gotrad = 1;
378 c = shgetc(f);
379 /* Count zeros after the radix point before significand */
380 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
381 }
382
383 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
384 if (c=='.') {
385 if (gotrad) break;
386 rp = dc;
387 gotrad = 1;
388 } else {
389 gotdig = 1;
390 if (c > '9') d = (c|32)+10-'a';
391 else d = c-'0';
392 if (dc<8) {
393 x = x*16 + d;
394 } else if (dc < LDBL_MANT_DIG/4+1) {
395 y += d*(scale/=16);
396 } else if (d && !gottail) {
397 y += 0.5*scale;
398 gottail = 1;
399 }
400 dc++;
401 }
402 }
403 if (!gotdig) {
404 shunget(f);
405 if (pok) {
406 shunget(f);
407 if (gotrad) shunget(f);
408 } else {
409 shlim(f, 0);
410 }
411 return sign * 0.0;
412 }
413 if (!gotrad) rp = dc;
414 while (dc<8) x *= 16, dc++;
415 if ((c|32)=='p') {
416 e2 = scanexp(f, pok);
417 if (e2 == LLONG_MIN) {
418 if (pok) {
419 shunget(f);
420 } else {
421 shlim(f, 0);
422 return 0;
423 }
424 e2 = 0;
425 }
426 } else {
427 shunget(f);
428 }
429 e2 += 4*rp - 32;
430
431 if (!x) return sign * 0.0;
432 if (e2 > -emin) {
433 errno = ERANGE;
434 return sign * LDBL_MAX * LDBL_MAX;
435 }
436 if (e2 < emin-2*LDBL_MANT_DIG) {
437 errno = ERANGE;
438 return sign * LDBL_MIN * LDBL_MIN;
439 }
440
441 while (x < 0x80000000) {
442 if (y>=0.5) {
443 x += x + 1;
444 y += y - 1;
445 } else {
446 x += x;
447 y += y;
448 }
449 e2--;
450 }
451
452 if (bits > 32+e2-emin) {
453 bits = 32+e2-emin;
454 if (bits<0) bits=0;
455 }
456
457 if (bits < LDBL_MANT_DIG)
458 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
459
460 if (bits<32 && y && !(x&1)) x++, y=0;
461
462 #if defined(__wasilibc_printscan_no_long_double)
463 y = bias + sign*(long_double)x + sign*y;
464 #else
465 y = bias + sign*(long double)x + sign*y;
466 #endif
467 y -= bias;
468
469 if (!y) errno = ERANGE;
470
471 return scalbnl(y, e2);
472 }
473
474 #if defined(__wasilibc_printscan_no_long_double)
475 long_double __floatscan(FILE *f, int prec, int pok)
476 #else
477 long double __floatscan(FILE *f, int prec, int pok)
478 #endif
479 {
480 int sign = 1;
481 size_t i;
482 int bits;
483 int emin;
484 int c;
485
486 switch (prec) {
487 case 0:
488 bits = FLT_MANT_DIG;
489 emin = FLT_MIN_EXP-bits;
490 break;
491 case 1:
492 bits = DBL_MANT_DIG;
493 emin = DBL_MIN_EXP-bits;
494 break;
495 case 2:
496 bits = LDBL_MANT_DIG;
497 emin = LDBL_MIN_EXP-bits;
498 break;
499 default:
500 return 0;
501 }
502
503 while (isspace((c=shgetc(f))));
504
505 if (c=='+' || c=='-') {
506 sign -= 2*(c=='-');
507 c = shgetc(f);
508 }
509
510 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
511 if (i<7) c = shgetc(f);
512 if (i==3 || i==8 || (i>3 && pok)) {
513 if (i!=8) {
514 shunget(f);
515 if (pok) for (; i>3; i--) shunget(f);
516 }
517 return sign * INFINITY;
518 }
519 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
520 if (i<2) c = shgetc(f);
521 if (i==3) {
522 if (shgetc(f) != '(') {
523 shunget(f);
524 return NAN;
525 }
526 for (i=1; ; i++) {
527 c = shgetc(f);
528 if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
529 continue;
530 if (c==')') return NAN;
531 shunget(f);
532 if (!pok) {
533 errno = EINVAL;
534 shlim(f, 0);
535 return 0;
536 }
537 while (i--) shunget(f);
538 return NAN;
539 }
540 return NAN;
541 }
542
543 if (i) {
544 shunget(f);
545 errno = EINVAL;
546 shlim(f, 0);
547 return 0;
548 }
549
550 if (c=='0') {
551 c = shgetc(f);
552 if ((c|32) == 'x')
553 return hexfloat(f, bits, emin, sign, pok);
554 shunget(f);
555 c = '0';
556 }
557
558 return decfloat(f, c, bits, emin, sign, pok);
559 }