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