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
28 static long long scanexp(FILE *f, int pok)
36 if (c=='+' || c=='-') {
39 if (c-'0'>=10U && pok) shunget(f);
45 for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
47 for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
49 for (; c-'0'<10U; c = shgetc(f));
55 static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
58 static const uint32_t th[] = { LD_B1B_MAX };
60 long long lrp=0, dc=0;
63 int gotdig = 0, gotrad = 0;
69 static const int p10s[] = { 10, 100, 1000, 10000,
70 100000, 1000000, 10000000, 100000000 };
75 /* Don't let leading zeros consume buffer space */
76 for (; c=='0'; c = shgetc(f)) gotdig=1;
79 for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
83 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
88 } else if (k < KMAX-2) {
91 if (j) x[k] = x[k]*10 + c-'0';
100 if (c!='0') x[KMAX-3] |= 1;
105 if (gotdig && (c|32)=='e') {
106 e10 = scanexp(f, pok);
107 if (e10 == LLONG_MIN) {
126 /* Handle zero specially to avoid nasty special cases later */
127 if (!x[0]) return sign * 0.0;
129 /* Optimize small integers (w/no exponent) and over/under-flow */
130 if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
131 return sign * (long double)x[0];
134 return sign * LDBL_MAX * LDBL_MAX;
136 if (lrp < emin-2*LDBL_MANT_DIG) {
138 return sign * LDBL_MIN * LDBL_MIN;
141 /* Align incomplete final B1B digit */
143 for (; j<9; j++) x[k]*=10;
153 /* Optimize small to mid-size integers (even in exp. notation) */
154 if (lnz<9 && lnz<=rp && rp < 18) {
155 if (rp == 9) return sign * (long double)x[0];
156 if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
157 int bitlim = bits-3*(int)(rp-9);
158 if (bitlim>30 || x[0]>>bitlim==0)
159 return sign * (long double)x[0] * p10s[rp-10];
162 /* Align radix point to B1B digit boundary */
164 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
165 int p10 = p10s[8-rpm9];
167 for (k=a; k!=z; k++) {
168 uint32_t tmp = x[k] % p10;
169 x[k] = x[k]/p10 + carry;
170 carry = 1000000000/p10 * tmp;
176 if (carry) x[z++] = carry;
180 /* Upscale until desired number of bits are left of radix point */
181 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
184 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
185 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
186 if (tmp > 1000000000) {
187 carry = tmp / 1000000000;
188 x[k] = tmp % 1000000000;
193 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
200 x[z-1 & MASK] |= x[z];
207 /* Downscale until exactly number of bits are left of radix point */
211 for (i=0; i<LD_B1B_DIG; i++) {
213 if (k == z || x[k] < th[i]) {
217 if (x[a+i & MASK] > th[i]) break;
219 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
220 /* FIXME: find a way to compute optimal sh */
221 if (rp > 9+9*LD_B1B_DIG) sh = 9;
223 for (k=a; k!=z; k=(k+1 & MASK)) {
224 uint32_t tmp = x[k] & (1<<sh)-1;
225 x[k] = (x[k]>>sh) + carry;
226 carry = (1000000000>>sh) * tmp;
234 if ((z+1 & MASK) != a) {
237 } else x[z-1 & MASK] |= 1;
241 /* Assemble desired bits into floating point variable */
242 for (y=i=0; i<LD_B1B_DIG; i++) {
243 if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
244 y = 1000000000.0L * y + x[a+i & MASK];
249 /* Limit precision for denormal results */
250 if (bits > LDBL_MANT_DIG+e2-emin) {
251 bits = LDBL_MANT_DIG+e2-emin;
255 /* Calculate bias term to force rounding, move out lower bits */
256 if (bits < LDBL_MANT_DIG) {
257 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
258 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
263 /* Process tail of decimal input so it can affect rounding */
264 if ((a+i & MASK) != z) {
265 uint32_t t = x[a+i & MASK];
266 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
268 else if (t > 500000000)
270 else if (t == 500000000) {
271 if ((a+i+1 & MASK) == z)
276 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
285 if (!y) errno = ERANGE;
290 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
294 long double scale = 1;
295 long double bias = 0;
296 int gottail = 0, gotrad = 0, gotdig = 0;
305 /* Skip leading zeros */
306 for (; c=='0'; c = shgetc(f)) gotdig = 1;
311 /* Count zeros after the radix point before significand */
312 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
315 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
322 if (c > '9') d = (c|32)+10-'a';
326 } else if (dc < LDBL_MANT_DIG/4+1) {
328 } else if (d && !gottail) {
339 if (gotrad) shunget(f);
345 if (!gotrad) rp = dc;
346 while (dc<8) x *= 16, dc++;
348 e2 = scanexp(f, pok);
349 if (e2 == LLONG_MIN) {
363 if (!x) return sign * 0.0;
366 return sign * LDBL_MAX * LDBL_MAX;
368 if (e2 < emin-2*LDBL_MANT_DIG) {
370 return sign * LDBL_MIN * LDBL_MIN;
373 while (x < 0x80000000) {
384 if (bits > 32+e2-emin) {
389 if (bits < LDBL_MANT_DIG)
390 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
392 if (bits<32 && y && !(x&1)) x++, y=0;
394 y = bias + sign*(long double)x + sign*y;
397 if (!y) errno = ERANGE;
399 return scalbnl(y, e2);
402 long double __floatscan(FILE *f, int prec, int pok)
413 emin = FLT_MIN_EXP-bits;
417 emin = DBL_MIN_EXP-bits;
420 bits = LDBL_MANT_DIG;
421 emin = LDBL_MIN_EXP-bits;
427 while (isspace((c=shgetc(f))));
429 if (c=='+' || c=='-') {
434 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
435 if (i<7) c = shgetc(f);
436 if (i==3 || i==8 || (i>3 && pok)) {
439 if (pok) for (; i>3; i--) shunget(f);
441 return sign * INFINITY;
443 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
444 if (i<2) c = shgetc(f);
459 return hexfloat(f, bits, emin, sign, pok);
464 return decfloat(f, c, bits, emin, sign, pok);