home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / whetbenm.zip / WHETD.C
C/C++ Source or Header  |  1995-09-17  |  15KB  |  647 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  *      Whetstone benchmark in C.  This program is a translation of the   *
  4.  *      original Algol version in "A Synthetic Benchmark" by H.J. Curnow  *
  5.  *      and B.A. Wichman in Computer Journal, Vol  19 #1, February 1976.  *
  6.  *                                                                        *
  7.  *      Used to test compiler efficiency, optimization, and double        *
  8.  *      precision floating-point performance.  This version is specific   *
  9.  *      to the Turbo-Amiga and Amiga but it can be easily adapted to      *
  10.  *      other systems by replacing the clock() routine with your own. *
  11.  *                                                                        *
  12.  **************************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <time.h>
  17. #include <stdlib.h>
  18.  
  19. /* Specify pragma on(387); for the best High C timings with a 387. */
  20.  
  21. #define ITERATIONS   10       /* 1 Million Whetstone instructions    */
  22.  
  23. /* #define POUT  */           /* Leave as is for 'Official' result.  */
  24.  
  25. /* #define MTEST */           /* define for Module timing results    */
  26.                               /* only.  Leave as is for 'Official'   */
  27.                               /* result.                             */
  28.  
  29. double   e1[4],e2[4];
  30. double   t, t1, t2, x, y, z, x1, x2, x3, x4;
  31. long     i, j, k, l;
  32. long     n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11;
  33. long     m, m1, m2, loops, KWhets;
  34.  
  35. clock_t  starttime, stoptime, nulltime;
  36. clock_t  sumtime, benchtime;
  37.  
  38. void main(void)
  39. {
  40.    printf("\n   Whetstone C Benchmark V1.0\n");
  41.  
  42.    starttime = clock();               /* Calculate timer call delay */
  43.    stoptime  = clock();
  44.    nulltime  = stoptime - starttime;
  45.  
  46.    /************************/
  47.    /* initialize constants */
  48.    /************************/
  49.  
  50.    t   =   0.499975;
  51.    t1  =   0.500250;
  52.    t2  =   2.0;
  53.  
  54.    /***********************/
  55.    /* Set Module Weights. */
  56.    /***********************/
  57.  
  58.    m = 10;                    /* m = 10 is used to obtain better timing  */
  59.    loops = m * ITERATIONS;    /* accuracy only.  Slow systems should use */
  60.    n1  =   0 * loops;         /* m = 1.                                  */
  61.    n2  =  12 * loops;
  62.    n3  =  14 * loops;
  63.    n4  = 345 * loops;
  64.    n5  =   0 * loops;
  65.    n6  = 210 * loops;
  66.    n7  =  32 * loops;
  67.    n8  = 899 * loops;
  68.    n9  = 616 * loops;
  69.    n10 =   0 * loops;
  70.    n11 =  93 * loops;
  71.  
  72.  
  73.                               /* Start Timing the Whetstone here. */
  74.    starttime = clock();
  75.  
  76.    /*********************************/
  77.    /* MODULE 1:  simple identifiers */
  78.    /*********************************/
  79.  
  80.    x1 =  1.0;
  81.    x2 = -1.0;
  82.    x3 = -1.0;
  83.    x4 = -1.0;
  84.  
  85.    if( n1 > 0 )
  86.    {
  87.      for(i = 1; i <= n1; i++)
  88.      {
  89.       x1 = ( x1 + x2 + x3 - x4 ) * t;
  90.       x2 = ( x1 + x2 - x3 - x4 ) * t;
  91.       x3 = ( x1 - x2 + x3 + x4 ) * t;
  92.       x4 = (-x1 + x2 + x3 + x4 ) * t;
  93.      }
  94.    }
  95.  
  96. #ifdef MTEST                                    /* Module 1 Run Time */
  97.    stoptime  = clock();
  98.    benchtime = stoptime - starttime - nulltime;
  99.    m = 1;
  100.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  101.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  102. #endif
  103.  
  104. #ifdef POUT
  105.    Pout(n1, n1, n1, x1, x2, x3, x4);
  106. #endif
  107.  
  108.    /*****************************/
  109.    /* MODULE 2:  Array Elements */
  110.    /*****************************/
  111. #ifdef MTEST
  112.    starttime = clock();
  113. #endif
  114.  
  115.    e1[0] =  1.0;        /* Start at element 0 in C, vice 1 in Fortran */
  116.    e1[1] = -1.0;
  117.    e1[2] = -1.0;
  118.    e1[3] = -1.0;
  119.  
  120.    if( n2 > 0 )
  121.    {
  122.      for (i = 1; i <= n2; i++)
  123.      {
  124.       e1[0] = ( e1[0] + e1[1] + e1[2] - e1[3] ) * t;
  125.       e1[1] = ( e1[0] + e1[1] - e1[2] + e1[3] ) * t;
  126.       e1[2] = ( e1[0] - e1[1] + e1[2] + e1[3] ) * t;
  127.       e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3] ) * t;
  128.      }
  129.    }
  130.  
  131. #ifdef MTEST
  132.    stoptime  = clock();
  133.    benchtime = stoptime - starttime - nulltime;
  134.    sumtime   = benchtime;
  135.    m = 2;
  136.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  137.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  138. #endif
  139.  
  140. #ifdef POUT
  141.    Pout(n2, n3, n2, e1[0], e1[1], e1[2], e1[3]);
  142. #endif
  143.  
  144.    /*********************************/
  145.    /* MODULE 3:  Array as Parameter */
  146.    /*********************************/
  147. #ifdef MTEST
  148.    starttime = clock();
  149. #endif
  150.  
  151.    if( n3 > 0 )
  152.    {
  153.      for (i = 1; i <= n3; i++)
  154.      {
  155.       pa(e1);
  156.      }
  157.    }
  158.  
  159. #ifdef MTEST
  160.    stoptime  = clock();
  161.    benchtime = stoptime - starttime - nulltime;
  162.    m1 = benchtime;
  163.    m = 3;
  164.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  165.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  166. #endif
  167.  
  168. #ifdef POUT
  169.    Pout(n3, n2, n2, e1[0], e1[1], e1[2], e1[3]);
  170. #endif
  171.  
  172.    /********************************/
  173.    /* MODULE 4:  Conditional Jumps */
  174.    /********************************/
  175. #ifdef MTEST
  176.    starttime = clock();
  177. #endif
  178.  
  179.    j = 1;
  180.  
  181.    if( n4 > 0 )
  182.    {
  183.      for (i = 1; i <= n4; i++)
  184.      {
  185.       if (j == 1)
  186.          j = 2;
  187.       else
  188.          j = 3;
  189.  
  190.       if (j > 2)
  191.          j = 0;
  192.       else
  193.          j = 1;
  194.  
  195.       if (j < 1 )
  196.          j = 1;
  197.       else
  198.          j = 0;
  199.      }
  200.    }
  201.  
  202. #ifdef MTEST
  203.    stoptime  = clock();
  204.    benchtime = stoptime - starttime - nulltime;
  205.    sumtime   = sumtime + benchtime;
  206.    m = 4;
  207.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  208.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  209.    starttime = clock();
  210. #endif
  211.  
  212. #ifdef POUT
  213.    Pout(n4, j, j, x1, x2, x3, x4);
  214. #endif
  215.  
  216.    /**********************/
  217.    /* MODULE 5:  Omitted */
  218.    /**********************/
  219.  
  220.  
  221. #ifdef MTEST
  222.    benchtime = 0;
  223.    m = 5;
  224.    x = 0.0;
  225.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  226. #endif
  227.  
  228. #ifdef POUT
  229.    Pout(n4, j, j, x1, x2, x3, x4);
  230. #endif
  231.  
  232.    /*********************************/
  233.    /* MODULE 6:  Integer Arithmetic */
  234.    /*********************************/
  235. #ifdef MTEST
  236.    starttime = clock();
  237. #endif
  238.  
  239.    j = 1;
  240.    k = 2;
  241.    l = 3;
  242.  
  243.    if( n6 > 0 )
  244.    {
  245.      for (i = 1; i <= n6; i++)
  246.      {
  247.       j = j * (k - j) * (l -k);
  248.       k = l * k - (l - j) * k;
  249.       l = (l - k) * (k + j);
  250.  
  251.       e1[l - 2] = j + k + l;          /* Remember we started at e1[0]. */
  252.       e1[k - 2] = j * k * l;          /* l-2 in C, vice l-1 in Fortran */
  253.      }
  254.    }
  255.  
  256. #ifdef MTEST
  257.    stoptime  = clock();
  258.    benchtime = stoptime - starttime - nulltime;
  259.    sumtime   = sumtime + benchtime;
  260.    m = 6;
  261.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  262.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  263. #endif
  264.  
  265. #ifdef POUT
  266.    Pout(n6, j, k, e1[0], e1[1], e1[2], e1[3]);
  267. #endif
  268.  
  269.    /**************************************/
  270.    /* MODULE 7:  Trigonometric Functions */
  271.    /**************************************/
  272. #ifdef MTEST
  273.    starttime = clock();
  274. #endif
  275.  
  276.    x = 0.5;
  277.    y = 0.5;
  278.  
  279.    if( n7 > 0 )
  280.    {
  281.      for(i = 1; i <= n7; i++)
  282.      {
  283.       x = t * atan(t2*sin(x)*cos(x)/(cos(x+y)+cos(x-y)-1.0));
  284.       y = t * atan(t2*sin(y)*cos(y)/(cos(x+y)+cos(x-y)-1.0));
  285.      }
  286.    }
  287.  
  288. #ifdef MTEST
  289.    stoptime  = clock();
  290.    benchtime = stoptime - starttime - nulltime;
  291.    sumtime   = sumtime + benchtime;
  292.    m = 7;
  293.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  294.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x1);
  295. #endif
  296.  
  297. #ifdef POUT
  298.    Pout(n7, j, k, x, x, y, y);
  299. #endif
  300.  
  301.    /******************************/
  302.    /* MODULE 8:  Procedure Calls */
  303.    /******************************/
  304. #ifdef MTEST
  305.    starttime = clock();
  306. #endif
  307.  
  308.    x = 1.0;
  309.    y = 1.0;
  310.    z = 1.0;
  311.  
  312.    if( n8 > 0 )
  313.    {
  314.      for (i = 1; i <= n8; i++)
  315.      {
  316.       p3(x, y, &z);
  317.      }
  318.    }
  319.  
  320. #ifdef MTEST
  321.    stoptime  = clock();
  322.    benchtime = stoptime - starttime - nulltime;
  323.    m1 = m1 + benchtime;
  324.    m = 8;
  325.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  326.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x1);
  327. #endif
  328.  
  329. #ifdef POUT
  330.    Pout(n8, j, k, x, y, z, z);
  331. #endif
  332.  
  333.    /*******************************/
  334.    /* MODULE 9:  Array References */
  335.    /*******************************/
  336. #ifdef MTEST
  337.    starttime = clock();
  338. #endif
  339.  
  340.    j = 1;
  341.    k = 2;
  342.    l = 3;
  343.  
  344.    e1[0] = 1.0;
  345.    e1[1] = 2.0;
  346.    e1[2] = 3.0;
  347.  
  348.    if( n9 > 0 )
  349.    {
  350.      for(i = 1; i <= n9; i++)
  351.      {
  352.       p0();
  353.      }
  354.    }
  355.  
  356. #ifdef MTEST
  357.    stoptime  = clock();
  358.    benchtime = stoptime - starttime - nulltime;
  359.    m1 = m1 + benchtime;
  360.    m = 9;
  361.    x = (double)benchtime/(double)CLOCKS_PER_SEC;
  362.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x);
  363. #endif
  364.  
  365. #ifdef POUT
  366.    Pout(n9, j, k, e1[0], e1[1], e1[2], e1[3]);
  367. #endif
  368.  
  369.    /**********************************/
  370.    /* MODULE 10:  Integer Arithmetic */
  371.    /**********************************/
  372. #ifdef MTEST
  373.    starttime = clock();
  374. #endif
  375.  
  376.    j = 2;
  377.    k = 3;
  378.  
  379.    if( n10 > 0 )
  380.    {
  381.      for(i = 1; i <= n10; i++)
  382.      {
  383.       j = j + k;
  384.       k = j + k;
  385.       j = k - j;
  386.       k = k - j - j;
  387.      }
  388.    }
  389.  
  390. #ifdef MTEST
  391.    stoptime  = clock();
  392.    benchtime = 0;
  393.    m = 10;
  394.    x = 0.0;
  395.    printf("   End Module %ld.  Benchtime(sec) = %lf\n",m,x);
  396. #endif
  397.  
  398. #ifdef POUT
  399.    Pout(n10, j, k, x1, x2, x3, x4);
  400. #endif
  401.  
  402.    /**********************************/
  403.    /* MODULE 11:  Standard Functions */
  404.    /**********************************/
  405. #ifdef MTEST
  406.    starttime = clock();
  407. #endif
  408.  
  409.    x = 0.75;
  410.  
  411.    if( n11 > 0 )
  412.    {
  413.      for(i = 1; i <= n11; i++)
  414.      {
  415.       x = sqrt( exp( log(x) / t1) );
  416.      }
  417.    }
  418.  
  419. #ifdef MTEST
  420.    stoptime  = clock();
  421.    benchtime = stoptime - starttime - nulltime;
  422.    sumtime   = sumtime + benchtime;
  423.    m = 11;
  424.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  425.    printf("   End Module %ld.  Benchtime(sec) = %lf\n\n",m,x1);
  426. #endif
  427.  
  428. #ifdef POUT
  429.    Pout(n11, j, k, x, x, x, x);
  430. #endif
  431.  
  432.    /********************************************************/
  433.    /* MODULE 12:  Array as a Parameter.                    */
  434.    /* Same as Module 3 except Subroutine overhead removed. */
  435.    /********************************************************/
  436.  
  437. #ifdef MTEST
  438.  
  439.    starttime = clock();
  440.    if( n3 > 0 )
  441.    {
  442.      for (i = 1; i <= n3; i++)
  443.      {
  444.       j = 0;
  445.         lab:
  446.       e2[0] = (  e2[0] + e2[1] + e2[2] - e2[3] ) * t;
  447.       e2[1] = (  e2[0] + e2[1] - e2[2] + e2[3] ) * t;
  448.       e2[2] = (  e2[0] - e2[1] + e2[2] + e2[3] ) * t;
  449.       e2[3] = ( -e2[0] + e2[1] + e2[2] + e2[3] ) /t2;
  450.       j++;
  451.  
  452.       if (j < 6)
  453.          goto lab;
  454.      }
  455.    }
  456.  
  457.    stoptime  = clock();
  458.    benchtime = stoptime - starttime - nulltime;
  459.    m2   = benchtime;
  460.    m = 3;
  461.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  462.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x1);
  463.  
  464. #endif
  465.  
  466. #ifdef POUT
  467.    Pout(n11, j, k, x, x, x, x);
  468. #endif
  469.  
  470.    /********************************************************/
  471.    /* MODULE 13:  Array as a Parameter.                    */
  472.    /* Same as Module 8 except Subroutine overhead removed. */
  473.    /********************************************************/
  474.  
  475. #ifdef MTEST
  476.  
  477.    starttime = clock();
  478.    x = 1.0;
  479.    y = 1.0;
  480.    z = 1.0;
  481.  
  482.    if( n8 > 0 )
  483.    {
  484.      for (i = 1; i <= n8; i++)
  485.      {
  486.       x = t * (x + y);
  487.       y = t * (x + y);
  488.       z = (x + y) /t2;
  489.      }
  490.    }
  491.  
  492.    stoptime  = clock();
  493.    benchtime = stoptime - starttime - nulltime;
  494.    m2   = m2 + benchtime;
  495.    m = 8;
  496.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  497.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x1);
  498.  
  499. #endif
  500.  
  501. #ifdef POUT
  502.    Pout(n11, j, k, x, x, x, x);
  503. #endif
  504.  
  505.    /********************************************************/
  506.    /* MODULE 14:  Array as a Parameter.                    */
  507.    /* Same as Module 9 except Subroutine overhead removed. */
  508.    /********************************************************/
  509.  
  510. #ifdef MTEST
  511.  
  512.    starttime = clock();
  513.    j = 1;
  514.    k = 2;
  515.    l = 3;
  516.  
  517.    e1[0] = 1.0;
  518.    e1[1] = 2.0;
  519.    e1[2] = 3.0;
  520.  
  521.    if( n9 > 0 )
  522.    {
  523.      for(i = 1; i <= n9; i++)
  524.      {
  525.       e1[j] = e1[k];
  526.       e1[k] = e1[l];
  527.       e1[l] = e1[j];
  528.      }
  529.    }
  530.  
  531.    stoptime  = clock();
  532.    benchtime = stoptime - starttime - nulltime;
  533.    m2 = m2 + benchtime;
  534.    m = 9;
  535.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  536.    printf("   End Module %ld.   Benchtime(sec) = %lf\n",m,x1);
  537.  
  538. #endif
  539.  
  540. #ifdef POUT
  541.    Pout(n11, j, k, x, x, x, x);
  542. #endif
  543.  
  544.    /**************************/
  545.    /* End of Whetstone Tests */
  546.    /**************************/
  547.  
  548. #ifdef MTEST
  549.    m1 = m1 + sumtime;
  550.    m2 = m2 + sumtime;
  551.    x1 = (double)m1/(double)CLOCKS_PER_SEC;
  552.    x2 = (double)m2/(double)CLOCKS_PER_SEC;
  553.  
  554.    printf("Standard Whetstone Result\n");
  555.    printf("   Benchtime(sec) = %lf\n",x1);
  556.    x  = 100.0 * (double)loops / x1;
  557.    KWhets = (long)x;
  558.    printf("   KWhets/sec     = %ld\n",KWhets);
  559.  
  560.    printf("Result with subroutine call replaced with subroutine code\n");
  561.    printf("   Benchtime(sec) = %lf\n",x2);
  562.    x  = 100.0 * (double)loops / x2;
  563.    KWhets = (long)x;
  564.    printf("   KWhets/sec     = %ld\n",KWhets);
  565.  
  566. #else
  567.  
  568.    stoptime  = clock();
  569.    benchtime = stoptime - starttime - nulltime;
  570.    x1 = (double)benchtime/(double)CLOCKS_PER_SEC;
  571.    printf("   Benchtime(sec) = %lf\n",x1);
  572.    x2 = 100.0 * (double)loops / x1;
  573.    KWhets = (long)x2;
  574.    printf("   KWhets/sec     = %ld\n\n",KWhets);
  575.  
  576. #endif
  577.  
  578.    exit(0);
  579.  
  580. }
  581.  
  582. /*******************/
  583. /* Subroutine pa() */
  584. /*******************/
  585.  
  586. pa(e)                /* Exactly as in the Algol 60 version, but we */
  587.                      /* could do away with that 'goto'.            */
  588. double e[4];
  589.  
  590. {
  591.    int j;
  592.  
  593.    j = 0;
  594.      lab:
  595.    e[0] = (  e[0] + e[1] + e[2] - e[3] ) * t;
  596.    e[1] = (  e[0] + e[1] - e[2] + e[3] ) * t;
  597.    e[2] = (  e[0] - e[1] + e[2] + e[3] ) * t;
  598.    e[3] = ( -e[0] + e[1] + e[2] + e[3] ) / t2;
  599.    j ++;
  600.  
  601.    if (j < 6)
  602.       goto lab;
  603. }
  604.  
  605. /************************/
  606. /* Subroutine p3(x,y,z) */
  607. /************************/
  608.  
  609. p3(x, y, z)
  610.  
  611. double x, y, *z;
  612.  
  613. {
  614.    x  = t * (x + y);
  615.    y  = t * (x + y);
  616.    *z = (x + y) /t2;
  617. }
  618.  
  619. /*******************/
  620. /* Subroutine p0() */
  621. /*******************/
  622.  
  623. p0()
  624. {
  625.    e1[j] = e1[k];
  626.    e1[k] = e1[l];
  627.    e1[l] = e1[j];
  628. }
  629.  
  630. /*********************/
  631. /* Subroutine Pout() */
  632. /*********************/
  633.  
  634. #ifdef POUT
  635. Pout(n, j, k, x1, x2, x3, x4)
  636.  
  637. long  n, j, k;
  638. double x1, x2, x3, x4;
  639.  
  640. {
  641.    printf("%5ld %5ld %5ld   %11.3le %11.3le %11.3le %11.3le\n",
  642.       n, j, k, x1, x2, x3, x4);
  643. }
  644. #endif
  645.  
  646. /*-- End Of Whetstone C Source Code -----------------*/
  647.