DHAMP.C 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* The dhampstone benchmark. Written by Jack purdum. */
  2. /* version 1.0, August 1,1985 */
  3. #include "stdio.h"
  4. #define BELL 7 /* ASCII BELL code */
  5. #define FIB 24
  6. #define TINY 100
  7. #define MAXINT 179
  8. #define LITTLE 1000
  9. #define SMALL 9000
  10. #define PRECISION .000001
  11. #define FILENAME "zyxw.vut"
  12. #define NUMTEST 6
  13. #ifndef ERR
  14. #define ERR -1
  15. #endif
  16. struct
  17. {
  18. int cresult;
  19. int iresult;
  20. int cprsult;
  21. unsigned uresult;
  22. long lresult;
  23. double dresult;
  24. } results;
  25. main()
  26. {
  27. char buf1[TINY], buf2[TINY];
  28. int i = 0;
  29. unsigned fib ();
  30. long square, sq ();
  31. double dmath, sroot (), dply ();
  32. printf("Start...%c\n\n",BELL);
  33. while (i < NUMTEST)
  34. {
  35. switch (i)
  36. {
  37. case (0): /* Character test */
  38. results.cresult = stest (buf1,buf2);
  39. printf ("\ncresult = %d\n",results.cresult);
  40. break;
  41. case (1): /* Integer test */
  42. results.iresult = intest ();
  43. printf ("\niresult = %d\n",results.iresult);
  44. break;
  45. case (2): /* Unsigned test */
  46. results.uresult = fib (FIB);
  47. printf ("\nuresult = %u\n",results.uresult);
  48. break;
  49. case (3): /* Long test */
  50. square = 0L;
  51. results.lresult = sq (square);
  52. square = sq (results.lresult); /* Check the value */
  53. printf ("\nlresult = %ld",results.lresult);
  54. printf ("\n square = %ld\n",square);
  55. break;
  56. case (4): /* Double test */
  57. results.dresult = sroot ((double) results.lresult);
  58. printf ("\ndresult = %f\n",results.dresult);
  59. dmath = dply (results.dresult);
  60. printf (" dmath = %f\n",dmath);
  61. break;
  62. case (5): /* Disk copy */
  63. results.cprsult = mcopy ();
  64. printf ("\b copy = %d",results.cprsult);
  65. break;
  66. default:
  67. break;
  68. }
  69. ++i;
  70. } /* End while i */
  71. printf ("\n\n...End%c",BELL);
  72. }
  73. long sq (big) /* Function to square a number by iteration */
  74. long big;
  75. {
  76. int i;
  77. static long j = 1L;
  78. if (!big)
  79. for (i = 0; i < SMALL; ++i)
  80. {
  81. big += j;
  82. j += 2;
  83. }
  84. else
  85. for (i = 0; i < SMALL; ++i)
  86. {
  87. j -= 2;
  88. big -= j;
  89. }
  90. return (big);
  91. }
  92. double sroot (num) /* Find square root of number */
  93. double num;
  94. {
  95. double temp1, temp2, abs ();
  96. temp2 = num / 2.0;
  97. temp1 = num;
  98. while (temp1 > PRECISION * temp2)
  99. {
  100. temp1 = (num / temp2) - temp2;
  101. temp1 = abs (temp1);
  102. temp2 = ((num / temp2) + temp2) / 2.0;
  103. }
  104. return (temp2);
  105. }
  106. double abs (x) /* Absolute value of a double */
  107. double x;
  108. {
  109. return (x < 0 ? -x : x);
  110. }
  111. double dply (x) /* Exercise some doubles */
  112. double x;
  113. {
  114. int i = TINY;
  115. double y;
  116. while (i--)
  117. {
  118. y = x * x * x * x * x * x * x;
  119. y = y / x / x / x / x / x / x;
  120. y = y + x + x + x + x + x + x;
  121. y = y - x - x - x - x - x - x;
  122. }
  123. return (y);
  124. }
  125. unsigned fib (x) /* Common Fibonacci function */
  126. int x;
  127. {
  128. if (x > 2)
  129. return (fib (x-1) + fib (x-2));
  130. else
  131. return (1);
  132. }
  133. int stest (b1,b2) /* String test using strcpy() and strcmp() */
  134. char *b1, *b2;
  135. {
  136. int i,j;
  137. void mstrcpy ();
  138. for (i = 0, j = 0; i < SMALL; ++i)
  139. {
  140. mstrcpy (b1, "0123456789abcdef");
  141. mstrcpy (b2, "0123456789abcdee"); /* Note it's a different string. */
  142. j += mstrcmp (b1,b2);
  143. }
  144. return (j);
  145. }
  146. int mstrcmp (c,d) /* External string compare */
  147. char *c, *d;
  148. {
  149. while (*c == *d)
  150. {
  151. if (!*c)
  152. return (0);
  153. ++c;
  154. ++d;
  155. }
  156. return (*c - *d);
  157. }
  158. void mstrcpy (c,d) /* External string copy */
  159. char *c, *d;
  160. {
  161. while (*c++ = *d++)
  162. ;
  163. }
  164. int mcopy () /* Disk copy. Test assumes file doesn't exist */
  165. {
  166. FILE *fp, *fopen ();
  167. char buf[TINY];
  168. int i, j;
  169. mstrcpy (buf, "Disk I/O test");
  170. if ((fp = fopen(FILENAME,"w")) == NULL)
  171. {
  172. printf ("Cannot open file");
  173. exit (ERR);
  174. }
  175. i = 0;
  176. while (++i < LITTLE)
  177. for (j = 0; buf[j]; ++j)
  178. putc (buf[j], fp);
  179. fclose (fp);
  180. return (i);
  181. }
  182. int intest () /* Square an integer by iteration */
  183. {
  184. int i, j, k, sum;
  185. for (i = 0; i < LITTLE; ++i)
  186. {
  187. sum = 0;
  188. for (j = 0, k = 1; j < MAXINT; ++j)
  189. {
  190. sum += k;
  191. k += 2;
  192. }
  193. }
  194. return (sum);
  195. }
  196.