vrshoot

view libs/assimp/fast_atof.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
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
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 // ------------------------------------------------------------------------------------
15 #ifndef __FAST_A_TO_F_H_INCLUDED__
16 #define __FAST_A_TO_F_H_INCLUDED__
18 #include <math.h>
20 namespace Assimp
21 {
23 const float fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
24 0.f,
25 0.1f,
26 0.01f,
27 0.001f,
28 0.0001f,
29 0.00001f,
30 0.000001f,
31 0.0000001f,
32 0.00000001f,
33 0.000000001f,
34 0.0000000001f,
35 0.00000000001f,
36 0.000000000001f,
37 0.0000000000001f,
38 0.00000000000001f,
39 0.000000000000001f
40 };
43 // ------------------------------------------------------------------------------------
44 // Convert a string in decimal format to a number
45 // ------------------------------------------------------------------------------------
46 inline unsigned int strtoul10( const char* in, const char** out=0)
47 {
48 unsigned int value = 0;
50 bool running = true;
51 while ( running )
52 {
53 if ( *in < '0' || *in > '9' )
54 break;
56 value = ( value * 10 ) + ( *in - '0' );
57 ++in;
58 }
59 if (out)*out = in;
60 return value;
61 }
63 // ------------------------------------------------------------------------------------
64 // Convert a string in octal format to a number
65 // ------------------------------------------------------------------------------------
66 inline unsigned int strtoul8( const char* in, const char** out=0)
67 {
68 unsigned int value = 0;
70 bool running = true;
71 while ( running )
72 {
73 if ( *in < '0' || *in > '7' )
74 break;
76 value = ( value << 3 ) + ( *in - '0' );
77 ++in;
78 }
79 if (out)*out = in;
80 return value;
81 }
83 // ------------------------------------------------------------------------------------
84 // Convert a string in hex format to a number
85 // ------------------------------------------------------------------------------------
86 inline unsigned int strtoul16( const char* in, const char** out=0)
87 {
88 unsigned int value = 0;
90 bool running = true;
91 while ( running )
92 {
93 if ( *in >= '0' && *in <= '9' )
94 {
95 value = ( value << 4u ) + ( *in - '0' );
96 }
97 else if (*in >= 'A' && *in <= 'F')
98 {
99 value = ( value << 4u ) + ( *in - 'A' ) + 10;
100 }
101 else if (*in >= 'a' && *in <= 'f')
102 {
103 value = ( value << 4u ) + ( *in - 'a' ) + 10;
104 }
105 else break;
106 ++in;
107 }
108 if (out)*out = in;
109 return value;
110 }
112 // ------------------------------------------------------------------------------------
113 // Convert just one hex digit
114 // Return value is UINT_MAX if the input character is not a hex digit.
115 // ------------------------------------------------------------------------------------
116 inline unsigned int HexDigitToDecimal(char in)
117 {
118 unsigned int out = UINT_MAX;
119 if (in >= '0' && in <= '9')
120 out = in - '0';
122 else if (in >= 'a' && in <= 'f')
123 out = 10u + in - 'a';
125 else if (in >= 'A' && in <= 'F')
126 out = 10u + in - 'A';
128 // return value is UINT_MAX if the input is not a hex digit
129 return out;
130 }
132 // ------------------------------------------------------------------------------------
133 // Convert a hex-encoded octet (2 characters, i.e. df or 1a).
134 // ------------------------------------------------------------------------------------
135 inline uint8_t HexOctetToDecimal(const char* in)
136 {
137 return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
138 }
141 // ------------------------------------------------------------------------------------
142 // signed variant of strtoul10
143 // ------------------------------------------------------------------------------------
144 inline int strtol10( const char* in, const char** out=0)
145 {
146 bool inv = (*in=='-');
147 if (inv || *in=='+')
148 ++in;
150 int value = strtoul10(in,out);
151 if (inv) {
152 value = -value;
153 }
154 return value;
155 }
157 // ------------------------------------------------------------------------------------
158 // Parse a C++-like integer literal - hex and oct prefixes.
159 // 0xNNNN - hex
160 // 0NNN - oct
161 // NNN - dec
162 // ------------------------------------------------------------------------------------
163 inline unsigned int strtoul_cppstyle( const char* in, const char** out=0)
164 {
165 if ('0' == in[0])
166 {
167 return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
168 }
169 return strtoul10(in, out);
170 }
172 // ------------------------------------------------------------------------------------
173 // Special version of the function, providing higher accuracy and safety
174 // It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
175 // ------------------------------------------------------------------------------------
176 inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0)
177 {
178 unsigned int cur = 0;
179 uint64_t value = 0;
181 bool running = true;
182 while ( running )
183 {
184 if ( *in < '0' || *in > '9' )
185 break;
187 const uint64_t new_value = ( value * 10 ) + ( *in - '0' );
189 if (new_value < value) /* numeric overflow, we rely on you */
190 return value;
192 value = new_value;
194 ++in;
195 ++cur;
197 if (max_inout && *max_inout == cur) {
199 if (out) { /* skip to end */
200 while (*in >= '0' && *in <= '9')
201 ++in;
202 *out = in;
203 }
205 return value;
206 }
207 }
208 if (out)
209 *out = in;
211 if (max_inout)
212 *max_inout = cur;
214 return value;
215 }
217 // Number of relevant decimals for floating-point parsing.
218 #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>
226 inline const char* fast_atoreal_move( const char* c, Real& out)
227 {
228 Real f;
230 bool inv = (*c=='-');
231 if (inv || *c=='+') {
232 ++c;
233 }
235 f = static_cast<Real>( strtoul10_64 ( c, &c) );
236 if (*c == '.' || (c[0] == ',' && c[1] >= '0' && c[1] <= '9')) // allow for commas, too
237 {
238 ++c;
240 // NOTE: The original implementation is highly inaccurate here. The precision of a single
241 // IEEE 754 float is not high enough, everything behind the 6th digit tends to be more
242 // inaccurate than it would need to be. Casting to double seems to solve the problem.
243 // strtol_64 is used to prevent integer overflow.
245 // Another fix: this tends to become 0 for long numbers if we don't limit the maximum
246 // number of digits to be read. AI_FAST_ATOF_RELAVANT_DECIMALS can be a value between
247 // 1 and 15.
248 unsigned int diff = AI_FAST_ATOF_RELAVANT_DECIMALS;
249 double pl = static_cast<double>( strtoul10_64 ( c, &c, &diff ));
251 pl *= fast_atof_table[diff];
252 f += static_cast<Real>( pl );
253 }
255 // A major 'E' must be allowed. Necessary for proper reading of some DXF files.
256 // Thanks to Zhao Lei to point out that this if() must be outside the if (*c == '.' ..)
257 if (*c == 'e' || *c == 'E') {
259 ++c;
260 const bool einv = (*c=='-');
261 if (einv || *c=='+') {
262 ++c;
263 }
265 // The reason float constants are used here is that we've seen cases where compilers
266 // would perform such casts on compile-time constants at runtime, which would be
267 // bad considering how frequently fast_atoreal_move<float> is called in Assimp.
268 Real exp = static_cast<Real>( strtoul10_64(c, &c) );
269 if (einv) {
270 exp = -exp;
271 }
272 f *= pow(static_cast<Real>(10.0f), exp);
273 }
275 if (inv) {
276 f = -f;
277 }
278 out = f;
279 return c;
280 }
282 // ------------------------------------------------------------------------------------
283 // The same but more human.
284 inline float fast_atof(const char* c)
285 {
286 float ret;
287 fast_atoreal_move<float>(c, ret);
288 return ret;
289 }
292 inline float fast_atof( const char* c, const char** cout)
293 {
294 float ret;
295 *cout = fast_atoreal_move<float>(c, ret);
297 return ret;
298 }
300 inline float fast_atof( const char** inout)
301 {
302 float ret;
303 *inout = fast_atoreal_move<float>(*inout, ret);
305 return ret;
306 }
309 inline double fast_atod(const char* c)
310 {
311 double ret;
312 fast_atoreal_move<double>(c, ret);
313 return ret;
314 }
317 inline double fast_atod( const char* c, const char** cout)
318 {
319 double ret;
320 *cout = fast_atoreal_move<double>(c, ret);
322 return ret;
323 }
325 inline double fast_atod( const char** inout)
326 {
327 double ret;
328 *inout = fast_atoreal_move<double>(*inout, ret);
330 return ret;
331 }
333 } // end of namespace Assimp
335 #endif