f2cd58efa059857c6effe397acfcb5e6f014b628
[riscv-tests.git] / benchmarks / dhrystone / dhrystone_main.c
1 // See LICENSE for license details.
2
3 //**************************************************************************
4 // Dhrystone bencmark
5 //--------------------------------------------------------------------------
6 //
7 // This is the classic Dhrystone synthetic integer benchmark.
8 // You should not change anything except the HOST_DEBUG and
9 // PREALLOCATE macros for your timing run.
10
11 #pragma GCC optimize ("no-inline")
12
13 #include "dhrystone.h"
14
15 //--------------------------------------------------------------------------
16 // Macros
17
18 // Set HOST_DEBUG to 1 if you are going to compile this for a host
19 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
20 // to 0 if you are compiling with the smips-gcc toolchain.
21
22 #ifndef HOST_DEBUG
23 #define HOST_DEBUG 0
24 #endif
25
26 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
27 // function before starting stats. If you have instruction/data
28 // caches and you don't want to count the overhead of misses, then
29 // you will need to use preallocation.
30
31 #ifndef PREALLOCATE
32 #define PREALLOCATE 0
33 #endif
34
35 // Set SET_STATS to 1 if you want to carve out the piece that actually
36 // does the computation.
37
38 #ifndef SET_STATS
39 #define SET_STATS 0
40 #endif
41
42 #if HOST_DEBUG
43 # define debug_printf printf
44 #else
45 void debug_printf(const char* str, ...);
46 #endif
47
48 #include "util.h"
49
50 #include <alloca.h>
51
52 /* Global Variables: */
53
54 Rec_Pointer Ptr_Glob,
55 Next_Ptr_Glob;
56 int Int_Glob;
57 Boolean Bool_Glob;
58 char Ch_1_Glob,
59 Ch_2_Glob;
60 int Arr_1_Glob [50];
61 int Arr_2_Glob [50] [50];
62
63 extern char *malloc ();
64 Enumeration Func_1 ();
65 /* forward declaration necessary since Enumeration may not simply be int */
66
67 #ifndef REG
68 Boolean Reg = false;
69 #define REG
70 /* REG becomes defined as empty */
71 /* i.e. no register variables */
72 #else
73 Boolean Reg = true;
74 #undef REG
75 #define REG register
76 #endif
77
78 Boolean Done;
79
80 long Begin_Time,
81 End_Time,
82 User_Time;
83 long Microseconds,
84 Dhrystones_Per_Second;
85
86 /* end of variables for time measurement */
87
88
89 int main (int argc, char** argv)
90 /*****/
91 /* main program, corresponds to procedures */
92 /* Main and Proc_0 in the Ada version */
93 {
94 One_Fifty Int_1_Loc;
95 REG One_Fifty Int_2_Loc;
96 One_Fifty Int_3_Loc;
97 REG char Ch_Index;
98 Enumeration Enum_Loc;
99 Str_30 Str_1_Loc;
100 Str_30 Str_2_Loc;
101 REG int Run_Index;
102 REG int Number_Of_Runs;
103
104 /* Arguments */
105 #if HOST_DEBUG
106 if (argc > 2)
107 {
108 printf("Usage: %s [number of loops]\n", argv[0]);
109 exit (1);
110 }
111 if (argc == 2)
112 {
113 Number_Of_Runs = atoi (argv[1]);
114 }
115 else if (Number_Of_Runs <= 0)
116 #endif
117 {
118 Number_Of_Runs = NUMBER_OF_RUNS;
119 }
120
121 /* Initializations */
122
123 Next_Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
124 Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
125
126 Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
127 Ptr_Glob->Discr = Ident_1;
128 Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
129 Ptr_Glob->variant.var_1.Int_Comp = 40;
130 strcpy (Ptr_Glob->variant.var_1.Str_Comp,
131 "DHRYSTONE PROGRAM, SOME STRING");
132 strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
133
134 Arr_2_Glob [8][7] = 10;
135 /* Was missing in published program. Without this statement, */
136 /* Arr_2_Glob [8][7] would have an undefined value. */
137 /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
138 /* overflow may occur for this array element. */
139
140 debug_printf("\n");
141 debug_printf("Dhrystone Benchmark, Version %s\n", Version);
142 if (Reg)
143 {
144 debug_printf("Program compiled with 'register' attribute\n");
145 }
146 else
147 {
148 debug_printf("Program compiled without 'register' attribute\n");
149 }
150 debug_printf("Using %s, HZ=%d\n", CLOCK_TYPE, HZ);
151 debug_printf("\n");
152
153 Done = false;
154 while (!Done) {
155 debug_printf("Trying %d runs through Dhrystone:\n", Number_Of_Runs);
156
157 /***************/
158 /* Start timer */
159 /***************/
160
161 setStats(1);
162 Start_Timer();
163
164 for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)
165 {
166
167 Proc_5();
168 Proc_4();
169 /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
170 Int_1_Loc = 2;
171 Int_2_Loc = 3;
172 strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
173 Enum_Loc = Ident_2;
174 Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
175 /* Bool_Glob == 1 */
176 while (Int_1_Loc < Int_2_Loc) /* loop body executed once */
177 {
178 Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
179 /* Int_3_Loc == 7 */
180 Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
181 /* Int_3_Loc == 7 */
182 Int_1_Loc += 1;
183 } /* while */
184 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
185 Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
186 /* Int_Glob == 5 */
187 Proc_1 (Ptr_Glob);
188 for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
189 /* loop body executed twice */
190 {
191 if (Enum_Loc == Func_1 (Ch_Index, 'C'))
192 /* then, not executed */
193 {
194 Proc_6 (Ident_1, &Enum_Loc);
195 strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
196 Int_2_Loc = Run_Index;
197 Int_Glob = Run_Index;
198 }
199 }
200 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
201 Int_2_Loc = Int_2_Loc * Int_1_Loc;
202 Int_1_Loc = Int_2_Loc / Int_3_Loc;
203 Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
204 /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
205 Proc_2 (&Int_1_Loc);
206 /* Int_1_Loc == 5 */
207
208 } /* loop "for Run_Index" */
209
210 /**************/
211 /* Stop timer */
212 /**************/
213
214 Stop_Timer();
215 setStats(0);
216
217 User_Time = End_Time - Begin_Time;
218
219 if (User_Time < Too_Small_Time)
220 {
221 printf("Measured time too small to obtain meaningful results\n");
222 Number_Of_Runs = Number_Of_Runs * 10;
223 printf("\n");
224 } else Done = true;
225 }
226
227 debug_printf("Final values of the variables used in the benchmark:\n");
228 debug_printf("\n");
229 debug_printf("Int_Glob: %d\n", Int_Glob);
230 debug_printf(" should be: %d\n", 5);
231 debug_printf("Bool_Glob: %d\n", Bool_Glob);
232 debug_printf(" should be: %d\n", 1);
233 debug_printf("Ch_1_Glob: %c\n", Ch_1_Glob);
234 debug_printf(" should be: %c\n", 'A');
235 debug_printf("Ch_2_Glob: %c\n", Ch_2_Glob);
236 debug_printf(" should be: %c\n", 'B');
237 debug_printf("Arr_1_Glob[8]: %d\n", Arr_1_Glob[8]);
238 debug_printf(" should be: %d\n", 7);
239 debug_printf("Arr_2_Glob[8][7]: %d\n", Arr_2_Glob[8][7]);
240 debug_printf(" should be: Number_Of_Runs + 10\n");
241 debug_printf("Ptr_Glob->\n");
242 debug_printf(" Ptr_Comp: %d\n", (long) Ptr_Glob->Ptr_Comp);
243 debug_printf(" should be: (implementation-dependent)\n");
244 debug_printf(" Discr: %d\n", Ptr_Glob->Discr);
245 debug_printf(" should be: %d\n", 0);
246 debug_printf(" Enum_Comp: %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
247 debug_printf(" should be: %d\n", 2);
248 debug_printf(" Int_Comp: %d\n", Ptr_Glob->variant.var_1.Int_Comp);
249 debug_printf(" should be: %d\n", 17);
250 debug_printf(" Str_Comp: %s\n", Ptr_Glob->variant.var_1.Str_Comp);
251 debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
252 debug_printf("Next_Ptr_Glob->\n");
253 debug_printf(" Ptr_Comp: %d\n", (long) Next_Ptr_Glob->Ptr_Comp);
254 debug_printf(" should be: (implementation-dependent), same as above\n");
255 debug_printf(" Discr: %d\n", Next_Ptr_Glob->Discr);
256 debug_printf(" should be: %d\n", 0);
257 debug_printf(" Enum_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
258 debug_printf(" should be: %d\n", 1);
259 debug_printf(" Int_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
260 debug_printf(" should be: %d\n", 18);
261 debug_printf(" Str_Comp: %s\n",
262 Next_Ptr_Glob->variant.var_1.Str_Comp);
263 debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
264 debug_printf("Int_1_Loc: %d\n", Int_1_Loc);
265 debug_printf(" should be: %d\n", 5);
266 debug_printf("Int_2_Loc: %d\n", Int_2_Loc);
267 debug_printf(" should be: %d\n", 13);
268 debug_printf("Int_3_Loc: %d\n", Int_3_Loc);
269 debug_printf(" should be: %d\n", 7);
270 debug_printf("Enum_Loc: %d\n", Enum_Loc);
271 debug_printf(" should be: %d\n", 1);
272 debug_printf("Str_1_Loc: %s\n", Str_1_Loc);
273 debug_printf(" should be: DHRYSTONE PROGRAM, 1'ST STRING\n");
274 debug_printf("Str_2_Loc: %s\n", Str_2_Loc);
275 debug_printf(" should be: DHRYSTONE PROGRAM, 2'ND STRING\n");
276 debug_printf("\n");
277
278
279 Microseconds = ((User_Time / Number_Of_Runs) * Mic_secs_Per_Second) / HZ;
280 Dhrystones_Per_Second = (HZ * Number_Of_Runs) / User_Time;
281
282 printf("Microseconds for one run through Dhrystone: %ld\n", Microseconds);
283 printf("Dhrystones per Second: %ld\n", Dhrystones_Per_Second);
284
285 return 0;
286 }
287
288
289 Proc_1 (Ptr_Val_Par)
290 /******************/
291
292 REG Rec_Pointer Ptr_Val_Par;
293 /* executed once */
294 {
295 REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
296 /* == Ptr_Glob_Next */
297 /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
298 /* corresponds to "rename" in Ada, "with" in Pascal */
299
300 structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
301 Ptr_Val_Par->variant.var_1.Int_Comp = 5;
302 Next_Record->variant.var_1.Int_Comp
303 = Ptr_Val_Par->variant.var_1.Int_Comp;
304 Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
305 Proc_3 (&Next_Record->Ptr_Comp);
306 /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
307 == Ptr_Glob->Ptr_Comp */
308 if (Next_Record->Discr == Ident_1)
309 /* then, executed */
310 {
311 Next_Record->variant.var_1.Int_Comp = 6;
312 Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
313 &Next_Record->variant.var_1.Enum_Comp);
314 Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
315 Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
316 &Next_Record->variant.var_1.Int_Comp);
317 }
318 else /* not executed */
319 structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
320 } /* Proc_1 */
321
322
323 Proc_2 (Int_Par_Ref)
324 /******************/
325 /* executed once */
326 /* *Int_Par_Ref == 1, becomes 4 */
327
328 One_Fifty *Int_Par_Ref;
329 {
330 One_Fifty Int_Loc;
331 Enumeration Enum_Loc;
332
333 Int_Loc = *Int_Par_Ref + 10;
334 do /* executed once */
335 if (Ch_1_Glob == 'A')
336 /* then, executed */
337 {
338 Int_Loc -= 1;
339 *Int_Par_Ref = Int_Loc - Int_Glob;
340 Enum_Loc = Ident_1;
341 } /* if */
342 while (Enum_Loc != Ident_1); /* true */
343 } /* Proc_2 */
344
345
346 Proc_3 (Ptr_Ref_Par)
347 /******************/
348 /* executed once */
349 /* Ptr_Ref_Par becomes Ptr_Glob */
350
351 Rec_Pointer *Ptr_Ref_Par;
352
353 {
354 if (Ptr_Glob != Null)
355 /* then, executed */
356 *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
357 Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
358 } /* Proc_3 */
359
360
361 Proc_4 () /* without parameters */
362 /*******/
363 /* executed once */
364 {
365 Boolean Bool_Loc;
366
367 Bool_Loc = Ch_1_Glob == 'A';
368 Bool_Glob = Bool_Loc | Bool_Glob;
369 Ch_2_Glob = 'B';
370 } /* Proc_4 */
371
372
373 Proc_5 () /* without parameters */
374 /*******/
375 /* executed once */
376 {
377 Ch_1_Glob = 'A';
378 Bool_Glob = false;
379 } /* Proc_5 */