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_ATOMIC);
146 Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_ATOMIC);
148 kfree(Next_Ptr_Glob);
152 Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
153 Ptr_Glob->Discr = Ident_1;
154 Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
155 Ptr_Glob->variant.var_1.Int_Comp = 40;
156 strcpy(Ptr_Glob->variant.var_1.Str_Comp,
157 "DHRYSTONE PROGRAM, SOME STRING");
158 strcpy(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
160 Arr_2_Glob[8][7] = 10;
161 /* Was missing in published program. Without this statement, */
162 /* Arr_2_Glob[8][7] would have an undefined value. */
163 /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
164 /* overflow may occur for this array element. */
166 pr_debug("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
170 pr_debug("Execution starts, %d runs through Dhrystone\n",
177 Begin_Time = ktime_get();
179 for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
182 /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
185 strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
187 Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
189 while (Int_1_Loc < Int_2_Loc) {
190 /* loop body executed once */
191 Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
193 Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
197 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
198 Proc_8(Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
201 for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index) {
202 /* loop body executed twice */
203 if (Enum_Loc == Func_1(Ch_Index, 'C')) {
204 /* then, not executed */
205 Proc_6(Ident_1, &Enum_Loc);
206 strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
207 Int_2_Loc = Run_Index;
208 Int_Glob = Run_Index;
211 /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
212 Int_2_Loc = Int_2_Loc * Int_1_Loc;
213 Int_1_Loc = Int_2_Loc / Int_3_Loc;
214 Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
215 /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
219 } /* loop "for Run_Index" */
225 End_Time = ktime_get();
227 #define dhry_assert_int_eq(val, expected) \
228 if (val != expected) \
229 pr_err("%s: %d (FAIL, expected %d)\n", #val, val, \
232 pr_debug("%s: %d (OK)\n", #val, val)
234 #define dhry_assert_char_eq(val, expected) \
235 if (val != expected) \
236 pr_err("%s: %c (FAIL, expected %c)\n", #val, val, \
239 pr_debug("%s: %c (OK)\n", #val, val)
241 #define dhry_assert_string_eq(val, expected) \
242 if (strcmp(val, expected)) \
243 pr_err("%s: %s (FAIL, expected %s)\n", #val, val, \
246 pr_debug("%s: %s (OK)\n", #val, val)
248 pr_debug("Execution ends\n");
249 pr_debug("Final values of the variables used in the benchmark:\n");
250 dhry_assert_int_eq(Int_Glob, 5);
251 dhry_assert_int_eq(Bool_Glob, 1);
252 dhry_assert_char_eq(Ch_1_Glob, 'A');
253 dhry_assert_char_eq(Ch_2_Glob, 'B');
254 dhry_assert_int_eq(Arr_1_Glob[8], 7);
255 dhry_assert_int_eq(Arr_2_Glob[8][7], Number_Of_Runs + 10);
256 pr_debug("Ptr_Comp: %px\n", Ptr_Glob->Ptr_Comp);
257 dhry_assert_int_eq(Ptr_Glob->Discr, 0);
258 dhry_assert_int_eq(Ptr_Glob->variant.var_1.Enum_Comp, 2);
259 dhry_assert_int_eq(Ptr_Glob->variant.var_1.Int_Comp, 17);
260 dhry_assert_string_eq(Ptr_Glob->variant.var_1.Str_Comp,
261 "DHRYSTONE PROGRAM, SOME STRING");
262 if (Next_Ptr_Glob->Ptr_Comp != Ptr_Glob->Ptr_Comp)
263 pr_err("Next_Ptr_Glob->Ptr_Comp: %px (expected %px)\n",
264 Next_Ptr_Glob->Ptr_Comp, Ptr_Glob->Ptr_Comp);
266 pr_debug("Next_Ptr_Glob->Ptr_Comp: %px\n",
267 Next_Ptr_Glob->Ptr_Comp);
268 dhry_assert_int_eq(Next_Ptr_Glob->Discr, 0);
269 dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Enum_Comp, 1);
270 dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Int_Comp, 18);
271 dhry_assert_string_eq(Next_Ptr_Glob->variant.var_1.Str_Comp,
272 "DHRYSTONE PROGRAM, SOME STRING");
273 dhry_assert_int_eq(Int_1_Loc, 5);
274 dhry_assert_int_eq(Int_2_Loc, 13);
275 dhry_assert_int_eq(Int_3_Loc, 7);
276 dhry_assert_int_eq(Enum_Loc, 1);
277 dhry_assert_string_eq(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
278 dhry_assert_string_eq(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
280 User_Time = ktime_to_ms(ktime_sub(End_Time, Begin_Time));
283 kfree(Next_Ptr_Glob);
285 /* Measurements should last at least 2 seconds */
286 if (User_Time < 2 * MSEC_PER_SEC)
289 return div_u64(mul_u32_u32(MSEC_PER_SEC, Number_Of_Runs), User_Time);