]>
Commit | Line | Data |
---|---|---|
320054e8 DG |
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> | |
e5f14be3 | 8 | #ifdef __wasilibc_unmodified_upstream // Changes to optimize printf/scanf when long double isn't needed |
320054e8 DG |
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 | y = 1000000000.0L * y + x[a+i & MASK]; | |
291 | } | |
292 | ||
293 | y *= sign; | |
294 | ||
295 | /* Limit precision for denormal results */ | |
296 | if (bits > LDBL_MANT_DIG+e2-emin) { | |
297 | bits = LDBL_MANT_DIG+e2-emin; | |
298 | if (bits<0) bits=0; | |
299 | denormal = 1; | |
300 | } | |
301 | ||
302 | /* Calculate bias term to force rounding, move out lower bits */ | |
303 | if (bits < LDBL_MANT_DIG) { | |
304 | bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y); | |
305 | frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits)); | |
306 | y -= frac; | |
307 | y += bias; | |
308 | } | |
309 | ||
310 | /* Process tail of decimal input so it can affect rounding */ | |
311 | if ((a+i & MASK) != z) { | |
312 | uint32_t t = x[a+i & MASK]; | |
313 | if (t < 500000000 && (t || (a+i+1 & MASK) != z)) | |
314 | frac += 0.25*sign; | |
315 | else if (t > 500000000) | |
316 | frac += 0.75*sign; | |
317 | else if (t == 500000000) { | |
318 | if ((a+i+1 & MASK) == z) | |
319 | frac += 0.5*sign; | |
320 | else | |
321 | frac += 0.75*sign; | |
322 | } | |
323 | if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1)) | |
324 | frac++; | |
325 | } | |
326 | ||
327 | y += frac; | |
328 | y -= bias; | |
329 | ||
330 | if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) { | |
331 | if (fabs(y) >= CONCAT(0x1p, LDBL_MANT_DIG)) { | |
332 | if (denormal && bits==LDBL_MANT_DIG+e2-emin) | |
333 | denormal = 0; | |
334 | y *= 0.5; | |
335 | e2++; | |
336 | } | |
337 | if (e2+LDBL_MANT_DIG>emax || (denormal && frac)) | |
338 | errno = ERANGE; | |
339 | } | |
340 | ||
341 | return scalbnl(y, e2); | |
342 | } | |
343 | ||
344 | #if defined(__wasilibc_printscan_no_long_double) | |
345 | static long_double hexfloat(FILE *f, int bits, int emin, int sign, int pok) | |
346 | #else | |
347 | static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok) | |
348 | #endif | |
349 | { | |
350 | uint32_t x = 0; | |
351 | #if defined(__wasilibc_printscan_no_long_double) | |
352 | long_double y = 0; | |
353 | long_double scale = 1; | |
354 | long_double bias = 0; | |
355 | #else | |
356 | long double y = 0; | |
357 | long double scale = 1; | |
358 | long double bias = 0; | |
359 | #endif | |
360 | int gottail = 0, gotrad = 0, gotdig = 0; | |
361 | long long rp = 0; | |
362 | long long dc = 0; | |
363 | long long e2 = 0; | |
364 | int d; | |
365 | int c; | |
366 | ||
367 | c = shgetc(f); | |
368 | ||
369 | /* Skip leading zeros */ | |
370 | for (; c=='0'; c = shgetc(f)) gotdig = 1; | |
371 | ||
372 | if (c=='.') { | |
373 | gotrad = 1; | |
374 | c = shgetc(f); | |
375 | /* Count zeros after the radix point before significand */ | |
376 | for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1; | |
377 | } | |
378 | ||
379 | for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) { | |
380 | if (c=='.') { | |
381 | if (gotrad) break; | |
382 | rp = dc; | |
383 | gotrad = 1; | |
384 | } else { | |
385 | gotdig = 1; | |
386 | if (c > '9') d = (c|32)+10-'a'; | |
387 | else d = c-'0'; | |
388 | if (dc<8) { | |
389 | x = x*16 + d; | |
390 | } else if (dc < LDBL_MANT_DIG/4+1) { | |
391 | y += d*(scale/=16); | |
392 | } else if (d && !gottail) { | |
393 | y += 0.5*scale; | |
394 | gottail = 1; | |
395 | } | |
396 | dc++; | |
397 | } | |
398 | } | |
399 | if (!gotdig) { | |
400 | shunget(f); | |
401 | if (pok) { | |
402 | shunget(f); | |
403 | if (gotrad) shunget(f); | |
404 | } else { | |
405 | shlim(f, 0); | |
406 | } | |
407 | return sign * 0.0; | |
408 | } | |
409 | if (!gotrad) rp = dc; | |
410 | while (dc<8) x *= 16, dc++; | |
411 | if ((c|32)=='p') { | |
412 | e2 = scanexp(f, pok); | |
413 | if (e2 == LLONG_MIN) { | |
414 | if (pok) { | |
415 | shunget(f); | |
416 | } else { | |
417 | shlim(f, 0); | |
418 | return 0; | |
419 | } | |
420 | e2 = 0; | |
421 | } | |
422 | } else { | |
423 | shunget(f); | |
424 | } | |
425 | e2 += 4*rp - 32; | |
426 | ||
427 | if (!x) return sign * 0.0; | |
428 | if (e2 > -emin) { | |
429 | errno = ERANGE; | |
430 | return sign * LDBL_MAX * LDBL_MAX; | |
431 | } | |
432 | if (e2 < emin-2*LDBL_MANT_DIG) { | |
433 | errno = ERANGE; | |
434 | return sign * LDBL_MIN * LDBL_MIN; | |
435 | } | |
436 | ||
437 | while (x < 0x80000000) { | |
438 | if (y>=0.5) { | |
439 | x += x + 1; | |
440 | y += y - 1; | |
441 | } else { | |
442 | x += x; | |
443 | y += y; | |
444 | } | |
445 | e2--; | |
446 | } | |
447 | ||
448 | if (bits > 32+e2-emin) { | |
449 | bits = 32+e2-emin; | |
450 | if (bits<0) bits=0; | |
451 | } | |
452 | ||
453 | if (bits < LDBL_MANT_DIG) | |
454 | bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign); | |
455 | ||
456 | if (bits<32 && y && !(x&1)) x++, y=0; | |
457 | ||
458 | #if defined(__wasilibc_printscan_no_long_double) | |
459 | y = bias + sign*(long_double)x + sign*y; | |
460 | #else | |
461 | y = bias + sign*(long double)x + sign*y; | |
462 | #endif | |
463 | y -= bias; | |
464 | ||
465 | if (!y) errno = ERANGE; | |
466 | ||
467 | return scalbnl(y, e2); | |
468 | } | |
469 | ||
470 | #if defined(__wasilibc_printscan_no_long_double) | |
471 | long_double __floatscan(FILE *f, int prec, int pok) | |
472 | #else | |
473 | long double __floatscan(FILE *f, int prec, int pok) | |
474 | #endif | |
475 | { | |
476 | int sign = 1; | |
477 | size_t i; | |
478 | int bits; | |
479 | int emin; | |
480 | int c; | |
481 | ||
482 | switch (prec) { | |
483 | case 0: | |
484 | bits = FLT_MANT_DIG; | |
485 | emin = FLT_MIN_EXP-bits; | |
486 | break; | |
487 | case 1: | |
488 | bits = DBL_MANT_DIG; | |
489 | emin = DBL_MIN_EXP-bits; | |
490 | break; | |
491 | case 2: | |
492 | bits = LDBL_MANT_DIG; | |
493 | emin = LDBL_MIN_EXP-bits; | |
494 | break; | |
495 | default: | |
496 | return 0; | |
497 | } | |
498 | ||
499 | while (isspace((c=shgetc(f)))); | |
500 | ||
501 | if (c=='+' || c=='-') { | |
502 | sign -= 2*(c=='-'); | |
503 | c = shgetc(f); | |
504 | } | |
505 | ||
506 | for (i=0; i<8 && (c|32)=="infinity"[i]; i++) | |
507 | if (i<7) c = shgetc(f); | |
508 | if (i==3 || i==8 || (i>3 && pok)) { | |
509 | if (i!=8) { | |
510 | shunget(f); | |
511 | if (pok) for (; i>3; i--) shunget(f); | |
512 | } | |
513 | return sign * INFINITY; | |
514 | } | |
515 | if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++) | |
516 | if (i<2) c = shgetc(f); | |
517 | if (i==3) { | |
518 | if (shgetc(f) != '(') { | |
519 | shunget(f); | |
520 | return NAN; | |
521 | } | |
522 | for (i=1; ; i++) { | |
523 | c = shgetc(f); | |
524 | if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_') | |
525 | continue; | |
526 | if (c==')') return NAN; | |
527 | shunget(f); | |
528 | if (!pok) { | |
529 | errno = EINVAL; | |
530 | shlim(f, 0); | |
531 | return 0; | |
532 | } | |
533 | while (i--) shunget(f); | |
534 | return NAN; | |
535 | } | |
536 | return NAN; | |
537 | } | |
538 | ||
539 | if (i) { | |
540 | shunget(f); | |
541 | errno = EINVAL; | |
542 | shlim(f, 0); | |
543 | return 0; | |
544 | } | |
545 | ||
546 | if (c=='0') { | |
547 | c = shgetc(f); | |
548 | if ((c|32) == 'x') | |
549 | return hexfloat(f, bits, emin, sign, pok); | |
550 | shunget(f); | |
551 | c = '0'; | |
552 | } | |
553 | ||
554 | return decfloat(f, c, bits, emin, sign, pok); | |
555 | } |