home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / SMARTMAG / V2.MSA / BENCHMRK.STQ_BENCHMRK.C < prev    next >
C/C++ Source or Header  |  2003-12-17  |  9KB  |  270 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  The Dhampstone Benchmark.  Written by Jack Purdum.   Ver. 1.1          */
  4. /*        Adapted for Alcyon C by Arick Anders and Michael Bendio.         */
  5. /*                                                                         */
  6. /*  START magazine, Fall 1986.                                             */
  7. /*                                                                         */
  8. /*  The Dhampstone benchmark was first published in Computer Languages     */
  9. /*       February 1985.  It is used here by permission.                    */
  10. /*                                                                         */
  11. /***************************************************************************/
  12.         /*        version 1.1, June 13, 1986                        */
  13.  
  14. #include <stdio.h>
  15.  
  16. #define FIB         24
  17. #define TINY        100
  18. #define MAXINT      179
  19. #define LITTLE      1000
  20. #define SMALL       9000
  21. #define PRECISION   .000001
  22. #define FILENAME    "zyxw.vut"
  23. #define NUMTEST     6
  24.  
  25. #ifndef ERR
  26. #define ERR     -1
  27. #endif
  28. #ifndef void
  29. #define void int
  30. #endif
  31.  
  32.         struct {
  33.                int      cresult;
  34.                int      iresult;
  35.                int      cprsult;
  36.                unsigned uresult;
  37.                long     lresult;
  38.                double   dresult;
  39.                } results;
  40.  
  41.  
  42. /* timing stuff */
  43. #include <osbind.h>
  44. #define Supexec(a) xbios(38, a)
  45.  
  46. long *ptr, *_vbclock = (long *)0x462;
  47.  
  48. gettime()
  49. {
  50.      *ptr = *_vbclock;
  51. }
  52. /* end of timing stuff */
  53.  
  54.         main()
  55.         {
  56.            char buf1[TINY], buf2[TINY];
  57.            int i = 0;
  58.            unsigned fib();
  59.            long square, sq(), t, t2;
  60.            double dmath, sroot(), dply();
  61.  
  62.            printf("Start...\n\n");
  63.  
  64.            while (i < NUMTEST) {
  65.               switch(i) {
  66.                  case (0):                             /* Character test  */
  67.                    ptr = &t;
  68.                    Supexec(gettime);
  69.                    ptr = &t2;
  70.                    results.cresult = stest(buf1, buf2);
  71.                    Supexec(gettime);
  72.                    printf("\ncresult = %d\n", results.cresult);
  73.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  74.                    break;
  75.                  case (1):
  76.                    ptr = &t;
  77.                    Supexec(gettime);
  78.                    ptr = &t2;
  79.                    results.iresult = intest();         /* Integer test    */
  80.                    Supexec(gettime);
  81.                    printf("\niresult = %d\n", results.iresult);
  82.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  83.                    break;
  84.                  case (2):
  85.                    ptr = &t;
  86.                    Supexec(gettime);
  87.                    ptr = &t2;
  88.                    results.uresult = fib(FIB);         /* Unsigned test   */
  89.                    Supexec(gettime);
  90.                    printf("\nuresult = %u\n", results.uresult);
  91.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  92.                    break;
  93.                  case (3):
  94.                    square = 0L;                        /* Long test       */
  95.                    ptr = &t;
  96.                    Supexec(gettime);
  97.                    ptr = &t2;
  98.                    results.lresult = sq(square);
  99.                    square = sq(results.lresult);       /* Check the value */
  100.                    Supexec(gettime);
  101.                    printf("\nlresult = %ld", results.lresult);
  102.                    printf("\n square = %ld\n", square);
  103.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  104.                    break;
  105.                  case (4):                              /* Double test    */
  106.                    ptr = &t;
  107.                    Supexec(gettime);
  108.                    ptr = &t2;
  109.                    results.dresult = sroot((double) results.lresult);
  110.                    dmath = dply(results.dresult);
  111.                    Supexec(gettime);
  112.                    printf("\ndresult = %f\n", results.dresult);
  113.                    printf("  dmath = %f\n", dmath);
  114.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  115.                    break;
  116.                  case (5):
  117.                    ptr = &t;
  118.                    Supexec(gettime);
  119.                    ptr = &t2;
  120.                    results.cprsult = mcopy();           /* Disk copy      */
  121.                    Supexec(gettime);
  122.                    printf("\n   copy = %d\n", results.cprsult);
  123.                    printf("Took %0.2f seconds\n", (float)(t2-t)/(Getrez() == 2 ? 70 : 60));
  124.                    break;
  125.                  default:
  126.                    break;
  127.               }
  128.               ++i;
  129.            }                                            /* End while i    */
  130.            printf("\n\n...End\n");
  131.            printf("Type Return to Continue\n");
  132.            getchar();
  133.         }
  134.  
  135.         long sq(big)          /* Function to square a number by iteration */
  136.         long big;
  137.         {
  138.             int i;
  139.             static long j = 1L;
  140.  
  141.             if (!big)
  142.                for (i = 0; i < SMALL; ++i) {
  143.                    big += j;
  144.                    j += 2;
  145.                }
  146.             else
  147.                 for (i = 0; i < SMALL; ++i) {
  148.                     j -= 2;
  149.                     big -= j;
  150.                 }
  151.             return (big);
  152.         }
  153.  
  154.         double sroot(num)                /* Find square root of number */
  155.         double num;
  156.         {
  157.             double temp1, temp2, _abs();
  158.  
  159.             temp2 = num / 2.0;
  160.             temp1 = num;
  161.  
  162.             while (temp1 > PRECISION * temp2) {
  163.                       temp1 = (num / temp2) - temp2;
  164.                       temp1 = abs(temp1);
  165.                       temp2 = ((num / temp2) + temp2) / 2.0;
  166.             }
  167.             return (temp2);
  168.         }
  169.  
  170.         double _abs(x)                    /* Absolute value of a double */
  171.         double x;
  172.         {
  173.             return (x < 0 ? -x : x);
  174.         }
  175.         double dply(x)                   /* Exercise some doubles */
  176.         double x;
  177.         {
  178.             int i = TINY;
  179.             double y;
  180.  
  181.             while (i--) {
  182.                      y = x * x * x * x * x * x * x;
  183.                      y = y / x / x / x / x / x / x;
  184.  
  185.                      y = y + x + x + x + x + x + x;
  186.                      y = y - x - x - x - x - x - x;
  187.              }
  188.  
  189.             return (y);
  190.         }
  191.  
  192.         unsigned fib(x)          /* Common Fibonacci function */
  193.         int x;
  194.         {
  195.             if (x > 2)
  196.                 return ( fib(x -1) + fib(x - 2));
  197.             else
  198.                 return (1);
  199.         }
  200.  
  201.         int stest(b1, b2)      /* String test using strcpy() and strcmp() */
  202.         char *b1, *b2;
  203.         {
  204.             int i, j;
  205.             void mstrcpy();
  206.  
  207.             for (i = 0, j = 0; i < SMALL; ++i) {
  208.                 mstrcpy(b1, "0123456789abcdef");
  209.                 mstrcpy(b2, "0123456789abcdee");   /* Note it's a */
  210.                 j += mstrcmp(b1, b2);              /* different string */
  211.             }
  212.  
  213.             return (j);
  214.         }
  215.  
  216.         int mstrcmp(c, d)                /* External string compare */
  217.         char *c, *d;
  218.         {
  219.             while (*c == *d) {
  220.                   if (!*c)
  221.                      return (0);
  222.                   ++c;
  223.                   ++d;
  224.            }
  225.            return (*c - *d);
  226.         }
  227.         void mstrcpy(c, d)       /* External string copy */
  228.         char *c, *d;
  229.         {
  230.             while (*c++ = *d++)
  231.                      ;
  232.         }
  233.  
  234.         int mcopy()         /* Disk copy. Test assumes file doesn't exist */
  235.         {
  236.             FILE *fp, *fopen();
  237.             char buf[TINY];
  238.             int i, j;
  239.  
  240.             mstrcpy(buf, "Disk I/O test");
  241.  
  242.             if ((fp = fopen(FILENAME, "w")) == NULL) {
  243.                printf("Cannot open file");
  244.                exit(ERR);
  245.             }
  246.  
  247.             i = 0;
  248.  
  249.             while (++i < LITTLE)
  250.                   for (j = 0; buf[j]; ++j)
  251.                       putc(buf[j], fp);
  252.  
  253.             fclose(fp);
  254.             return (i);
  255.         }
  256.  
  257.         int intest()            /* Square an integer by iteration */
  258.         {
  259.                 int i, j, k, sum;
  260.  
  261.                 for (i = 0; i < LITTLE; ++i) {
  262.                         sum = 0;
  263.                         for (j = 0, k = 1; j < MAXINT; ++j) {
  264.                                 sum += k;
  265.                                 k += 2;
  266.                         }
  267.                 }
  268.                 return (sum);
  269.         }
  270.