Various changes: Improve the formatting of the output. Stop the compiler optimizing...
authordooglus <dooglus@1f10aa63-cdf2-0310-b900-c93c546f37ac>
Wed, 10 Oct 2007 01:59:21 +0000 (01:59 +0000)
committerdooglus <dooglus@1f10aa63-cdf2-0310-b900-c93c546f37ac>
Wed, 10 Oct 2007 01:59:21 +0000 (01:59 +0000)
git-svn-id: http://svn.voria.com/code@872 1f10aa63-cdf2-0310-b900-c93c546f37ac

ETL/trunk/test/fixed.cpp

index 16cca25..369f0d1 100644 (file)
@@ -35,7 +35,7 @@
 
 #define ADD_SUB_TEST   20000000
 #define MUL_TEST               10000000
-#define DIV_TEST               10000000
+#define DIV_TEST               1048573 // at 1048573, fixed point numbers wrap around to zero
 using namespace etl;
 
 /* === C L A S S E S ======================================================= */
@@ -53,95 +53,42 @@ struct speed_test
                MyTimer.reset();
                for(i=0;i<ADD_SUB_TEST;i++)
                {
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
-                       a+=a;
-                       a+=b;
-                       a-=c;
-                       a-=a;
-
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
+                       a+=a; a-=b; a-=c; a+=a;
                }
 
-
+               fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
                return MyTimer();
        }
 
        double mul_test(void)
        {
                value_type a,b,c,d;
-               a=value_type(0.25);
+               a=value_type(2.25);
                b=value_type(2);
                c=value_type(4.5);
-               const value_type half(static_cast<value_type>(0.5));
+               d=value_type(1);
+               const value_type one_and_a_half(static_cast<value_type>(1.5));
                int i;
                etl::clock MyTimer;
                MyTimer.reset();
-               for(i=0;i<MUL_TEST;i++)
+               for(i=1;i<MUL_TEST;i++)
                {
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
-                       d*=a;d*=b;d*=c;d*=3;d*=i;
-                       b*=c;b*=d;b*=d;b*=3;
-                       c*=d;c*=half;c*=a;c*=b;c*=3;
-                       a*=c;a*=b;a*=3;
-
+                       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;
+                       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;
+                       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;
+                       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;
+                       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;
+                       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;
+                       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;
                }
+
+               fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
                return MyTimer();
        }
 
@@ -149,44 +96,22 @@ struct speed_test
        {
                value_type a(30);
                value_type b(40);
-               int i;
+               value_type acc(0);
+               int i, j;
                etl::clock MyTimer;
                MyTimer.reset();
-               for(i=1;i<DIV_TEST;i++)
-               {
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-                       a=3+i;
-                       b=40+i;
-                       b/=a;
-                       a/=(i%20)+1;
-
-               }
-
-
+               for(j=0;j<10;j++)
+                       for(i=1;i<DIV_TEST;i++)
+                       {
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                               a=3+i; b=40+i; b/=a; a/=(i%20)+1; acc+= a;
+                       }
+
+               fprintf(stderr, "[%1d]...", int(int(a)/1e9)+5); // so the compiler doesn't optimize everything out
                return MyTimer();
        }
 };
@@ -225,7 +150,11 @@ int basic_test(void)
        d-=(double)a;
        fprintf(stderr,"fixed: ( 1043 / 27 + 10.42 ) / 6 * PI --- Difference: %f\n",d);
        if(d<0.0)d=-d;
+#ifdef ROUND_TO_NEAREST_INTEGER
        if( d>0.0005)
+#else
+       if( d>0.0025)
+#endif
        {
                fprintf(stderr,"fixed: Failed test on line %d in "__FILE__".\n",__LINE__);
                ret++;
@@ -291,11 +220,6 @@ int char_test(void)
 
 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();
@@ -307,19 +231,19 @@ int main()
 
        {
                double flt,fix,inte;
-               fprintf(stderr,"Addition/subtraction test...");
+               fprintf(stderr,"\nAddition/subtraction test...\n");
 
-               fprintf(stderr,"calculating float...");
+               fprintf(stderr,"  calculating float.....");
                flt=float_test.add_sub_test();
-               fprintf(stderr,"float time: %fsec\n",flt);
+               fprintf(stderr,"   float time: %f sec\n",flt);
 
-               fprintf(stderr,"calculating fixed...");
+               fprintf(stderr,"  calculating fixed.....");
                fix=fixed_test.add_sub_test();
-               fprintf(stderr,"fixed time: %fsec\n",fix);
+               fprintf(stderr,"   fixed time: %f sec\n",fix);
 
-               fprintf(stderr,"calculating integer...");
+               fprintf(stderr,"  calculating integer...");
                inte=int_test.add_sub_test();
-               fprintf(stderr,"integer time: %fsec\n",inte);
+               fprintf(stderr," integer time: %f sec\n",inte);
 
                if(flt>fix)
                        fprintf(stderr,"Fixed point wins by %f seconds! (%f%% faster)\n",flt-fix,flt/fix*100.0f-100.0f);
@@ -330,40 +254,39 @@ int main()
 
        {
                double flt,fix,inte;
-               fprintf(stderr,"Product test...");
-               fprintf(stderr,"calculating float...");
+               fprintf(stderr,"\nProduct test...\n");
+               fprintf(stderr,"  calculating float.....");
                flt=float_test.mul_test();
-               fprintf(stderr,"float time: %fsec\n",flt);
-               fprintf(stderr,"calculating fixed...");
+               fprintf(stderr,"   float time: %f sec\n",flt);
+               fprintf(stderr,"  calculating fixed.....");
                fix=fixed_test.mul_test();
-               fprintf(stderr,"fixed time: %fsec\n",fix);
-               fprintf(stderr,"calculating integer...");
+               fprintf(stderr,"   fixed time: %f sec\n",fix);
+               fprintf(stderr,"  calculating integer...");
                inte=int_test.mul_test();
-               fprintf(stderr,"integer time: %fsec\n",inte);
+               fprintf(stderr," integer time: %f sec\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...");
+               fprintf(stderr,"\nDivision test...\n");
+               fprintf(stderr,"  calculating float.....");
                flt=float_test.div_test();
-               fprintf(stderr,"float time: %fsec\n",flt);
-               fprintf(stderr,"calculating fixed...");
+               fprintf(stderr,"   float time: %f sec\n",flt);
+               fprintf(stderr,"  calculating fixed.....");
                fix=fixed_test.div_test();
-               fprintf(stderr,"fixed time: %fsec\n",fix);
-               fprintf(stderr,"calculating integer...");
+               fprintf(stderr,"   fixed time: %f sec\n",fix);
+               fprintf(stderr,"  calculating integer...");
                inte=int_test.div_test();
-               fprintf(stderr,"integer time: %fsec\n",inte);
+               fprintf(stderr," integer time: %f sec\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);
-
+               fprintf(stderr,"\n");
        }
 
        return error;