7d9a452412592a06955e2f4cf7aeeb1cd004a4b6
[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
7 #include "shgetc.h"
8 #include "floatscan.h"
9
10 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
11
12 #define LD_B1B_DIG 2
13 #define LD_B1B_MAX 9007199, 254740991
14 #define KMAX 128
15
16 #else /* LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 */
17
18 #define LD_B1B_DIG 3
19 #define LD_B1B_MAX 18, 446744073, 709551615
20 #define KMAX 2048
21
22 #endif
23
24 #define MASK (KMAX-1)
25
26
27 static long long scanexp(FILE *f, int pok)
28 {
29         int c;
30         int x;
31         long long y;
32         int neg = 0;
33         
34         c = shgetc(f);
35         if (c=='+' || c=='-') {
36                 neg = (c=='-');
37                 c = shgetc(f);
38                 if (c-'0'>=10U && pok) shunget(f);
39         }
40         if (c-'0'>=10U) {
41                 shunget(f);
42                 return LLONG_MIN;
43         }
44         for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
45                 x = 10*x + c-'0';
46         for (y=x; c-'0'<10U && x<LLONG_MAX/10; c = shgetc(f))
47                 y = 10*y + c-'0';
48         for (; c-'0'<10U; c = shgetc(f));
49         shunget(f);
50         return neg ? -y : y;
51 }
52
53
54 static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
55 {
56         uint32_t x[KMAX];
57         static const uint32_t th[] = { LD_B1B_MAX };
58         int i, j, k, a, z;
59         long long lrp=-1, dc=0;
60         long long e10=0;
61         int gotdig = 0;
62         int rp;
63         int e2;
64         long double y;
65         long double frac=0;
66         long double bias=0;
67         int c;
68
69         j=0;
70         k=0;
71
72         c = shgetc(f);
73
74         /* Don't let leading zeros consume buffer space */
75         for (; c=='0'; c = shgetc(f)) gotdig=1;
76
77         x[0] = 0;
78         for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
79                 if (c == '.') {
80                         if (lrp!=-1) break;
81                         lrp = dc;
82                 } else if (k < KMAX) {
83                         dc++;
84                         if (j) x[k] = x[k]*10 + c-'0';
85                         else x[k] = c-'0';
86                         if (++j==9) {
87                                 k++;
88                                 j=0;
89                         }
90                         gotdig=1;
91                 } else {
92                         dc++;
93                         x[KMAX-1] |= c-'0';
94                 }
95         }
96         if (lrp==-1) lrp=dc;
97
98         if (gotdig && (c|32)=='e') {
99                 e10 = scanexp(f, pok);
100                 if (e10 == LLONG_MIN) {
101                         if (pok) {
102                                 shunget(f);
103                         } else {
104                                 shlim(f, 0);
105                                 return 0;
106                         }
107                         e10 = 0;
108                 }
109                 lrp += e10;
110         } else if (c>=0) {
111                 shunget(f);
112         }
113         if (!gotdig) {
114                 shlim(f, 0);
115                 return 0;
116         }
117
118         if (!x[0])
119                 return sign * 0.0;
120         if (lrp==dc && (!k || (k==1 && !j)) && (bits>30 || x[0]>>bits==0))
121                 return sign * (long double)x[0];
122         if (lrp > -emin/2)
123                 return sign * LDBL_MAX * LDBL_MAX;
124         if (lrp < emin-2*LDBL_MANT_DIG)
125                 return sign * LDBL_MIN * LDBL_MIN;
126
127         if (k<KMAX && j) {
128                 for (; j<9; j++) x[k]*=10;
129                 k++;
130                 j=0;
131         }
132
133         a = 0;
134         z = k;
135         e2 = 0;
136         rp = lrp;
137
138         while (rp < 18+9*LD_B1B_DIG) {
139                 uint32_t carry = 0;
140                 e2 -= 29;
141                 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
142                         uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
143                         if (tmp > 1000000000) {
144                                 carry = tmp / 1000000000;
145                                 x[k] = tmp % 1000000000;
146                         } else {
147                                 carry = 0;
148                                 x[k] = tmp;
149                         }
150                         if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
151                         if (k==a) break;
152                 }
153                 if (carry) {
154                         rp += 9;
155                         if (a == z) {
156                                 z = (z-1 & MASK);
157                                 x[z-1 & MASK] |= x[z];
158                         }
159                         a = (a-1 & MASK);
160                         x[a] = carry;
161                 }
162         }
163
164         if (rp % 9) {
165                 static const int p10s[] = {
166                         100000000, 10000000, 1000000, 100000,
167                         10000, 1000, 100, 10
168                 };
169                 int rpm9 = rp % 9;
170                 int p10 = p10s[rpm9-1];
171                 uint32_t carry = 0;
172                 for (k=a; k!=z; k=(k+1 & MASK)) {
173                         uint32_t tmp = x[k] % p10;
174                         x[k] = x[k]/p10 + carry;
175                         carry = 1000000000/p10 * tmp;
176                         if (k==a && !x[k]) {
177                                 a = (a+1 & MASK);
178                                 rp -= 9;
179                         }
180                 }
181                 if (carry) {
182                         if ((z+1 & MASK) != a) {
183                                 x[z] = carry;
184                                 z = (z+1 & MASK);
185                         } else x[z-1 & MASK] |= 1;
186                 }
187                 rp += 9-rpm9;
188         }
189
190         for (;;) {
191                 uint32_t carry = 0;
192                 int sh = 1;
193                 for (i=0; i<LD_B1B_DIG; i++) {
194                         k = (a+i & MASK);
195                         if (k == z || x[k] < th[i]) {
196                                 i=LD_B1B_DIG;
197                                 break;
198                         }
199                         if (x[a+i & MASK] > th[i]) break;
200                 }
201                 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
202                 /* FIXME: find a way to compute optimal sh */
203                 if (rp > 9+9*LD_B1B_DIG) sh = 9;
204                 e2 += sh;
205                 for (k=a; k!=z; k=(k+1 & MASK)) {
206                         uint32_t tmp = x[k] & (1<<sh)-1;
207                         x[k] = (x[k]>>sh) + carry;
208                         carry = (1000000000>>sh) * tmp;
209                         if (k==a && !x[k]) {
210                                 a = (a+1 & MASK);
211                                 rp -= 9;
212                         }
213                 }
214                 if (carry) {
215                         if ((z+1 & MASK) != a) {
216                                 x[z] = carry;
217                                 z = (z+1 & MASK);
218                         } else x[z-1 & MASK] |= 1;
219                 }
220         }
221
222         for (y=i=0; i<LD_B1B_DIG && (a+i & MASK)!=z; i++)
223                 y = 1000000000.0L * y + x[a+i & MASK];
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         return y;
261 }
262
263 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
264 {
265         uint32_t x = 0;
266         long double y = 0;
267         long double scale = 1;
268         long double bias = 0;
269         int gottail = 0, gotrad = 0, gotdig = 0;
270         long long rp = 0;
271         long long dc = 0;
272         long long e2 = 0;
273         int d;
274         int c;
275
276         c = shgetc(f);
277
278         /* Skip leading zeros */
279         for (; c=='0'; c = shgetc(f)) gotdig = 1;
280
281         if (c=='.') {
282                 gotrad = 1;
283                 c = shgetc(f);
284                 /* Count zeros after the radix point before significand */
285                 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
286         }
287
288         for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
289                 if (c=='.') {
290                         if (gotrad) break;
291                         rp = dc;
292                         gotrad = 1;
293                 } else {
294                         gotdig = 1;
295                         if (c > '9') d = (c|32)+10-'a';
296                         else d = c-'0';
297                         if (dc<8) {
298                                 x = x*16 + d;
299                         } else if (dc < LDBL_MANT_DIG/4+1) {
300                                 y += d*(scale/=16);
301                         } else if (d && !gottail) {
302                                 y += 0.5*scale;
303                                 gottail = 1;
304                         }
305                         dc++;
306                 }
307         }
308         if (!gotdig) {
309                 shunget(f);
310                 if (pok) {
311                         shunget(f);
312                         if (gotrad) shunget(f);
313                 } else {
314                         shlim(f, 0);
315                 }
316                 return 0;
317         }
318         if (!gotrad) rp = dc;
319         while (dc<8) x *= 16, dc++;
320         if ((c|32)=='p') {
321                 e2 = scanexp(f, pok);
322                 if (e2 == LLONG_MIN) {
323                         if (pok) {
324                                 shunget(f);
325                         } else {
326                                 shlim(f, 0);
327                                 return 0;
328                         }
329                         e2 = 0;
330                 }
331         }
332         e2 += 4*rp - 32;
333
334         if (!x) return sign * 0.0;
335         if (e2 > -emin) return sign * LDBL_MAX * LDBL_MAX;
336         if (e2 < emin-2*LDBL_MANT_DIG) return sign * LDBL_MIN * LDBL_MIN;
337
338         while (x < 0x80000000) {
339                 if (y>=0.5) {
340                         x += x + 1;
341                         y += y - 1;
342                 } else {
343                         x += x;
344                         y += y;
345                 }
346                 e2--;
347         }
348
349         if (bits > 32+e2-emin) {
350                 bits = 32+e2-emin;
351                 if (bits<0) bits=0;
352         }
353
354         if (bits < LDBL_MANT_DIG)
355                 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
356
357         if (bits<32 && y && !(x&1)) x++, y=0;
358
359         y = bias + sign*(long double)x + sign*y;
360         y -= bias;
361
362         return scalbnl(y, e2);
363 }
364
365 long double __floatscan(FILE *f, int c, int prec, int pok)
366 {
367         int sign = 1;
368         int i;
369         int bits;
370         int emin;
371
372         switch (prec) {
373         case 0:
374                 bits = 24;
375                 emin = -149;
376                 break;
377         case 1:
378                 bits = 53;
379                 emin = -1074;
380                 break;
381         case 2:
382                 bits = LDBL_MANT_DIG;
383                 emin = -16445;
384                 break;
385         default:
386                 return 0;
387         }
388
389         if (c<0) c = shgetc(f);
390
391         if (c=='+' || c=='-') {
392                 sign -= 2*(c=='-');
393                 c = shgetc(f);
394         }
395
396         for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
397                 if (i<7) c = shgetc(f);
398         if (i==3 || i==8 || (i>3 && pok)) {
399                 if (i==3) shunget(f);
400                 if (pok) for (; i>3; i--) shunget(f);
401                 else shlim(f, 0);
402                 return sign * INFINITY;
403         }
404         if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
405                 if (i<3) c = shgetc(f);
406         if (i==3) {
407                 return NAN;
408         }
409
410         if (i) {
411                 shunget(f);
412                 shlim(f, 0);
413                 return 0;
414         }
415
416         if (c=='0') {
417                 c = shgetc(f);
418                 if ((c|32) == 'x')
419                         return hexfloat(f, bits, emin, sign, pok);
420                 c = '0';
421         }
422
423         shunget(f);
424         return decfloat(f, bits, emin, sign, pok);
425 }