1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Google, Inc
4  *
5  * Dhrystone is widely available in the public domain. A GPL license is
6  * chosen for U-Boot.
7  */
8 
9 /*****************************************************************************
10  *  The BYTE UNIX Benchmarks - Release 3
11  *          Module: dhry_1.c   SID: 3.4 5/15/91 19:30:21
12  *
13  *****************************************************************************
14  * Bug reports, patches, comments, suggestions should be sent to:
15  *
16  *	Ben Smith, Rick Grehan or Tom Yager
17  *	ben@bytepb.byte.com   rick_g@bytepb.byte.com   tyager@bytepb.byte.com
18  *
19  *****************************************************************************
20  *
21  * *** WARNING ****  With BYTE's modifications applied, results obtained with
22  *     *******       this version of the Dhrystone program may not be applicable
23  *                   to other versions.
24  *
25  *  Modification Log:
26  *  10/22/97 - code cleanup to remove ANSI C compiler warnings
27  *             Andy Kahn <kahn@zk3.dec.com>
28  *
29  *  Adapted from:
30  *
31  *                   "DHRYSTONE" Benchmark Program
32  *                   -----------------------------
33  *
34  *  Version:    C, Version 2.1
35  *
36  *  File:       dhry_1.c (part 2 of 3)
37  *
38  *  Date:       May 25, 1988
39  *
40  *  Author:     Reinhold P. Weicker
41  *
42  ***************************************************************************/
43 char SCCSid[] = "@(#) @(#)dhry_1.c:3.4 -- 5/15/91 19:30:21";
44 
45 #include <malloc.h>
46 #include <stdio.h>
47 
48 #include "dhry.h"
49 
50 unsigned long Run_Index;
51 
report(void)52 void report(void)
53 {
54 	printf("%ld loops\n", Run_Index);
55 }
56 
57 /* Global Variables: */
58 
59 Rec_Pointer     Ptr_Glob,
60                 Next_Ptr_Glob;
61 int             Int_Glob;
62 Boolean         Bool_Glob;
63 char            Ch_1_Glob,
64                 Ch_2_Glob;
65 int             Arr_1_Glob [50];
66 int             Arr_2_Glob [50] [50];
67 
68 Enumeration     Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val);
69   /* forward declaration necessary since Enumeration may not simply be int */
70 
71 #ifndef REG
72         Boolean Reg = false;
73 #define REG
74         /* REG becomes defined as empty */
75         /* i.e. no register variables   */
76 #else
77         Boolean Reg = true;
78 #endif
79 
80 /* variables for time measurement: */
81 
82 #ifdef TIMES
83 #define Too_Small_Time 120
84                 /* Measurements should last at least about 2 seconds */
85 #endif
86 #ifdef TIME
87 extern long     time();
88                 /* see library function "time"  */
89 #define Too_Small_Time 2
90                 /* Measurements should last at least 2 seconds */
91 #endif
92 
93 long            Begin_Time,
94                 End_Time,
95                 User_Time;
96 
97 /* end of variables for time measurement */
98 
99 void Proc_1 (REG Rec_Pointer Ptr_Val_Par);
100 void Proc_2 (One_Fifty   *Int_Par_Ref);
101 void Proc_3 (Rec_Pointer *Ptr_Ref_Par);
102 void Proc_4 (void);
103 void Proc_5 (void);
104 
105 extern Boolean Func_2(Str_30, Str_30);
106 extern void Proc_6(Enumeration, Enumeration *);
107 extern void Proc_7(One_Fifty, One_Fifty, One_Fifty *);
108 extern void Proc_8(Arr_1_Dim, Arr_2_Dim, int, int);
109 
dhry(int Number_Of_Runs)110 void dhry(int Number_Of_Runs)
111   /* main program, corresponds to procedures        */
112   /* Main and Proc_0 in the Ada version             */
113 {
114         One_Fifty       Int_1_Loc;
115   REG   One_Fifty       Int_2_Loc;
116         One_Fifty       Int_3_Loc;
117   REG   char            Ch_Index;
118         Enumeration     Enum_Loc;
119         Str_30          Str_1_Loc;
120         Str_30          Str_2_Loc;
121 
122   /* Initializations */
123 
124   Next_Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
125   Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
126 
127   Ptr_Glob->Ptr_Comp                    = Next_Ptr_Glob;
128   Ptr_Glob->Discr                       = Ident_1;
129   Ptr_Glob->variant.var_1.Enum_Comp     = Ident_3;
130   Ptr_Glob->variant.var_1.Int_Comp      = 40;
131   strcpy (Ptr_Glob->variant.var_1.Str_Comp,
132           "DHRYSTONE PROGRAM, SOME STRING");
133   strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
134 
135   Arr_2_Glob [8][7] = 10;
136         /* Was missing in published program. Without this statement,    */
137         /* Arr_2_Glob [8][7] would have an undefined value.             */
138         /* Warning: With 16-Bit processors and Number_Of_Runs > 32000,  */
139         /* overflow may occur for this array element.                   */
140 
141 #ifdef PRATTLE
142   printf ("\n");
143   printf ("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
144   printf ("\n");
145   if (Reg)
146   {
147     printf ("Program compiled with 'register' attribute\n");
148     printf ("\n");
149   }
150   else
151   {
152     printf ("Program compiled without 'register' attribute\n");
153     printf ("\n");
154   }
155   printf ("Please give the number of runs through the benchmark: ");
156   {
157     int n;
158     scanf ("%d", &n);
159     Number_Of_Runs = n;
160   }
161   printf ("\n");
162 
163   printf ("Execution starts, %d runs through Dhrystone\n", Number_Of_Runs);
164 #endif /* PRATTLE */
165 
166   Run_Index = 0;
167 
168   /***************/
169   /* Start timer */
170   /***************/
171 
172 #ifdef SELF_TIMED
173 #ifdef TIMES
174   times (&time_info);
175   Begin_Time = (long) time_info.tms_utime;
176 #endif
177 #ifdef TIME
178   Begin_Time = time ( (long *) 0);
179 #endif
180 #endif /* SELF_TIMED */
181 
182   for (Run_Index = 1; Run_Index < Number_Of_Runs; ++Run_Index)
183   {
184 
185     Proc_5();
186     Proc_4();
187       /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
188     Int_1_Loc = 2;
189     Int_2_Loc = 3;
190     strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
191     Enum_Loc = Ident_2;
192     Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
193       /* Bool_Glob == 1 */
194     while (Int_1_Loc < Int_2_Loc)  /* loop body executed once */
195     {
196       Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
197         /* Int_3_Loc == 7 */
198       Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
199         /* Int_3_Loc == 7 */
200       Int_1_Loc += 1;
201     } /* while */
202       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
203     Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
204       /* Int_Glob == 5 */
205     Proc_1 (Ptr_Glob);
206     for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
207                              /* loop body executed twice */
208     {
209       if (Enum_Loc == Func_1 (Ch_Index, 'C'))
210           /* then, not executed */
211         {
212         Proc_6 (Ident_1, &Enum_Loc);
213         strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
214         Int_2_Loc = Run_Index;
215         Int_Glob = Run_Index;
216         }
217     }
218       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
219     Int_2_Loc = Int_2_Loc * Int_1_Loc;
220     Int_1_Loc = Int_2_Loc / Int_3_Loc;
221     Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
222       /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
223     Proc_2 (&Int_1_Loc);
224       /* Int_1_Loc == 5 */
225 
226   } /* loop "for Run_Index" */
227 
228   /**************/
229   /* Stop timer */
230   /**************/
231 #ifdef SELF_TIMED
232 #ifdef TIMES
233   times (&time_info);
234   End_Time = (long) time_info.tms_utime;
235 #endif
236 #ifdef TIME
237   End_Time = time ( (long *) 0);
238 #endif
239 #endif /* SELF_TIMED */
240 
241   /* BYTE version never executes this stuff */
242 #ifdef SELF_TIMED
243   printf ("Execution ends\n");
244   printf ("\n");
245   printf ("Final values of the variables used in the benchmark:\n");
246   printf ("\n");
247   printf ("Int_Glob:            %d\n", Int_Glob);
248   printf ("        should be:   %d\n", 5);
249   printf ("Bool_Glob:           %d\n", Bool_Glob);
250   printf ("        should be:   %d\n", 1);
251   printf ("Ch_1_Glob:           %c\n", Ch_1_Glob);
252   printf ("        should be:   %c\n", 'A');
253   printf ("Ch_2_Glob:           %c\n", Ch_2_Glob);
254   printf ("        should be:   %c\n", 'B');
255   printf ("Arr_1_Glob[8]:       %d\n", Arr_1_Glob[8]);
256   printf ("        should be:   %d\n", 7);
257   printf ("Arr_2_Glob[8][7]:    %d\n", Arr_2_Glob[8][7]);
258   printf ("        should be:   Number_Of_Runs + 10\n");
259   printf ("Ptr_Glob->\n");
260   printf ("  Ptr_Comp:          %d\n", (int) Ptr_Glob->Ptr_Comp);
261   printf ("        should be:   (implementation-dependent)\n");
262   printf ("  Discr:             %d\n", Ptr_Glob->Discr);
263   printf ("        should be:   %d\n", 0);
264   printf ("  Enum_Comp:         %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
265   printf ("        should be:   %d\n", 2);
266   printf ("  Int_Comp:          %d\n", Ptr_Glob->variant.var_1.Int_Comp);
267   printf ("        should be:   %d\n", 17);
268   printf ("  Str_Comp:          %s\n", Ptr_Glob->variant.var_1.Str_Comp);
269   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
270   printf ("Next_Ptr_Glob->\n");
271   printf ("  Ptr_Comp:          %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
272   printf ("        should be:   (implementation-dependent), same as above\n");
273   printf ("  Discr:             %d\n", Next_Ptr_Glob->Discr);
274   printf ("        should be:   %d\n", 0);
275   printf ("  Enum_Comp:         %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
276   printf ("        should be:   %d\n", 1);
277   printf ("  Int_Comp:          %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
278   printf ("        should be:   %d\n", 18);
279   printf ("  Str_Comp:          %s\n",
280                                 Next_Ptr_Glob->variant.var_1.Str_Comp);
281   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
282   printf ("Int_1_Loc:           %d\n", Int_1_Loc);
283   printf ("        should be:   %d\n", 5);
284   printf ("Int_2_Loc:           %d\n", Int_2_Loc);
285   printf ("        should be:   %d\n", 13);
286   printf ("Int_3_Loc:           %d\n", Int_3_Loc);
287   printf ("        should be:   %d\n", 7);
288   printf ("Enum_Loc:            %d\n", Enum_Loc);
289   printf ("        should be:   %d\n", 1);
290   printf ("Str_1_Loc:           %s\n", Str_1_Loc);
291   printf ("        should be:   DHRYSTONE PROGRAM, 1'ST STRING\n");
292   printf ("Str_2_Loc:           %s\n", Str_2_Loc);
293   printf ("        should be:   DHRYSTONE PROGRAM, 2'ND STRING\n");
294   printf ("\n");
295 
296   User_Time = End_Time - Begin_Time;
297 
298   if (User_Time < Too_Small_Time)
299   {
300     printf ("Measured time too small to obtain meaningful results\n");
301     printf ("Please increase number of runs\n");
302     printf ("\n");
303   }
304   else
305   {
306 #ifdef TIME
307     Microseconds = (float) User_Time * Mic_secs_Per_Second
308                         / (float) Number_Of_Runs;
309     Dhrystones_Per_Second = (float) Number_Of_Runs / (float) User_Time;
310 #else
311     Microseconds = (float) User_Time * Mic_secs_Per_Second
312                         / ((float) HZ * ((float) Number_Of_Runs));
313     Dhrystones_Per_Second = ((float) HZ * (float) Number_Of_Runs)
314                         / (float) User_Time;
315 #endif
316     printf ("Microseconds for one run through Dhrystone: ");
317     printf ("%6.1f \n", Microseconds);
318     printf ("Dhrystones per Second:                      ");
319     printf ("%6.1f \n", Dhrystones_Per_Second);
320     printf ("\n");
321   }
322 #endif /* SELF_TIMED */
323 }
324 
Proc_1(REG Rec_Pointer Ptr_Val_Par)325 void Proc_1 (REG Rec_Pointer Ptr_Val_Par)
326     /* executed once */
327 {
328   REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
329                                         /* == Ptr_Glob_Next */
330   /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp,    */
331   /* corresponds to "rename" in Ada, "with" in Pascal           */
332 
333   structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
334   Ptr_Val_Par->variant.var_1.Int_Comp = 5;
335   Next_Record->variant.var_1.Int_Comp
336         = Ptr_Val_Par->variant.var_1.Int_Comp;
337   Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
338   Proc_3 (&Next_Record->Ptr_Comp);
339     /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
340                         == Ptr_Glob->Ptr_Comp */
341   if (Next_Record->Discr == Ident_1)
342     /* then, executed */
343   {
344     Next_Record->variant.var_1.Int_Comp = 6;
345     Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
346            &Next_Record->variant.var_1.Enum_Comp);
347     Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
348     Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
349            &Next_Record->variant.var_1.Int_Comp);
350   }
351   else /* not executed */
352     structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
353 } /* Proc_1 */
354 
Proc_2(One_Fifty * Int_Par_Ref)355 void Proc_2 (One_Fifty   *Int_Par_Ref)
356     /* executed once */
357     /* *Int_Par_Ref == 1, becomes 4 */
358 {
359   One_Fifty  Int_Loc;
360   Enumeration   Enum_Loc;
361 
362   Enum_Loc = 0;
363 
364   Int_Loc = *Int_Par_Ref + 10;
365   do /* executed once */
366     if (Ch_1_Glob == 'A')
367       /* then, executed */
368     {
369       Int_Loc -= 1;
370       *Int_Par_Ref = Int_Loc - Int_Glob;
371       Enum_Loc = Ident_1;
372     } /* if */
373   while (Enum_Loc != Ident_1); /* true */
374 } /* Proc_2 */
375 
Proc_3(Rec_Pointer * Ptr_Ref_Par)376 void Proc_3 (Rec_Pointer *Ptr_Ref_Par)
377     /* executed once */
378     /* Ptr_Ref_Par becomes Ptr_Glob */
379 {
380   if (Ptr_Glob != Null)
381     /* then, executed */
382     *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
383   Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
384 } /* Proc_3 */
385 
Proc_4(void)386 void Proc_4 (void) /* without parameters */
387     /* executed once */
388 {
389   Boolean Bool_Loc;
390 
391   Bool_Loc = Ch_1_Glob == 'A';
392   Bool_Glob = Bool_Loc | Bool_Glob;
393   Ch_2_Glob = 'B';
394 } /* Proc_4 */
395 
Proc_5(void)396 void Proc_5 (void) /* without parameters */
397 /*******/
398     /* executed once */
399 {
400   Ch_1_Glob = 'A';
401   Bool_Glob = false;
402 } /* Proc_5 */
403 
404         /* Procedure for the assignment of structures,          */
405         /* if the C compiler doesn't support this feature       */
406 #ifdef  NOSTRUCTASSIGN
memcpy(d,s,l)407 memcpy (d, s, l)
408 register char   *d;
409 register char   *s;
410 register int    l;
411 {
412         while (l--) *d++ = *s++;
413 }
414 #endif
415