fast_atof.h 9.2 KB

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