aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/include/fast_atof.h
diff options
context:
space:
mode:
authorDavid Walter Seikel2013-01-13 18:54:10 +1000
committerDavid Walter Seikel2013-01-13 18:54:10 +1000
commit959831f4ef5a3e797f576c3de08cd65032c997ad (patch)
treee7351908be5995f0b325b2ebeaa02d5a34b82583 /libraries/irrlicht-1.8/include/fast_atof.h
parentAdd info about changes to Irrlicht. (diff)
downloadSledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.zip
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.gz
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.bz2
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.xz
Remove damned ancient DOS line endings from Irrlicht. Hopefully I did not go overboard.
Diffstat (limited to 'libraries/irrlicht-1.8/include/fast_atof.h')
-rw-r--r--libraries/irrlicht-1.8/include/fast_atof.h728
1 files changed, 364 insertions, 364 deletions
diff --git a/libraries/irrlicht-1.8/include/fast_atof.h b/libraries/irrlicht-1.8/include/fast_atof.h
index 7d4917a..d11f7b4 100644
--- a/libraries/irrlicht-1.8/include/fast_atof.h
+++ b/libraries/irrlicht-1.8/include/fast_atof.h
@@ -1,364 +1,364 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt 1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine" and the "irrXML" project. 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 3// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
4 4
5#ifndef __FAST_ATOF_H_INCLUDED__ 5#ifndef __FAST_ATOF_H_INCLUDED__
6#define __FAST_ATOF_H_INCLUDED__ 6#define __FAST_ATOF_H_INCLUDED__
7 7
8#include "irrMath.h" 8#include "irrMath.h"
9#include "irrString.h" 9#include "irrString.h"
10 10
11namespace irr 11namespace irr
12{ 12{
13namespace core 13namespace core
14{ 14{
15 //! Selection of characters which count as decimal point in fast_atof 15 //! Selection of characters which count as decimal point in fast_atof
16 // TODO: This should probably also be used in irr::core::string, but the float-to-string code 16 // TODO: This should probably also be used in irr::core::string, but the float-to-string code
17 // used there has to be rewritten first. 17 // used there has to be rewritten first.
18 IRRLICHT_API extern irr::core::stringc LOCALE_DECIMAL_POINTS; 18 IRRLICHT_API extern irr::core::stringc LOCALE_DECIMAL_POINTS;
19 19
20// we write [17] here instead of [] to work around a swig bug 20// we write [17] here instead of [] to work around a swig bug
21const float fast_atof_table[17] = { 21const float fast_atof_table[17] = {
22 0.f, 22 0.f,
23 0.1f, 23 0.1f,
24 0.01f, 24 0.01f,
25 0.001f, 25 0.001f,
26 0.0001f, 26 0.0001f,
27 0.00001f, 27 0.00001f,
28 0.000001f, 28 0.000001f,
29 0.0000001f, 29 0.0000001f,
30 0.00000001f, 30 0.00000001f,
31 0.000000001f, 31 0.000000001f,
32 0.0000000001f, 32 0.0000000001f,
33 0.00000000001f, 33 0.00000000001f,
34 0.000000000001f, 34 0.000000000001f,
35 0.0000000000001f, 35 0.0000000000001f,
36 0.00000000000001f, 36 0.00000000000001f,
37 0.000000000000001f, 37 0.000000000000001f,
38 0.0000000000000001f 38 0.0000000000000001f
39}; 39};
40 40
41//! Convert a simple string of base 10 digits into an unsigned 32 bit integer. 41//! Convert a simple string of base 10 digits into an unsigned 32 bit integer.
42/** \param[in] in: The string of digits to convert. No leading chars are 42/** \param[in] in: The string of digits to convert. No leading chars are
43 allowed, only digits 0 to 9. Parsing stops at the first non-digit. 43 allowed, only digits 0 to 9. Parsing stops at the first non-digit.
44 \param[out] out: (optional) If provided, it will be set to point at the 44 \param[out] out: (optional) If provided, it will be set to point at the
45 first character not used in the calculation. 45 first character not used in the calculation.
46 \return The unsigned integer value of the digits. If the string specifies 46 \return The unsigned integer value of the digits. If the string specifies
47 too many digits to encode in an u32 then INT_MAX will be returned. 47 too many digits to encode in an u32 then INT_MAX will be returned.
48*/ 48*/
49inline u32 strtoul10(const char* in, const char** out=0) 49inline u32 strtoul10(const char* in, const char** out=0)
50{ 50{
51 if (!in) 51 if (!in)
52 { 52 {
53 if (out) 53 if (out)
54 *out = in; 54 *out = in;
55 return 0; 55 return 0;
56 } 56 }
57 57
58 bool overflow=false; 58 bool overflow=false;
59 u32 unsignedValue = 0; 59 u32 unsignedValue = 0;
60 while ( ( *in >= '0') && ( *in <= '9' )) 60 while ( ( *in >= '0') && ( *in <= '9' ))
61 { 61 {
62 const u32 tmp = ( unsignedValue * 10 ) + ( *in - '0' ); 62 const u32 tmp = ( unsignedValue * 10 ) + ( *in - '0' );
63 if (tmp<unsignedValue) 63 if (tmp<unsignedValue)
64 { 64 {
65 unsignedValue=(u32)0xffffffff; 65 unsignedValue=(u32)0xffffffff;
66 overflow=true; 66 overflow=true;
67 } 67 }
68 if (!overflow) 68 if (!overflow)
69 unsignedValue = tmp; 69 unsignedValue = tmp;
70 ++in; 70 ++in;
71 } 71 }
72 72
73 if (out) 73 if (out)
74 *out = in; 74 *out = in;
75 75
76 return unsignedValue; 76 return unsignedValue;
77} 77}
78 78
79//! Convert a simple string of base 10 digits into a signed 32 bit integer. 79//! Convert a simple string of base 10 digits into a signed 32 bit integer.
80/** \param[in] in: The string of digits to convert. Only a leading - or + 80/** \param[in] in: The string of digits to convert. Only a leading - or +
81 followed by digits 0 to 9 will be considered. Parsing stops at the first 81 followed by digits 0 to 9 will be considered. Parsing stops at the first
82 non-digit. 82 non-digit.
83 \param[out] out: (optional) If provided, it will be set to point at the 83 \param[out] out: (optional) If provided, it will be set to point at the
84 first character not used in the calculation. 84 first character not used in the calculation.
85 \return The signed integer value of the digits. If the string specifies 85 \return The signed integer value of the digits. If the string specifies
86 too many digits to encode in an s32 then +INT_MAX or -INT_MAX will be 86 too many digits to encode in an s32 then +INT_MAX or -INT_MAX will be
87 returned. 87 returned.
88*/ 88*/
89inline s32 strtol10(const char* in, const char** out=0) 89inline s32 strtol10(const char* in, const char** out=0)
90{ 90{
91 if (!in) 91 if (!in)
92 { 92 {
93 if (out) 93 if (out)
94 *out = in; 94 *out = in;
95 return 0; 95 return 0;
96 } 96 }
97 97
98 const bool negative = ('-' == *in); 98 const bool negative = ('-' == *in);
99 if (negative || ('+' == *in)) 99 if (negative || ('+' == *in))
100 ++in; 100 ++in;
101 101
102 const u32 unsignedValue = strtoul10(in,out); 102 const u32 unsignedValue = strtoul10(in,out);
103 if (unsignedValue > (u32)INT_MAX) 103 if (unsignedValue > (u32)INT_MAX)
104 { 104 {
105 if (negative) 105 if (negative)
106 return (s32)INT_MIN; 106 return (s32)INT_MIN;
107 else 107 else
108 return (s32)INT_MAX; 108 return (s32)INT_MAX;
109 } 109 }
110 else 110 else
111 { 111 {
112 if (negative) 112 if (negative)
113 return -((s32)unsignedValue); 113 return -((s32)unsignedValue);
114 else 114 else
115 return (s32)unsignedValue; 115 return (s32)unsignedValue;
116 } 116 }
117} 117}
118 118
119//! Convert a hex-encoded character to an unsigned integer. 119//! Convert a hex-encoded character to an unsigned integer.
120/** \param[in] in The digit to convert. Only digits 0 to 9 and chars A-F,a-f 120/** \param[in] in The digit to convert. Only digits 0 to 9 and chars A-F,a-f
121 will be considered. 121 will be considered.
122 \return The unsigned integer value of the digit. 0xffffffff if the input is 122 \return The unsigned integer value of the digit. 0xffffffff if the input is
123 not hex 123 not hex
124*/ 124*/
125inline u32 ctoul16(char in) 125inline u32 ctoul16(char in)
126{ 126{
127 if (in >= '0' && in <= '9') 127 if (in >= '0' && in <= '9')
128 return in - '0'; 128 return in - '0';
129 else if (in >= 'a' && in <= 'f') 129 else if (in >= 'a' && in <= 'f')
130 return 10u + in - 'a'; 130 return 10u + in - 'a';
131 else if (in >= 'A' && in <= 'F') 131 else if (in >= 'A' && in <= 'F')
132 return 10u + in - 'A'; 132 return 10u + in - 'A';
133 else 133 else
134 return 0xffffffff; 134 return 0xffffffff;
135} 135}
136 136
137//! Convert a simple string of base 16 digits into an unsigned 32 bit integer. 137//! Convert a simple string of base 16 digits into an unsigned 32 bit integer.
138/** \param[in] in: The string of digits to convert. No leading chars are 138/** \param[in] in: The string of digits to convert. No leading chars are
139 allowed, only digits 0 to 9 and chars A-F,a-f are allowed. Parsing stops 139 allowed, only digits 0 to 9 and chars A-F,a-f are allowed. Parsing stops
140 at the first illegal char. 140 at the first illegal char.
141 \param[out] out: (optional) If provided, it will be set to point at the 141 \param[out] out: (optional) If provided, it will be set to point at the
142 first character not used in the calculation. 142 first character not used in the calculation.
143 \return The unsigned integer value of the digits. If the string specifies 143 \return The unsigned integer value of the digits. If the string specifies
144 too many digits to encode in an u32 then INT_MAX will be returned. 144 too many digits to encode in an u32 then INT_MAX will be returned.
145*/ 145*/
146inline u32 strtoul16(const char* in, const char** out=0) 146inline u32 strtoul16(const char* in, const char** out=0)
147{ 147{
148 if (!in) 148 if (!in)
149 { 149 {
150 if (out) 150 if (out)
151 *out = in; 151 *out = in;
152 return 0; 152 return 0;
153 } 153 }
154 154
155 bool overflow=false; 155 bool overflow=false;
156 u32 unsignedValue = 0; 156 u32 unsignedValue = 0;
157 while (true) 157 while (true)
158 { 158 {
159 u32 tmp = 0; 159 u32 tmp = 0;
160 if ((*in >= '0') && (*in <= '9')) 160 if ((*in >= '0') && (*in <= '9'))
161 tmp = (unsignedValue << 4u) + (*in - '0'); 161 tmp = (unsignedValue << 4u) + (*in - '0');
162 else if ((*in >= 'A') && (*in <= 'F')) 162 else if ((*in >= 'A') && (*in <= 'F'))
163 tmp = (unsignedValue << 4u) + (*in - 'A') + 10; 163 tmp = (unsignedValue << 4u) + (*in - 'A') + 10;
164 else if ((*in >= 'a') && (*in <= 'f')) 164 else if ((*in >= 'a') && (*in <= 'f'))
165 tmp = (unsignedValue << 4u) + (*in - 'a') + 10; 165 tmp = (unsignedValue << 4u) + (*in - 'a') + 10;
166 else 166 else
167 break; 167 break;
168 if (tmp<unsignedValue) 168 if (tmp<unsignedValue)
169 { 169 {
170 unsignedValue=(u32)INT_MAX; 170 unsignedValue=(u32)INT_MAX;
171 overflow=true; 171 overflow=true;
172 } 172 }
173 if (!overflow) 173 if (!overflow)
174 unsignedValue = tmp; 174 unsignedValue = tmp;
175 ++in; 175 ++in;
176 } 176 }
177 177
178 if (out) 178 if (out)
179 *out = in; 179 *out = in;
180 180
181 return unsignedValue; 181 return unsignedValue;
182} 182}
183 183
184//! Convert a simple string of base 8 digits into an unsigned 32 bit integer. 184//! Convert a simple string of base 8 digits into an unsigned 32 bit integer.
185/** \param[in] in The string of digits to convert. No leading chars are 185/** \param[in] in The string of digits to convert. No leading chars are
186 allowed, only digits 0 to 7 are allowed. Parsing stops at the first illegal 186 allowed, only digits 0 to 7 are allowed. Parsing stops at the first illegal
187 char. 187 char.
188 \param[out] out (optional) If provided, it will be set to point at the 188 \param[out] out (optional) If provided, it will be set to point at the
189 first character not used in the calculation. 189 first character not used in the calculation.
190 \return The unsigned integer value of the digits. If the string specifies 190 \return The unsigned integer value of the digits. If the string specifies
191 too many digits to encode in an u32 then INT_MAX will be returned. 191 too many digits to encode in an u32 then INT_MAX will be returned.
192*/ 192*/
193inline u32 strtoul8(const char* in, const char** out=0) 193inline u32 strtoul8(const char* in, const char** out=0)
194{ 194{
195 if (!in) 195 if (!in)
196 { 196 {
197 if (out) 197 if (out)
198 *out = in; 198 *out = in;
199 return 0; 199 return 0;
200 } 200 }
201 201
202 bool overflow=false; 202 bool overflow=false;
203 u32 unsignedValue = 0; 203 u32 unsignedValue = 0;
204 while (true) 204 while (true)
205 { 205 {
206 u32 tmp = 0; 206 u32 tmp = 0;
207 if ((*in >= '0') && (*in <= '7')) 207 if ((*in >= '0') && (*in <= '7'))
208 tmp = (unsignedValue << 3u) + (*in - '0'); 208 tmp = (unsignedValue << 3u) + (*in - '0');
209 else 209 else
210 break; 210 break;
211 if (tmp<unsignedValue) 211 if (tmp<unsignedValue)
212 { 212 {
213 unsignedValue=(u32)INT_MAX; 213 unsignedValue=(u32)INT_MAX;
214 overflow=true; 214 overflow=true;
215 } 215 }
216 if (!overflow) 216 if (!overflow)
217 unsignedValue = tmp; 217 unsignedValue = tmp;
218 ++in; 218 ++in;
219 } 219 }
220 220
221 if (out) 221 if (out)
222 *out = in; 222 *out = in;
223 223
224 return unsignedValue; 224 return unsignedValue;
225} 225}
226 226
227//! Convert a C-style prefixed string (hex, oct, integer) into an unsigned 32 bit integer. 227//! Convert a C-style prefixed string (hex, oct, integer) into an unsigned 32 bit integer.
228/** \param[in] in The string of digits to convert. If string starts with 0x the 228/** \param[in] in The string of digits to convert. If string starts with 0x the
229 hex parser is used, if only leading 0 is used, oct parser is used. In all 229 hex parser is used, if only leading 0 is used, oct parser is used. In all
230 other cases, the usual unsigned parser is used. 230 other cases, the usual unsigned parser is used.
231 \param[out] out (optional) If provided, it will be set to point at the 231 \param[out] out (optional) If provided, it will be set to point at the
232 first character not used in the calculation. 232 first character not used in the calculation.
233 \return The unsigned integer value of the digits. If the string specifies 233 \return The unsigned integer value of the digits. If the string specifies
234 too many digits to encode in an u32 then INT_MAX will be returned. 234 too many digits to encode in an u32 then INT_MAX will be returned.
235*/ 235*/
236inline u32 strtoul_prefix(const char* in, const char** out=0) 236inline u32 strtoul_prefix(const char* in, const char** out=0)
237{ 237{
238 if (!in) 238 if (!in)
239 { 239 {
240 if (out) 240 if (out)
241 *out = in; 241 *out = in;
242 return 0; 242 return 0;
243 } 243 }
244 if ('0'==in[0]) 244 if ('0'==in[0])
245 return ('x'==in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out)); 245 return ('x'==in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out));
246 return strtoul10(in,out); 246 return strtoul10(in,out);
247} 247}
248 248
249//! Converts a sequence of digits into a whole positive floating point value. 249//! Converts a sequence of digits into a whole positive floating point value.
250/** Only digits 0 to 9 are parsed. Parsing stops at any other character, 250/** Only digits 0 to 9 are parsed. Parsing stops at any other character,
251 including sign characters or a decimal point. 251 including sign characters or a decimal point.
252 \param in: the sequence of digits to convert. 252 \param in: the sequence of digits to convert.
253 \param out: (optional) will be set to point at the first non-converted 253 \param out: (optional) will be set to point at the first non-converted
254 character. 254 character.
255 \return The whole positive floating point representation of the digit 255 \return The whole positive floating point representation of the digit
256 sequence. 256 sequence.
257*/ 257*/
258inline f32 strtof10(const char* in, const char** out = 0) 258inline f32 strtof10(const char* in, const char** out = 0)
259{ 259{
260 if (!in) 260 if (!in)
261 { 261 {
262 if (out) 262 if (out)
263 *out = in; 263 *out = in;
264 return 0.f; 264 return 0.f;
265 } 265 }
266 266
267 const u32 MAX_SAFE_U32_VALUE = UINT_MAX / 10 - 10; 267 const u32 MAX_SAFE_U32_VALUE = UINT_MAX / 10 - 10;
268 u32 intValue = 0; 268 u32 intValue = 0;
269 269
270 // Use integer arithmetic for as long as possible, for speed 270 // Use integer arithmetic for as long as possible, for speed
271 // and precision. 271 // and precision.
272 while ( ( *in >= '0') && ( *in <= '9' ) ) 272 while ( ( *in >= '0') && ( *in <= '9' ) )
273 { 273 {
274 // If it looks like we're going to overflow, bail out 274 // If it looks like we're going to overflow, bail out
275 // now and start using floating point. 275 // now and start using floating point.
276 if (intValue >= MAX_SAFE_U32_VALUE) 276 if (intValue >= MAX_SAFE_U32_VALUE)
277 break; 277 break;
278 278
279 intValue = (intValue * 10) + (*in - '0'); 279 intValue = (intValue * 10) + (*in - '0');
280 ++in; 280 ++in;
281 } 281 }
282 282
283 f32 floatValue = (f32)intValue; 283 f32 floatValue = (f32)intValue;
284 284
285 // If there are any digits left to parse, then we need to use 285 // If there are any digits left to parse, then we need to use
286 // floating point arithmetic from here. 286 // floating point arithmetic from here.
287 while ( ( *in >= '0') && ( *in <= '9' ) ) 287 while ( ( *in >= '0') && ( *in <= '9' ) )
288 { 288 {
289 floatValue = (floatValue * 10.f) + (f32)(*in - '0'); 289 floatValue = (floatValue * 10.f) + (f32)(*in - '0');
290 ++in; 290 ++in;
291 if (floatValue > FLT_MAX) // Just give up. 291 if (floatValue > FLT_MAX) // Just give up.
292 break; 292 break;
293 } 293 }
294 294
295 if (out) 295 if (out)
296 *out = in; 296 *out = in;
297 297
298 return floatValue; 298 return floatValue;
299} 299}
300 300
301//! Provides a fast function for converting a string into a float. 301//! Provides a fast function for converting a string into a float.
302/** This is not guaranteed to be as accurate as atof(), but is 302/** This is not guaranteed to be as accurate as atof(), but is
303 approximately 6 to 8 times as fast. 303 approximately 6 to 8 times as fast.
304 \param[in] in The string to convert. 304 \param[in] in The string to convert.
305 \param[out] result The resultant float will be written here. 305 \param[out] result The resultant float will be written here.
306 \return Pointer to the first character in the string that wasn't used 306 \return Pointer to the first character in the string that wasn't used
307 to create the float value. 307 to create the float value.
308*/ 308*/
309inline const char* fast_atof_move(const char* in, f32& result) 309inline const char* fast_atof_move(const char* in, f32& result)
310{ 310{
311 // Please run the regression test when making any modifications to this function. 311 // Please run the regression test when making any modifications to this function.
312 312
313 result = 0.f; 313 result = 0.f;
314 if (!in) 314 if (!in)
315 return 0; 315 return 0;
316 316
317 const bool negative = ('-' == *in); 317 const bool negative = ('-' == *in);
318 if (negative || ('+'==*in)) 318 if (negative || ('+'==*in))
319 ++in; 319 ++in;
320 320
321 f32 value = strtof10(in, &in); 321 f32 value = strtof10(in, &in);
322 322
323 if ( LOCALE_DECIMAL_POINTS.findFirst(*in) >= 0 ) 323 if ( LOCALE_DECIMAL_POINTS.findFirst(*in) >= 0 )
324 { 324 {
325 const char* afterDecimal = ++in; 325 const char* afterDecimal = ++in;
326 const f32 decimal = strtof10(in, &afterDecimal); 326 const f32 decimal = strtof10(in, &afterDecimal);
327 value += decimal * fast_atof_table[afterDecimal - in]; 327 value += decimal * fast_atof_table[afterDecimal - in];
328 in = afterDecimal; 328 in = afterDecimal;
329 } 329 }
330 330
331 if ('e' == *in || 'E' == *in) 331 if ('e' == *in || 'E' == *in)
332 { 332 {
333 ++in; 333 ++in;
334 // Assume that the exponent is a whole number. 334 // Assume that the exponent is a whole number.
335 // strtol10() will deal with both + and - signs, 335 // strtol10() will deal with both + and - signs,
336 // but calculate as f32 to prevent overflow at FLT_MAX 336 // but calculate as f32 to prevent overflow at FLT_MAX
337 value *= powf(10.f, (f32)strtol10(in, &in)); 337 value *= powf(10.f, (f32)strtol10(in, &in));
338 } 338 }
339 339
340 result = negative?-value:value; 340 result = negative?-value:value;
341 return in; 341 return in;
342} 342}
343 343
344//! Convert a string to a floating point number 344//! Convert a string to a floating point number
345/** \param floatAsString The string to convert. 345/** \param floatAsString The string to convert.
346 \param out Optional pointer to the first character in the string that 346 \param out Optional pointer to the first character in the string that
347 wasn't used to create the float value. 347 wasn't used to create the float value.
348 \result Float value parsed from the input string 348 \result Float value parsed from the input string
349*/ 349*/
350inline float fast_atof(const char* floatAsString, const char** out=0) 350inline float fast_atof(const char* floatAsString, const char** out=0)
351{ 351{
352 float ret; 352 float ret;
353 if (out) 353 if (out)
354 *out=fast_atof_move(floatAsString, ret); 354 *out=fast_atof_move(floatAsString, ret);
355 else 355 else
356 fast_atof_move(floatAsString, ret); 356 fast_atof_move(floatAsString, ret);
357 return ret; 357 return ret;
358} 358}
359 359
360} // end namespace core 360} // end namespace core
361} // end namespace irr 361} // end namespace irr
362 362
363#endif 363#endif
364 364