5 long double strtold(const char *s1, char **p)
7 const unsigned char *s = (void *)s1;
15 if (!p) p = (char **)&s1;
17 /* Initial whitespace */
18 for (; isspace(*s); s++);
21 if (*s == '-') sign = *s++;
22 else if (*s == '+') s++;
24 /* Handle infinities and NaNs. */
25 if ((s[0]|32)=='i' && (s[1]|32)=='n' && (s[2]|32)=='f') {
27 return sign ? -1.0/0.0 : 1.0/0.0;
28 } else if ((s[0]|32)=='n' && (s[1]|32)=='a' && (s[2]|32)=='n') {
33 /* Possible hex float */
34 if (s[0]=='0' && (s[1]|32)=='x') {
35 /* Mantissa must be non-degenerate */
36 if (!isxdigit(s[2]) && (s[2]!=radix || !isxdigit(s[3]))) {
37 /* Decimal float 0, 'x' extraneous */
41 /* We have a real hex float */
43 for (; isxdigit(*s); s++) {
44 x = 16*x + (isdigit(*s)?*s-'0':(*s|32)-'a');
45 if (*s!='0') nonzero=1;
49 for (s++; isxdigit(*s); s++) {
50 x += frac * (isdigit(*s)?*s-'0':(*s|32)-'a');
52 if (*s!='0') nonzero=1;
56 e = strtol((void *)(s+1), (void *)&s, 10);
57 for (; e>0; e--) x *= 2.0;
58 for (; e<0; e++) x *= 0.5;
60 if ((nonzero && !x) || !(1.0/x))
66 /* Mantissa must be non-degenerate */
67 if (!isdigit(s[0]) && (s[0]!=radix || !isdigit(s[1]))) {
72 for (; isdigit(*s); s++) {
74 if (*s!='0') nonzero=1;
78 for (s++; isdigit(*s); s++) {
81 if (*s!='0') nonzero=1;
85 e = strtol((void *)++s, (void *)&s, 10);
86 for (; e>0; e--) x *= 10.0;
87 for (; e<0; e++) x /= 10.0;
89 if ((nonzero && !x) || !(1.0/x))