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