home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / BENCHMARKS / whetstone.c < prev   
C/C++ Source or Header  |  2009-11-06  |  4KB  |  241 lines

  1. /*
  2.  *      Whetstone benchmark in C.  This program is a translation of the
  3.  *    original Algol version in "A Synthetic Benchmark" by H.J. Curnow
  4.  *      and B.A. Wichman in Computer Journal, Vol  19 #1, February 1976.
  5.  *
  6.  *    Used to test compiler optimization and floating point performance.
  7.  *
  8.  *    Compile by:        cc -O -s -o whet whet.c
  9.  *    or:            cc -O -DPOUT -s -o whet whet.c
  10.  *    if output is desired.
  11.  */
  12.  
  13. #define ITERATIONS    20 /* 1 Million Whetstone instructions */
  14.  
  15. #include "math.h"
  16. #include <time.h>
  17.  
  18. double        x1, x2, x3, x4, x, y, z, t, t1, t2;
  19. double         e1[4];
  20. int        i, j, k, l, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
  21. time_t start_t, end_t;
  22. double secs;
  23.  
  24. main()
  25. {
  26.  
  27.     /* initialize constants */
  28.  
  29.     t   =   0.499975;
  30.     t1  =   0.50025;
  31.     t2  =   2.0;
  32.  
  33.     /* set values of module weights */
  34.  
  35.     n1  =   0 * ITERATIONS;
  36.     n2  =  12 * ITERATIONS;
  37.     n3  =  14 * ITERATIONS;
  38.     n4  = 345 * ITERATIONS;
  39.     n6  = 210 * ITERATIONS;
  40.     n7  =  32 * ITERATIONS;
  41.     n8  = 899 * ITERATIONS;
  42.     n9  = 616 * ITERATIONS;
  43.     n10 =   0 * ITERATIONS;
  44.     n11 =  93 * ITERATIONS;
  45.  
  46.     start_t = clock();
  47.  
  48. /* MODULE 1:  simple identifiers */
  49.  
  50.     x1 =  1.0;
  51.     x2 = x3 = x4 = -1.0;
  52.  
  53.     for(i = 1; i <= n1; i += 1) {
  54.         x1 = ( x1 + x2 + x3 - x4 ) * t;
  55.         x2 = ( x1 + x2 - x3 - x4 ) * t;
  56.         x3 = ( x1 - x2 + x3 + x4 ) * t;
  57.         x4 = (-x1 + x2 + x3 + x4 ) * t;
  58.     }
  59. #ifdef POUT
  60.     pout(n1, n1, n1, x1, x2, x3, x4);
  61. #endif
  62.  
  63.  
  64. /* MODULE 2:  array elements */
  65.  
  66.     e1[0] =  1.0;
  67.     e1[1] = e1[2] = e1[3] = -1.0;
  68.  
  69.     for (i = 1; i <= n2; i +=1) {
  70.         e1[0] = ( e1[0] + e1[1] + e1[2] - e1[3] ) * t;
  71.         e1[1] = ( e1[0] + e1[1] - e1[2] + e1[3] ) * t;
  72.         e1[2] = ( e1[0] - e1[1] + e1[2] + e1[3] ) * t;
  73.         e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3] ) * t;
  74.     }
  75. #ifdef POUT
  76.     pout(n2, n3, n2, e1[0], e1[1], e1[2], e1[3]);
  77. #endif
  78.  
  79. /* MODULE 3:  array as parameter */
  80.  
  81.     for (i = 1; i <= n3; i += 1)
  82.         pa(e1);
  83. #ifdef POUT
  84.     pout(n3, n2, n2, e1[0], e1[1], e1[2], e1[3]);
  85. #endif
  86.  
  87. /* MODULE 4:  conditional jumps */
  88.  
  89.     j = 1;
  90.     for (i = 1; i <= n4; i += 1) {
  91.         if (j == 1)
  92.             j = 2;
  93.         else
  94.             j = 3;
  95.  
  96.         if (j > 2)
  97.             j = 0;
  98.         else
  99.             j = 1;
  100.  
  101.         if (j < 1 )
  102.             j = 1;
  103.         else
  104.             j = 0;
  105.     }
  106. #ifdef POUT
  107.     pout(n4, j, j, x1, x2, x3, x4);
  108. #endif
  109.  
  110. /* MODULE 5:  omitted */
  111.  
  112. /* MODULE 6:  integer arithmetic */
  113.  
  114.     j = 1;
  115.     k = 2;
  116.     l = 3;
  117.  
  118.     for (i = 1; i <= n6; i += 1) {
  119.         j = j * (k - j) * (l -k);
  120.         k = l * k - (l - j) * k;
  121.         l = (l - k) * (k + j);
  122.  
  123.         e1[l - 2] = j + k + l;        /* C arrays are zero based */
  124.         e1[k - 2] = j * k * l;
  125.     }
  126. #ifdef POUT
  127.     pout(n6, j, k, e1[0], e1[1], e1[2], e1[3]);
  128. #endif
  129.  
  130. /* MODULE 7:  trig. functions */
  131.  
  132.     x = y = 0.5;
  133.  
  134.     for(i = 1; i <= n7; i +=1) {
  135.         x = t * atan(t2*sin(x)*cos(x)/(cos(x+y)+cos(x-y)-1.0));
  136.         y = t * atan(t2*sin(y)*cos(y)/(cos(x+y)+cos(x-y)-1.0));
  137.     }
  138. #ifdef POUT
  139.     pout(n7, j, k, x, x, y, y);
  140. #endif
  141.  
  142. /* MODULE 8:  procedure calls */
  143.  
  144.     x = y = z = 1.0;
  145.  
  146.     for (i = 1; i <= n8; i +=1)
  147.         p3(x, y, &z);
  148. #ifdef POUT
  149.     pout(n8, j, k, x, y, z, z);
  150. #endif
  151.  
  152. /* MODULE9:  array references */
  153.  
  154.     j = 1;
  155.     k = 2;
  156.     l = 3;
  157.  
  158.     e1[0] = 1.0;
  159.     e1[1] = 2.0;
  160.     e1[2] = 3.0;
  161.  
  162.     for(i = 1; i <= n9; i += 1)
  163.         p0();
  164. #ifdef POUT
  165.     pout(n9, j, k, e1[0], e1[1], e1[2], e1[3]);
  166. #endif
  167.  
  168. /* MODULE10:  integer arithmetic */
  169.  
  170.     j = 2;
  171.     k = 3;
  172.  
  173.     for(i = 1; i <= n10; i +=1) {
  174.         j = j + k;
  175.         k = j + k;
  176.         j = k - j;
  177.         k = k - j - j;
  178.     }
  179. #ifdef POUT
  180.     pout(n10, j, k, x1, x2, x3, x4);
  181. #endif
  182.  
  183. /* MODULE11:  standard functions */
  184.  
  185.     x = 0.75;
  186.     for(i = 1; i <= n11; i +=1)
  187.         x = sqrt( exp( log(x) / t1));
  188.  
  189.     end_t = clock();
  190.     secs = (double)(end_t - start_t) / (double)CLK_TCK;
  191.     printf("Seconds = %5.2f\n",secs);
  192.     printf("Whetstones = %12.3f\n",(double)1000000.0 / secs);
  193.  
  194. #ifdef POUT
  195.     pout(n11, j, k, x, x, x, x);
  196. #endif
  197. }
  198.  
  199. pa(e)
  200. double e[4];
  201. {
  202.     register int j;
  203.  
  204.     j = 0;
  205.      lab:
  206.     e[0] = (  e[0] + e[1] + e[2] - e[3] ) * t;
  207.     e[1] = (  e[0] + e[1] - e[2] + e[3] ) * t;
  208.     e[2] = (  e[0] - e[1] + e[2] + e[3] ) * t;
  209.     e[3] = ( -e[0] + e[1] + e[2] + e[3] ) / t2;
  210.     j += 1;
  211.     if (j < 6)
  212.         goto lab;
  213. }
  214.  
  215.  
  216. p3(x, y, z)
  217. double x, y, *z;
  218. {
  219.     x  = t * (x + y);
  220.     y  = t * (x + y);
  221.     *z = (x + y) /t2;
  222. }
  223.  
  224.  
  225. p0()
  226. {
  227.     e1[j] = e1[k];
  228.     e1[k] = e1[l];
  229.     e1[l] = e1[j];
  230. }
  231.  
  232. #ifdef POUT
  233. pout(n, j, k, x1, x2, x3, x4)
  234. int n, j, k;
  235. double x1, x2, x3, x4;
  236. {
  237.     printf("%6d%6d%6d  %5e  %5e  %5e  %5e\n",
  238.         n, j, k, x1, x2, x3, x4);
  239. }
  240. #endif
  241.