10 #include "floatscan.h"
12 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
15 #define LD_B1B_MAX 9007199, 254740991
18 #else /* LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 */
21 #define LD_B1B_MAX 18, 446744073, 709551615
28 #define CONCAT2(x,y) x ## y
29 #define CONCAT(x,y) CONCAT2(x,y)
31 static long long scanexp(FILE *f, int pok)
39 if (c=='+' || c=='-') {
42 if (c-'0'>=10U && pok) shunget(f);
48 for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
50 for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
52 for (; c-'0'<10U; c = shgetc(f));
58 static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
61 static const uint32_t th[] = { LD_B1B_MAX };
63 long long lrp=0, dc=0;
66 int gotdig = 0, gotrad = 0;
69 int emax = -emin-bits+3;
74 static const int p10s[] = { 10, 100, 1000, 10000,
75 100000, 1000000, 10000000, 100000000 };
80 /* Don't let leading zeros consume buffer space */
81 for (; c=='0'; c = shgetc(f)) gotdig=1;
84 for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
88 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
93 } else if (k < KMAX-3) {
96 if (j) x[k] = x[k]*10 + c-'0';
105 if (c!='0') x[KMAX-4] |= 1;
110 if (gotdig && (c|32)=='e') {
111 e10 = scanexp(f, pok);
112 if (e10 == LLONG_MIN) {
131 /* Handle zero specially to avoid nasty special cases later */
132 if (!x[0]) return sign * 0.0;
134 /* Optimize small integers (w/no exponent) and over/under-flow */
135 if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
136 return sign * (long double)x[0];
139 return sign * LDBL_MAX * LDBL_MAX;
141 if (lrp < emin-2*LDBL_MANT_DIG) {
143 return sign * LDBL_MIN * LDBL_MIN;
146 /* Align incomplete final B1B digit */
148 for (; j<9; j++) x[k]*=10;
158 /* Optimize small to mid-size integers (even in exp. notation) */
159 if (lnz<9 && lnz<=rp && rp < 18) {
160 if (rp == 9) return sign * (long double)x[0];
161 if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
162 int bitlim = bits-3*(int)(rp-9);
163 if (bitlim>30 || x[0]>>bitlim==0)
164 return sign * (long double)x[0] * p10s[rp-10];
167 /* Align radix point to B1B digit boundary */
169 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
170 int p10 = p10s[8-rpm9];
172 for (k=a; k!=z; k++) {
173 uint32_t tmp = x[k] % p10;
174 x[k] = x[k]/p10 + carry;
175 carry = 1000000000/p10 * tmp;
181 if (carry) x[z++] = carry;
185 /* Upscale until desired number of bits are left of radix point */
186 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
189 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
190 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
191 if (tmp > 1000000000) {
192 carry = tmp / 1000000000;
193 x[k] = tmp % 1000000000;
198 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
206 x[z-1 & MASK] |= x[z];
212 /* Downscale until exactly number of bits are left of radix point */
216 for (i=0; i<LD_B1B_DIG; i++) {
218 if (k == z || x[k] < th[i]) {
222 if (x[a+i & MASK] > th[i]) break;
224 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
225 /* FIXME: find a way to compute optimal sh */
226 if (rp > 9+9*LD_B1B_DIG) sh = 9;
228 for (k=a; k!=z; k=(k+1 & MASK)) {
229 uint32_t tmp = x[k] & (1<<sh)-1;
230 x[k] = (x[k]>>sh) + carry;
231 carry = (1000000000>>sh) * tmp;
239 if ((z+1 & MASK) != a) {
242 } else x[z-1 & MASK] |= 1;
246 /* Assemble desired bits into floating point variable */
247 for (y=i=0; i<LD_B1B_DIG; i++) {
248 if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
249 y = 1000000000.0L * y + x[a+i & MASK];
254 /* Limit precision for denormal results */
255 if (bits > LDBL_MANT_DIG+e2-emin) {
256 bits = LDBL_MANT_DIG+e2-emin;
261 /* Calculate bias term to force rounding, move out lower bits */
262 if (bits < LDBL_MANT_DIG) {
263 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
264 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
269 /* Process tail of decimal input so it can affect rounding */
270 if ((a+i & MASK) != z) {
271 uint32_t t = x[a+i & MASK];
272 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
274 else if (t > 500000000)
276 else if (t == 500000000) {
277 if ((a+i+1 & MASK) == z)
282 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
289 if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) {
290 if (fabs(y) >= CONCAT(0x1p, LDBL_MANT_DIG)) {
291 if (denormal && bits==LDBL_MANT_DIG+e2-emin)
296 if (e2+LDBL_MANT_DIG>emax || (denormal && frac))
300 return scalbnl(y, e2);
303 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
307 long double scale = 1;
308 long double bias = 0;
309 int gottail = 0, gotrad = 0, gotdig = 0;
318 /* Skip leading zeros */
319 for (; c=='0'; c = shgetc(f)) gotdig = 1;
324 /* Count zeros after the radix point before significand */
325 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
328 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
335 if (c > '9') d = (c|32)+10-'a';
339 } else if (dc < LDBL_MANT_DIG/4+1) {
341 } else if (d && !gottail) {
352 if (gotrad) shunget(f);
358 if (!gotrad) rp = dc;
359 while (dc<8) x *= 16, dc++;
361 e2 = scanexp(f, pok);
362 if (e2 == LLONG_MIN) {
376 if (!x) return sign * 0.0;
379 return sign * LDBL_MAX * LDBL_MAX;
381 if (e2 < emin-2*LDBL_MANT_DIG) {
383 return sign * LDBL_MIN * LDBL_MIN;
386 while (x < 0x80000000) {
397 if (bits > 32+e2-emin) {
402 if (bits < LDBL_MANT_DIG)
403 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
405 if (bits<32 && y && !(x&1)) x++, y=0;
407 y = bias + sign*(long double)x + sign*y;
410 if (!y) errno = ERANGE;
412 return scalbnl(y, e2);
415 long double __floatscan(FILE *f, int prec, int pok)
426 emin = FLT_MIN_EXP-bits;
430 emin = DBL_MIN_EXP-bits;
433 bits = LDBL_MANT_DIG;
434 emin = LDBL_MIN_EXP-bits;
440 while (isspace((c=shgetc(f))));
442 if (c=='+' || c=='-') {
447 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
448 if (i<7) c = shgetc(f);
449 if (i==3 || i==8 || (i>3 && pok)) {
452 if (pok) for (; i>3; i--) shunget(f);
454 return sign * INFINITY;
456 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
457 if (i<2) c = shgetc(f);
459 if (shgetc(f) != '(') {
465 if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
467 if (c==')') return NAN;
474 while (i--) shunget(f);
490 return hexfloat(f, bits, emin, sign, pok);
495 return decfloat(f, c, bits, emin, sign, pok);