dhry_1.c 13 KB

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