c_stdlib.c 5.7 KB

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