benchmarks initial commit
[riscv-tests.git] / benchmarks / dhrystone / dhrystone.h
1 #ifndef _DHRYSTONE_H
2 #define _DHRYSTONE_H
3
4 /****************** "DHRYSTONE" Benchmark Program ***************************/
5 #define Version "C, Version 2.2"
6 /* File: dhry_1.c (part 2 of 3)
7 * Author: Reinhold P. Weicker
8 * Siemens Nixdorf, Paderborn/Germany
9 * weicker@specbench.org
10 * Date: May 25, 1988
11 * Modified: Steven Pemberton, CWI, Amsterdam; Steven.Pemberton@cwi.nl
12 * Date: October, 1993; March 1995
13 * Included both files into one source, that gets compiled
14 * in two passes. Made program auto-compiling, and auto-running,
15 * and generally made it much easier to use.
16 *
17 * Original Version (in Ada) published in
18 * "Communications of the ACM" vol. 27., no. 10 (Oct. 1984),
19 * pp. 1013 - 1030, together with the statistics
20 * on which the distribution of statements etc. is based.
21 *
22 * In this C version, the following C library functions are used:
23 * - strcpy, strcmp (inside the measurement loop)
24 * - printf, scanf (outside the measurement loop)
25 * In addition, Berkeley UNIX system calls "times ()" or "time ()"
26 * are used for execution time measurement. For measurements
27 * on other systems, these calls have to be changed.
28 *
29 * Collection of Results:
30 * Reinhold Weicker (address see above) and
31 *
32 * Rick Richardson
33 * PC Research. Inc.
34 * 94 Apple Orchard Drive
35 * Tinton Falls, NJ 07724
36 * Phone: (201) 389-8963 (9-17 EST)
37 * Usenet: ...!uunet!pcrat!rick
38 *
39 * Please send results to Rick Richardson and/or Reinhold Weicker.
40 * Complete information should be given on hardware and software used.
41 * Hardware information includes: Machine type, CPU, type and size
42 * of caches; for microprocessors: clock frequency, memory speed
43 * (number of wait states).
44 * Software information includes: Compiler (and runtime library)
45 * manufacturer and version, compilation switches, OS version.
46 * The Operating System version may give an indication about the compiler;
47 * Dhrystone itself performs no OS calls in the measurement loop.
48 *
49 * The complete output generated by the program should be mailed
50 * such that at least some checks for correctness can be made.
51 *
52 ***************************************************************************
53 *
54 * Defines: The following "Defines" are possible:
55 * -DREG (default: Not defined)
56 * As an approximation to what an average C programmer
57 * might do, causes the "register" storage class to be applied
58 * - for local variables, if they are used (dynamically)
59 * five or more times
60 * - for parameters if they are used (dynamically)
61 * six or more times
62 * Note that an optimal "register" strategy is
63 * compiler-dependent, and that "register" declarations
64 * do not necessarily lead to faster execution.
65 * -DNOSTRUCTASSIGN (default: Not defined)
66 * Define if the C compiler does not support
67 * assignment of structures.
68 * -DNOENUMS (default: Not defined)
69 * Define if the C compiler does not support
70 * enumeration types.
71 * -DTIMES (default)
72 * -DTIME
73 * The "times" function of UNIX (returning process times)
74 * or the "time" function (returning wallclock time)
75 * is used for measurement.
76 * For single user machines, "time ()" is adequate. For
77 * multi-user machines where you cannot get single-user
78 * access, use the "times ()" function. If you have
79 * neither, use a stopwatch in the dead of night.
80 * "printf"s are provided marking the points "Start Timer"
81 * and "Stop Timer". DO NOT use the UNIX "time(1)"
82 * command, as this will measure the total time to
83 * run this program, which will (erroneously) include
84 * the time to allocate storage (malloc) and to perform
85 * the initialization.
86 * -DHZ=nnn
87 * In Berkeley UNIX, the function "times" returns process
88 * time in 1/HZ seconds, with HZ = 60 for most systems.
89 * CHECK YOUR SYSTEM DESCRIPTION BEFORE YOU JUST APPLY
90 * A VALUE.
91 *
92 ***************************************************************************
93 *
94 * History: Version C/2.1 was made for two reasons:
95 *
96 * 1) There was an obvious need for a common C version of
97 * Dhrystone, since C is at present the most popular system
98 * programming language for the class of processors
99 * (microcomputers, minicomputers) where Dhrystone is used most.
100 * There should be, as far as possible, only one C version of
101 * Dhrystone such that results can be compared without
102 * restrictions. In the past, the C versions distributed
103 * by Rick Richardson (Version 1.1) and by Reinhold Weicker
104 * had small (though not significant) differences.
105 *
106 * 2) As far as it is possible without changes to the Dhrystone
107 * statistics, optimizing compilers should be prevented from
108 * removing significant statements.
109 *
110 * This C version has been developed in cooperation with
111 * Rick Richardson (Tinton Falls, NJ), it incorporates many
112 * ideas from the "Version 1.1" distributed previously by
113 * him over the UNIX network Usenet.
114 * I also thank Chaim Benedelac (National Semiconductor),
115 * David Ditzel (SUN), Earl Killian and John Mashey (MIPS),
116 * Alan Smith and Rafael Saavedra-Barrera (UC at Berkeley)
117 * for their help with comments on earlier versions of the
118 * benchmark.
119 *
120 * Changes: In the initialization part, this version follows mostly
121 * Rick Richardson's version distributed via Usenet, not the
122 * version distributed earlier via floppy disk by Reinhold Weicker.
123 * As a concession to older compilers, names have been made
124 * unique within the first 8 characters.
125 * Inside the measurement loop, this version follows the
126 * version previously distributed by Reinhold Weicker.
127 *
128 * At several places in the benchmark, code has been added,
129 * but within the measurement loop only in branches that
130 * are not executed. The intention is that optimizing compilers
131 * should be prevented from moving code out of the measurement
132 * loop, or from removing code altogether. Since the statements
133 * that are executed within the measurement loop have NOT been
134 * changed, the numbers defining the "Dhrystone distribution"
135 * (distribution of statements, operand types and locality)
136 * still hold. Except for sophisticated optimizing compilers,
137 * execution times for this version should be the same as
138 * for previous versions.
139 *
140 * Since it has proven difficult to subtract the time for the
141 * measurement loop overhead in a correct way, the loop check
142 * has been made a part of the benchmark. This does have
143 * an impact - though a very minor one - on the distribution
144 * statistics which have been updated for this version.
145 *
146 * All changes within the measurement loop are described
147 * and discussed in the companion paper "Rationale for
148 * Dhrystone version 2".
149 *
150 * Because of the self-imposed limitation that the order and
151 * distribution of the executed statements should not be
152 * changed, there are still cases where optimizing compilers
153 * may not generate code for some statements. To a certain
154 * degree, this is unavoidable for small synthetic benchmarks.
155 * Users of the benchmark are advised to check code listings
156 * whether code is generated for all statements of Dhrystone.
157 *
158 * Version 2.1 is identical to version 2.0 distributed via
159 * the UNIX network Usenet in March 1988 except that it corrects
160 * some minor deficiencies that were found by users of version 2.0.
161 * The only change within the measurement loop is that a
162 * non-executed "else" part was added to the "if" statement in
163 * Func_3, and a non-executed "else" part removed from Proc_3.
164 *
165 * Version C/2.2, Steven Pemberton, October 1993
166 * Functionally, identical to version 2.2; the changes are in
167 * how you compile and use it:
168 * - Everything is in one file now, but compiled in 2 passes
169 * - Compile (and run) by running the file through the shell: 'sh dhry.c"
170 * - Uses the system definition of HZ if one can be found
171 * - HZ must be defined, otherwise it won't compile (no defaults here)
172 * - The (uninteresting) output is printed to stderr (dhry2 > /dev/null)
173 * - The number of loops is passed as a parameter, rather than read
174 * (dhry2 500000)
175 * - If the number of loops is insufficient to get a good result,
176 * it repeats it with loops*10 until it is enough (rather than just
177 * stopping)
178 * - Output says which sort of clock it is using, and the HZ value
179 * - You can use -DREG instead of the -DREG=register of previous versions
180 * - Some stylistic cleanups.
181 *
182 ***************************************************************************
183 *
184 * Compilation model and measurement (IMPORTANT):
185 *
186 * The following "ground rules" apply for measurements:
187 * - Separate compilation
188 * - No procedure merging
189 * - Otherwise, compiler optimizations are allowed but should be indicated
190 * - Default results are those without register declarations
191 * See the companion paper "Rationale for Dhrystone Version 2" for a more
192 * detailed discussion of these ground rules.
193 *
194 * For 16-Bit processors (e.g. 80186, 80286), times for all compilation
195 * models ("small", "medium", "large" etc.) should be given if possible,
196 * together with a definition of these models for the compiler system used.
197 *
198 **************************************************************************
199 *
200 * Dhrystone (C version) statistics:
201 *
202 * [Comment from the first distribution, updated for version 2.
203 * Note that because of language differences, the numbers are slightly
204 * different from the Ada version.]
205 *
206 * The following program contains statements of a high level programming
207 * language (here: C) in a distribution considered representative:
208 *
209 * assignments 52 (51.0 %)
210 * control statements 33 (32.4 %)
211 * procedure, function calls 17 (16.7 %)
212 *
213 * 103 statements are dynamically executed. The program is balanced with
214 * respect to the three aspects:
215 *
216 * - statement type
217 * - operand type
218 * - operand locality
219 * operand global, local, parameter, or constant.
220 *
221 * The combination of these three aspects is balanced only approximately.
222 *
223 * 1. Statement Type:
224 * ----------------- number
225 *
226 * V1 = V2 9
227 * (incl. V1 = F(..)
228 * V = Constant 12
229 * Assignment, 7
230 * with array element
231 * Assignment, 6
232 * with record component
233 * --
234 * 34 34
235 *
236 * X = Y +|-|"&&"|"|" Z 5
237 * X = Y +|-|"==" Constant 6
238 * X = X +|- 1 3
239 * X = Y *|/ Z 2
240 * X = Expression, 1
241 * two operators
242 * X = Expression, 1
243 * three operators
244 * --
245 * 18 18
246 *
247 * if .... 14
248 * with "else" 7
249 * without "else" 7
250 * executed 3
251 * not executed 4
252 * for ... 7 | counted every time
253 * while ... 4 | the loop condition
254 * do ... while 1 | is evaluated
255 * switch ... 1
256 * break 1
257 * declaration with 1
258 * initialization
259 * --
260 * 34 34
261 *
262 * P (...) procedure call 11
263 * user procedure 10
264 * library procedure 1
265 * X = F (...)
266 * function call 6
267 * user function 5
268 * library function 1
269 * --
270 * 17 17
271 * ---
272 * 103
273 *
274 * The average number of parameters in procedure or function calls
275 * is 1.82 (not counting the function values aX *
276 *
277 * 2. Operators
278 * ------------
279 * number approximate
280 * percentage
281 *
282 * Arithmetic 32 50.8
283 *
284 * + 21 33.3
285 * - 7 11.1
286 * * 3 4.8
287 * / (int div) 1 1.6
288 *
289 * Comparison 27 42.8
290 *
291 * == 9 14.3
292 * /= 4 6.3
293 * > 1 1.6
294 * < 3 4.8
295 * >= 1 1.6
296 * <= 9 14.3
297 *
298 * Logic 4 6.3
299 *
300 * && (AND-THEN) 1 1.6
301 * | (OR) 1 1.6
302 * ! (NOT) 2 3.2
303 *
304 * -- -----
305 * 63 100.1
306 *
307 *
308 * 3. Operand Type (counted once per operand reference):
309 * ---------------
310 * number approximate
311 * percentage
312 *
313 * Integer 175 72.3 %
314 * Character 45 18.6 %
315 * Pointer 12 5.0 %
316 * String30 6 2.5 %
317 * Array 2 0.8 %
318 * Record 2 0.8 %
319 * --- -------
320 * 242 100.0 %
321 *
322 * When there is an access path leading to the final operand (e.g. a record
323 * component), only the final data type on the access path is counted.
324 *
325 *
326 * 4. Operand Locality:
327 * -------------------
328 * number approximate
329 * percentage
330 *
331 * local variable 114 47.1 %
332 * global variable 22 9.1 %
333 * parameter 45 18.6 %
334 * value 23 9.5 %
335 * reference 22 9.1 %
336 * function result 6 2.5 %
337 * constant 55 22.7 %
338 * --- -------
339 * 242 100.0 %
340 *
341 * The program does not compute anything meaningful, but it is syntactically
342 * and semantically correct. All variables have a value assigned to them
343 * before they are used as a source operand.
344 *
345 * There has been no explicit effort to account for the effects of a
346 * cache, or to balance the use of long or short displacements for code or
347 * data.
348 *
349 ***************************************************************************
350 */
351
352 /* Compiler and system dependent definitions: */
353
354 /* variables for time measurement: */
355
356 #ifdef TIME
357
358 #define CLOCK_TYPE "time()"
359 #undef HZ
360 #define HZ (1) /* time() returns time in seconds */
361 extern long time(); /* see library function "time" */
362 #define Too_Small_Time 2 /* Measurements should last at least 2 seconds */
363 #define Start_Timer() Begin_Time = time ( (long *) 0)
364 #define Stop_Timer() End_Time = time ( (long *) 0)
365
366 #else
367
368 #ifdef MSC_CLOCK /* Use Microsoft C hi-res clock */
369
370 #undef HZ
371 #undef TIMES
372 #include <time.h>
373 #define HZ CLK_TCK
374 #define CLOCK_TYPE "MSC clock()"
375 extern clock_t clock();
376 #define Too_Small_Time (2*HZ)
377 #define Start_Timer() Begin_Time = clock()
378 #define Stop_Timer() End_Time = clock()
379
380 #elif defined(__riscv)
381
382 #define HZ 976563
383 #define Too_Small_Time 50
384 #define rdcycle() ({ \
385 long __x; \
386 asm volatile("rdcycle %0; srl %0, %0, 10" : "=r"(__x)); \
387 __x; })
388 #define CLOCK_TYPE "rdcycle()"
389 #define Start_Timer() Begin_Time = rdcycle()
390 #define Stop_Timer() End_Time = rdcycle()
391
392 #else
393 /* Use times(2) time function unless */
394 /* explicitly defined otherwise */
395 #define CLOCK_TYPE "times()"
396 #include <sys/types.h>
397 #include <sys/times.h>
398 #ifndef HZ /* Added by SP 900619 */
399 #include <sys/param.h> /* If your system doesn't have this, use -DHZ=xxx */
400 #else
401 *** You must define HZ!!! ***
402 #endif /* HZ */
403 #ifndef PASS2
404 struct tms time_info;
405 #endif
406 /*extern int times ();*/
407 /* see library function "times" */
408 #define Too_Small_Time (2*HZ)
409 /* Measurements should last at least about 2 seconds */
410 #define Start_Timer() times(&time_info); Begin_Time=(long)time_info.tms_utime
411 #define Stop_Timer() times(&time_info); End_Time = (long)time_info.tms_utime
412
413 #endif /* MSC_CLOCK */
414 #endif /* TIME */
415
416
417 #define Mic_secs_Per_Second 1000000.0
418 #define NUMBER_OF_RUNS 500 /* Default number of runs */
419
420 #ifdef NOSTRUCTASSIGN
421 #define structassign(d, s) memcpy(&(d), &(s), sizeof(d))
422 #else
423 #define structassign(d, s) d = s
424 #endif
425
426 #ifdef NOENUM
427 #define Ident_1 0
428 #define Ident_2 1
429 #define Ident_3 2
430 #define Ident_4 3
431 #define Ident_5 4
432 typedef int Enumeration;
433 #else
434 typedef enum {Ident_1, Ident_2, Ident_3, Ident_4, Ident_5}
435 Enumeration;
436 #endif
437 /* for boolean and enumeration types in Ada, Pascal */
438
439 /* General definitions: */
440
441 #include <stdio.h>
442 #include <string.h>
443 /* for strcpy, strcmp */
444
445 #define Null 0
446 /* Value of a Null pointer */
447 #define true 1
448 #define false 0
449
450 typedef int One_Thirty;
451 typedef int One_Fifty;
452 typedef char Capital_Letter;
453 typedef int Boolean;
454 typedef char Str_30 [31];
455 typedef int Arr_1_Dim [50];
456 typedef int Arr_2_Dim [50] [50];
457
458 typedef struct record
459 {
460 struct record *Ptr_Comp;
461 Enumeration Discr;
462 union {
463 struct {
464 Enumeration Enum_Comp;
465 int Int_Comp;
466 char Str_Comp [31];
467 } var_1;
468 struct {
469 Enumeration E_Comp_2;
470 char Str_2_Comp [31];
471 } var_2;
472 struct {
473 char Ch_1_Comp;
474 char Ch_2_Comp;
475 } var_3;
476 } variant;
477 } Rec_Type, *Rec_Pointer;
478
479 void Proc_1(Rec_Pointer Ptr_Val_Par);
480 void Proc_2(int* Int_Par_Ref);
481 void Proc_3(Rec_Pointer* Ptr_Ref_Par);
482 void Proc_4();
483 void Proc_5();
484 void Proc_6(Enumeration Enum_Val_Par, Enumeration* Enum_Ref_Par);
485 void Proc_7(int Int_1_Par_Val, int Int_2_Par_Val, int* Int_Par_Ref);
486 void Proc_8(Arr_1_Dim Arr_1_Par_Ref, Arr_2_Dim Arr_2_Par_Ref, int Int_1_Par_Val, int Int_2_Par_Val);
487 Enumeration Func_1 (char Ch_1_Par_Val, char Ch_2_Par_Val);
488 Boolean Func_2(Str_30 Str_1_Par_Ref, Str_30 Str_2_Par_Ref);
489 Boolean Func_3(Enumeration Enum_Par_Val);
490
491 #endif