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 1048573 // at 1048573, fixed point numbers wrap around to zero
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++)
56 a+=a; a-=b; a-=c; a+=a;
57 a+=a; a-=b; a-=c; a+=a;
58 a+=a; a-=b; a-=c; a+=a;
59 a+=a; a-=b; a-=c; a+=a;
60 a+=a; a-=b; a-=c; a+=a;
61 a+=a; a-=b; a-=c; a+=a;
62 a+=a; a-=b; a-=c; a+=a;
65 fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
76 const value_type one_and_a_half(static_cast<value_type>(1.5));
80 for(i=1;i<MUL_TEST;i++)
82 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
83 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
84 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
85 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
86 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
87 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
88 d*=a;d*=b;d*=c;d*=3;d*=i; b*=c;b*=d;b*=d;b*=3; c*=d;c*=one_and_a_half;c*=a;c*=b;c*=3; a*=c;a*=b;a*=3;
91 fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
104 for(i=1;i<DIV_TEST;i++)
106 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
107 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
108 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
109 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
110 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
111 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
114 fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
119 /* === P R O C E D U R E S ================================================= */
132 fprintf(stderr,"fixed: abs() failure on line %d in "__FILE__".\n",__LINE__);
136 d=(double)(fixed(2.5)*fixed(3.0f)/7)-(2.5f*3.0f/7.0f);
137 fprintf(stderr,"fixed: 2.5 * 2 / 7 --- Difference: %f\n",d);
141 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
151 fprintf(stderr,"fixed: ( 1043 / 27 + 10.42 ) / 6 * PI --- Difference: %f\n",d);
153 #ifdef ROUND_TO_NEAREST_INTEGER
159 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
170 fixed_base<unsigned char,8> fix;
173 if(sizeof(fix)!=sizeof(unsigned char))
176 fprintf(stderr,"fixed: Size of fixed_base<unsigned char,8> is wrong!\n");
181 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
185 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
189 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
193 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
197 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
201 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
205 //fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
209 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
213 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
219 /* === E N T R Y P O I N T ================================================= */
228 speed_test<float> float_test;
229 speed_test<int> int_test;
230 speed_test<fixed> fixed_test;
234 fprintf(stderr,"\nAddition/subtraction test...\n");
236 fprintf(stderr," calculating float.....");
237 flt=float_test.add_sub_test();
238 fprintf(stderr," float time: %f sec\n",flt);
240 fprintf(stderr," calculating fixed.....");
241 fix=fixed_test.add_sub_test();
242 fprintf(stderr," fixed time: %f sec\n",fix);
244 fprintf(stderr," calculating integer...");
245 inte=int_test.add_sub_test();
246 fprintf(stderr," integer time: %f sec\n",inte);
249 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
251 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
257 fprintf(stderr,"\nProduct test...\n");
258 fprintf(stderr," calculating float.....");
259 flt=float_test.mul_test();
260 fprintf(stderr," float time: %f sec\n",flt);
261 fprintf(stderr," calculating fixed.....");
262 fix=fixed_test.mul_test();
263 fprintf(stderr," fixed time: %f sec\n",fix);
264 fprintf(stderr," calculating integer...");
265 inte=int_test.mul_test();
266 fprintf(stderr," integer time: %f sec\n",inte);
268 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
270 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
275 fprintf(stderr,"\nDivision test...\n");
276 fprintf(stderr," calculating float.....");
277 flt=float_test.div_test();
278 fprintf(stderr," float time: %f sec\n",flt);
279 fprintf(stderr," calculating fixed.....");
280 fix=fixed_test.div_test();
281 fprintf(stderr," fixed time: %f sec\n",fix);
282 fprintf(stderr," calculating integer...");
283 inte=int_test.div_test();
284 fprintf(stderr," integer time: %f sec\n",inte);
286 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
288 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
289 fprintf(stderr,"\n");