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