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