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