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