My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "stdafx.h"

extern int g_iCasePassed;
extern int g_iCaseFailed;

#define _USE_MATH_DEFINES
#include <math.h>


///////////////////////////////////////////////////////////////////////
/// giFloat
///////////////////////////////////////////////////////////////////////

bool test_giFloat_logical()
{
float x = (float)M_PI;
giFloat f;
f = x;
return f==x
&& !(f!=x)
&& f <= x
&& f >= x
&& f != x + giFloat::kEpsilon * 2
&& !( f == x + giFloat::kEpsilon * 2 )
&& f < x + giFloat::kEpsilon * 2
&& !( f >= x + giFloat::kEpsilon * 2)
&& f > x - giFloat::kEpsilon * 2
&& !( f <= x - giFloat::kEpsilon * 2);
}

bool test_giFloat_add()
{
float x = (float)M_PI;
float y = (float)M_PI_2;
giFloat f1(x), f2(y);
giFloat f3( f1 + f2 );
giFloat f4 = f1; f4 += f2;

return f3 == x+y && f3 == f4;
}

bool test_giFloat_minus()
{
float x = (float)M_PI;
float y = (float)M_PI_2;
giFloat f1(x), f2(y);
giFloat f3( f1 - f2 );
giFloat f4 = f1; f4 -= f2;
giFloat f5( f1 );
f5 = -f5;

return f3 == x-y && f3 == f4 && f5 == -x;
}

bool test_giFloat_multiple()
{
float x = (float)M_PI;
float y = (float)M_PI_2;
giFloat f1(x), f2(y);
giFloat f3( f1 * f2 );
giFloat f4 = f1; f4 *= f2;

return f3 == x*y && f3 == f4;
}

bool test_giFloat_divide()
{
float x = (float)M_PI;
float y = (float)M_PI_2;
giFloat f1(x), f2(y);
giFloat f3( f1 / f2 );
giFloat f4 = f1; f4 /= f2;

return f3 == x/y && f3 == f4;
}

bool test_giFloat_unary()
{
float x = (float)M_PI;
giFloat f1(x);
return x == (float)(+f1) && -x == (float)-f1;
}

bool test_giFloat_prepostfix()
{
float x = (float)M_PI;
float y = x + 1;
giFloat f1(x);
giFloat f2(f1);

if ( x != (float)(f2++) ) return false;
if ( y != (float)f2 ) return false;

if ( y != (float)(++f1) ) return false;

return true;
}

void test_giFloat()
{
printf ( "\n====== " __FUNCTION__ " ======\n" );
V(test_giFloat_logical());
V(test_giFloat_add());
V(test_giFloat_minus());
V(test_giFloat_multiple());
V(test_giFloat_divide());
V(test_giFloat_unary());
V(test_giFloat_prepostfix());
}


///////////////////////////////////////////////////////////////////////
/// giDouble
///////////////////////////////////////////////////////////////////////

bool test_giDouble_logical()
{
double x = M_PI;
giDouble f;
f = x;
return f==x && !(f!=x) && f != x + giDouble::kEpsilon * 2 && !( f == x + giDouble::kEpsilon * 2 );
}

bool test_giDouble_add()
{
double x = M_PI;
double y = M_PI_2;
giDouble f1(x), f2(y);
giDouble f3( f1 + f2 );
giDouble f4 = f1; f4 += f2;

return f3 == x+y && f3 == f4;
}

bool test_giDouble_minus()
{
double x = M_PI;
double y = M_PI_2;
giDouble f1(x), f2(y);
giDouble f3( f1 - f2 );
giDouble f4 = f1; f4 -= f2;
giDouble f5( f1 );
f5 = -f5;

return f3 == x-y && f3 == f4 && f5 == -x;
}

bool test_giDouble_multiple()
{
double x = M_PI;
double y = M_PI_2;
giDouble f1(x), f2(y);
giDouble f3( f1 * f2 );
giDouble f4 = f1; f4 *= f2;

return f3 == x*y && f3 == f4;
}

bool test_giDouble_divide()
{
double x = M_PI;
double y = M_PI_2;
giDouble f1(x), f2(y);
giDouble f3( f1 / f2 );
giDouble f4 = f1; f4 /= f2;

return f3 == x/y && f3 == f4;
}

bool test_giDouble_unary()
{
double x = M_PI;
giDouble f1(x);
return x == (double)(+f1) && -x == (double)-f1;
}

bool test_giDouble_prepostfix()
{
double x = (double)M_PI;
double y = x + 1;
giDouble f1(x);
giDouble f2(f1);

if ( x != (double)(f2++) ) return false;
if ( y != (double)f2 ) return false;

if ( y != (double)(++f1) ) return false;

return true;
}

void test_giDouble()
{
printf ( "\n====== " __FUNCTION__ " ======\n" );
V(test_giDouble_logical());
V(test_giDouble_add());
V(test_giDouble_minus());
V(test_giDouble_multiple());
V(test_giDouble_divide());
V(test_giDouble_unary());
V(test_giDouble_prepostfix());
}



///////////////////////////////////////////////////////////////////////
/// giFloat interact with giDouble
///////////////////////////////////////////////////////////////////////

bool test_giDouble_giFloat_logical()
{
double x = M_PI;
giFloat f((float)x);
giDouble d(x);
return f == d && d != f; // d != f: because of the precision lost during conversion
}

void test_giDouble_giFloat()
{
printf ( "\n====== " __FUNCTION__ " ======\n" );
V(test_giDouble_giFloat_logical());
}


///////////////////////////////////////////////////////////////////////
/// Performance: giFloat and giDouble
///////////////////////////////////////////////////////////////////////

bool test_giReal_Perf_Arithmetic()
{
float N=15000000;
#ifdef _DEBUG
N /= 10;
#endif

float c1 = 0;
double t1 = giCurrentMillisecond();
for( float i = 0; i < N; ++i )
{
float x = (float)i;
x = x + (float)M_PI_2; x += (float)M_PI_2;
x = x * (float)M_E; x *= (float)M_E;
x = x - (float)M_PI_4; x -= (float)M_PI_4;
x = x / (float)M_LN2; x /= (float)M_LN2;
c1 += x;
}

double c2 = 0;
double t2 = giCurrentMillisecond();
for( double i = 0; i < N; ++i )
{
double x = i;
x = x + M_PI_2; x += M_PI_2;
x = x * M_E; x *= M_E;
x = x - M_PI_4; x -= M_PI_4;
x = x / M_LN2; x /= M_LN2;
c2 += x;
}

float c3 = 0;
double t3 = giCurrentMillisecond();
for( float i = 0; i < N; ++i )
{
giFloat x = (float)i;
x = x + (float)M_PI_2; x += (float)M_PI_2;
x = x * (float)M_E; x *= (float)M_E;
x = x - (float)M_PI_4; x -= (float)M_PI_4;
x = x / (float)M_LN2; x /= (float)M_LN2;
c3 += (float)x;
}

double c4 = 0;
double t4 = giCurrentMillisecond();
for( double i = 0; i < N; ++i )
{
giDouble x = i;
x = x + M_PI_2; x += M_PI_2;
x = x * M_E; x *= M_E;
x = x - M_PI_4; x -= M_PI_4;
x = x / M_LN2; x /= M_LN2;
c4 += (double)x;
}

double t5 = giCurrentMillisecond();

printf("- Arithmetic:\tfloat:\t\t%d\tdouble:\t\t%d\n\t\tgiFloat:\t%d\tgiDouble:\t%d\n", (int)(t2-t1), (int)(t3-t2), (int)(t4-t3), (int)(t5-t4) );
return c1 == c3 && c2 == c4;
}

inline bool fuzzyEquals(float a, float b) { return fabs(a-b) < giFloat::kEpsilon; }
inline bool fuzzyLargerThan(float a, float b) { return a>b && !fuzzyEquals(a,b); }
inline bool fuzzyLargerThanEqual(float a, float b) { return a>b || fuzzyEquals(a,b); }

inline bool fuzzyEquals(double a, double b) { return fabs(a-b) < giDouble::kEpsilon; }
inline bool fuzzyLargerThan(double a, double b) { return a>b && !fuzzyEquals(a,b); }
inline bool fuzzyLargerThanEqual(double a, double b) { return a>b || fuzzyEquals(a,b); }

bool test_giReal_Perf_Relational()
{
float N=15000000;
#ifdef _DEBUG
N /= 10;
#endif

float fVal = 1e5f;
double dVal = 1e5;

float c1 = 0, d1 = 0;
double t1 = giCurrentMillisecond();
for( float i = 0; i < N; ++i )
{
float x = i;
c1 += ( x == fVal ) + ( x > fVal ) + ( x >= fVal );
d1 += ( x != fVal ) + ( x < fVal ) + ( x <= fVal );
}

double c2 = 0, d2 = 0;
double t2 = giCurrentMillisecond();
for( double i = 0; i < N; ++i )
{
double x = i;
c2 += ( x == dVal ) + ( x > dVal ) + ( x >= dVal );
d2 += ( x != dVal ) + ( x < dVal ) + ( x <= dVal );
}

float c3 = 0, d3 = 0;
double t3 = giCurrentMillisecond();
for( float i = 0; i < N; ++i )
{
giFloat x(i);
c3 += ( x == fVal ) + ( x > fVal ) + ( x >= fVal );
d3 += ( x != fVal ) + ( x < fVal ) + ( x <= fVal );
}

double c4 = 0, d4 = 0;
double t4 = giCurrentMillisecond();
for( double i = 0; i < N; ++i )
{
giDouble x(i);
c4 += ( x == dVal ) + ( x > dVal ) + ( x >= dVal );
d4 += ( x != dVal ) + ( x < dVal ) + ( x <= dVal );
}

float c5 = 0, d5 = 0;
double t5 = giCurrentMillisecond();
for( float i = 0; i < N; ++i )
{
float x = i;
c5 += fuzzyEquals(x,fVal) + fuzzyLargerThan(x,fVal) + fuzzyLargerThanEqual(x, fVal);
d5 += !fuzzyEquals(x,fVal) + !fuzzyLargerThan(x,fVal) + !fuzzyLargerThanEqual(x, fVal);
}

double c6 = 0, d6 = 0;
double t6 = giCurrentMillisecond();
for( double i = 0; i < N; ++i )
{
double x = i;
c6 += fuzzyEquals(x,dVal) + fuzzyLargerThan(x,dVal) + fuzzyLargerThanEqual(x, dVal);
d6 += !fuzzyEquals(x,dVal) + !fuzzyLargerThan(x,dVal) + !fuzzyLargerThanEqual(x, dVal);
}

double t7 = giCurrentMillisecond();

printf("- ControlFlow:\tfloat:\t\t%d\tdouble:\t\t%d\n\t\tgiFloat:\t%d\tgiDouble:\t%d\n\t\tfz float:\t%d\tfz double:\t%d\n",
(int)(t2-t1), (int)(t3-t2), (int)(t4-t3), (int)(t5-t4), (int)(t6-t5), (int)(t7-t6) );
return c1 == c3 && d1 == d3 && c2 == c4 && d2 == d4 && c3 == c5 && d3 == d5 && c4 == c6 && d4 == d6;
}

void test_giReal_Performance()
{
printf ( "\n====== " __FUNCTION__ " ======\n" );

V(test_giReal_Perf_Arithmetic());
V(test_giReal_Perf_Relational());
printf( "size: giFloat:%d, giDouble:%d\n", sizeof(giFloat), sizeof(giDouble) );
}

Change log

r8 by hao.hzhang on Sep 10, 2008   Diff
giPoint3, giVector3, wait for unit test
Go to: 
Project members, sign in to write a code review

Older revisions

r7 by hao.hzhang on Sep 9, 2008   Diff
Adjusted giFloat class for better
performance and testing script
r6 by hao.hzhang on Sep 9, 2008   Diff
giFloat and giDouble, and unit test
All revisions of this file

File info

Size: 9881 bytes, 377 lines
Powered by Google Project Hosting