fast_atof.h 11 KB

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