fast_atof.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright (C) 2002-2007 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. // ------------------------------------------------------------------------------------
  5. // Original description: (Schrompf)
  6. // Adapted to the ASSIMP library because the builtin atof indeed takes AGES to parse a
  7. // float inside a large string. Before parsing, it does a strlen on the given point.
  8. // Changes:
  9. // 22nd October 08 (Aramis_acg): Added temporary cast to double, added strtoul10_64
  10. // to ensure long numbers are handled correctly
  11. // ------------------------------------------------------------------------------------
  12. #ifndef __FAST_A_TO_F_H_INCLUDED__
  13. #define __FAST_A_TO_F_H_INCLUDED__
  14. #include <cmath>
  15. #include <limits>
  16. #include <stdint.h>
  17. #include <stdexcept>
  18. #include <assimp/defs.h>
  19. #include "StringComparison.h"
  20. #ifdef _MSC_VER
  21. # include <stdint.h>
  22. #else
  23. # include <assimp/Compiler/pstdint.h>
  24. #endif
  25. namespace Assimp
  26. {
  27. const double fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
  28. 0.0,
  29. 0.1,
  30. 0.01,
  31. 0.001,
  32. 0.0001,
  33. 0.00001,
  34. 0.000001,
  35. 0.0000001,
  36. 0.00000001,
  37. 0.000000001,
  38. 0.0000000001,
  39. 0.00000000001,
  40. 0.000000000001,
  41. 0.0000000000001,
  42. 0.00000000000001,
  43. 0.000000000000001
  44. };
  45. // ------------------------------------------------------------------------------------
  46. // Convert a string in decimal format to a number
  47. // ------------------------------------------------------------------------------------
  48. inline unsigned int strtoul10( const char* in, const char** out=0)
  49. {
  50. unsigned int value = 0;
  51. bool running = true;
  52. while ( running )
  53. {
  54. if ( *in < '0' || *in > '9' )
  55. break;
  56. value = ( value * 10 ) + ( *in - '0' );
  57. ++in;
  58. }
  59. if (out)*out = in;
  60. return value;
  61. }
  62. // ------------------------------------------------------------------------------------
  63. // Convert a string in octal format to a number
  64. // ------------------------------------------------------------------------------------
  65. inline unsigned int strtoul8( const char* in, const char** out=0)
  66. {
  67. unsigned int value = 0;
  68. bool running = true;
  69. while ( running )
  70. {
  71. if ( *in < '0' || *in > '7' )
  72. break;
  73. value = ( value << 3 ) + ( *in - '0' );
  74. ++in;
  75. }
  76. if (out)*out = in;
  77. return value;
  78. }
  79. // ------------------------------------------------------------------------------------
  80. // Convert a string in hex format to a number
  81. // ------------------------------------------------------------------------------------
  82. inline unsigned int strtoul16( const char* in, const char** out=0)
  83. {
  84. unsigned int value = 0;
  85. bool running = true;
  86. while ( running )
  87. {
  88. if ( *in >= '0' && *in <= '9' )
  89. {
  90. value = ( value << 4u ) + ( *in - '0' );
  91. }
  92. else if (*in >= 'A' && *in <= 'F')
  93. {
  94. value = ( value << 4u ) + ( *in - 'A' ) + 10;
  95. }
  96. else if (*in >= 'a' && *in <= 'f')
  97. {
  98. value = ( value << 4u ) + ( *in - 'a' ) + 10;
  99. }
  100. else break;
  101. ++in;
  102. }
  103. if (out)*out = in;
  104. return value;
  105. }
  106. // ------------------------------------------------------------------------------------
  107. // Convert just one hex digit
  108. // Return value is UINT_MAX if the input character is not a hex digit.
  109. // ------------------------------------------------------------------------------------
  110. inline unsigned int HexDigitToDecimal(char in)
  111. {
  112. unsigned int out = UINT_MAX;
  113. if (in >= '0' && in <= '9')
  114. out = in - '0';
  115. else if (in >= 'a' && in <= 'f')
  116. out = 10u + in - 'a';
  117. else if (in >= 'A' && in <= 'F')
  118. out = 10u + in - 'A';
  119. // return value is UINT_MAX if the input is not a hex digit
  120. return out;
  121. }
  122. // ------------------------------------------------------------------------------------
  123. // Convert a hex-encoded octet (2 characters, i.e. df or 1a).
  124. // ------------------------------------------------------------------------------------
  125. inline uint8_t HexOctetToDecimal(const char* in)
  126. {
  127. return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
  128. }
  129. // ------------------------------------------------------------------------------------
  130. // signed variant of strtoul10
  131. // ------------------------------------------------------------------------------------
  132. inline int strtol10( const char* in, const char** out=0)
  133. {
  134. bool inv = (*in=='-');
  135. if (inv || *in=='+')
  136. ++in;
  137. int value = strtoul10(in,out);
  138. if (inv) {
  139. value = -value;
  140. }
  141. return value;
  142. }
  143. // ------------------------------------------------------------------------------------
  144. // Parse a C++-like integer literal - hex and oct prefixes.
  145. // 0xNNNN - hex
  146. // 0NNN - oct
  147. // NNN - dec
  148. // ------------------------------------------------------------------------------------
  149. inline unsigned int strtoul_cppstyle( const char* in, const char** out=0)
  150. {
  151. if ('0' == in[0])
  152. {
  153. return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
  154. }
  155. return strtoul10(in, out);
  156. }
  157. // ------------------------------------------------------------------------------------
  158. // Special version of the function, providing higher accuracy and safety
  159. // It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
  160. // ------------------------------------------------------------------------------------
  161. inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0)
  162. {
  163. unsigned int cur = 0;
  164. uint64_t value = 0;
  165. if ( *in < '0' || *in > '9' )
  166. throw std::invalid_argument(std::string("The string \"") + in + "\" cannot be converted into a value.");
  167. bool running = true;
  168. while ( running )
  169. {
  170. if ( *in < '0' || *in > '9' )
  171. break;
  172. const uint64_t new_value = ( value * 10 ) + ( *in - '0' );
  173. if (new_value < value) /* numeric overflow, we rely on you */
  174. throw std::overflow_error(std::string("Converting the string \"") + in + "\" into a value resulted in overflow.");
  175. value = new_value;
  176. ++in;
  177. ++cur;
  178. if (max_inout && *max_inout == cur) {
  179. if (out) { /* skip to end */
  180. while (*in >= '0' && *in <= '9')
  181. ++in;
  182. *out = in;
  183. }
  184. return value;
  185. }
  186. }
  187. if (out)
  188. *out = in;
  189. if (max_inout)
  190. *max_inout = cur;
  191. return value;
  192. }
  193. // ------------------------------------------------------------------------------------
  194. // signed variant of strtoul10_64
  195. // ------------------------------------------------------------------------------------
  196. inline int64_t strtol10_64(const char* in, const char** out = 0, unsigned int* max_inout = 0)
  197. {
  198. bool inv = (*in == '-');
  199. if (inv || *in == '+')
  200. ++in;
  201. int64_t value = strtoul10_64(in, out, max_inout);
  202. if (inv) {
  203. value = -value;
  204. }
  205. return value;
  206. }
  207. // Number of relevant decimals for floating-point parsing.
  208. #define AI_FAST_ATOF_RELAVANT_DECIMALS 15
  209. // ------------------------------------------------------------------------------------
  210. //! Provides a fast function for converting a string into a float,
  211. //! about 6 times faster than atof in win32.
  212. // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
  213. // ------------------------------------------------------------------------------------
  214. template <typename Real>
  215. inline const char* fast_atoreal_move(const char* c, Real& out, bool check_comma = true)
  216. {
  217. Real f = 0;
  218. bool inv = (*c == '-');
  219. if (inv || *c == '+') {
  220. ++c;
  221. }
  222. if ((c[0] == 'N' || c[0] == 'n') && ASSIMP_strincmp(c, "nan", 3) == 0)
  223. {
  224. out = std::numeric_limits<Real>::quiet_NaN();
  225. c += 3;
  226. return c;
  227. }
  228. if ((c[0] == 'I' || c[0] == 'i') && ASSIMP_strincmp(c, "inf", 3) == 0)
  229. {
  230. out = std::numeric_limits<Real>::infinity();
  231. if (inv) {
  232. out = -out;
  233. }
  234. c += 3;
  235. if ((c[0] == 'I' || c[0] == 'i') && ASSIMP_strincmp(c, "inity", 5) == 0)
  236. {
  237. c += 5;
  238. }
  239. return c;
  240. }
  241. if (!(c[0] >= '0' && c[0] <= '9') &&
  242. !((c[0] == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9'))
  243. {
  244. throw std::invalid_argument("Cannot parse string "
  245. "as real number: does not start with digit "
  246. "or decimal point followed by digit.");
  247. }
  248. if (*c != '.' && (! check_comma || c[0] != ','))
  249. {
  250. f = static_cast<Real>( strtoul10_64 ( c, &c) );
  251. }
  252. if ((*c == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9')
  253. {
  254. ++c;
  255. // NOTE: The original implementation is highly inaccurate here. The precision of a single
  256. // IEEE 754 float is not high enough, everything behind the 6th digit tends to be more
  257. // inaccurate than it would need to be. Casting to double seems to solve the problem.
  258. // strtol_64 is used to prevent integer overflow.
  259. // Another fix: this tends to become 0 for long numbers if we don't limit the maximum
  260. // number of digits to be read. AI_FAST_ATOF_RELAVANT_DECIMALS can be a value between
  261. // 1 and 15.
  262. unsigned int diff = AI_FAST_ATOF_RELAVANT_DECIMALS;
  263. double pl = static_cast<double>( strtoul10_64 ( c, &c, &diff ));
  264. pl *= fast_atof_table[diff];
  265. f += static_cast<Real>( pl );
  266. }
  267. // For backwards compatibility: eat trailing dots, but not trailing commas.
  268. else if (*c == '.') {
  269. ++c;
  270. }
  271. // A major 'E' must be allowed. Necessary for proper reading of some DXF files.
  272. // Thanks to Zhao Lei to point out that this if() must be outside the if (*c == '.' ..)
  273. if (*c == 'e' || *c == 'E') {
  274. ++c;
  275. const bool einv = (*c=='-');
  276. if (einv || *c=='+') {
  277. ++c;
  278. }
  279. // The reason float constants are used here is that we've seen cases where compilers
  280. // would perform such casts on compile-time constants at runtime, which would be
  281. // bad considering how frequently fast_atoreal_move<float> is called in Assimp.
  282. Real exp = static_cast<Real>( strtoul10_64(c, &c) );
  283. if (einv) {
  284. exp = -exp;
  285. }
  286. f *= std::pow(static_cast<Real>(10.0), exp);
  287. }
  288. if (inv) {
  289. f = -f;
  290. }
  291. out = f;
  292. return c;
  293. }
  294. // ------------------------------------------------------------------------------------
  295. // The same but more human.
  296. inline ai_real fast_atof(const char* c)
  297. {
  298. ai_real ret;
  299. fast_atoreal_move<ai_real>(c, ret);
  300. return ret;
  301. }
  302. inline ai_real fast_atof( const char* c, const char** cout)
  303. {
  304. ai_real ret;
  305. *cout = fast_atoreal_move<ai_real>(c, ret);
  306. return ret;
  307. }
  308. inline ai_real fast_atof( const char** inout)
  309. {
  310. ai_real ret;
  311. *inout = fast_atoreal_move<ai_real>(*inout, ret);
  312. return ret;
  313. }
  314. } // end of namespace Assimp
  315. #endif