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;
72 /* Don't let leading zeros consume buffer space */
73 for (; c=='0'; c = shgetc(f)) gotdig=1;
76 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
80 } else if (k < KMAX) {
82 if (j) x[k] = x[k]*10 + c-'0';
96 if (gotdig && (c|32)=='e') {
97 e10 = scanexp(f, pok);
98 if (e10 == LLONG_MIN) {
119 if (lrp==dc && (!k || (k==1 && !j)) && (bits>30 || x[0]>>bits==0))
120 return sign * (long double)x[0];
123 return sign * LDBL_MAX * LDBL_MAX;
125 if (lrp < emin-2*LDBL_MANT_DIG) {
127 return sign * LDBL_MIN * LDBL_MIN;
131 for (; j<9; j++) x[k]*=10;
142 static const int p10s[] = {
143 100000000, 10000000, 1000000, 100000,
146 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
147 int p10 = p10s[rpm9-1];
149 for (k=a; k!=z; k=(k+1 & MASK)) {
150 uint32_t tmp = x[k] % p10;
151 x[k] = x[k]/p10 + carry;
152 carry = 1000000000/p10 * tmp;
159 if ((z+1 & MASK) != a) {
162 } else x[z-1 & MASK] |= 1;
167 while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[0]<th[0])) {
170 for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
171 uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
172 if (tmp > 1000000000) {
173 carry = tmp / 1000000000;
174 x[k] = tmp % 1000000000;
179 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
186 x[z-1 & MASK] |= x[z];
196 for (i=0; i<LD_B1B_DIG; i++) {
198 if (k == z || x[k] < th[i]) {
202 if (x[a+i & MASK] > th[i]) break;
204 if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
205 /* FIXME: find a way to compute optimal sh */
206 if (rp > 9+9*LD_B1B_DIG) sh = 9;
208 for (k=a; k!=z; k=(k+1 & MASK)) {
209 uint32_t tmp = x[k] & (1<<sh)-1;
210 x[k] = (x[k]>>sh) + carry;
211 carry = (1000000000>>sh) * tmp;
218 if ((z+1 & MASK) != a) {
221 } else x[z-1 & MASK] |= 1;
225 for (y=i=0; i<LD_B1B_DIG; i++) {
226 if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
227 y = 1000000000.0L * y + x[a+i & MASK];
232 if (bits > LDBL_MANT_DIG+e2-emin) {
233 bits = LDBL_MANT_DIG+e2-emin;
237 if (bits < LDBL_MANT_DIG) {
238 bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
239 frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
244 if ((a+i & MASK) != z) {
245 uint32_t t = x[a+i & MASK];
246 if (t < 500000000 && (t || (a+i+1 & MASK) != z))
248 else if (t > 500000000)
250 else if (t == 500000000) {
251 if ((a+i+1 & MASK) == z)
256 if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
265 if (!y) errno = ERANGE;
270 static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
274 long double scale = 1;
275 long double bias = 0;
276 int gottail = 0, gotrad = 0, gotdig = 0;
285 /* Skip leading zeros */
286 for (; c=='0'; c = shgetc(f)) gotdig = 1;
291 /* Count zeros after the radix point before significand */
292 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
295 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
302 if (c > '9') d = (c|32)+10-'a';
306 } else if (dc < LDBL_MANT_DIG/4+1) {
308 } else if (d && !gottail) {
319 if (gotrad) shunget(f);
325 if (!gotrad) rp = dc;
326 while (dc<8) x *= 16, dc++;
328 e2 = scanexp(f, pok);
329 if (e2 == LLONG_MIN) {
343 if (!x) return sign * 0.0;
346 return sign * LDBL_MAX * LDBL_MAX;
348 if (e2 < emin-2*LDBL_MANT_DIG) {
350 return sign * LDBL_MIN * LDBL_MIN;
353 while (x < 0x80000000) {
364 if (bits > 32+e2-emin) {
369 if (bits < LDBL_MANT_DIG)
370 bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
372 if (bits<32 && y && !(x&1)) x++, y=0;
374 y = bias + sign*(long double)x + sign*y;
377 if (!y) errno = ERANGE;
379 return scalbnl(y, e2);
382 long double __floatscan(FILE *f, int c, int prec, int pok)
399 bits = LDBL_MANT_DIG;
406 if (c<0) c = shgetc(f);
408 if (c=='+' || c=='-') {
413 for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
414 if (i<7) c = shgetc(f);
415 if (i==3 || i==8 || (i>3 && pok)) {
416 if (i==3) shunget(f);
417 if (pok) for (; i>3; i--) shunget(f);
419 return sign * INFINITY;
421 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
422 if (i<3) c = shgetc(f);
437 return hexfloat(f, bits, emin, sign, pok);
442 return decfloat(f, c, bits, emin, sign, pok);