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 ================================================= */
299 speed_test<float> float_test;
300 speed_test<int> int_test;
301 speed_test<fixed> fixed_test;
305 fprintf(stderr,"Addition/subtraction test...");
307 fprintf(stderr,"calculating float...");
308 flt=float_test.add_sub_test();
309 fprintf(stderr,"float time: %fsec\n",flt);
311 fprintf(stderr,"calculating fixed...");
312 fix=fixed_test.add_sub_test();
313 fprintf(stderr,"fixed time: %fsec\n",fix);
315 fprintf(stderr,"calculating integer...");
316 inte=int_test.add_sub_test();
317 fprintf(stderr,"integer time: %fsec\n",inte);
320 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
322 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
328 fprintf(stderr,"Product test...");
329 fprintf(stderr,"calculating float...");
330 flt=float_test.mul_test();
331 fprintf(stderr,"float time: %fsec\n",flt);
332 fprintf(stderr,"calculating fixed...");
333 fix=fixed_test.mul_test();
334 fprintf(stderr,"fixed time: %fsec\n",fix);
335 fprintf(stderr,"calculating integer...");
336 inte=int_test.mul_test();
337 fprintf(stderr,"integer time: %fsec\n",inte);
339 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
341 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
347 fprintf(stderr,"Division test...");
348 fprintf(stderr,"calculating float...");
349 flt=float_test.div_test();
350 fprintf(stderr,"float time: %fsec\n",flt);
351 fprintf(stderr,"calculating fixed...");
352 fix=fixed_test.div_test();
353 fprintf(stderr,"fixed time: %fsec\n",fix);
354 fprintf(stderr,"calculating integer...");
355 inte=int_test.div_test();
356 fprintf(stderr,"integer time: %fsec\n",inte);
358 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
360 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);