optimize floatscan: avoid excessive upscaling
[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) {
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                         x[KMAX-1] |= c-'0';
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=(k+1 & MASK)) {
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) {
159                         if ((z+1 & MASK) != a) {
160                                 x[z] = carry;
161                                 z = (z+1 & MASK);
162                         } else x[z-1 & MASK] |= 1;
163                 }
164                 rp += 9-rpm9;
165         }
166
167         while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[0]<th[0])) {
168                 uint32_t carry = 0;
169                 e2 -= 29;
170                 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
171                         uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
172                         if (tmp > 1000000000) {
173                                 carry = tmp / 1000000000;
174                                 x[k] = tmp % 1000000000;
175                         } else {
176                                 carry = 0;
177                                 x[k] = tmp;
178                         }
179                         if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
180                         if (k==a) break;
181                 }
182                 if (carry) {
183                         rp += 9;
184                         if (a == z) {
185                                 z = (z-1 & MASK);
186                                 x[z-1 & MASK] |= x[z];
187                         }
188                         a = (a-1 & MASK);
189                         x[a] = carry;
190                 }
191         }
192
193         for (;;) {
194                 uint32_t carry = 0;
195                 int sh = 1;
196                 for (i=0; i<LD_B1B_DIG; i++) {
197                         k = (a+i & MASK);
198                         if (k == z || x[k] < th[i]) {
199                                 i=LD_B1B_DIG;
200                                 break;
201                         }
202                         if (x[a+i & MASK] > th[i]) break;
203                 }
204                 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
205                 /* FIXME: find a way to compute optimal sh */
206                 if (rp > 9+9*LD_B1B_DIG) sh = 9;
207                 e2 += sh;
208                 for (k=a; k!=z; k=(k+1 & MASK)) {
209                         uint32_t tmp = x[k] & (1<<sh)-1;
210                         x[k] = (x[k]>>sh) + carry;
211                         carry = (1000000000>>sh) * tmp;
212                         if (k==a && !x[k]) {
213                                 a = (a+1 & MASK);
214                                 rp -= 9;
215                         }
216                 }
217                 if (carry) {
218                         if ((z+1 & MASK) != a) {
219                                 x[z] = carry;
220                                 z = (z+1 & MASK);
221                         } else x[z-1 & MASK] |= 1;
222                 }
223         }
224
225         for (y=i=0; i<LD_B1B_DIG; i++) {
226                 if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
227                 y = 1000000000.0L * y + x[a+i & MASK];
228         }
229
230         y *= sign;
231
232         if (bits > LDBL_MANT_DIG+e2-emin) {
233                 bits = LDBL_MANT_DIG+e2-emin;
234                 if (bits<0) bits=0;
235         }
236
237         if (bits < LDBL_MANT_DIG) {
238                 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
239                 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
240                 y -= frac;
241                 y += bias;
242         }
243
244         if ((a+i & MASK) != z) {
245                 uint32_t t = x[a+i & MASK];
246                 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
247                         frac += 0.25*sign;
248                 else if (t > 500000000)
249                         frac += 0.75*sign;
250                 else if (t == 500000000) {
251                         if ((a+i+1 & MASK) == z)
252                                 frac += 0.5*sign;
253                         else
254                                 frac += 0.75*sign;
255                 }
256                 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
257                         frac++;
258         }
259
260         y += frac;
261         y -= bias;
262
263         y = scalbnl(y, e2);
264
265         if (!y) errno = ERANGE;
266
267         return y;
268 }
269
270 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
271 {
272         uint32_t x = 0;
273         long double y = 0;
274         long double scale = 1;
275         long double bias = 0;
276         int gottail = 0, gotrad = 0, gotdig = 0;
277         long long rp = 0;
278         long long dc = 0;
279         long long e2 = 0;
280         int d;
281         int c;
282
283         c = shgetc(f);
284
285         /* Skip leading zeros */
286         for (; c=='0'; c = shgetc(f)) gotdig = 1;
287
288         if (c=='.') {
289                 gotrad = 1;
290                 c = shgetc(f);
291                 /* Count zeros after the radix point before significand */
292                 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
293         }
294
295         for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
296                 if (c=='.') {
297                         if (gotrad) break;
298                         rp = dc;
299                         gotrad = 1;
300                 } else {
301                         gotdig = 1;
302                         if (c > '9') d = (c|32)+10-'a';
303                         else d = c-'0';
304                         if (dc<8) {
305                                 x = x*16 + d;
306                         } else if (dc < LDBL_MANT_DIG/4+1) {
307                                 y += d*(scale/=16);
308                         } else if (d && !gottail) {
309                                 y += 0.5*scale;
310                                 gottail = 1;
311                         }
312                         dc++;
313                 }
314         }
315         if (!gotdig) {
316                 shunget(f);
317                 if (pok) {
318                         shunget(f);
319                         if (gotrad) shunget(f);
320                 } else {
321                         shlim(f, 0);
322                 }
323                 return 0;
324         }
325         if (!gotrad) rp = dc;
326         while (dc<8) x *= 16, dc++;
327         if ((c|32)=='p') {
328                 e2 = scanexp(f, pok);
329                 if (e2 == LLONG_MIN) {
330                         if (pok) {
331                                 shunget(f);
332                         } else {
333                                 shlim(f, 0);
334                                 return 0;
335                         }
336                         e2 = 0;
337                 }
338         } else {
339                 shunget(f);
340         }
341         e2 += 4*rp - 32;
342
343         if (!x) return sign * 0.0;
344         if (e2 > -emin) {
345                 errno = ERANGE;
346                 return sign * LDBL_MAX * LDBL_MAX;
347         }
348         if (e2 < emin-2*LDBL_MANT_DIG) {
349                 errno = ERANGE;
350                 return sign * LDBL_MIN * LDBL_MIN;
351         }
352
353         while (x < 0x80000000) {
354                 if (y>=0.5) {
355                         x += x + 1;
356                         y += y - 1;
357                 } else {
358                         x += x;
359                         y += y;
360                 }
361                 e2--;
362         }
363
364         if (bits > 32+e2-emin) {
365                 bits = 32+e2-emin;
366                 if (bits<0) bits=0;
367         }
368
369         if (bits < LDBL_MANT_DIG)
370                 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
371
372         if (bits<32 && y && !(x&1)) x++, y=0;
373
374         y = bias + sign*(long double)x + sign*y;
375         y -= bias;
376
377         if (!y) errno = ERANGE;
378
379         return scalbnl(y, e2);
380 }
381
382 long double __floatscan(FILE *f, int c, int prec, int pok)
383 {
384         int sign = 1;
385         int i;
386         int bits;
387         int emin;
388
389         switch (prec) {
390         case 0:
391                 bits = 24;
392                 emin = -149;
393                 break;
394         case 1:
395                 bits = 53;
396                 emin = -1074;
397                 break;
398         case 2:
399                 bits = LDBL_MANT_DIG;
400                 emin = -16445;
401                 break;
402         default:
403                 return 0;
404         }
405
406         if (c<0) c = shgetc(f);
407
408         if (c=='+' || c=='-') {
409                 sign -= 2*(c=='-');
410                 c = shgetc(f);
411         }
412
413         for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
414                 if (i<7) c = shgetc(f);
415         if (i==3 || i==8 || (i>3 && pok)) {
416                 if (i==3) shunget(f);
417                 if (pok) for (; i>3; i--) shunget(f);
418                 else shlim(f, 0);
419                 return sign * INFINITY;
420         }
421         if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
422                 if (i<3) c = shgetc(f);
423         if (i==3) {
424                 return NAN;
425         }
426
427         if (i) {
428                 shunget(f);
429                 errno = EINVAL;
430                 shlim(f, 0);
431                 return 0;
432         }
433
434         if (c=='0') {
435                 c = shgetc(f);
436                 if ((c|32) == 'x')
437                         return hexfloat(f, bits, emin, sign, pok);
438                 shunget(f);
439                 c = '0';
440         }
441
442         return decfloat(f, c, bits, emin, sign, pok);
443 }