fast_atof.h 12 KB

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