1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 ****************************************************************************
5 * "DHRYSTONE" Benchmark Program
6 * -----------------------------
8 * Version: C, Version 2.1
10 * File: dhry_1.c (part 2 of 3)
14 * Author: Reinhold P. Weicker
16 ****************************************************************************
21 #include <linux/ktime.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
25 /* Global Variables: */
30 static Rec_Pointer Ptr_Glob, Next_Ptr_Glob;
31 static Boolean Bool_Glob;
32 static char Ch_2_Glob;
33 static int Arr_1_Glob[50];
34 static int Arr_2_Glob[50][50];
36 static void Proc_3(Rec_Pointer *Ptr_Ref_Par)
39 /* Ptr_Ref_Par becomes Ptr_Glob */
43 *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
45 Proc_7(10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
49 static void Proc_1(Rec_Pointer Ptr_Val_Par)
53 Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
54 /* == Ptr_Glob_Next */
55 /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
56 /* corresponds to "rename" in Ada, "with" in Pascal */
58 *Ptr_Val_Par->Ptr_Comp = *Ptr_Glob;
59 Ptr_Val_Par->variant.var_1.Int_Comp = 5;
60 Next_Record->variant.var_1.Int_Comp =
61 Ptr_Val_Par->variant.var_1.Int_Comp;
62 Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
63 Proc_3(&Next_Record->Ptr_Comp);
64 /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp == Ptr_Glob->Ptr_Comp */
65 if (Next_Record->Discr == Ident_1) {
67 Next_Record->variant.var_1.Int_Comp = 6;
68 Proc_6(Ptr_Val_Par->variant.var_1.Enum_Comp,
69 &Next_Record->variant.var_1.Enum_Comp);
70 Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
71 Proc_7(Next_Record->variant.var_1.Int_Comp, 10,
72 &Next_Record->variant.var_1.Int_Comp);
75 *Ptr_Val_Par = *Ptr_Val_Par->Ptr_Comp;
80 static void Proc_2(One_Fifty *Int_Par_Ref)
83 /* *Int_Par_Ref == 1, becomes 4 */
88 Int_Loc = *Int_Par_Ref + 10;
91 if (Ch_1_Glob == 'A') {
94 *Int_Par_Ref = Int_Loc - Int_Glob;
97 } while (Enum_Loc != Ident_1); /* true */
101 static void Proc_4(void)
107 Bool_Loc = Ch_1_Glob == 'A';
108 Bool_Glob = Bool_Loc | Bool_Glob;
113 static void Proc_5(void)
125 /* main program, corresponds to procedures */
126 /* Main and Proc_0 in the Ada version */
132 Enumeration Enum_Loc;
137 ktime_t Begin_Time, End_Time;
140 /* Initializations */
142 Next_Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
143 Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
145 Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
146 Ptr_Glob->Discr = Ident_1;
147 Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
148 Ptr_Glob->variant.var_1.Int_Comp = 40;
149 strcpy(Ptr_Glob->variant.var_1.Str_Comp,
150 "DHRYSTONE PROGRAM, SOME STRING");
151 strcpy(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
153 Arr_2_Glob[8][7] = 10;
154 /* Was missing in published program. Without this statement, */
155 /* Arr_2_Glob[8][7] would have an undefined value. */
156 /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
157 /* overflow may occur for this array element. */
159 pr_debug("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
163 pr_debug("Execution starts, %d runs through Dhrystone\n",
170 Begin_Time = ktime_get();
172 for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
175 /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
178 strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
180 Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
182 while (Int_1_Loc < Int_2_Loc) {
183 /* loop body executed once */
184 Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
186 Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
190 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
191 Proc_8(Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
194 for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index) {
195 /* loop body executed twice */
196 if (Enum_Loc == Func_1(Ch_Index, 'C')) {
197 /* then, not executed */
198 Proc_6(Ident_1, &Enum_Loc);
199 strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
200 Int_2_Loc = Run_Index;
201 Int_Glob = Run_Index;
204 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
205 Int_2_Loc = Int_2_Loc * Int_1_Loc;
206 Int_1_Loc = Int_2_Loc / Int_3_Loc;
207 Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
208 /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
212 } /* loop "for Run_Index" */
218 End_Time = ktime_get();
220 #define dhry_assert_int_eq(val, expected) \
221 if (val != expected) \
222 pr_err("%s: %d (FAIL, expected %d)\n", #val, val, \
225 pr_debug("%s: %d (OK)\n", #val, val)
227 #define dhry_assert_char_eq(val, expected) \
228 if (val != expected) \
229 pr_err("%s: %c (FAIL, expected %c)\n", #val, val, \
232 pr_debug("%s: %c (OK)\n", #val, val)
234 #define dhry_assert_string_eq(val, expected) \
235 if (strcmp(val, expected)) \
236 pr_err("%s: %s (FAIL, expected %s)\n", #val, val, \
239 pr_debug("%s: %s (OK)\n", #val, val)
241 pr_debug("Execution ends\n");
242 pr_debug("Final values of the variables used in the benchmark:\n");
243 dhry_assert_int_eq(Int_Glob, 5);
244 dhry_assert_int_eq(Bool_Glob, 1);
245 dhry_assert_char_eq(Ch_1_Glob, 'A');
246 dhry_assert_char_eq(Ch_2_Glob, 'B');
247 dhry_assert_int_eq(Arr_1_Glob[8], 7);
248 dhry_assert_int_eq(Arr_2_Glob[8][7], Number_Of_Runs + 10);
249 pr_debug("Ptr_Comp: %px\n", Ptr_Glob->Ptr_Comp);
250 dhry_assert_int_eq(Ptr_Glob->Discr, 0);
251 dhry_assert_int_eq(Ptr_Glob->variant.var_1.Enum_Comp, 2);
252 dhry_assert_int_eq(Ptr_Glob->variant.var_1.Int_Comp, 17);
253 dhry_assert_string_eq(Ptr_Glob->variant.var_1.Str_Comp,
254 "DHRYSTONE PROGRAM, SOME STRING");
255 if (Next_Ptr_Glob->Ptr_Comp != Ptr_Glob->Ptr_Comp)
256 pr_err("Next_Ptr_Glob->Ptr_Comp: %px (expected %px)\n",
257 Next_Ptr_Glob->Ptr_Comp, Ptr_Glob->Ptr_Comp);
259 pr_debug("Next_Ptr_Glob->Ptr_Comp: %px\n",
260 Next_Ptr_Glob->Ptr_Comp);
261 dhry_assert_int_eq(Next_Ptr_Glob->Discr, 0);
262 dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Enum_Comp, 1);
263 dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Int_Comp, 18);
264 dhry_assert_string_eq(Next_Ptr_Glob->variant.var_1.Str_Comp,
265 "DHRYSTONE PROGRAM, SOME STRING");
266 dhry_assert_int_eq(Int_1_Loc, 5);
267 dhry_assert_int_eq(Int_2_Loc, 13);
268 dhry_assert_int_eq(Int_3_Loc, 7);
269 dhry_assert_int_eq(Enum_Loc, 1);
270 dhry_assert_string_eq(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
271 dhry_assert_string_eq(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
273 User_Time = ktime_to_ms(ktime_sub(End_Time, Begin_Time));
276 kfree(Next_Ptr_Glob);
278 /* Measurements should last at least 2 seconds */
279 if (User_Time < 2 * MSEC_PER_SEC)
282 return div_u64(mul_u32_u32(MSEC_PER_SEC, Number_Of_Runs), User_Time);