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