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