fast_atof.h 9.0 KB

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