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