1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Fixed-Point Math Test
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2007 Chris Moore
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
25 #define ETL_FIXED_BITS 12
31 /* === M A C R O S ========================================================= */
34 # define PI (3.1415926535897932384626433832795029L)
37 #define ADD_SUB_TEST 20000000
38 #define MUL_TEST 10000000
39 #define DIV_TEST 1048573 // at 1048573, fixed point numbers wrap around to zero
42 /* === C L A S S E S ======================================================= */
44 template <class value_type>
47 double add_sub_test(void)
55 for(i=0;i<ADD_SUB_TEST;i++)
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;
63 a+=a; a-=b; a-=c; a+=a;
66 fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
77 const value_type one_and_a_half(static_cast<value_type>(1.5));
81 for(i=1;i<MUL_TEST;i++)
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;
89 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;
92 fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
105 for(i=1;i<DIV_TEST;i++)
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;
112 a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
115 fprintf(stderr, "[%1d]...", int(int(acc)/1e9)+5); // so the compiler doesn't optimize everything out
120 /* === P R O C E D U R E S ================================================= */
133 fprintf(stderr,"fixed: abs() failure on line %d in "__FILE__".\n",__LINE__);
137 d=(double)(fixed(2.5)*fixed(3.0f)/7)-(2.5f*3.0f/7.0f);
138 fprintf(stderr,"fixed: 2.5 * 2 / 7 --- Difference: %f\n",d);
142 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
152 fprintf(stderr,"fixed: ( 1043 / 27 + 10.42 ) / 6 * PI --- Difference: %f\n",d);
154 #ifdef ROUND_TO_NEAREST_INTEGER
160 fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
171 fixed_base<unsigned char,8> fix;
174 if(sizeof(fix)!=sizeof(unsigned char))
177 fprintf(stderr,"fixed: Size of fixed_base<unsigned char,8> is wrong!\n");
182 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
186 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
190 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
194 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
198 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
202 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
206 //fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
210 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
214 fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt);
220 /* === E N T R Y P O I N T ================================================= */
229 speed_test<float> float_test;
230 speed_test<int> int_test;
231 speed_test<fixed> fixed_test;
235 fprintf(stderr,"\nAddition/subtraction test...\n");
237 fprintf(stderr," calculating float.....");
238 flt=float_test.add_sub_test();
239 fprintf(stderr," float time: %f sec\n",flt);
241 fprintf(stderr," calculating fixed.....");
242 fix=fixed_test.add_sub_test();
243 fprintf(stderr," fixed time: %f sec\n",fix);
245 fprintf(stderr," calculating integer...");
246 inte=int_test.add_sub_test();
247 fprintf(stderr," integer time: %f sec\n",inte);
250 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
252 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
258 fprintf(stderr,"\nProduct test...\n");
259 fprintf(stderr," calculating float.....");
260 flt=float_test.mul_test();
261 fprintf(stderr," float time: %f sec\n",flt);
262 fprintf(stderr," calculating fixed.....");
263 fix=fixed_test.mul_test();
264 fprintf(stderr," fixed time: %f sec\n",fix);
265 fprintf(stderr," calculating integer...");
266 inte=int_test.mul_test();
267 fprintf(stderr," integer time: %f sec\n",inte);
269 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
271 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
276 fprintf(stderr,"\nDivision test...\n");
277 fprintf(stderr," calculating float.....");
278 flt=float_test.div_test();
279 fprintf(stderr," float time: %f sec\n",flt);
280 fprintf(stderr," calculating fixed.....");
281 fix=fixed_test.div_test();
282 fprintf(stderr," fixed time: %f sec\n",fix);
283 fprintf(stderr," calculating integer...");
284 inte=int_test.div_test();
285 fprintf(stderr," integer time: %f sec\n",inte);
287 fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
289 fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f);
290 fprintf(stderr,"\n");