home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.3 / IEEE / src / bench / calcpi.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.0 KB  |  152 lines

  1. /*------------------- Start calcpi.c source code -----------------------*/
  2.  
  3. /*****************************/
  4. /*         CalcPI.c          */
  5. /*      Amiga Version        */
  6. /*    Manx Aztec C V3.4B     */
  7. /*       20 Nov 1987         */
  8. /*****************************/
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. /*double atan(),acos(),asin(),tan(),cos(),sin();
  13. double log(),exp(),sqrt();*/
  14.  
  15.  
  16. long     starttime,stoptime,nulltime;
  17. long     looptime,benchtime,timeticks();  /* timeticks() is specific to  */
  18. double   counts_per_sec;                  /* my system. Please replace   */
  19.                                           /* with your own system timer. */
  20.                                           /* Also change 'counts_per_sec'*/
  21.                                           /* to correspond to your timer.*/
  22.  
  23.  
  24. main()
  25. {
  26.    double atanone,one,two,four,SumCheck,SumLog2;
  27.    double sk,xi,xmax;
  28.    double pi_prog,pi_atan,pi_ref,pi_err;
  29.    double sl2_prog,sl2_ref,sl2_err;
  30.    double tloop,tbench,usec,tflops;
  31.    long   i,imax,jmax;
  32.  
  33.    counts_per_sec = 50.0;                 /* Change this to your timer */
  34.                                           /* counts (ticks) per second.*/
  35.  
  36.    imax = 125000;
  37.    xmax = (double)imax;
  38.    four = 4.0;
  39.    pi_ref = 3.1415926535897932;
  40.  
  41.    printf("\n");
  42.    printf("   CalcPI (Double Precision) --------- Version: 20 Nov 1987.\n");
  43.    printf("   A Floating-Point Add, Subtract, Multiply and Divide Test.\n");
  44.    printf("   A total of one million floating-point operations (FLOPS).\n\n");
  45.  
  46.    starttime = timeticks();
  47.    stoptime  = timeticks();
  48.    nulltime  = stoptime - starttime;
  49.  
  50.    sk  = -1.0;
  51.    SumCheck = 0.0;
  52.  
  53. /******************/
  54. /*   First Loop   */
  55. /******************/
  56.    starttime = timeticks();               /* Calculates 'sk', 'SumCheck',*/
  57.                                           /* and 'looptime'.             */
  58.       for ( i = 1 ; i<= imax ; i++ )      /* Correct sign and values of  */
  59.       {                                   /* 'sk' and 'SumCheck' required*/
  60.       sk = -sk;                           /* for Second Loop.            */
  61.       xi = (double)i;
  62.       SumCheck = SumCheck - sk * xi;
  63.       }
  64.  
  65.    stoptime  = timeticks();
  66.    looptime  = stoptime - starttime - nulltime;
  67.  
  68.    atanone = 0.0;
  69.    SumLog2 = 0.0;
  70.    one = 1.0;
  71.    two = 2.0;
  72.    jmax = (long)(two * SumCheck);         /* jmax should be 125000 here, */
  73.                                           /* same as imax (!). jmax is   */
  74.                                           /* is printed out at the end   */
  75.                                           /* of the program.             */
  76.  
  77. /*****************/
  78. /*  Second Loop  */
  79. /*****************/
  80.    starttime = timeticks();               /* Estimates PI,and log(4.0).  */
  81.                                           /* benchtime - looptime is the */
  82.       for ( i = 1 ; i<= jmax ; i++)       /* time (sec) to do 1 million  */
  83.       {                                   /* (+,-,*,/) DP FP operations. */
  84.       sk = -sk;                           /* SumCheck must = xmax (to    */
  85.       xi= (double)i;                      /* machine precision) at the   */
  86.       SumCheck = SumCheck - sk * xi;      /* end of this loop.           */
  87.  
  88.       atanone  = atanone + sk/(two * xi - one);
  89.       SumLog2  = SumLog2 - sk/(xi * (xi + one));
  90.       }
  91.  
  92.    stoptime  = timeticks();
  93.    benchtime = stoptime - starttime - nulltime;
  94.  
  95. /************************************************/
  96. /* Calculate and print out performance results. */
  97. /************************************************/
  98.    pi_prog = four * atanone + one / xmax;
  99.    pi_atan = four * atan(one);
  100.    pi_err  = pi_prog - pi_ref;
  101.  
  102.    sl2_prog= one - SumLog2 + one/(two * xmax * xmax);
  103.    sl2_ref = log(four);
  104.    sl2_err = sl2_prog - sl2_ref;
  105.  
  106.    tloop  = (double)looptime  / counts_per_sec;
  107.    tbench = (double)benchtime / counts_per_sec;
  108.    usec   = tbench - tloop;
  109.    tflops = 1000.0 / usec;
  110.  
  111.    SumCheck = SumCheck - xmax;
  112.  
  113.    printf("   Iterations (125,000) = %9ld\n",jmax);
  114.    printf("   Looptime (sec)       = %9.4lf\n",tloop);
  115.    printf("   Benchtime(sec)       = %9.4lf\n\n",tbench);
  116.  
  117.    printf("   log(4): error        =%11.4le\n",sl2_err);
  118.    printf("   SumCheck             =%11.4le\n\n",SumCheck);
  119.  
  120.    printf("   PI: 4 * atan(1)      = %18.16lf\n",pi_atan);
  121.    printf("   PI: Program          = %18.16lf\n",pi_prog);
  122.    printf("   PI: Reference        = %18.16lf\n",pi_ref);
  123.    printf("   PI: Error            =%11.4le\n\n",pi_err);
  124.  
  125.    printf("   This Program Conducted: %-9.2lf Kflops/sec\n",tflops);
  126.  
  127.    printf("   Average Time per flop:  %-9.2lf Microseconds\n\n",usec);
  128.  
  129. }
  130.  
  131. /**********************************************************/
  132. /* Function returns time ticks (50 * sec) since midnight. */
  133. /* Amiga specific. Replace with your own system routine.  */
  134. /**********************************************************/
  135. long  timeticks()
  136. {
  137.    struct   tt {
  138.       long  days;
  139.       long  minutes;
  140.       long  ticks;
  141.    } tt;
  142.    DateStamp(&tt);
  143.  
  144.    return (tt.ticks + (tt.minutes * 60L * 50L));
  145. }
  146.  
  147. /*---------------------- End calcpi.c source code ----------------------*/
  148.  
  149.  
  150.  
  151.  
  152.