X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ETL%2Ftags%2FETL_0_04_10_rc3%2Ftest%2Ffixed.cpp;fp=ETL%2Ftags%2FETL_0_04_10_rc3%2Ftest%2Ffixed.cpp;h=16cca250e7c21099d157d88eb26df6bdd6a2cf61;hb=7a4d1b9ab079cc84ccfb4cf46f074c84b8caef2e;hp=0000000000000000000000000000000000000000;hpb=122d8294382e6363da0203ab321880418492a05d;p=synfig.git diff --git a/ETL/tags/ETL_0_04_10_rc3/test/fixed.cpp b/ETL/tags/ETL_0_04_10_rc3/test/fixed.cpp new file mode 100644 index 0000000..16cca25 --- /dev/null +++ b/ETL/tags/ETL_0_04_10_rc3/test/fixed.cpp @@ -0,0 +1,370 @@ +/*! ======================================================================== +** Extended Template and Library Test Suite +** Fixed-Point Math Test +** $Id$ +** +** Copyright (c) 2002 Robert B. Quattlebaum Jr. +** +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. +** +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** +** === N O T E S =========================================================== +** +** ========================================================================= */ + +/* === H E A D E R S ======================================================= */ + +#define ETL_FIXED_BITS 12 + +#include +#include +#include + +/* === M A C R O S ========================================================= */ + +#ifndef PI +# define PI (3.1415926535897932384626433832795029L) +#endif + +#define ADD_SUB_TEST 20000000 +#define MUL_TEST 10000000 +#define DIV_TEST 10000000 +using namespace etl; + +/* === C L A S S E S ======================================================= */ + +template +struct speed_test +{ + double add_sub_test(void) + { + value_type a=1; + value_type b=2; + value_type c=3; + int i; + etl::clock MyTimer; + MyTimer.reset(); + for(i=0;i(0.5)); + int i; + etl::clock MyTimer; + MyTimer.reset(); + for(i=0;i0.0005) + { + fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__); + ret++; + } + + a=1043;d=1043; + a/=27;d/=27; + a+=10.42;d+=10.42; + a/=6;d/=6; + a*=PI;d*=PI; + d-=(double)a; + fprintf(stderr,"fixed: ( 1043 / 27 + 10.42 ) / 6 * PI --- Difference: %f\n",d); + if(d<0.0)d=-d; + if( d>0.0005) + { + fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__); + ret++; + } + + return ret; +} + +int char_test(void) +{ + int ret=0; + + fixed_base fix; + double flt; + + if(sizeof(fix)!=sizeof(unsigned char)) + { + ret++; + fprintf(stderr,"fixed: Size of fixed_base is wrong!\n"); + } + + flt=1.0; + fix=1.0; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=0.7; + fix*=0.7; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=0.7; + fix*=0.7; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=0.7; + fix*=0.7; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=0.7; + fix*=0.7; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=0.7; + fix*=0.7; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + //flt/=0.7; + //fix/=0.7; + //fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt+=0.3; + fix+=0.3; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + flt*=2; + fix*=2; + fprintf(stderr,"fixed: value=%f, data=%d, shouldbe=%f, error=%f\n",(float)fix,fix.data(),flt,(float)fix-flt); + + + return ret; +} + +/* === E N T R Y P O I N T ================================================= */ + +int main() +{ + fprintf(stderr, "\n***\n"); + fprintf(stderr, "*** tests in fixed.cpp fail - see http://wiki.synfig.com/Source:ETL_make_check for an explanation ***\n"); + fprintf(stderr, "***\n\n"); + sleep(5); + + int error=0; + + error+=basic_test(); + error+=char_test(); + + speed_test float_test; + speed_test int_test; + speed_test fixed_test; + + { + double flt,fix,inte; + fprintf(stderr,"Addition/subtraction test..."); + + fprintf(stderr,"calculating float..."); + flt=float_test.add_sub_test(); + fprintf(stderr,"float time: %fsec\n",flt); + + fprintf(stderr,"calculating fixed..."); + fix=fixed_test.add_sub_test(); + fprintf(stderr,"fixed time: %fsec\n",fix); + + fprintf(stderr,"calculating integer..."); + inte=int_test.add_sub_test(); + fprintf(stderr,"integer time: %fsec\n",inte); + + if(flt>fix) + fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f); + else + fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f); + + } + + { + double flt,fix,inte; + fprintf(stderr,"Product test..."); + fprintf(stderr,"calculating float..."); + flt=float_test.mul_test(); + fprintf(stderr,"float time: %fsec\n",flt); + fprintf(stderr,"calculating fixed..."); + fix=fixed_test.mul_test(); + fprintf(stderr,"fixed time: %fsec\n",fix); + fprintf(stderr,"calculating integer..."); + inte=int_test.mul_test(); + fprintf(stderr,"integer time: %fsec\n",inte); + if(flt>fix) + fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f); + else + fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f); + + } + + { + double flt,fix,inte; + fprintf(stderr,"Division test..."); + fprintf(stderr,"calculating float..."); + flt=float_test.div_test(); + fprintf(stderr,"float time: %fsec\n",flt); + fprintf(stderr,"calculating fixed..."); + fix=fixed_test.div_test(); + fprintf(stderr,"fixed time: %fsec\n",fix); + fprintf(stderr,"calculating integer..."); + inte=int_test.div_test(); + fprintf(stderr,"integer time: %fsec\n",inte); + if(flt>fix) + fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f); + else + fprintf(stderr,"Floating point wins by %f seconds! (%f%% faster)\n",fix-flt,fix/flt*100.0f-100.0f); + + } + + return error; +}