fast_atof.h 9.8 KB

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