Mask off large constants for RV32
[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 Enumeration Func_1 ();
64 /* forward declaration necessary since Enumeration may not simply be int */
65
66 #ifndef REG
67 Boolean Reg = false;
68 #define REG
69 /* REG becomes defined as empty */
70 /* i.e. no register variables */
71 #else
72 Boolean Reg = true;
73 #undef REG
74 #define REG register
75 #endif
76
77 Boolean Done;
78
79 long Begin_Time,
80 End_Time,
81 User_Time;
82 long Microseconds,
83 Dhrystones_Per_Second;
84
85 /* end of variables for time measurement */
86
87
88 int main (int argc, char** argv)
89 /*****/
90 /* main program, corresponds to procedures */
91 /* Main and Proc_0 in the Ada version */
92 {
93 One_Fifty Int_1_Loc;
94 REG One_Fifty Int_2_Loc;
95 One_Fifty Int_3_Loc;
96 REG char Ch_Index;
97 Enumeration Enum_Loc;
98 Str_30 Str_1_Loc;
99 Str_30 Str_2_Loc;
100 REG int Run_Index;
101 REG int Number_Of_Runs;
102
103 /* Arguments */
104 #if HOST_DEBUG
105 if (argc > 2)
106 {
107 printf("Usage: %s [number of loops]\n", argv[0]);
108 exit (1);
109 }
110 if (argc == 2)
111 {
112 Number_Of_Runs = atoi (argv[1]);
113 }
114 else if (Number_Of_Runs <= 0)
115 #endif
116 {
117 Number_Of_Runs = NUMBER_OF_RUNS;
118 }
119
120 /* Initializations */
121
122 Next_Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
123 Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
124
125 Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
126 Ptr_Glob->Discr = Ident_1;
127 Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
128 Ptr_Glob->variant.var_1.Int_Comp = 40;
129 strcpy (Ptr_Glob->variant.var_1.Str_Comp,
130 "DHRYSTONE PROGRAM, SOME STRING");
131 strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
132
133 Arr_2_Glob [8][7] = 10;
134 /* Was missing in published program. Without this statement, */
135 /* Arr_2_Glob [8][7] would have an undefined value. */
136 /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
137 /* overflow may occur for this array element. */
138
139 debug_printf("\n");
140 debug_printf("Dhrystone Benchmark, Version %s\n", Version);
141 if (Reg)
142 {
143 debug_printf("Program compiled with 'register' attribute\n");
144 }
145 else
146 {
147 debug_printf("Program compiled without 'register' attribute\n");
148 }
149 debug_printf("Using %s, HZ=%d\n", CLOCK_TYPE, HZ);
150 debug_printf("\n");
151
152 Done = false;
153 while (!Done) {
154 debug_printf("Trying %d runs through Dhrystone:\n", Number_Of_Runs);
155
156 /***************/
157 /* Start timer */
158 /***************/
159
160 setStats(1);
161 Start_Timer();
162
163 for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)
164 {
165
166 Proc_5();
167 Proc_4();
168 /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
169 Int_1_Loc = 2;
170 Int_2_Loc = 3;
171 strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
172 Enum_Loc = Ident_2;
173 Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
174 /* Bool_Glob == 1 */
175 while (Int_1_Loc < Int_2_Loc) /* loop body executed once */
176 {
177 Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
178 /* Int_3_Loc == 7 */
179 Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
180 /* Int_3_Loc == 7 */
181 Int_1_Loc += 1;
182 } /* while */
183 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
184 Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
185 /* Int_Glob == 5 */
186 Proc_1 (Ptr_Glob);
187 for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
188 /* loop body executed twice */
189 {
190 if (Enum_Loc == Func_1 (Ch_Index, 'C'))
191 /* then, not executed */
192 {
193 Proc_6 (Ident_1, &Enum_Loc);
194 strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
195 Int_2_Loc = Run_Index;
196 Int_Glob = Run_Index;
197 }
198 }
199 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
200 Int_2_Loc = Int_2_Loc * Int_1_Loc;
201 Int_1_Loc = Int_2_Loc / Int_3_Loc;
202 Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
203 /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
204 Proc_2 (&Int_1_Loc);
205 /* Int_1_Loc == 5 */
206
207 } /* loop "for Run_Index" */
208
209 /**************/
210 /* Stop timer */
211 /**************/
212
213 Stop_Timer();
214 setStats(0);
215
216 User_Time = End_Time - Begin_Time;
217
218 if (User_Time < Too_Small_Time)
219 {
220 printf("Measured time too small to obtain meaningful results\n");
221 Number_Of_Runs = Number_Of_Runs * 10;
222 printf("\n");
223 } else Done = true;
224 }
225
226 debug_printf("Final values of the variables used in the benchmark:\n");
227 debug_printf("\n");
228 debug_printf("Int_Glob: %d\n", Int_Glob);
229 debug_printf(" should be: %d\n", 5);
230 debug_printf("Bool_Glob: %d\n", Bool_Glob);
231 debug_printf(" should be: %d\n", 1);
232 debug_printf("Ch_1_Glob: %c\n", Ch_1_Glob);
233 debug_printf(" should be: %c\n", 'A');
234 debug_printf("Ch_2_Glob: %c\n", Ch_2_Glob);
235 debug_printf(" should be: %c\n", 'B');
236 debug_printf("Arr_1_Glob[8]: %d\n", Arr_1_Glob[8]);
237 debug_printf(" should be: %d\n", 7);
238 debug_printf("Arr_2_Glob[8][7]: %d\n", Arr_2_Glob[8][7]);
239 debug_printf(" should be: Number_Of_Runs + 10\n");
240 debug_printf("Ptr_Glob->\n");
241 debug_printf(" Ptr_Comp: %d\n", (long) Ptr_Glob->Ptr_Comp);
242 debug_printf(" should be: (implementation-dependent)\n");
243 debug_printf(" Discr: %d\n", Ptr_Glob->Discr);
244 debug_printf(" should be: %d\n", 0);
245 debug_printf(" Enum_Comp: %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
246 debug_printf(" should be: %d\n", 2);
247 debug_printf(" Int_Comp: %d\n", Ptr_Glob->variant.var_1.Int_Comp);
248 debug_printf(" should be: %d\n", 17);
249 debug_printf(" Str_Comp: %s\n", Ptr_Glob->variant.var_1.Str_Comp);
250 debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
251 debug_printf("Next_Ptr_Glob->\n");
252 debug_printf(" Ptr_Comp: %d\n", (long) Next_Ptr_Glob->Ptr_Comp);
253 debug_printf(" should be: (implementation-dependent), same as above\n");
254 debug_printf(" Discr: %d\n", Next_Ptr_Glob->Discr);
255 debug_printf(" should be: %d\n", 0);
256 debug_printf(" Enum_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
257 debug_printf(" should be: %d\n", 1);
258 debug_printf(" Int_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
259 debug_printf(" should be: %d\n", 18);
260 debug_printf(" Str_Comp: %s\n",
261 Next_Ptr_Glob->variant.var_1.Str_Comp);
262 debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
263 debug_printf("Int_1_Loc: %d\n", Int_1_Loc);
264 debug_printf(" should be: %d\n", 5);
265 debug_printf("Int_2_Loc: %d\n", Int_2_Loc);
266 debug_printf(" should be: %d\n", 13);
267 debug_printf("Int_3_Loc: %d\n", Int_3_Loc);
268 debug_printf(" should be: %d\n", 7);
269 debug_printf("Enum_Loc: %d\n", Enum_Loc);
270 debug_printf(" should be: %d\n", 1);
271 debug_printf("Str_1_Loc: %s\n", Str_1_Loc);
272 debug_printf(" should be: DHRYSTONE PROGRAM, 1'ST STRING\n");
273 debug_printf("Str_2_Loc: %s\n", Str_2_Loc);
274 debug_printf(" should be: DHRYSTONE PROGRAM, 2'ND STRING\n");
275 debug_printf("\n");
276
277
278 Microseconds = ((User_Time / Number_Of_Runs) * Mic_secs_Per_Second) / HZ;
279 Dhrystones_Per_Second = (HZ * Number_Of_Runs) / User_Time;
280
281 printf("Microseconds for one run through Dhrystone: %ld\n", Microseconds);
282 printf("Dhrystones per Second: %ld\n", Dhrystones_Per_Second);
283
284 return 0;
285 }
286
287
288 Proc_1 (Ptr_Val_Par)
289 /******************/
290
291 REG Rec_Pointer Ptr_Val_Par;
292 /* executed once */
293 {
294 REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
295 /* == Ptr_Glob_Next */
296 /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
297 /* corresponds to "rename" in Ada, "with" in Pascal */
298
299 structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
300 Ptr_Val_Par->variant.var_1.Int_Comp = 5;
301 Next_Record->variant.var_1.Int_Comp
302 = Ptr_Val_Par->variant.var_1.Int_Comp;
303 Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
304 Proc_3 (&Next_Record->Ptr_Comp);
305 /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
306 == Ptr_Glob->Ptr_Comp */
307 if (Next_Record->Discr == Ident_1)
308 /* then, executed */
309 {
310 Next_Record->variant.var_1.Int_Comp = 6;
311 Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
312 &Next_Record->variant.var_1.Enum_Comp);
313 Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
314 Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
315 &Next_Record->variant.var_1.Int_Comp);
316 }
317 else /* not executed */
318 structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
319 } /* Proc_1 */
320
321
322 Proc_2 (Int_Par_Ref)
323 /******************/
324 /* executed once */
325 /* *Int_Par_Ref == 1, becomes 4 */
326
327 One_Fifty *Int_Par_Ref;
328 {
329 One_Fifty Int_Loc;
330 Enumeration Enum_Loc;
331
332 Int_Loc = *Int_Par_Ref + 10;
333 do /* executed once */
334 if (Ch_1_Glob == 'A')
335 /* then, executed */
336 {
337 Int_Loc -= 1;
338 *Int_Par_Ref = Int_Loc - Int_Glob;
339 Enum_Loc = Ident_1;
340 } /* if */
341 while (Enum_Loc != Ident_1); /* true */
342 } /* Proc_2 */
343
344
345 Proc_3 (Ptr_Ref_Par)
346 /******************/
347 /* executed once */
348 /* Ptr_Ref_Par becomes Ptr_Glob */
349
350 Rec_Pointer *Ptr_Ref_Par;
351
352 {
353 if (Ptr_Glob != Null)
354 /* then, executed */
355 *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
356 Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
357 } /* Proc_3 */
358
359
360 Proc_4 () /* without parameters */
361 /*******/
362 /* executed once */
363 {
364 Boolean Bool_Loc;
365
366 Bool_Loc = Ch_1_Glob == 'A';
367 Bool_Glob = Bool_Loc | Bool_Glob;
368 Ch_2_Glob = 'B';
369 } /* Proc_4 */
370
371
372 Proc_5 () /* without parameters */
373 /*******/
374 /* executed once */
375 {
376 Ch_1_Glob = 'A';
377 Bool_Glob = false;
378 } /* Proc_5 */