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 && x<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=-1, dc=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-'0'<10U || c=='.'; c = shgetc(f)) {
83 } else if (k < KMAX-2) {
86 if (j) x[k] = x[k]*10 + c-'0';
95 if (c!='0') x[KMAX-3] |= 1;
100 if (gotdig && (c|32)=='e') {
101 e10 = scanexp(f, pok);
102 if (e10 == LLONG_MIN) {
121 /* Handle zero specially to avoid nasty special cases later */
122 if (!x[0]) return sign * 0.0;
124 /* Optimize small integers (w/no exponent) and over/under-flow */
125 if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
126 return sign * (long double)x[0];
129 return sign * LDBL_MAX * LDBL_MAX;
131 if (lrp < emin-2*LDBL_MANT_DIG) {
133 return sign * LDBL_MIN * LDBL_MIN;
136 /* Align incomplete final B1B digit */
138 for (; j<9; j++) x[k]*=10;
148 /* Optimize small to mid-size integers (even in exp. notation) */
149 if (lnz<9 && lnz<=rp && rp < 18) {
150 if (rp == 9) return sign * (long double)x[0];
151 if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
152 int bitlim = bits-3*(int)(rp-9);
153 if (bitlim>30 || x[0]>>bitlim==0)
154 return sign * (long double)x[0] * p10s[rp-10];
157 /* Align radix point to B1B digit boundary */
159 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
160 int p10 = p10s[8-rpm9];
162 for (k=a; k!=z; k++) {
163 uint32_t tmp = x[k] % p10;
164 x[k] = x[k]/p10 + carry;
165 carry = 1000000000/p10 * tmp;
171 if (carry) x[z++] = carry;
175 /* Upscale until desired number of bits are left of radix point */
176 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
179 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
180 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
181 if (tmp > 1000000000) {
182 carry = tmp / 1000000000;
183 x[k] = tmp % 1000000000;
188 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
195 x[z-1 & MASK] |= x[z];
202 /* Downscale until exactly number of bits are left of radix point */
206 for (i=0; i<LD_B1B_DIG; i++) {
208 if (k == z || x[k] < th[i]) {
212 if (x[a+i & MASK] > th[i]) break;
214 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
215 /* FIXME: find a way to compute optimal sh */
216 if (rp > 9+9*LD_B1B_DIG) sh = 9;
218 for (k=a; k!=z; k=(k+1 & MASK)) {
219 uint32_t tmp = x[k] & (1<<sh)-1;
220 x[k] = (x[k]>>sh) + carry;
221 carry = (1000000000>>sh) * tmp;
229 if ((z+1 & MASK) != a) {
232 } else x[z-1 & MASK] |= 1;
236 /* Assemble desired bits into floating point variable */
237 for (y=i=0; i<LD_B1B_DIG; i++) {
238 if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
239 y = 1000000000.0L * y + x[a+i & MASK];
244 /* Limit precision for denormal results */
245 if (bits > LDBL_MANT_DIG+e2-emin) {
246 bits = LDBL_MANT_DIG+e2-emin;
250 /* Calculate bias term to force rounding, move out lower bits */
251 if (bits < LDBL_MANT_DIG) {
252 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
253 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
258 /* Process tail of decimal input so it can affect rounding */
259 if ((a+i & MASK) != z) {
260 uint32_t t = x[a+i & MASK];
261 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
263 else if (t > 500000000)
265 else if (t == 500000000) {
266 if ((a+i+1 & MASK) == z)
271 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
280 if (!y) errno = ERANGE;
285 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
289 long double scale = 1;
290 long double bias = 0;
291 int gottail = 0, gotrad = 0, gotdig = 0;
300 /* Skip leading zeros */
301 for (; c=='0'; c = shgetc(f)) gotdig = 1;
306 /* Count zeros after the radix point before significand */
307 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
310 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
317 if (c > '9') d = (c|32)+10-'a';
321 } else if (dc < LDBL_MANT_DIG/4+1) {
323 } else if (d && !gottail) {
334 if (gotrad) shunget(f);
340 if (!gotrad) rp = dc;
341 while (dc<8) x *= 16, dc++;
343 e2 = scanexp(f, pok);
344 if (e2 == LLONG_MIN) {
358 if (!x) return sign * 0.0;
361 return sign * LDBL_MAX * LDBL_MAX;
363 if (e2 < emin-2*LDBL_MANT_DIG) {
365 return sign * LDBL_MIN * LDBL_MIN;
368 while (x < 0x80000000) {
379 if (bits > 32+e2-emin) {
384 if (bits < LDBL_MANT_DIG)
385 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
387 if (bits<32 && y && !(x&1)) x++, y=0;
389 y = bias + sign*(long double)x + sign*y;
392 if (!y) errno = ERANGE;
394 return scalbnl(y, e2);
397 long double __floatscan(FILE *f, int c, int prec, int pok)
407 emin = FLT_MIN_EXP-bits;
411 emin = DBL_MIN_EXP-bits;
414 bits = LDBL_MANT_DIG;
415 emin = LDBL_MIN_EXP-bits;
421 if (c<0) c = shgetc(f);
423 if (c=='+' || c=='-') {
428 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
429 if (i<7) c = shgetc(f);
430 if (i==3 || i==8 || (i>3 && pok)) {
431 if (i==3) shunget(f);
432 if (pok) for (; i>3; i--) shunget(f);
434 return sign * INFINITY;
436 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
437 if (i<3) c = shgetc(f);
452 return hexfloat(f, bits, emin, sign, pok);
457 return decfloat(f, c, bits, emin, sign, pok);