fast_atof.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <math.h>
  15. namespace Assimp
  16. {
  17. const float fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
  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. };
  35. // ------------------------------------------------------------------------------------
  36. // Convert a string in decimal format to a number
  37. // ------------------------------------------------------------------------------------
  38. inline unsigned int strtoul10( const char* in, const char** out=0)
  39. {
  40. unsigned int value = 0;
  41. bool running = true;
  42. while ( running )
  43. {
  44. if ( *in < '0' || *in > '9' )
  45. break;
  46. value = ( value * 10 ) + ( *in - '0' );
  47. ++in;
  48. }
  49. if (out)*out = in;
  50. return value;
  51. }
  52. // ------------------------------------------------------------------------------------
  53. // Convert a string in octal format to a number
  54. // ------------------------------------------------------------------------------------
  55. inline unsigned int strtoul8( const char* in, const char** out=0)
  56. {
  57. unsigned int value = 0;
  58. bool running = true;
  59. while ( running )
  60. {
  61. if ( *in < '0' || *in > '7' )
  62. break;
  63. value = ( value << 3 ) + ( *in - '0' );
  64. ++in;
  65. }
  66. if (out)*out = in;
  67. return value;
  68. }
  69. // ------------------------------------------------------------------------------------
  70. // Convert a string in hex format to a number
  71. // ------------------------------------------------------------------------------------
  72. inline unsigned int strtoul16( const char* in, const char** out=0)
  73. {
  74. unsigned int value = 0;
  75. bool running = true;
  76. while ( running )
  77. {
  78. if ( *in >= '0' && *in <= '9' )
  79. {
  80. value = ( value << 4u ) + ( *in - '0' );
  81. }
  82. else if (*in >= 'A' && *in <= 'F')
  83. {
  84. value = ( value << 4u ) + ( *in - 'A' ) + 10;
  85. }
  86. else if (*in >= 'a' && *in <= 'f')
  87. {
  88. value = ( value << 4u ) + ( *in - 'a' ) + 10;
  89. }
  90. else break;
  91. ++in;
  92. }
  93. if (out)*out = in;
  94. return value;
  95. }
  96. // ------------------------------------------------------------------------------------
  97. // Convert just one hex digit
  98. // Return value is UINT_MAX if the input character is not a hex digit.
  99. // ------------------------------------------------------------------------------------
  100. inline unsigned int HexDigitToDecimal(char in)
  101. {
  102. unsigned int out = UINT_MAX;
  103. if (in >= '0' && in <= '9')
  104. out = in - '0';
  105. else if (in >= 'a' && in <= 'f')
  106. out = 10u + in - 'a';
  107. else if (in >= 'A' && in <= 'F')
  108. out = 10u + in - 'A';
  109. // return value is UINT_MAX if the input is not a hex digit
  110. return out;
  111. }
  112. // ------------------------------------------------------------------------------------
  113. // Convert a hex-encoded octet (2 characters, i.e. df or 1a).
  114. // ------------------------------------------------------------------------------------
  115. inline uint8_t HexOctetToDecimal(const char* in)
  116. {
  117. return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
  118. }
  119. // ------------------------------------------------------------------------------------
  120. // signed variant of strtoul10
  121. // ------------------------------------------------------------------------------------
  122. inline int strtol10( const char* in, const char** out=0)
  123. {
  124. bool inv = (*in=='-');
  125. if (inv || *in=='+')
  126. ++in;
  127. int value = strtoul10(in,out);
  128. if (inv) {
  129. value = -value;
  130. }
  131. return value;
  132. }
  133. // ------------------------------------------------------------------------------------
  134. // Parse a C++-like integer literal - hex and oct prefixes.
  135. // 0xNNNN - hex
  136. // 0NNN - oct
  137. // NNN - dec
  138. // ------------------------------------------------------------------------------------
  139. inline unsigned int strtoul_cppstyle( const char* in, const char** out=0)
  140. {
  141. if ('0' == in[0])
  142. {
  143. return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
  144. }
  145. return strtoul10(in, out);
  146. }
  147. // ------------------------------------------------------------------------------------
  148. // Special version of the function, providing higher accuracy and safety
  149. // It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
  150. // ------------------------------------------------------------------------------------
  151. inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0)
  152. {
  153. unsigned int cur = 0;
  154. uint64_t value = 0;
  155. bool running = true;
  156. while ( running )
  157. {
  158. if ( *in < '0' || *in > '9' )
  159. break;
  160. const uint64_t new_value = ( value * 10 ) + ( *in - '0' );
  161. if (new_value < value) /* numeric overflow, we rely on you */
  162. return value;
  163. value = new_value;
  164. ++in;
  165. ++cur;
  166. if (max_inout && *max_inout == cur) {
  167. if (out) { /* skip to end */
  168. while (*in >= '0' && *in <= '9')
  169. ++in;
  170. *out = in;
  171. }
  172. return value;
  173. }
  174. }
  175. if (out)
  176. *out = in;
  177. if (max_inout)
  178. *max_inout = cur;
  179. return value;
  180. }
  181. // Number of relevant decimals for floating-point parsing.
  182. #define AI_FAST_ATOF_RELAVANT_DECIMALS 15
  183. // ------------------------------------------------------------------------------------
  184. //! Provides a fast function for converting a string into a float,
  185. //! about 6 times faster than atof in win32.
  186. // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
  187. // ------------------------------------------------------------------------------------
  188. template <typename Real>
  189. inline const char* fast_atoreal_move( const char* c, Real& out)
  190. {
  191. Real f;
  192. bool inv = (*c=='-');
  193. if (inv || *c=='+') {
  194. ++c;
  195. }
  196. f = static_cast<Real>( strtoul10_64 ( c, &c) );
  197. if (*c == '.' || (c[0] == ',' && c[1] >= '0' && c[1] <= '9')) // allow for commas, too
  198. {
  199. ++c;
  200. // NOTE: The original implementation is highly inaccurate here. The precision of a single
  201. // IEEE 754 float is not high enough, everything behind the 6th digit tends to be more
  202. // inaccurate than it would need to be. Casting to double seems to solve the problem.
  203. // strtol_64 is used to prevent integer overflow.
  204. // Another fix: this tends to become 0 for long numbers if we don't limit the maximum
  205. // number of digits to be read. AI_FAST_ATOF_RELAVANT_DECIMALS can be a value between
  206. // 1 and 15.
  207. unsigned int diff = AI_FAST_ATOF_RELAVANT_DECIMALS;
  208. double pl = static_cast<double>( strtoul10_64 ( c, &c, &diff ));
  209. pl *= fast_atof_table[diff];
  210. f += static_cast<Real>( pl );
  211. }
  212. // A major 'E' must be allowed. Necessary for proper reading of some DXF files.
  213. // Thanks to Zhao Lei to point out that this if() must be outside the if (*c == '.' ..)
  214. if (*c == 'e' || *c == 'E') {
  215. ++c;
  216. const bool einv = (*c=='-');
  217. if (einv || *c=='+') {
  218. ++c;
  219. }
  220. // The reason float constants are used here is that we've seen cases where compilers
  221. // would perform such casts on compile-time constants at runtime, which would be
  222. // bad considering how frequently fast_atoreal_move<float> is called in Assimp.
  223. Real exp = static_cast<Real>( strtoul10_64(c, &c) );
  224. if (einv) {
  225. exp = -exp;
  226. }
  227. f *= pow(static_cast<Real>(10.0f), exp);
  228. }
  229. if (inv) {
  230. f = -f;
  231. }
  232. out = f;
  233. return c;
  234. }
  235. // ------------------------------------------------------------------------------------
  236. // The same but more human.
  237. inline float fast_atof(const char* c)
  238. {
  239. float ret;
  240. fast_atoreal_move<float>(c, ret);
  241. return ret;
  242. }
  243. inline float fast_atof( const char* c, const char** cout)
  244. {
  245. float ret;
  246. *cout = fast_atoreal_move<float>(c, ret);
  247. return ret;
  248. }
  249. inline float fast_atof( const char** inout)
  250. {
  251. float ret;
  252. *inout = fast_atoreal_move<float>(*inout, ret);
  253. return ret;
  254. }
  255. inline double fast_atod(const char* c)
  256. {
  257. double ret;
  258. fast_atoreal_move<double>(c, ret);
  259. return ret;
  260. }
  261. inline double fast_atod( const char* c, const char** cout)
  262. {
  263. double ret;
  264. *cout = fast_atoreal_move<double>(c, ret);
  265. return ret;
  266. }
  267. inline double fast_atod( const char** inout)
  268. {
  269. double ret;
  270. *inout = fast_atoreal_move<double>(*inout, ret);
  271. return ret;
  272. }
  273. } // end of namespace Assimp
  274. #endif