floatscan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* Originally released by the musl project (http://www.musl-libc.org/) under the
  2. * MIT license. Taken from the file src/internal/floatscan.c*/
  3. #include <stdint.h>
  4. #include <math.h>
  5. #include <limits.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include "floatscan.h"
  9. #include "vfprintf.h"
  10. int shgetc(char* input, int *index);
  11. void shunget(int *index);
  12. int shlim(int a, int b);
  13. int shgetc(char* input, int *index){
  14. int res = input[*index];
  15. (*index)++;
  16. return res;
  17. }
  18. void shunget(int *index){
  19. (*index)--;
  20. }
  21. int shlim(int a, int b){
  22. return '0';
  23. }
  24. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  25. long double fmodl(long double x, long double y)
  26. {
  27. return fmod(x, y);
  28. }
  29. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  30. long double fmodl(long double x, long double y)
  31. {
  32. union ldshape ux = {x}, uy = {y};
  33. int ex = ux.i.se & 0x7fff;
  34. int ey = uy.i.se & 0x7fff;
  35. int sx = ux.i.se & 0x8000;
  36. if (y == 0 || isnan(y) || ex == 0x7fff)
  37. return (x*y)/(x*y);
  38. ux.i.se = (uint16_t)ex;
  39. uy.i.se = (uint16_t)ey;
  40. if (ux.f <= uy.f) {
  41. if (ux.f == uy.f)
  42. return 0*x;
  43. return x;
  44. }
  45. /* normalize x and y */
  46. if (!ex) {
  47. ux.f *= 0x1p120f;
  48. ex = ux.i.se - 120;
  49. }
  50. if (!ey) {
  51. uy.f *= 0x1p120f;
  52. ey = uy.i.se - 120;
  53. }
  54. /* x mod y */
  55. #if LDBL_MANT_DIG == 64
  56. uint64_t i, mx, my;
  57. mx = ux.i.m;
  58. my = uy.i.m;
  59. for (; ex > ey; ex--) {
  60. i = mx - my;
  61. if (mx >= my) {
  62. if (i == 0)
  63. return 0*x;
  64. mx = 2*i;
  65. } else if (2*mx < mx) {
  66. mx = 2*mx - my;
  67. } else {
  68. mx = 2*mx;
  69. }
  70. }
  71. i = mx - my;
  72. if (mx >= my) {
  73. if (i == 0)
  74. return 0*x;
  75. mx = i;
  76. }
  77. for (; mx >> 63 == 0; mx *= 2, ex--);
  78. ux.i.m = mx;
  79. #elif LDBL_MANT_DIG == 113
  80. uint64_t hi, lo, xhi, xlo, yhi, ylo;
  81. xhi = (ux.i2.hi & -1ULL>>16) | 1ULL<<48;
  82. yhi = (uy.i2.hi & -1ULL>>16) | 1ULL<<48;
  83. xlo = ux.i2.lo;
  84. ylo = uy.i2.lo;
  85. for (; ex > ey; ex--) {
  86. hi = xhi - yhi;
  87. lo = xlo - ylo;
  88. if (xlo < ylo)
  89. hi -= 1;
  90. if (hi >> 63 == 0) {
  91. if ((hi|lo) == 0)
  92. return 0*x;
  93. xhi = 2*hi + (lo>>63);
  94. xlo = 2*lo;
  95. } else {
  96. xhi = 2*xhi + (xlo>>63);
  97. xlo = 2*xlo;
  98. }
  99. }
  100. hi = xhi - yhi;
  101. lo = xlo - ylo;
  102. if (xlo < ylo)
  103. hi -= 1;
  104. if (hi >> 63 == 0) {
  105. if ((hi|lo) == 0)
  106. return 0*x;
  107. xhi = hi;
  108. xlo = lo;
  109. }
  110. for (; xhi >> 48 == 0; xhi = 2*xhi + (xlo>>63), xlo = 2*xlo, ex--);
  111. ux.i2.hi = xhi;
  112. ux.i2.lo = xlo;
  113. #endif
  114. /* scale result */
  115. if (ex <= 0) {
  116. ux.i.se = (uint16_t)((ex+120)|sx);
  117. ux.f *= (uint16_t)(0x1p-120f);
  118. } else
  119. ux.i.se = (uint16_t)(ex|sx);
  120. return ux.f;
  121. }
  122. #endif
  123. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  124. long double copysignl(long double x, long double y)
  125. {
  126. return copysign(x, y);
  127. }
  128. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  129. long double copysignl(long double x, long double y)
  130. {
  131. union ldshape ux = {x}, uy = {y};
  132. ux.i.se &= 0x7fff;
  133. ux.i.se = (uint16_t)(ux.i.se | (uy.i.se & 0x8000));
  134. return ux.f;
  135. }
  136. #endif
  137. double scalbn(double x, int n)
  138. {
  139. union {double f; uint64_t i;} u;
  140. double_t y = x;
  141. if (n > 1023) {
  142. y *= 0x1p1023;
  143. n -= 1023;
  144. if (n > 1023) {
  145. y *= 0x1p1023;
  146. n -= 1023;
  147. if (n > 1023)
  148. n = 1023;
  149. }
  150. } else if (n < -1022) {
  151. /* make sure final n < -53 to avoid double
  152. rounding in the subnormal range */
  153. y *= 0x1p-1022 * 0x1p53;
  154. n += 1022 - 53;
  155. if (n < -1022) {
  156. y *= 0x1p-1022 * 0x1p53;
  157. n += 1022 - 53;
  158. if (n < -1022)
  159. n = -1022;
  160. }
  161. }
  162. u.i = (uint64_t)(0x3ff+n)<<52;
  163. x = y * u.f;
  164. return x;
  165. }
  166. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  167. #define LD_B1B_DIG 2
  168. #define LD_B1B_MAX 9007199, 254740991
  169. #define KMAX 128
  170. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  171. #define LD_B1B_DIG 3
  172. #define LD_B1B_MAX 18, 446744073, 709551615
  173. #define KMAX 2048
  174. #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
  175. #define LD_B1B_DIG 4
  176. #define LD_B1B_MAX 10384593, 717069655, 257060992, 658440191
  177. #define KMAX 2048
  178. #else
  179. #error Unsupported long double representation
  180. #endif
  181. #define MASK (KMAX-1)
  182. #define CONCAT2(x,y) x ## y
  183. #define CONCAT(x,y) CONCAT2(x,y)
  184. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  185. long double scalbnl(long double x, int n)
  186. {
  187. return scalbn(x, n);
  188. }
  189. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  190. long double scalbnl(long double x, int n)
  191. {
  192. union ldshape u;
  193. if (n > 16383) {
  194. x *= 0x1p16383L;
  195. n -= 16383;
  196. if (n > 16383) {
  197. x *= 0x1p16383L;
  198. n -= 16383;
  199. if (n > 16383)
  200. n = 16383;
  201. }
  202. } else if (n < -16382) {
  203. x *= 0x1p-16382L * 0x1p113L;
  204. n += 16382 - 113;
  205. if (n < -16382) {
  206. x *= 0x1p-16382L * 0x1p113L;
  207. n += 16382 - 113;
  208. if (n < -16382)
  209. n = -16382;
  210. }
  211. }
  212. u.f = 1.0;
  213. u.i.se = (uint16_t)(0x3fff + n);
  214. return x * u.f;
  215. }
  216. #endif
  217. static long long scanexp(char* input, int *index, int pok)
  218. {
  219. int c;
  220. int x;
  221. long long y;
  222. int neg = 0;
  223. c = shgetc(input, index);
  224. if (c=='+' || c=='-') {
  225. neg = (c=='-');
  226. c = shgetc(input, index);
  227. if ((unsigned)(c-'0')>=10U && pok) shunget(index);
  228. }
  229. if ((unsigned)(c-'0')>=10U) {
  230. shunget(index);
  231. return LLONG_MIN;
  232. }
  233. for (x=0; (unsigned)(c-'0')<10U && x<INT_MAX/10; c = shgetc(input, index))
  234. x = 10*x + c-'0';
  235. for (y=x; (unsigned)(c-'0')<10U && y<LLONG_MAX/100; c = shgetc(input, index))
  236. y = 10*y + c-'0';
  237. for (; (unsigned)(c-'0')<10U; c = shgetc(input, index));
  238. shunget(index);
  239. return neg ? -y : y;
  240. }
  241. static long double decfloat(char *input, int *index, int c, int bits, int emin, int sign, int pok)
  242. {
  243. uint32_t x[KMAX];
  244. static const uint32_t th[] = { LD_B1B_MAX };
  245. int i, j, k, a, z;
  246. long long lrp=0, dc=0;
  247. long long e10=0;
  248. int lnz = 0;
  249. int gotdig = 0, gotrad = 0;
  250. int rp;
  251. int e2;
  252. int emax = -emin-bits+3;
  253. int denormal = 0;
  254. long double y;
  255. long double frac=0;
  256. long double bias=0;
  257. static const int p10s[] = { 10, 100, 1000, 10000,
  258. 100000, 1000000, 10000000, 100000000 };
  259. j=0;
  260. k=0;
  261. /* Don't let leading zeros consume buffer space */
  262. for (; c=='0'; c = shgetc(input, index)) gotdig=1;
  263. if (c=='.') {
  264. gotrad = 1;
  265. for (c = shgetc(input, index); c=='0'; c = shgetc(input, index)) gotdig=1, lrp--;
  266. }
  267. x[0] = 0;
  268. for (; (unsigned)(c-'0')<10U || c=='.'; c = shgetc(input, index)) {
  269. if (c == '.') {
  270. if (gotrad) break;
  271. gotrad = 1;
  272. lrp = dc;
  273. } else if (k < KMAX-3) {
  274. dc++;
  275. if (c!='0') lnz = (int)dc;
  276. if (j) x[k] = (x[k]*10 + (uint32_t)(c-'0'));
  277. else x[k] = (uint32_t)(c-'0');
  278. if (++j==9) {
  279. k++;
  280. j=0;
  281. }
  282. gotdig=1;
  283. } else {
  284. dc++;
  285. if (c!='0') {
  286. lnz = (KMAX-4)*9;
  287. x[KMAX-4] |= 1;
  288. }
  289. }
  290. }
  291. if (!gotrad) lrp=dc;
  292. if (gotdig && (c|32)=='e') {
  293. e10 = scanexp(input, index, pok);
  294. if (e10 == LLONG_MIN) {
  295. if (pok) {
  296. shunget(index);
  297. } else {
  298. //shlim(f, 0);
  299. return 0;
  300. }
  301. e10 = 0;
  302. }
  303. lrp += e10;
  304. } else if (c>=0) {
  305. shunget(index);
  306. }
  307. if (!gotdig) {
  308. errno = EINVAL;
  309. //shlim(f, 0);
  310. return 0;
  311. }
  312. /* Handle zero specially to avoid nasty special cases later */
  313. if (!x[0]) return sign * 0.0;
  314. /* Optimize small integers (w/no exponent) and over/under-flow */
  315. if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
  316. return sign * (long double)x[0];
  317. if (lrp > -emin/2) {
  318. errno = ERANGE;
  319. return sign * LDBL_MAX * LDBL_MAX;
  320. }
  321. if (lrp < emin-2*LDBL_MANT_DIG) {
  322. errno = ERANGE;
  323. return sign * LDBL_MIN * LDBL_MIN;
  324. }
  325. /* Align incomplete final B1B digit */
  326. if (j) {
  327. for (; j<9; j++) x[k]*=10;
  328. k++;
  329. //j=0;
  330. }
  331. a = 0;
  332. z = k;
  333. e2 = 0;
  334. rp = (int)lrp;
  335. /* Optimize small to mid-size integers (even in exp. notation) */
  336. if (lnz<9 && lnz<=rp && rp < 18) {
  337. if (rp == 9) return sign * (long double)x[0];
  338. if (rp < 9) return sign * (long double)x[0] / p10s[8-rp];
  339. int bitlim = bits-3*(int)(rp-9);
  340. if (bitlim>30 || x[0]>>bitlim==0)
  341. return sign * (long double)x[0] * p10s[rp-10];
  342. }
  343. /* Drop trailing zeros */
  344. for (; !x[z-1]; z--);
  345. /* Align radix point to B1B digit boundary */
  346. if (rp % 9) {
  347. int rpm9 = rp>=0 ? rp%9 : rp%9+9;
  348. int p10 = p10s[8-rpm9];
  349. uint32_t carry = 0;
  350. for (k=a; k!=z; k++) {
  351. uint32_t tmp = (x[k] % (uint32_t)p10);
  352. x[k] = x[k]/(uint32_t)p10 + carry;
  353. carry = 1000000000/(uint32_t)p10 * tmp;
  354. if (k==a && !x[k]) {
  355. a = ((a+1) & MASK);
  356. rp -= 9;
  357. }
  358. }
  359. if (carry) x[z++] = carry;
  360. rp += 9-rpm9;
  361. }
  362. /* Upscale until desired number of bits are left of radix point */
  363. while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
  364. uint32_t carry = 0;
  365. e2 -= 29;
  366. for (k=((z-1) & MASK); ; k=((k-1) & MASK)) {
  367. uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
  368. if (tmp > 1000000000) {
  369. carry = (uint32_t)(tmp / 1000000000);
  370. x[k] = (uint32_t)(tmp % 1000000000);
  371. } else {
  372. carry = 0;
  373. x[k] = (uint32_t)tmp;
  374. }
  375. if (k==((z-1) & MASK) && k!=a && !x[k]) z = k;
  376. if (k==a) break;
  377. }
  378. if (carry) {
  379. rp += 9;
  380. a = ((a-1) & MASK);
  381. if (a == z) {
  382. z = ((z-1) & MASK);
  383. x[(z-1) & MASK] |= x[z];
  384. }
  385. x[a] = carry;
  386. }
  387. }
  388. /* Downscale until exactly number of bits are left of radix point */
  389. for (;;) {
  390. uint32_t carry = 0;
  391. int sh = 1;
  392. for (i=0; i<LD_B1B_DIG; i++) {
  393. k = ((a+i) & MASK);
  394. if (k == z || x[k] < th[i]) {
  395. i=LD_B1B_DIG;
  396. break;
  397. }
  398. if (x[(a+i) & MASK] > th[i]) break;
  399. }
  400. if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
  401. /* FIXME: find a way to compute optimal sh */
  402. if (rp > 9+9*LD_B1B_DIG) sh = 9;
  403. e2 += sh;
  404. for (k=a; k!=z; k=((k+1) & MASK)) {
  405. uint32_t tmp = (x[k] & (uint32_t)((1<<sh)-1));
  406. x[k] = (x[k]>>sh) + carry;
  407. carry = ((uint32_t)(1000000000>>sh) * tmp);
  408. if (k==a && !x[k]) {
  409. a = ((a+1) & MASK);
  410. i--;
  411. rp -= 9;
  412. }
  413. }
  414. if (carry) {
  415. if (((z+1) & MASK) != a) {
  416. x[z] = carry;
  417. z = ((z+1) & MASK);
  418. } else x[(z-1) & MASK] |= 1;
  419. }
  420. }
  421. /* Assemble desired bits into floating point variable */
  422. for (y=i=0; i<LD_B1B_DIG; i++) {
  423. if (((a+i) & MASK)==z) x[(z=((z+1) & MASK))-1] = 0;
  424. y = 1000000000.0L * y + x[(a+i) & MASK];
  425. }
  426. y *= sign;
  427. /* Limit precision for denormal results */
  428. if (bits > LDBL_MANT_DIG+e2-emin) {
  429. bits = LDBL_MANT_DIG+e2-emin;
  430. if (bits<0) bits=0;
  431. denormal = 1;
  432. }
  433. /* Calculate bias term to force rounding, move out lower bits */
  434. if (bits < LDBL_MANT_DIG) {
  435. bias = copysignl(scalbn(1, 2*LDBL_MANT_DIG-bits-1), y);
  436. frac = fmodl(y, scalbn(1, LDBL_MANT_DIG-bits));
  437. y -= frac;
  438. y += bias;
  439. }
  440. /* Process tail of decimal input so it can affect rounding */
  441. if (((a+i) & MASK) != z) {
  442. uint32_t t = x[(a+i) & MASK];
  443. if (t < 500000000 && (t || ((a+i+1) & MASK) != z))
  444. frac += 0.25*sign;
  445. else if (t > 500000000)
  446. frac += 0.75*sign;
  447. else if (t == 500000000) {
  448. if (((a+i+1) & MASK) == z)
  449. frac += 0.5*sign;
  450. else
  451. frac += 0.75*sign;
  452. }
  453. //if (LDBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1)) implicit conversion turns floating-point number into integer:
  454. if (LDBL_MANT_DIG-bits >= 2 && !((_Bool)fmodl(frac, 1)))
  455. frac++;
  456. }
  457. y += frac;
  458. y -= bias;
  459. if (((e2+LDBL_MANT_DIG) & INT_MAX) > emax-5) {
  460. if (fabs((double)y) >= CONCAT(0x1p, LDBL_MANT_DIG)) {
  461. if (denormal && bits==LDBL_MANT_DIG+e2-emin)
  462. denormal = 0;
  463. y *= 0.5;
  464. e2++;
  465. }
  466. //if (e2+LDBL_MANT_DIG>emax || (denormal && frac)) implicit conversion turns floating-point number into integer:
  467. if (e2+LDBL_MANT_DIG>emax || ((_Bool)denormal && (_Bool)frac))
  468. errno = ERANGE;
  469. }
  470. return scalbnl(y, e2);
  471. }
  472. static long double hexfloat(char *input, int *index, int bits, int emin, int sign, int pok)
  473. {
  474. uint32_t x = 0;
  475. long double y = 0;
  476. long double scale = 1;
  477. long double bias = 0;
  478. int gottail = 0, gotrad = 0, gotdig = 0;
  479. long long rp = 0;
  480. long long dc = 0;
  481. long long e2 = 0;
  482. int d;
  483. int c;
  484. c = shgetc(input, index);
  485. /* Skip leading zeros */
  486. for (; c=='0'; c = shgetc(input, index)) gotdig = 1;
  487. if (c=='.') {
  488. gotrad = 1;
  489. c = shgetc(input, index);
  490. /* Count zeros after the radix point before significand */
  491. for (rp=0; c=='0'; c = shgetc(input, index), rp--) gotdig = 1;
  492. }
  493. for (; (unsigned)(c-'0')<10U || (unsigned)((c|32)-'a')<6U || c=='.'; c = shgetc(input, index)) {
  494. if (c=='.') {
  495. if (gotrad) break;
  496. rp = dc;
  497. gotrad = 1;
  498. } else {
  499. gotdig = 1;
  500. if (c > '9') d = (c|32)+10-'a';
  501. else d = c-'0';
  502. if (dc<8) {
  503. x = (x*16 + (uint32_t)d);
  504. } else if (dc < LDBL_MANT_DIG/4+1) {
  505. y += d*(scale/=16);
  506. } else if (d && !gottail) {
  507. y += 0.5*scale;
  508. gottail = 1;
  509. }
  510. dc++;
  511. }
  512. }
  513. if (!gotdig) {
  514. shunget(index);
  515. if (pok) {
  516. shunget(index);
  517. if (gotrad) shunget(index);
  518. } else {
  519. //shlim(f, 0);
  520. }
  521. return sign * 0.0;
  522. }
  523. if (!gotrad) rp = dc;
  524. while (dc<8) x *= 16, dc++;
  525. if ((c|32)=='p') {
  526. e2 = scanexp(input, index, pok);
  527. if (e2 == LLONG_MIN) {
  528. if (pok) {
  529. shunget(index);
  530. } else {
  531. //shlim(f, 0);
  532. return 0;
  533. }
  534. e2 = 0;
  535. }
  536. } else {
  537. shunget(index);
  538. }
  539. e2 += 4*rp - 32;
  540. if (!x) return sign * 0.0;
  541. if (e2 > -emin) {
  542. errno = ERANGE;
  543. return sign * LDBL_MAX * LDBL_MAX;
  544. }
  545. if (e2 < emin-2*LDBL_MANT_DIG) {
  546. errno = ERANGE;
  547. return sign * LDBL_MIN * LDBL_MIN;
  548. }
  549. while (x < 0x80000000) {
  550. if (y>=0.5) {
  551. x += x + 1;
  552. y += y - 1;
  553. } else {
  554. x += x;
  555. y += y;
  556. }
  557. e2--;
  558. }
  559. if (bits > 32+e2-emin) {
  560. bits =(int)(32+e2-emin);
  561. if (bits<0) bits=0;
  562. }
  563. if (bits < LDBL_MANT_DIG)
  564. bias = copysignl(scalbn(1, 32+LDBL_MANT_DIG-bits-1), sign);
  565. if (bits<32 && (_Bool)y && !(x&1)) x++, y=0;
  566. y = bias + sign*(long double)x + sign*y;
  567. y -= bias;
  568. if (!((_Bool)y)) errno = ERANGE;
  569. return scalbnl(y, (int)e2);
  570. }
  571. long double __floatscan(char* input, int prec, int pok)
  572. {
  573. int index = 0;
  574. int sign = 1;
  575. //size_t i;
  576. int i;
  577. int bits;
  578. int emin;
  579. int c;
  580. switch (prec) {
  581. case 0:
  582. bits = FLT_MANT_DIG;
  583. emin = FLT_MIN_EXP-bits;
  584. break;
  585. case 1:
  586. bits = DBL_MANT_DIG;
  587. emin = DBL_MIN_EXP-bits;
  588. break;
  589. case 2:
  590. bits = LDBL_MANT_DIG;
  591. emin = LDBL_MIN_EXP-bits;
  592. break;
  593. default:
  594. return 0;
  595. }
  596. while (isspace((c=shgetc(input, &index))));
  597. if (c=='+' || c=='-') {
  598. sign -= 2*(c=='-');
  599. c = shgetc(input, &index);
  600. }
  601. for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
  602. if (i<7) c = shgetc(input, &index);
  603. if (i==3 || i==8 || (i>3 && pok)) {
  604. if (i!=8) {
  605. shunget(&index);
  606. if (pok) for (; i>3; i--) shunget(&index);
  607. }
  608. return ((float)sign * INFINITY);
  609. }
  610. if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
  611. if (i<2) c = shgetc(input, &index);
  612. if (i==3) {
  613. if (shgetc(input, &index) != '(') {
  614. shunget(&index);
  615. return NAN;
  616. }
  617. for (i=1; ; i++) {
  618. c = shgetc(input, &index);
  619. if ((unsigned)(c-'0')<10U || (unsigned)(c-'A')<26U || (unsigned)(c-'a')<26U || c=='_')
  620. continue;
  621. if (c==')') return NAN;
  622. shunget(&index);
  623. if (!pok) {
  624. errno = EINVAL;
  625. //shlim(0, 0);
  626. return 0;
  627. }
  628. while (i--) shunget(&index);
  629. return NAN;
  630. }
  631. return NAN;
  632. }
  633. if (i) {
  634. shunget(&index);
  635. errno = EINVAL;
  636. //shlim(0, 0);
  637. return 0;
  638. }
  639. if (c=='0') {
  640. c = shgetc(input, &index);
  641. if ((c|32) == 'x')
  642. return hexfloat(input, &index, bits, emin, sign, pok);
  643. shunget(&index);
  644. c = '0';
  645. }
  646. return decfloat(input, &index, c, bits, emin, sign, pok);
  647. }