xref: /openbmc/linux/lib/dhry_1.c (revision ae6385af)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /*
3  ****************************************************************************
4  *
5  *                   "DHRYSTONE" Benchmark Program
6  *                   -----------------------------
7  *
8  *  Version:    C, Version 2.1
9  *
10  *  File:       dhry_1.c (part 2 of 3)
11  *
12  *  Date:       May 25, 1988
13  *
14  *  Author:     Reinhold P. Weicker
15  *
16  ****************************************************************************
17  */
18 
19 #include "dhry.h"
20 
21 #include <linux/ktime.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 
25 /* Global Variables: */
26 
27 int Int_Glob;
28 char Ch_1_Glob;
29 
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];
35 
36 static void Proc_3(Rec_Pointer *Ptr_Ref_Par)
37 /******************/
38 /* executed once */
39 /* Ptr_Ref_Par becomes Ptr_Glob */
40 {
41 	if (Ptr_Glob) {
42 		/* then, executed */
43 		*Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
44 	}
45 	Proc_7(10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
46 } /* Proc_3 */
47 
48 
49 static void Proc_1(Rec_Pointer Ptr_Val_Par)
50 /******************/
51 /* executed once */
52 {
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           */
57 
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) {
66 		/* then, executed */
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);
73 	} else {
74 		/* not executed */
75 		*Ptr_Val_Par = *Ptr_Val_Par->Ptr_Comp;
76 	}
77 } /* Proc_1 */
78 
79 
80 static void Proc_2(One_Fifty *Int_Par_Ref)
81 /******************/
82 /* executed once */
83 /* *Int_Par_Ref == 1, becomes 4 */
84 {
85 	One_Fifty  Int_Loc;
86 	Enumeration   Enum_Loc;
87 
88 	Int_Loc = *Int_Par_Ref + 10;
89 	do {
90 		/* executed once */
91 		if (Ch_1_Glob == 'A') {
92 			/* then, executed */
93 			Int_Loc -= 1;
94 			*Int_Par_Ref = Int_Loc - Int_Glob;
95 			Enum_Loc = Ident_1;
96 		} /* if */
97 	} while (Enum_Loc != Ident_1); /* true */
98 } /* Proc_2 */
99 
100 
101 static void Proc_4(void)
102 /*******/
103 /* executed once */
104 {
105 	Boolean Bool_Loc;
106 
107 	Bool_Loc = Ch_1_Glob == 'A';
108 	Bool_Glob = Bool_Loc | Bool_Glob;
109 	Ch_2_Glob = 'B';
110 } /* Proc_4 */
111 
112 
113 static void Proc_5(void)
114 /*******/
115 /* executed once */
116 {
117 	Ch_1_Glob = 'A';
118 	Bool_Glob = false;
119 } /* Proc_5 */
120 
121 
122 int dhry(int n)
123 /*****/
124 
125   /* main program, corresponds to procedures        */
126   /* Main and Proc_0 in the Ada version             */
127 {
128 	One_Fifty Int_1_Loc;
129 	One_Fifty Int_2_Loc;
130 	One_Fifty Int_3_Loc;
131 	char Ch_Index;
132 	Enumeration Enum_Loc;
133 	Str_30 Str_1_Loc;
134 	Str_30 Str_2_Loc;
135 	int Run_Index;
136 	int Number_Of_Runs;
137 	ktime_t Begin_Time, End_Time;
138 	u32 User_Time;
139 
140 	/* Initializations */
141 
142 	Next_Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
143 	Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
144 
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");
152 
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.                   */
158 
159 	pr_debug("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
160 
161 	Number_Of_Runs = n;
162 
163 	pr_debug("Execution starts, %d runs through Dhrystone\n",
164 		 Number_Of_Runs);
165 
166 	/***************/
167 	/* Start timer */
168 	/***************/
169 
170 	Begin_Time = ktime_get();
171 
172 	for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
173 		Proc_5();
174 		Proc_4();
175 		/* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
176 		Int_1_Loc = 2;
177 		Int_2_Loc = 3;
178 		strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
179 		Enum_Loc = Ident_2;
180 		Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
181 		/* Bool_Glob == 1 */
182 		while (Int_1_Loc < Int_2_Loc) {
183 			/* loop body executed once */
184 			Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
185 			/* Int_3_Loc == 7 */
186 			Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
187 			/* Int_3_Loc == 7 */
188 			Int_1_Loc += 1;
189 		} /* while */
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);
192 		/* Int_Glob == 5 */
193 		Proc_1(Ptr_Glob);
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;
202 			}
203 		}
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 */
209 		Proc_2(&Int_1_Loc);
210 		/* Int_1_Loc == 5 */
211 
212 	} /* loop "for Run_Index" */
213 
214 	/**************/
215 	/* Stop timer */
216 	/**************/
217 
218 	End_Time = ktime_get();
219 
220 #define dhry_assert_int_eq(val, expected)				\
221 	if (val != expected)						\
222 		pr_err("%s: %d (FAIL, expected %d)\n", #val, val,	\
223 		       expected);					\
224 	else								\
225 		pr_debug("%s: %d (OK)\n", #val, val)
226 
227 #define dhry_assert_char_eq(val, expected)				\
228 	if (val != expected)						\
229 		pr_err("%s: %c (FAIL, expected %c)\n", #val, val,	\
230 		       expected);					\
231 	else								\
232 		pr_debug("%s: %c (OK)\n", #val, val)
233 
234 #define dhry_assert_string_eq(val, expected)				\
235 	if (strcmp(val, expected))					\
236 		pr_err("%s: %s (FAIL, expected %s)\n", #val, val,	\
237 		       expected);					\
238 	else								\
239 		pr_debug("%s: %s (OK)\n", #val, val)
240 
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);
258 	else
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");
272 
273 	User_Time = ktime_to_ms(ktime_sub(End_Time, Begin_Time));
274 
275 	kfree(Ptr_Glob);
276 	kfree(Next_Ptr_Glob);
277 
278 	/* Measurements should last at least 2 seconds */
279 	if (User_Time < 2 * MSEC_PER_SEC)
280 		return -EAGAIN;
281 
282 	return div_u64(mul_u32_u32(MSEC_PER_SEC, Number_Of_Runs), User_Time);
283 }
284