1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Fixed-Point Math Test
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
24 #define ETL_FIXED_BITS 12
30 /* === M A C R O S ========================================================= */
33 # define PI (3.1415926535897932384626433832795029L)
36 #define ADD_SUB_TEST 20000000
37 #define MUL_TEST 10000000
38 #define DIV_TEST 10000000
41 /* === C L A S S E S ======================================================= */
43 template <class value_type>
46 double add_sub_test(void)
54 for(i=0;i<ADD_SUB_TEST;i++)
103 const value_type half(static_cast<value_type>(0.5));
107 for(i=0;i<MUL_TEST;i++)
109 d*=a;d*=b;d*=c;d*=3;d*=i;
111 c*=d;c*=half;c*=a;c*=b;c*=3;
114 d*=a;d*=b;d*=c;d*=3;d*=i;
116 c*=d;c*=half;c*=a;c*=b;c*=3;
119 d*=a;d*=b;d*=c;d*=3;d*=i;
121 c*=d;c*=half;c*=a;c*=b;c*=3;
124 d*=a;d*=b;d*=c;d*=3;d*=i;
126 c*=d;c*=half;c*=a;c*=b;c*=3;
129 d*=a;d*=b;d*=c;d*=3;d*=i;
131 c*=d;c*=half;c*=a;c*=b;c*=3;
134 d*=a;d*=b;d*=c;d*=3;d*=i;
136 c*=d;c*=half;c*=a;c*=b;c*=3;
139 d*=a;d*=b;d*=c;d*=3;d*=i;
141 c*=d;c*=half;c*=a;c*=b;c*=3;
148 double div_test(void)
155 for(i=1;i<DIV_TEST;i++)
194 /* === P R O C E D U R E S ================================================= */
207 fprintf(stderr,"fixed: abs() failure on line %d in "__FILE__".\n",__LINE__);
211 d=(double)(fixed(2.5)*fixed(3.0f)/7)-(2.5f*3.0f/7.0f);
212 fprintf(stderr,"fixed: 2.5 * 2 / 7 --- Difference: %f\n",d);
216 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
226 fprintf(stderr,"fixed: ( 1043 / 27 + 10.42 ) / 6 * PI --- Difference: %f\n",d);
230 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
241 fixed_base<unsigned char,8> fix;
244 if(sizeof(fix)!=sizeof(unsigned char))
247 fprintf(stderr,"fixed: Size of fixed_base<unsigned char,8> is wrong!\n");
252 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
256 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
260 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
264 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
268 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
272 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
276 //fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
280 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
284 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
290 /* === E N T R Y P O I N T ================================================= */
294 fprintf(stderr, "\n***\n");
295 fprintf(stderr, "*** tests in fixed.cpp fail - see http://wiki.synfig.com/Source:ETL_make_check for an explanation ***\n");
296 fprintf(stderr, "***\n\n");
304 speed_test<float> float_test;
305 speed_test<int> int_test;
306 speed_test<fixed> fixed_test;
310 fprintf(stderr,"Addition/subtraction test...");
312 fprintf(stderr,"calculating float...");
313 flt=float_test.add_sub_test();
314 fprintf(stderr,"float time: %fsec\n",flt);
316 fprintf(stderr,"calculating fixed...");
317 fix=fixed_test.add_sub_test();
318 fprintf(stderr,"fixed time: %fsec\n",fix);
320 fprintf(stderr,"calculating integer...");
321 inte=int_test.add_sub_test();
322 fprintf(stderr,"integer time: %fsec\n",inte);
325 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
327 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
333 fprintf(stderr,"Product test...");
334 fprintf(stderr,"calculating float...");
335 flt=float_test.mul_test();
336 fprintf(stderr,"float time: %fsec\n",flt);
337 fprintf(stderr,"calculating fixed...");
338 fix=fixed_test.mul_test();
339 fprintf(stderr,"fixed time: %fsec\n",fix);
340 fprintf(stderr,"calculating integer...");
341 inte=int_test.mul_test();
342 fprintf(stderr,"integer time: %fsec\n",inte);
344 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
346 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
352 fprintf(stderr,"Division test...");
353 fprintf(stderr,"calculating float...");
354 flt=float_test.div_test();
355 fprintf(stderr,"float time: %fsec\n",flt);
356 fprintf(stderr,"calculating fixed...");
357 fix=fixed_test.div_test();
358 fprintf(stderr,"fixed time: %fsec\n",fix);
359 fprintf(stderr,"calculating integer...");
360 inte=int_test.div_test();
361 fprintf(stderr,"integer time: %fsec\n",inte);
363 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
365 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);