xref: /openbmc/linux/lib/dhry_1.c (revision 91202ce7)
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_ATOMIC);
143 	if (!Next_Ptr_Glob)
144 		return -ENOMEM;
145 
146 	Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_ATOMIC);
147 	if (!Ptr_Glob) {
148 		kfree(Next_Ptr_Glob);
149 		return -ENOMEM;
150 	}
151 
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");
159 
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.                   */
165 
166 	pr_debug("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
167 
168 	Number_Of_Runs = n;
169 
170 	pr_debug("Execution starts, %d runs through Dhrystone\n",
171 		 Number_Of_Runs);
172 
173 	/***************/
174 	/* Start timer */
175 	/***************/
176 
177 	Begin_Time = ktime_get();
178 
179 	for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
180 		Proc_5();
181 		Proc_4();
182 		/* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
183 		Int_1_Loc = 2;
184 		Int_2_Loc = 3;
185 		strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
186 		Enum_Loc = Ident_2;
187 		Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
188 		/* Bool_Glob == 1 */
189 		while (Int_1_Loc < Int_2_Loc) {
190 			/* loop body executed once */
191 			Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
192 			/* Int_3_Loc == 7 */
193 			Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
194 			/* Int_3_Loc == 7 */
195 			Int_1_Loc += 1;
196 		} /* while */
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);
199 		/* Int_Glob == 5 */
200 		Proc_1(Ptr_Glob);
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;
209 			}
210 		}
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 */
216 		Proc_2(&Int_1_Loc);
217 		/* Int_1_Loc == 5 */
218 
219 	} /* loop "for Run_Index" */
220 
221 	/**************/
222 	/* Stop timer */
223 	/**************/
224 
225 	End_Time = ktime_get();
226 
227 #define dhry_assert_int_eq(val, expected)				\
228 	if (val != expected)						\
229 		pr_err("%s: %d (FAIL, expected %d)\n", #val, val,	\
230 		       expected);					\
231 	else								\
232 		pr_debug("%s: %d (OK)\n", #val, val)
233 
234 #define dhry_assert_char_eq(val, expected)				\
235 	if (val != expected)						\
236 		pr_err("%s: %c (FAIL, expected %c)\n", #val, val,	\
237 		       expected);					\
238 	else								\
239 		pr_debug("%s: %c (OK)\n", #val, val)
240 
241 #define dhry_assert_string_eq(val, expected)				\
242 	if (strcmp(val, expected))					\
243 		pr_err("%s: %s (FAIL, expected %s)\n", #val, val,	\
244 		       expected);					\
245 	else								\
246 		pr_debug("%s: %s (OK)\n", #val, val)
247 
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);
265 	else
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");
279 
280 	User_Time = ktime_to_ms(ktime_sub(End_Time, Begin_Time));
281 
282 	kfree(Ptr_Glob);
283 	kfree(Next_Ptr_Glob);
284 
285 	/* Measurements should last at least 2 seconds */
286 	if (User_Time < 2 * MSEC_PER_SEC)
287 		return -EAGAIN;
288 
289 	return div_u64(mul_u32_u32(MSEC_PER_SEC, Number_Of_Runs), User_Time);
290 }
291