1
// Copyright (C) 2008-2011 Colin MacDonald
2
// No rights reserved: this software is in the public domain.
9
//! This was an older Irrlicht implementation, tested against for reference.
10
static inline u32 old_strtol10(const char* in, const char** out=0)
14
while ( ( *in >= '0') && ( *in <= '9' ))
16
value = ( value * 10 ) + ( *in - '0' );
24
//! This was an older Irrlicht implementation, tested against for reference.
25
static inline const char* old_fast_atof_move( const char* c, float& out)
37
//f = (float)strtol(c, &t, 10);
38
f = (float) old_strtol10 ( c, &c );
44
//float pl = (float)strtol(c, &t, 10);
45
float pl = (float) old_strtol10 ( c, &t );
46
pl *= fast_atof_table[t-c];
55
//float exp = (float)strtol(c, &t, 10);
56
bool einv = (*c=='-');
60
float exp = (float)old_strtol10(c, &c);
64
f *= (float)pow(10.0f, exp);
75
//! This was an older Irrlicht implementation, tested against for reference.
76
static inline float old_fast_atof(const char* c)
79
old_fast_atof_move(c, ret);
84
static bool testCalculation_atof(const char * valueString)
86
const f32 newFastValue = fast_atof(valueString);
87
const f32 oldFastValue = old_fast_atof(valueString);
88
const f32 atofValue = (f32)atof(valueString);
90
logTestString("\n String '%s'\n New fast %.40f\n Old fast %.40f\n atof %.40f\n",
91
valueString, newFastValue, oldFastValue, atofValue);
93
bool accurate = fabs(newFastValue - atofValue) <= fabs(oldFastValue - atofValue);
96
logTestString("*** ERROR - less accurate than old method ***\n\n");
101
static bool testCalculation_strtol(const char * valueString)
103
const s32 newFastValue = strtol10(valueString);
104
const s32 oldFastValue = old_strtol10(valueString);
105
const s32 strtolValue = (s32)strtol(valueString, 0, 10);
107
logTestString("\n String '%s'\n New fast %d\n Old fast %d\n strtol %d\n",
108
valueString, newFastValue, oldFastValue, strtolValue);
110
bool accurate = (newFastValue == strtolValue) || (oldFastValue != strtolValue);
113
logTestString("*** ERROR - wrong calculation in new method ***\n\n");
118
//! Test both the accuracy and speed of Irrlicht's fast_atof() implementation.
119
bool test_fast_atof(void)
121
bool accurate = true;
123
accurate &= testCalculation_atof("340282346638528859811704183484516925440.000000");
124
accurate &= testCalculation_atof("3.402823466e+38F");
125
accurate &= testCalculation_atof("3402823466e+29F");
126
accurate &= testCalculation_atof("-340282346638528859811704183484516925440.000000");
127
accurate &= testCalculation_atof("-3.402823466e+38F");
128
accurate &= testCalculation_atof("-3402823466e+29F");
129
accurate &= testCalculation_atof("34028234663852885981170418348451692544.000000");
130
accurate &= testCalculation_atof("3.402823466e+37F");
131
accurate &= testCalculation_atof("3402823466e+28F");
132
accurate &= testCalculation_atof("-34028234663852885981170418348451692544.000000");
133
accurate &= testCalculation_atof("-3.402823466e+37F");
134
accurate &= testCalculation_atof("-3402823466e+28F");
135
accurate &= testCalculation_atof(".00234567");
136
accurate &= testCalculation_atof("-.00234567");
137
accurate &= testCalculation_atof("0.00234567");
138
accurate &= testCalculation_atof("-0.00234567");
139
accurate &= testCalculation_atof("1.175494351e-38F");
140
accurate &= testCalculation_atof("1175494351e-47F");
141
accurate &= testCalculation_atof("1.175494351e-37F");
142
accurate &= testCalculation_atof("1.175494351e-36F");
143
accurate &= testCalculation_atof("-1.175494351e-36F");
144
accurate &= testCalculation_atof("123456.789");
145
accurate &= testCalculation_atof("-123456.789");
146
accurate &= testCalculation_atof("0000123456.789");
147
accurate &= testCalculation_atof("-0000123456.789");
148
accurate &= testCalculation_atof("-0.0690462109446526");
152
logTestString("Calculation is not accurate, so the speed is irrelevant\n");
156
IrrlichtDevice* device = createDevice(video::EDT_NULL);
159
ITimer* timer = device->getTimer();
161
const int ITERATIONS = 100000;
165
u32 then = timer->getRealTime();
166
for(i = 0; i < ITERATIONS; ++i)
167
value = (f32)atof("-340282346638528859811704183484516925440.000000");
169
const u32 atofTime = timer->getRealTime() - then;
172
for(i = 0; i < ITERATIONS; ++i)
173
value = fast_atof("-340282346638528859811704183484516925440.000000");
174
const u32 fastAtofTime = timer->getRealTime() - then;
176
then += fastAtofTime;
177
for(i = 0; i < ITERATIONS; ++i)
178
value = old_fast_atof("-340282346638528859811704183484516925440.000000");
179
const u32 oldFastAtofTime = timer->getRealTime() - then;
181
logTestString("Speed test\n atof time = %d\n fast_atof Time = %d\nold fast_atof time = %d\n",
182
atofTime, fastAtofTime, oldFastAtofTime);
184
device->closeDevice();
188
if(fastAtofTime > (1.2f*atofTime))
190
logTestString("The fast method is slower than atof()\n");
197
//! Test both the accuracy and speed of Irrlicht's strtol10() implementation.
198
bool test_strtol(void)
200
bool accurate = true;
202
accurate &= testCalculation_strtol("340282346638528859811704183484516925440");
203
accurate &= testCalculation_strtol("3402823466");
204
accurate &= testCalculation_strtol("3402823466e+29F");
205
accurate &= testCalculation_strtol("-340282346638528859811704183484516925440");
206
accurate &= testCalculation_strtol("-3402823466");
207
accurate &= testCalculation_strtol("-3402823466e+29F");
208
accurate &= testCalculation_strtol("402823466385288598117");
209
accurate &= testCalculation_strtol("402823466");
210
accurate &= testCalculation_strtol("402823466e+28F");
211
accurate &= testCalculation_strtol("402823466385288598117");
212
accurate &= testCalculation_strtol("-402823466");
213
accurate &= testCalculation_strtol("-402823466e+28F");
214
accurate &= testCalculation_strtol(".00234567");
215
accurate &= testCalculation_strtol("-234567");
216
accurate &= testCalculation_strtol("234567");
217
accurate &= testCalculation_strtol("-234567");
218
accurate &= testCalculation_strtol("1175494351");
219
accurate &= testCalculation_strtol("11754943512");
220
accurate &= testCalculation_strtol("11754943513");
221
accurate &= testCalculation_strtol("11754943514");
222
accurate &= testCalculation_strtol("-1175494351");
223
accurate &= testCalculation_strtol("123456789");
224
accurate &= testCalculation_strtol("-123456789");
225
accurate &= testCalculation_strtol("123456.789");
226
accurate &= testCalculation_strtol("-123456.789");
227
accurate &= testCalculation_strtol("-109446526");
231
logTestString("Calculation is not accurate, so the speed is irrelevant\n");
235
IrrlichtDevice* device = createDevice(video::EDT_NULL);
238
ITimer* timer = device->getTimer();
240
const int ITERATIONS = 1000000;
244
u32 then = timer->getRealTime();
245
for(i = 0; i < ITERATIONS; ++i)
246
value = strtol("-3402823466", 0, 10);
248
const u32 strtolTime = timer->getRealTime() - then;
251
for(i = 0; i < ITERATIONS; ++i)
252
value = strtol10("-3402823466");
253
const u32 strtol10Time = timer->getRealTime() - then;
255
then += strtol10Time;
256
for(i = 0; i < ITERATIONS; ++i)
257
value = old_strtol10("-3402823466");
258
const u32 oldstrtol10Time = timer->getRealTime() - then;
260
logTestString("Speed test\n strtol time = %d\n strtol10 time = %d\nold strtol10 time = %d\n",
261
strtolTime, strtol10Time, oldstrtol10Time);
263
device->closeDevice();
267
if (strtol10Time > (1.2f*strtolTime))
269
logTestString("The fast method is slower than strtol()\n");
278
return test_fast_atof() && test_strtol();