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