c_stdlib.c 5.9 KB

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