11 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
14 #define LD_B1B_MAX 9007199, 254740991
17 #else /* LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 */
20 #define LD_B1B_MAX 18, 446744073, 709551615
27 #define CONCAT2(x,y) x ## y
28 #define CONCAT(x,y) CONCAT2(x,y)
30 static long long scanexp(FILE *f, int pok)
38 if (c=='+' || c=='-') {
41 if (c-'0'>=10U && pok) shunget(f);
47 for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
49 for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
51 for (; c-'0'<10U; c = shgetc(f));
57 static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
60 static const uint32_t th[] = { LD_B1B_MAX };
62 long long lrp=0, dc=0;
65 int gotdig = 0, gotrad = 0;
68 int emax = -emin-bits+3;
73 static const int p10s[] = { 10, 100, 1000, 10000,
74 100000, 1000000, 10000000, 100000000 };
79 /* Don't let leading zeros consume buffer space */
80 for (; c=='0'; c = shgetc(f)) gotdig=1;
83 for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
87 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
92 } else if (k < KMAX-3) {
95 if (j) x[k] = x[k]*10 + c-'0';
104 if (c!='0') x[KMAX-4] |= 1;
109 if (gotdig && (c|32)=='e') {
110 e10 = scanexp(f, pok);
111 if (e10 == LLONG_MIN) {
130 /* Handle zero specially to avoid nasty special cases later */
131 if (!x[0]) return sign * 0.0;
133 /* Optimize small integers (w/no exponent) and over/under-flow */
134 if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
135 return sign * (long double)x[0];
138 return sign * LDBL_MAX * LDBL_MAX;
140 if (lrp < emin-2*LDBL_MANT_DIG) {
142 return sign * LDBL_MIN * LDBL_MIN;
145 /* Align incomplete final B1B digit */
147 for (; j<9; j++) x[k]*=10;
157 /* Optimize small to mid-size integers (even in exp. notation) */
158 if (lnz<9 && lnz<=rp && rp < 18) {
159 if (rp == 9) return sign * (long double)x[0];
160 if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
161 int bitlim = bits-3*(int)(rp-9);
162 if (bitlim>30 || x[0]>>bitlim==0)
163 return sign * (long double)x[0] * p10s[rp-10];
166 /* Align radix point to B1B digit boundary */
168 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
169 int p10 = p10s[8-rpm9];
171 for (k=a; k!=z; k++) {
172 uint32_t tmp = x[k] % p10;
173 x[k] = x[k]/p10 + carry;
174 carry = 1000000000/p10 * tmp;
180 if (carry) x[z++] = carry;
184 /* Upscale until desired number of bits are left of radix point */
185 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
188 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
189 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
190 if (tmp > 1000000000) {
191 carry = tmp / 1000000000;
192 x[k] = tmp % 1000000000;
197 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
205 x[z-1 & MASK] |= x[z];
211 /* Downscale until exactly number of bits are left of radix point */
215 for (i=0; i<LD_B1B_DIG; i++) {
217 if (k == z || x[k] < th[i]) {
221 if (x[a+i & MASK] > th[i]) break;
223 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
224 /* FIXME: find a way to compute optimal sh */
225 if (rp > 9+9*LD_B1B_DIG) sh = 9;
227 for (k=a; k!=z; k=(k+1 & MASK)) {
228 uint32_t tmp = x[k] & (1<<sh)-1;
229 x[k] = (x[k]>>sh) + carry;
230 carry = (1000000000>>sh) * tmp;
238 if ((z+1 & MASK) != a) {
241 } else x[z-1 & MASK] |= 1;
245 /* Assemble desired bits into floating point variable */
246 for (y=i=0; i<LD_B1B_DIG; i++) {
247 if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
248 y = 1000000000.0L * y + x[a+i & MASK];
253 /* Limit precision for denormal results */
254 if (bits > LDBL_MANT_DIG+e2-emin) {
255 bits = LDBL_MANT_DIG+e2-emin;
260 /* Calculate bias term to force rounding, move out lower bits */
261 if (bits < LDBL_MANT_DIG) {
262 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
263 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
268 /* Process tail of decimal input so it can affect rounding */
269 if ((a+i & MASK) != z) {
270 uint32_t t = x[a+i & MASK];
271 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
273 else if (t > 500000000)
275 else if (t == 500000000) {
276 if ((a+i+1 & MASK) == z)
281 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
288 if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) {
289 if (fabs(y) >= CONCAT(0x1p, LDBL_MANT_DIG)) {
290 if (denormal && bits==LDBL_MANT_DIG+e2-emin)
295 if (e2+LDBL_MANT_DIG>emax || (denormal && frac))
299 return scalbnl(y, e2);
302 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
306 long double scale = 1;
307 long double bias = 0;
308 int gottail = 0, gotrad = 0, gotdig = 0;
317 /* Skip leading zeros */
318 for (; c=='0'; c = shgetc(f)) gotdig = 1;
323 /* Count zeros after the radix point before significand */
324 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
327 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
334 if (c > '9') d = (c|32)+10-'a';
338 } else if (dc < LDBL_MANT_DIG/4+1) {
340 } else if (d && !gottail) {
351 if (gotrad) shunget(f);
357 if (!gotrad) rp = dc;
358 while (dc<8) x *= 16, dc++;
360 e2 = scanexp(f, pok);
361 if (e2 == LLONG_MIN) {
375 if (!x) return sign * 0.0;
378 return sign * LDBL_MAX * LDBL_MAX;
380 if (e2 < emin-2*LDBL_MANT_DIG) {
382 return sign * LDBL_MIN * LDBL_MIN;
385 while (x < 0x80000000) {
396 if (bits > 32+e2-emin) {
401 if (bits < LDBL_MANT_DIG)
402 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
404 if (bits<32 && y && !(x&1)) x++, y=0;
406 y = bias + sign*(long double)x + sign*y;
409 if (!y) errno = ERANGE;
411 return scalbnl(y, e2);
414 long double __floatscan(FILE *f, int prec, int pok)
425 emin = FLT_MIN_EXP-bits;
429 emin = DBL_MIN_EXP-bits;
432 bits = LDBL_MANT_DIG;
433 emin = LDBL_MIN_EXP-bits;
439 while (isspace((c=shgetc(f))));
441 if (c=='+' || c=='-') {
446 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
447 if (i<7) c = shgetc(f);
448 if (i==3 || i==8 || (i>3 && pok)) {
451 if (pok) for (; i>3; i--) shunget(f);
453 return sign * INFINITY;
455 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
456 if (i<2) c = shgetc(f);
471 return hexfloat(f, bits, emin, sign, pok);
476 return decfloat(f, c, bits, emin, sign, pok);