fast_atof.h 12 KB

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