fast_atof.h 11 KB

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