stdlib.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "user_config.h"
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #ifdef LUA_CROSS_COMPILER
  7. #define ICACHE_RODATA_ATTR
  8. #define TRUE 1
  9. #define FALSE 0
  10. #endif
  11. double powersOf10[] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = /* Table giving binary powers of 10. Entry */
  12. {
  13. 10., /* is 10^2^i. Used to convert decimal */
  14. 100., /* exponents into floating-point numbers. */
  15. 1.0e4,
  16. 1.0e8,
  17. 1.0e16,
  18. 1.0e32,
  19. 1.0e64,
  20. 1.0e128,
  21. 1.0e256
  22. };
  23. double strtod(const char *string, char **endPtr)
  24. {
  25. int maxExponent = 511; /* Largest possible base 10 exponent. Any
  26. * exponent larger than this will already
  27. * produce underflow or overflow, so there's
  28. * no need to worry about additional digits.
  29. */
  30. int sign, expSign = FALSE;
  31. double fraction, dblExp, *d;
  32. register const char *p;
  33. register int c;
  34. int exp = 0; /* Exponent read from "EX" field. */
  35. int fracExp = 0; /* Exponent that derives from the fractional
  36. * part. Under normal circumstatnces, it is
  37. * the negative of the number of digits in F.
  38. * However, if I is very long, the last digits
  39. * of I get dropped (otherwise a long I with a
  40. * large negative exponent could cause an
  41. * unnecessary overflow on I alone). In this
  42. * case, fracExp is incremented one for each
  43. * dropped digit. */
  44. int mantSize; /* Number of digits in mantissa. */
  45. int decPt; /* Number of mantissa digits BEFORE decimal
  46. * point. */
  47. const char *pExp; /* Temporarily holds location of exponent
  48. * in string. */
  49. /*
  50. * Strip off leading blanks and check for a sign.
  51. */
  52. p = string;
  53. while (isspace((unsigned char)(*p)))
  54. {
  55. p += 1;
  56. }
  57. if (*p == '-')
  58. {
  59. sign = TRUE;
  60. p += 1;
  61. }
  62. else
  63. {
  64. if (*p == '+')
  65. {
  66. p += 1;
  67. }
  68. sign = FALSE;
  69. }
  70. /*
  71. * Count the number of digits in the mantissa (including the decimal
  72. * point), and also locate the decimal point.
  73. */
  74. decPt = -1;
  75. for (mantSize = 0; ; mantSize += 1)
  76. {
  77. c = *p;
  78. if (!isdigit(c))
  79. {
  80. if ((c != '.') || (decPt >= 0))
  81. {
  82. break;
  83. }
  84. decPt = mantSize;
  85. }
  86. p += 1;
  87. }
  88. /*
  89. * Now suck up the digits in the mantissa. Use two integers to
  90. * collect 9 digits each (this is faster than using floating-point).
  91. * If the mantissa has more than 18 digits, ignore the extras, since
  92. * they can't affect the value anyway.
  93. */
  94. pExp = p;
  95. p -= mantSize;
  96. if (decPt < 0)
  97. {
  98. decPt = mantSize;
  99. }
  100. else
  101. {
  102. mantSize -= 1; /* One of the digits was the point. */
  103. }
  104. if (mantSize > 18)
  105. {
  106. fracExp = decPt - 18;
  107. mantSize = 18;
  108. }
  109. else
  110. {
  111. fracExp = decPt - mantSize;
  112. }
  113. if (mantSize == 0)
  114. {
  115. fraction = 0.0;
  116. p = string;
  117. goto done;
  118. }
  119. else
  120. {
  121. int frac1, frac2;
  122. frac1 = 0;
  123. for ( ; mantSize > 9; mantSize -= 1)
  124. {
  125. c = *p;
  126. p += 1;
  127. if (c == '.')
  128. {
  129. c = *p;
  130. p += 1;
  131. }
  132. frac1 = 10 * frac1 + (c - '0');
  133. }
  134. frac2 = 0;
  135. for (; mantSize > 0; mantSize -= 1)
  136. {
  137. c = *p;
  138. p += 1;
  139. if (c == '.')
  140. {
  141. c = *p;
  142. p += 1;
  143. }
  144. frac2 = 10 * frac2 + (c - '0');
  145. }
  146. fraction = (1.0e9 * frac1) + frac2;
  147. }
  148. /*
  149. * Skim off the exponent.
  150. */
  151. p = pExp;
  152. if ((*p == 'E') || (*p == 'e'))
  153. {
  154. p += 1;
  155. if (*p == '-')
  156. {
  157. expSign = TRUE;
  158. p += 1;
  159. }
  160. else
  161. {
  162. if (*p == '+')
  163. {
  164. p += 1;
  165. }
  166. expSign = FALSE;
  167. }
  168. if (!isdigit((unsigned char)(*p)))
  169. {
  170. p = pExp;
  171. goto done;
  172. }
  173. while (isdigit((unsigned char)(*p)))
  174. {
  175. exp = exp * 10 + (*p - '0');
  176. p += 1;
  177. }
  178. }
  179. if (expSign)
  180. {
  181. exp = fracExp - exp;
  182. }
  183. else
  184. {
  185. exp = fracExp + exp;
  186. }
  187. /*
  188. * Generate a floating-point number that represents the exponent.
  189. * Do this by processing the exponent one bit at a time to combine
  190. * many powers of 2 of 10. Then combine the exponent with the
  191. * fraction.
  192. */
  193. if (exp < 0)
  194. {
  195. expSign = TRUE;
  196. exp = -exp;
  197. }
  198. else
  199. {
  200. expSign = FALSE;
  201. }
  202. if (exp > maxExponent)
  203. {
  204. exp = maxExponent;
  205. // errno = ERANGE;
  206. }
  207. dblExp = 1.0;
  208. for (d = powersOf10; exp != 0; exp >>= 1, d += 1)
  209. {
  210. if (exp & 01)
  211. {
  212. dblExp *= *d;
  213. }
  214. }
  215. if (expSign)
  216. {
  217. fraction /= dblExp;
  218. }
  219. else
  220. {
  221. fraction *= dblExp;
  222. }
  223. done:
  224. if (endPtr != NULL)
  225. {
  226. *endPtr = (char *) p;
  227. }
  228. if (sign)
  229. {
  230. return -fraction;
  231. }
  232. return fraction;
  233. }