Home | History | Annotate | Download | only in fec
      1 /* Test dot-product function */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <memory.h>
      6 #include <math.h>
      7 #include "config.h"
      8 #ifdef HAVE_GETOPT_H
      9 #include <getopt.h>
     10 #endif
     11 #include "fec.h"
     12 
     13 #if HAVE_GETOPT_LONG
     14 struct option Options[] = {
     15   {"force-altivec",0,NULL,'a'},
     16   {"force-port",0,NULL,'p'},
     17   {"force-mmx",0,NULL,'m'},
     18   {"force-sse",0,NULL,'s'},
     19   {"force-sse2",0,NULL,'t'},
     20   {"trials",0,NULL,'n'},
     21   {NULL},
     22 };
     23 #endif
     24 
     25 int main(int argc,char *argv[]){
     26   short coeffs[512];
     27   short input[2048];
     28   int trials=1000,d;
     29   int errors = 0;
     30 
     31 #if HAVE_GETOPT_LONG
     32   while((d = getopt_long(argc,argv,"apmstn:",Options,NULL)) != EOF){
     33 #else
     34   while((d = getopt(argc,argv,"apmstn:")) != EOF){
     35 #endif
     36     switch(d){
     37     case 'a':
     38       Cpu_mode = ALTIVEC;
     39       break;
     40     case 'p':
     41       Cpu_mode = PORT;
     42       break;
     43     case 'm':
     44       Cpu_mode = MMX;
     45       break;
     46     case 's':
     47       Cpu_mode = SSE;
     48       break;
     49     case 't':
     50       Cpu_mode = SSE2;
     51       break;
     52     case 'n':
     53       trials = atoi(optarg);
     54       break;
     55     }
     56   }
     57 
     58   while(trials--){
     59     long port_result;
     60     long simd_result;
     61     int ntaps;
     62     int i;
     63     int csum = 0;
     64     int offset;
     65     void *dp_simd,*dp_port;
     66 
     67     /* Generate set of coefficients
     68      * limit sum of absolute values to 32767 to avoid overflow
     69      */
     70     memset(coeffs,0,sizeof(coeffs));
     71     for(i=0;i<512;i++){
     72       double gv;
     73 
     74       gv = normal_rand(0.,100.);
     75       if(csum + fabs(gv) > 32767)
     76 	break;
     77       coeffs[i] = gv;
     78       csum += fabs(gv);
     79     }
     80     ntaps = i;
     81 
     82     /* Compare results to portable C version for a bunch of random data buffers and offsets */
     83     dp_simd = initdp(coeffs,ntaps);
     84     dp_port = initdp_port(coeffs,ntaps);
     85 
     86     for(i=0;i<2048;i++)
     87       input[i] = random();
     88 
     89     offset = random() & 511;
     90 
     91     simd_result = dotprod(dp_simd,input+offset);
     92     port_result = dotprod_port(dp_port,input+offset);
     93     if(simd_result != port_result){
     94       errors++;
     95     }
     96   }
     97   printf("dtest: %d errors\n",errors);
     98   exit(0);
     99 }
    100