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