~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/fast_atof.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008-2011 Colin MacDonald
 
2
// No rights reserved: this software is in the public domain.
 
3
 
 
4
#include "testUtils.h"
 
5
 
 
6
using namespace irr;
 
7
using namespace core;
 
8
 
 
9
//! This was an older Irrlicht implementation, tested against for reference.
 
10
static inline u32 old_strtol10(const char* in, const char** out=0)
 
11
{
 
12
        u32 value = 0;
 
13
 
 
14
        while ( ( *in >= '0') && ( *in <= '9' ))
 
15
        {
 
16
                value = ( value * 10 ) + ( *in - '0' );
 
17
                ++in;
 
18
        }
 
19
        if (out)
 
20
                *out = in;
 
21
        return value;
 
22
}
 
23
 
 
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)
 
26
{
 
27
        bool inv = false;
 
28
        const char *t;
 
29
        float f;
 
30
 
 
31
        if (*c=='-')
 
32
        {
 
33
                ++c;
 
34
                inv = true;
 
35
        }
 
36
 
 
37
        //f = (float)strtol(c, &t, 10);
 
38
        f = (float) old_strtol10 ( c, &c );
 
39
 
 
40
        if (*c == '.')
 
41
        {
 
42
                ++c;
 
43
 
 
44
                //float pl = (float)strtol(c, &t, 10);
 
45
                float pl = (float) old_strtol10 ( c, &t );
 
46
                pl *= fast_atof_table[t-c];
 
47
 
 
48
                f += pl;
 
49
 
 
50
                c = t;
 
51
 
 
52
                if (*c == 'e')
 
53
                {
 
54
                        ++c;
 
55
                        //float exp = (float)strtol(c, &t, 10);
 
56
                        bool einv = (*c=='-');
 
57
                        if (einv)
 
58
                                ++c;
 
59
 
 
60
                        float exp = (float)old_strtol10(c, &c);
 
61
                        if (einv)
 
62
                                exp *= -1.0f;
 
63
 
 
64
                        f *= (float)pow(10.0f, exp);
 
65
                }
 
66
        }
 
67
 
 
68
        if (inv)
 
69
                f *= -1.0f;
 
70
 
 
71
        out = f;
 
72
        return c;
 
73
}
 
74
 
 
75
//! This was an older Irrlicht implementation, tested against for reference.
 
76
static inline float old_fast_atof(const char* c)
 
77
{
 
78
        float ret;
 
79
        old_fast_atof_move(c, ret);
 
80
        return ret;
 
81
}
 
82
 
 
83
 
 
84
static bool testCalculation_atof(const char * valueString)
 
85
{
 
86
        const f32 newFastValue = fast_atof(valueString);
 
87
        const f32 oldFastValue = old_fast_atof(valueString);
 
88
        const f32 atofValue = (f32)atof(valueString);
 
89
 
 
90
        logTestString("\n String '%s'\n New fast %.40f\n Old fast %.40f\n     atof %.40f\n",
 
91
                valueString, newFastValue, oldFastValue, atofValue);
 
92
 
 
93
        bool accurate = fabs(newFastValue - atofValue) <= fabs(oldFastValue - atofValue);
 
94
 
 
95
        if(!accurate)
 
96
                logTestString("*** ERROR - less accurate than old method ***\n\n");
 
97
 
 
98
        return accurate;
 
99
}
 
100
 
 
101
static bool testCalculation_strtol(const char * valueString)
 
102
{
 
103
        const s32 newFastValue = strtol10(valueString);
 
104
        const s32 oldFastValue = old_strtol10(valueString);
 
105
        const s32 strtolValue = (s32)strtol(valueString, 0, 10);
 
106
 
 
107
        logTestString("\n String '%s'\n New fast %d\n Old fast %d\n   strtol %d\n",
 
108
                valueString, newFastValue, oldFastValue, strtolValue);
 
109
 
 
110
        bool accurate = (newFastValue == strtolValue) || (oldFastValue != strtolValue);
 
111
 
 
112
        if (!accurate)
 
113
                logTestString("*** ERROR - wrong calculation in new method ***\n\n");
 
114
 
 
115
        return accurate;
 
116
}
 
117
 
 
118
//! Test both the accuracy and speed of Irrlicht's fast_atof() implementation.
 
119
bool test_fast_atof(void)
 
120
{
 
121
        bool accurate = true;
 
122
 
 
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");
 
149
 
 
150
        if (!accurate)
 
151
        {
 
152
                logTestString("Calculation is not accurate, so the speed is irrelevant\n");
 
153
                return false;
 
154
        }
 
155
 
 
156
        IrrlichtDevice* device = createDevice(video::EDT_NULL);
 
157
        if (!device)
 
158
                return false;
 
159
        ITimer* timer = device->getTimer();
 
160
 
 
161
        const int ITERATIONS = 100000;
 
162
        int i;
 
163
 
 
164
        f32 value;
 
165
        u32 then = timer->getRealTime();
 
166
        for(i = 0; i < ITERATIONS; ++i)
 
167
                value = (f32)atof("-340282346638528859811704183484516925440.000000");
 
168
 
 
169
        const u32 atofTime = timer->getRealTime() - then;
 
170
 
 
171
        then += atofTime;
 
172
        for(i = 0; i < ITERATIONS; ++i)
 
173
                value = fast_atof("-340282346638528859811704183484516925440.000000");
 
174
        const u32 fastAtofTime = timer->getRealTime() - then;
 
175
 
 
176
        then += fastAtofTime;
 
177
        for(i = 0; i < ITERATIONS; ++i)
 
178
                value = old_fast_atof("-340282346638528859811704183484516925440.000000");
 
179
        const u32 oldFastAtofTime = timer->getRealTime() - then;
 
180
 
 
181
        logTestString("Speed test\n         atof time = %d\n    fast_atof Time = %d\nold fast_atof time = %d\n",
 
182
                atofTime, fastAtofTime, oldFastAtofTime);
 
183
 
 
184
        device->closeDevice();
 
185
        device->run();
 
186
        device->drop();
 
187
 
 
188
        if(fastAtofTime > (1.2f*atofTime))
 
189
        {
 
190
                logTestString("The fast method is slower than atof()\n");
 
191
                return false;
 
192
        }
 
193
 
 
194
        return true;
 
195
}
 
196
 
 
197
//! Test both the accuracy and speed of Irrlicht's strtol10() implementation.
 
198
bool test_strtol(void)
 
199
{
 
200
        bool accurate = true;
 
201
 
 
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");
 
228
 
 
229
        if(!accurate)
 
230
        {
 
231
                logTestString("Calculation is not accurate, so the speed is irrelevant\n");
 
232
                return false;
 
233
        }
 
234
 
 
235
        IrrlichtDevice* device = createDevice(video::EDT_NULL);
 
236
        if (!device)
 
237
                return false;
 
238
        ITimer* timer = device->getTimer();
 
239
 
 
240
        const int ITERATIONS = 1000000;
 
241
        int i;
 
242
 
 
243
        s32 value;
 
244
        u32 then = timer->getRealTime();
 
245
        for(i = 0; i < ITERATIONS; ++i)
 
246
                value = strtol("-3402823466", 0, 10);
 
247
 
 
248
        const u32 strtolTime = timer->getRealTime() - then;
 
249
 
 
250
        then += strtolTime;
 
251
        for(i = 0; i < ITERATIONS; ++i)
 
252
                value = strtol10("-3402823466");
 
253
        const u32 strtol10Time = timer->getRealTime() - then;
 
254
 
 
255
        then += strtol10Time;
 
256
        for(i = 0; i < ITERATIONS; ++i)
 
257
                value = old_strtol10("-3402823466");
 
258
        const u32 oldstrtol10Time = timer->getRealTime() - then;
 
259
 
 
260
        logTestString("Speed test\n      strtol time = %d\n    strtol10 time = %d\nold strtol10 time = %d\n",
 
261
                strtolTime, strtol10Time, oldstrtol10Time);
 
262
 
 
263
        device->closeDevice();
 
264
        device->run();
 
265
        device->drop();
 
266
 
 
267
        if (strtol10Time > (1.2f*strtolTime))
 
268
        {
 
269
                logTestString("The fast method is slower than strtol()\n");
 
270
                return false;
 
271
        }
 
272
 
 
273
        return true;
 
274
}
 
275
 
 
276
bool fast_atof(void)
 
277
{
 
278
        return test_fast_atof() && test_strtol();
 
279
}