2
0

fast_atof.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
  4. #pragma once
  5. #include "irrTypes.h"
  6. #include <cfloat>
  7. #include <climits>
  8. #include <cmath>
  9. namespace irr
  10. {
  11. namespace core
  12. {
  13. #define IRR_ATOF_TABLE_SIZE 17
  14. // we write [IRR_ATOF_TABLE_SIZE] here instead of [] to work around a swig bug
  15. const float fast_atof_table[17] = {
  16. 0.f,
  17. 0.1f,
  18. 0.01f,
  19. 0.001f,
  20. 0.0001f,
  21. 0.00001f,
  22. 0.000001f,
  23. 0.0000001f,
  24. 0.00000001f,
  25. 0.000000001f,
  26. 0.0000000001f,
  27. 0.00000000001f,
  28. 0.000000000001f,
  29. 0.0000000000001f,
  30. 0.00000000000001f,
  31. 0.000000000000001f,
  32. 0.0000000000000001f};
  33. //! Convert a simple string of base 10 digits into an unsigned 32 bit integer.
  34. /** \param[in] in: The string of digits to convert. No leading chars are
  35. allowed, only digits 0 to 9. Parsing stops at the first non-digit.
  36. \param[out] out: (optional) If provided, it will be set to point at the
  37. first character not used in the calculation.
  38. \return The unsigned integer value of the digits. If the string specifies
  39. too many digits to encode in an u32 then INT_MAX will be returned.
  40. */
  41. inline u32 strtoul10(const char *in, const char **out = 0)
  42. {
  43. if (!in) {
  44. if (out)
  45. *out = in;
  46. return 0;
  47. }
  48. bool overflow = false;
  49. u32 unsignedValue = 0;
  50. while ((*in >= '0') && (*in <= '9')) {
  51. const u32 tmp = (unsignedValue * 10) + (*in - '0');
  52. if (tmp < unsignedValue) {
  53. unsignedValue = (u32)0xffffffff;
  54. overflow = true;
  55. }
  56. if (!overflow)
  57. unsignedValue = tmp;
  58. ++in;
  59. }
  60. if (out)
  61. *out = in;
  62. return unsignedValue;
  63. }
  64. //! Convert a simple string of base 10 digits into a signed 32 bit integer.
  65. /** \param[in] in: The string of digits to convert. Only a leading - or +
  66. followed by digits 0 to 9 will be considered. Parsing stops at the first
  67. non-digit.
  68. \param[out] out: (optional) If provided, it will be set to point at the
  69. first character not used in the calculation.
  70. \return The signed integer value of the digits. If the string specifies
  71. too many digits to encode in an s32 then +INT_MAX or -INT_MAX will be
  72. returned.
  73. */
  74. inline s32 strtol10(const char *in, const char **out = 0)
  75. {
  76. if (!in) {
  77. if (out)
  78. *out = in;
  79. return 0;
  80. }
  81. const bool negative = ('-' == *in);
  82. if (negative || ('+' == *in))
  83. ++in;
  84. const u32 unsignedValue = strtoul10(in, out);
  85. if (unsignedValue > (u32)INT_MAX) {
  86. if (negative)
  87. return (s32)INT_MIN;
  88. else
  89. return (s32)INT_MAX;
  90. } else {
  91. if (negative)
  92. return -((s32)unsignedValue);
  93. else
  94. return (s32)unsignedValue;
  95. }
  96. }
  97. //! Convert a hex-encoded character to an unsigned integer.
  98. /** \param[in] in The digit to convert. Only digits 0 to 9 and chars A-F,a-f
  99. will be considered.
  100. \return The unsigned integer value of the digit. 0xffffffff if the input is
  101. not hex
  102. */
  103. inline u32 ctoul16(char in)
  104. {
  105. if (in >= '0' && in <= '9')
  106. return in - '0';
  107. else if (in >= 'a' && in <= 'f')
  108. return 10u + in - 'a';
  109. else if (in >= 'A' && in <= 'F')
  110. return 10u + in - 'A';
  111. else
  112. return 0xffffffff;
  113. }
  114. //! Convert a simple string of base 16 digits into an unsigned 32 bit integer.
  115. /** \param[in] in: The string of digits to convert. No leading chars are
  116. allowed, only digits 0 to 9 and chars A-F,a-f are allowed. Parsing stops
  117. at the first illegal char.
  118. \param[out] out: (optional) If provided, it will be set to point at the
  119. first character not used in the calculation.
  120. \return The unsigned integer value of the digits. If the string specifies
  121. too many digits to encode in an u32 then INT_MAX will be returned.
  122. */
  123. inline u32 strtoul16(const char *in, const char **out = 0)
  124. {
  125. if (!in) {
  126. if (out)
  127. *out = in;
  128. return 0;
  129. }
  130. bool overflow = false;
  131. u32 unsignedValue = 0;
  132. while (true) {
  133. u32 tmp = 0;
  134. if ((*in >= '0') && (*in <= '9'))
  135. tmp = (unsignedValue << 4u) + (*in - '0');
  136. else if ((*in >= 'A') && (*in <= 'F'))
  137. tmp = (unsignedValue << 4u) + (*in - 'A') + 10;
  138. else if ((*in >= 'a') && (*in <= 'f'))
  139. tmp = (unsignedValue << 4u) + (*in - 'a') + 10;
  140. else
  141. break;
  142. if (tmp < unsignedValue) {
  143. unsignedValue = (u32)INT_MAX;
  144. overflow = true;
  145. }
  146. if (!overflow)
  147. unsignedValue = tmp;
  148. ++in;
  149. }
  150. if (out)
  151. *out = in;
  152. return unsignedValue;
  153. }
  154. //! Convert a simple string of base 8 digits into an unsigned 32 bit integer.
  155. /** \param[in] in The string of digits to convert. No leading chars are
  156. allowed, only digits 0 to 7 are allowed. Parsing stops at the first illegal
  157. char.
  158. \param[out] out (optional) If provided, it will be set to point at the
  159. first character not used in the calculation.
  160. \return The unsigned integer value of the digits. If the string specifies
  161. too many digits to encode in an u32 then INT_MAX will be returned.
  162. */
  163. inline u32 strtoul8(const char *in, const char **out = 0)
  164. {
  165. if (!in) {
  166. if (out)
  167. *out = in;
  168. return 0;
  169. }
  170. bool overflow = false;
  171. u32 unsignedValue = 0;
  172. while (true) {
  173. u32 tmp = 0;
  174. if ((*in >= '0') && (*in <= '7'))
  175. tmp = (unsignedValue << 3u) + (*in - '0');
  176. else
  177. break;
  178. if (tmp < unsignedValue) {
  179. unsignedValue = (u32)INT_MAX;
  180. overflow = true;
  181. }
  182. if (!overflow)
  183. unsignedValue = tmp;
  184. ++in;
  185. }
  186. if (out)
  187. *out = in;
  188. return unsignedValue;
  189. }
  190. //! Convert a C-style prefixed string (hex, oct, integer) into an unsigned 32 bit integer.
  191. /** \param[in] in The string of digits to convert. If string starts with 0x the
  192. hex parser is used, if only leading 0 is used, oct parser is used. In all
  193. other cases, the usual unsigned parser is used.
  194. \param[out] out (optional) If provided, it will be set to point at the
  195. first character not used in the calculation.
  196. \return The unsigned integer value of the digits. If the string specifies
  197. too many digits to encode in an u32 then INT_MAX will be returned.
  198. */
  199. inline u32 strtoul_prefix(const char *in, const char **out = 0)
  200. {
  201. if (!in) {
  202. if (out)
  203. *out = in;
  204. return 0;
  205. }
  206. if ('0' == in[0])
  207. return ('x' == in[1] ? strtoul16(in + 2, out) : strtoul8(in + 1, out));
  208. return strtoul10(in, out);
  209. }
  210. //! Converts a sequence of digits into a whole positive floating point value.
  211. /** Only digits 0 to 9 are parsed. Parsing stops at any other character,
  212. including sign characters or a decimal point.
  213. \param in: the sequence of digits to convert.
  214. \param out: (optional) will be set to point at the first non-converted
  215. character.
  216. \return The whole positive floating point representation of the digit
  217. sequence.
  218. */
  219. inline f32 strtof10(const char *in, const char **out = 0)
  220. {
  221. if (!in) {
  222. if (out)
  223. *out = in;
  224. return 0.f;
  225. }
  226. const u32 MAX_SAFE_U32_VALUE = UINT_MAX / 10 - 10;
  227. u32 intValue = 0;
  228. // Use integer arithmetic for as long as possible, for speed
  229. // and precision.
  230. while ((*in >= '0') && (*in <= '9')) {
  231. // If it looks like we're going to overflow, bail out
  232. // now and start using floating point.
  233. if (intValue >= MAX_SAFE_U32_VALUE)
  234. break;
  235. intValue = (intValue * 10) + (*in - '0');
  236. ++in;
  237. }
  238. f32 floatValue = (f32)intValue;
  239. // If there are any digits left to parse, then we need to use
  240. // floating point arithmetic from here.
  241. while ((*in >= '0') && (*in <= '9')) {
  242. floatValue = (floatValue * 10.f) + (f32)(*in - '0');
  243. ++in;
  244. if (floatValue > FLT_MAX) // Just give up.
  245. break;
  246. }
  247. if (out)
  248. *out = in;
  249. return floatValue;
  250. }
  251. //! Provides a fast function for converting a string into a float.
  252. /** This is not guaranteed to be as accurate as atof(), but is
  253. approximately 6 to 8 times as fast.
  254. \param[in] in The string to convert.
  255. \param[out] result The resultant float will be written here.
  256. \return Pointer to the first character in the string that wasn't used
  257. to create the float value.
  258. */
  259. inline const char *fast_atof_move(const char *in, f32 &result)
  260. {
  261. // Please run the regression test when making any modifications to this function.
  262. result = 0.f;
  263. if (!in)
  264. return 0;
  265. const bool negative = ('-' == *in);
  266. if (negative || ('+' == *in))
  267. ++in;
  268. f32 value = strtof10(in, &in);
  269. if (*in == '.') {
  270. const char *afterDecimal = ++in;
  271. const f32 decimal = strtof10(in, &afterDecimal);
  272. const size_t numDecimals = afterDecimal - in;
  273. if (numDecimals < IRR_ATOF_TABLE_SIZE) {
  274. value += decimal * fast_atof_table[numDecimals];
  275. } else {
  276. value += decimal * std::pow(10.f, -(float)numDecimals);
  277. }
  278. in = afterDecimal;
  279. }
  280. if ('e' == *in || 'E' == *in) {
  281. ++in;
  282. // Assume that the exponent is a whole number.
  283. // strtol10() will deal with both + and - signs,
  284. // but calculate as f32 to prevent overflow at FLT_MAX
  285. // Using pow with float cast instead of powf as otherwise accuracy decreases.
  286. value *= std::pow(10.f, (f32)strtol10(in, &in));
  287. }
  288. result = negative ? -value : value;
  289. return in;
  290. }
  291. //! Convert a string to a floating point number
  292. /** \param floatAsString The string to convert.
  293. \param out Optional pointer to the first character in the string that
  294. wasn't used to create the float value.
  295. \result Float value parsed from the input string
  296. */
  297. inline float fast_atof(const char *floatAsString, const char **out = 0)
  298. {
  299. float ret;
  300. if (out)
  301. *out = fast_atof_move(floatAsString, ret);
  302. else
  303. fast_atof_move(floatAsString, ret);
  304. return ret;
  305. }
  306. } // end namespace core
  307. } // end namespace irr