Advertisement
Guest User

Untitled

a guest
Oct 24th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1.  
  2. void printf1(const char* format, ...);
  3.  
  4. /*http://www.netlib.org/benchmark/whetstone.c*/
  5.  
  6.  
  7. /*
  8. * C Converted Whetstone Double Precision Benchmark
  9. * Version 1.2 22 March 1998
  10. *
  11. * (c) Copyright 1998 Painter Engineering, Inc.
  12. * All Rights Reserved.
  13. *
  14. * Permission is granted to use, duplicate, and
  15. * publish this text and program as long as it
  16. * includes this entire comment block and limited
  17. * rights reference.
  18. *
  19. * Converted by Rich Painter, Painter Engineering, Inc. based on the
  20. * www.netlib.org benchmark/whetstoned version obtained 16 March 1998.
  21. *
  22. * A novel approach was used here to keep the look and feel of the
  23. * FORTRAN version. Altering the FORTRAN-based array indices,
  24. * starting at element 1, to start at element 0 for C, would require
  25. * numerous changes, including decrementing the variable indices by 1.
  26. * Instead, the array E1[] was declared 1 element larger in C. This
  27. * allows the FORTRAN index range to function without any literal or
  28. * variable indices changes. The array element E1[0] is simply never
  29. * used and does not alter the benchmark results.
  30. *
  31. * The major FORTRAN comment blocks were retained to minimize
  32. * differences between versions. Modules N5 and N12, like in the
  33. * FORTRAN version, have been eliminated here.
  34. *
  35. * An optional command-line argument has been provided [-c] to
  36. * offer continuous repetition of the entire benchmark.
  37. * An optional argument for setting an alternate LOOP count is also
  38. * provided. Define PRINTOUT to cause the POUT() function to print
  39. * outputs at various stages. Final timing measurements should be
  40. * made with the PRINTOUT undefined.
  41. *
  42. * Questions and comments may be directed to the author at
  43. * r.painter@ieee.org
  44. */
  45. /*
  46. C**********************************************************************
  47. C Benchmark #2 -- Double Precision Whetstone (A001)
  48. C
  49. C o This is a REAL*8 version of
  50. C the Whetstone benchmark program.
  51. C
  52. C o DO-loop semantics are ANSI-66 compatible.
  53. C
  54. C o Final measurements are to be made with all
  55. C WRITE statements and FORMAT sttements removed.
  56. C
  57. C**********************************************************************
  58. */
  59.  
  60. /* standard C library headers required */
  61. #include <stdlib.h>
  62. #include <stdio.h>
  63. #include <string.h>
  64. #include <math.h>
  65.  
  66. /* the following is optional depending on the timing function used */
  67. //#include <time.h>
  68. double second();
  69.  
  70. /* map the FORTRAN math functions, etc. to the C versions */
  71. #define DSIN sin
  72. #define DCOS cos
  73. #define DATAN atan
  74. #define DLOG log
  75. #define DEXP exp
  76. #define DSQRT sqrt
  77. #define IF if
  78.  
  79. /* function prototypes */
  80. void POUT(long N, long J, long K, double X1, double X2, double X3, double X4);
  81. void PA(double E[]);
  82. void P0(void);
  83. void P3(double X, double Y, double *Z);
  84. #define USAGE "usage: whetdc [-c] [loops]\n"
  85.  
  86. /*
  87. COMMON T,T1,T2,E1(4),J,K,L
  88. */
  89. double T,T1,T2,E1[5];
  90. int J,K,L;
  91.  
  92. int
  93. main_a(int argc, char *argv[])
  94. {
  95. /* used in the FORTRAN version */
  96. long I;
  97. long N1, N2, N3, N4, N6, N7, N8, N9, N10, N11;
  98. double X1,X2,X3,X4,X,Y,Z;
  99. long LOOP;
  100. int II, JJ;
  101.  
  102. /* added for this version */
  103. long loopstart;
  104. /* long startsec, finisec; */
  105. float startsec, finisec;
  106. float KIPS;
  107. int continuous;
  108.  
  109. loopstart = 1000; /* see the note about LOOP below */
  110. continuous = 0;
  111.  
  112. II = 1; /* start at the first arg (temp use of II here) */
  113. while (II < argc) {
  114. if (strncmp(argv[II], "-c", 2) == 0 || argv[II][0] == 'c') {
  115. continuous = 1;
  116. } else if (atol(argv[II]) > 0) {
  117. loopstart = atol(argv[II]);
  118. } else {
  119. // fprintf(stderr, USAGE);
  120. printf1(USAGE);
  121. return(1);
  122. }
  123. II++;
  124. }
  125.  
  126. LCONT:
  127. /*
  128. C
  129. C Start benchmark timing at this point.
  130. C
  131. */
  132. // startsec = time(0);
  133. startsec = second();
  134.  
  135. /*
  136. C
  137. C The actual benchmark starts here.
  138. C
  139. */
  140. T = .499975;
  141. T1 = 0.50025;
  142. T2 = 2.0;
  143. /*
  144. C
  145. C With loopcount LOOP=10, one million Whetstone instructions
  146. C will be executed in EACH MAJOR LOOP..A MAJOR LOOP IS EXECUTED
  147. C 'II' TIMES TO INCREASE WALL-CLOCK TIMING ACCURACY.
  148. C
  149. LOOP = 1000;
  150. */
  151. LOOP = loopstart;
  152. II = 1;
  153.  
  154. JJ = 1;
  155.  
  156. IILOOP:
  157. N1 = 0;
  158. N2 = 12 * LOOP;
  159. N3 = 14 * LOOP;
  160. N4 = 345 * LOOP;
  161. N6 = 210 * LOOP;
  162. N7 = 32 * LOOP;
  163. N8 = 899 * LOOP;
  164. N9 = 616 * LOOP;
  165. N10 = 0;
  166. N11 = 93 * LOOP;
  167. /*
  168. C
  169. C Module 1: Simple identifiers
  170. C
  171. */
  172. X1 = 1.0;
  173. X2 = -1.0;
  174. X3 = -1.0;
  175. X4 = -1.0;
  176.  
  177. for (I = 1; I <= N1; I++) {
  178. X1 = (X1 + X2 + X3 - X4) * T;
  179. X2 = (X1 + X2 - X3 + X4) * T;
  180. X3 = (X1 - X2 + X3 + X4) * T;
  181. X4 = (-X1+ X2 + X3 + X4) * T;
  182. }
  183. #ifdef PRINTOUT
  184. IF (JJ==II)POUT(N1,N1,N1,X1,X2,X3,X4);
  185. #endif
  186.  
  187. /*
  188. C
  189. C Module 2: Array elements
  190. C
  191. */
  192. E1[1] = 1.0;
  193. E1[2] = -1.0;
  194. E1[3] = -1.0;
  195. E1[4] = -1.0;
  196.  
  197. for (I = 1; I <= N2; I++) {
  198. E1[1] = ( E1[1] + E1[2] + E1[3] - E1[4]) * T;
  199. E1[2] = ( E1[1] + E1[2] - E1[3] + E1[4]) * T;
  200. E1[3] = ( E1[1] - E1[2] + E1[3] + E1[4]) * T;
  201. E1[4] = (-E1[1] + E1[2] + E1[3] + E1[4]) * T;
  202. }
  203.  
  204. #ifdef PRINTOUT
  205. IF (JJ==II)POUT(N2,N3,N2,E1[1],E1[2],E1[3],E1[4]);
  206. #endif
  207.  
  208. /*
  209. C
  210. C Module 3: Array as parameter
  211. C
  212. */
  213. for (I = 1; I <= N3; I++)
  214. PA(E1);
  215.  
  216. #ifdef PRINTOUT
  217. IF (JJ==II)POUT(N3,N2,N2,E1[1],E1[2],E1[3],E1[4]);
  218. #endif
  219.  
  220. /*
  221. C
  222. C Module 4: Conditional jumps
  223. C
  224. */
  225. J = 1;
  226. for (I = 1; I <= N4; I++) {
  227. if (J == 1)
  228. J = 2;
  229. else
  230. J = 3;
  231.  
  232. if (J > 2)
  233. J = 0;
  234. else
  235. J = 1;
  236.  
  237. if (J < 1)
  238. J = 1;
  239. else
  240. J = 0;
  241. }
  242.  
  243. #ifdef PRINTOUT
  244. IF (JJ==II)POUT(N4,J,J,X1,X2,X3,X4);
  245. #endif
  246.  
  247. /*
  248. C
  249. C Module 5: Omitted
  250. C Module 6: Integer arithmetic
  251. C
  252. */
  253.  
  254. J = 1;
  255. K = 2;
  256. L = 3;
  257.  
  258. for (I = 1; I <= N6; I++) {
  259. J = J * (K-J) * (L-K);
  260. K = L * K - (L-J) * K;
  261. L = (L-K) * (K+J);
  262. E1[L-1] = J + K + L;
  263. E1[K-1] = J * K * L;
  264. }
  265.  
  266. #ifdef PRINTOUT
  267. IF (JJ==II)POUT(N6,J,K,E1[1],E1[2],E1[3],E1[4]);
  268. #endif
  269.  
  270. /*
  271. C
  272. C Module 7: Trigonometric functions
  273. C
  274. */
  275. X = 0.5;
  276. Y = 0.5;
  277.  
  278. for (I = 1; I <= N7; I++) {
  279. X = T * DATAN(T2*DSIN(X)*DCOS(X)/(DCOS(X+Y)+DCOS(X-Y)-1.0));
  280. Y = T * DATAN(T2*DSIN(Y)*DCOS(Y)/(DCOS(X+Y)+DCOS(X-Y)-1.0));
  281. }
  282.  
  283. #ifdef PRINTOUT
  284. IF (JJ==II)POUT(N7,J,K,X,X,Y,Y);
  285. #endif
  286.  
  287. /*
  288. C
  289. C Module 8: Procedure calls
  290. C
  291. */
  292. X = 1.0;
  293. Y = 1.0;
  294. Z = 1.0;
  295.  
  296. for (I = 1; I <= N8; I++)
  297. P3(X,Y,&Z);
  298.  
  299. #ifdef PRINTOUT
  300. IF (JJ==II)POUT(N8,J,K,X,Y,Z,Z);
  301. #endif
  302.  
  303. /*
  304. C
  305. C Module 9: Array references
  306. C
  307. */
  308. J = 1;
  309. K = 2;
  310. L = 3;
  311. E1[1] = 1.0;
  312. E1[2] = 2.0;
  313. E1[3] = 3.0;
  314.  
  315. for (I = 1; I <= N9; I++)
  316. P0();
  317.  
  318. #ifdef PRINTOUT
  319. IF (JJ==II)POUT(N9,J,K,E1[1],E1[2],E1[3],E1[4]);
  320. #endif
  321.  
  322. /*
  323. C
  324. C Module 10: Integer arithmetic
  325. C
  326. */
  327. J = 2;
  328. K = 3;
  329.  
  330. for (I = 1; I <= N10; I++) {
  331. J = J + K;
  332. K = J + K;
  333. J = K - J;
  334. K = K - J - J;
  335. }
  336.  
  337. #ifdef PRINTOUT
  338. IF (JJ==II)POUT(N10,J,K,X1,X2,X3,X4);
  339. #endif
  340.  
  341. /*
  342. C
  343. C Module 11: Standard functions
  344. C
  345. */
  346. X = 0.75;
  347.  
  348. for (I = 1; I <= N11; I++)
  349. X = DSQRT(DEXP(DLOG(X)/T1));
  350.  
  351. #ifdef PRINTOUT
  352. IF (JJ==II)POUT(N11,J,K,X,X,X,X);
  353. #endif
  354.  
  355. /*
  356. C
  357. C THIS IS THE END OF THE MAJOR LOOP.
  358. C
  359. */
  360. if (++JJ <= II)
  361. goto IILOOP;
  362.  
  363. /*
  364. C
  365. C Stop benchmark timing at this point.
  366. C
  367. */
  368. // finisec = time(0);
  369. finisec = second();
  370.  
  371. /*
  372. C----------------------------------------------------------------
  373. C Performance in Whetstone KIP's per second is given by
  374. C
  375. C (100*LOOP*II)/TIME
  376. C
  377. C where TIME is in seconds.
  378. C--------------------------------------------------------------------
  379. */
  380. printf1("\r\n");
  381. if (finisec-startsec <= 0) {
  382. printf1("Insufficient duration- Increase the LOOP count\r\n");
  383. return(1);
  384. }
  385.  
  386. /* printf1("Loops: %ld, Iterations: %d, Duration: %ld sec.\r\n", */
  387. printf1("Loops: %ld, Iterations: %d, Duration: %f sec.\r\n",
  388. LOOP, II, finisec-startsec);
  389.  
  390. KIPS = (100.0*LOOP*II)/(float)(finisec-startsec);
  391. if (KIPS >= 1000.0)
  392. printf1("C Converted Double Precision Whetstones: %.1f MIPS\r\n", KIPS/1000.0);
  393. else
  394. printf1("C Converted Double Precision Whetstones: %.1f KIPS\r\n", KIPS);
  395.  
  396. if (continuous)
  397. goto LCONT;
  398.  
  399. return(0);
  400. }
  401.  
  402. void
  403. PA(double E[])
  404. {
  405. J = 0;
  406.  
  407. L10:
  408. E[1] = ( E[1] + E[2] + E[3] - E[4]) * T;
  409. E[2] = ( E[1] + E[2] - E[3] + E[4]) * T;
  410. E[3] = ( E[1] - E[2] + E[3] + E[4]) * T;
  411. E[4] = (-E[1] + E[2] + E[3] + E[4]) / T2;
  412. J += 1;
  413.  
  414. if (J < 6)
  415. goto L10;
  416. }
  417.  
  418. void
  419. P0(void)
  420. {
  421. E1[J] = E1[K];
  422. E1[K] = E1[L];
  423. E1[L] = E1[J];
  424. }
  425.  
  426. void
  427. P3(double X, double Y, double *Z)
  428. {
  429. double X1, Y1;
  430.  
  431. X1 = X;
  432. Y1 = Y;
  433. X1 = T * (X1 + Y1);
  434. Y1 = T * (X1 + Y1);
  435. *Z = (X1 + Y1) / T2;
  436. }
  437.  
  438. #ifdef PRINTOUT
  439. void
  440. POUT(long N, long J, long K, double X1, double X2, double X3, double X4)
  441. {
  442. printf1("%7ld %7ld %7ld %12.4e %12.4e %12.4e %12.4e\n",
  443. N, J, K, X1, X2, X3, X4);
  444. }
  445. #endif
  446.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement