dhrystone_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // See LICENSE for license details.
  2. //**************************************************************************
  3. // Dhrystone bencmark
  4. //--------------------------------------------------------------------------
  5. //
  6. // This is the classic Dhrystone synthetic integer benchmark.
  7. // You should not change anything except the HOST_DEBUG and
  8. // PREALLOCATE macros for your timing run.
  9. #pragma GCC optimize ("no-inline")
  10. #include "dhrystone.h"
  11. //--------------------------------------------------------------------------
  12. // Macros
  13. // Set HOST_DEBUG to 1 if you are going to compile this for a host
  14. // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
  15. // to 0 if you are compiling with the smips-gcc toolchain.
  16. #ifndef HOST_DEBUG
  17. #define HOST_DEBUG 0
  18. #endif
  19. // Set PREALLOCATE to 1 if you want to preallocate the benchmark
  20. // function before starting stats. If you have instruction/data
  21. // caches and you don't want to count the overhead of misses, then
  22. // you will need to use preallocation.
  23. #ifndef PREALLOCATE
  24. #define PREALLOCATE 0
  25. #endif
  26. // Set SET_STATS to 1 if you want to carve out the piece that actually
  27. // does the computation.
  28. #ifndef SET_STATS
  29. #define SET_STATS 0
  30. #endif
  31. #if HOST_DEBUG
  32. # define debug_printf printf
  33. #else
  34. void debug_printf(const char* str, ...);
  35. #endif
  36. #include "util.h"
  37. #include <alloca.h>
  38. /* Global Variables: */
  39. Rec_Pointer Ptr_Glob,
  40. Next_Ptr_Glob;
  41. int Int_Glob;
  42. Boolean Bool_Glob;
  43. char Ch_1_Glob,
  44. Ch_2_Glob;
  45. int Arr_1_Glob [50];
  46. int Arr_2_Glob [50] [50];
  47. Enumeration Func_1 ();
  48. /* forward declaration necessary since Enumeration may not simply be int */
  49. #ifndef REG
  50. Boolean Reg = false;
  51. #define REG
  52. /* REG becomes defined as empty */
  53. /* i.e. no register variables */
  54. #else
  55. Boolean Reg = true;
  56. #undef REG
  57. #define REG register
  58. #endif
  59. Boolean Done;
  60. long Begin_Time,
  61. End_Time,
  62. User_Time;
  63. long Microseconds,
  64. Dhrystones_Per_Second;
  65. /* end of variables for time measurement */
  66. int main (int argc, char** argv)
  67. /*****/
  68. /* main program, corresponds to procedures */
  69. /* Main and Proc_0 in the Ada version */
  70. {
  71. One_Fifty Int_1_Loc;
  72. REG One_Fifty Int_2_Loc;
  73. One_Fifty Int_3_Loc;
  74. REG char Ch_Index;
  75. Enumeration Enum_Loc;
  76. Str_30 Str_1_Loc;
  77. Str_30 Str_2_Loc;
  78. REG int Run_Index;
  79. REG int Number_Of_Runs;
  80. /* Arguments */
  81. #if HOST_DEBUG
  82. if (argc > 2)
  83. {
  84. printf("Usage: %s [number of loops]\n", argv[0]);
  85. exit (1);
  86. }
  87. if (argc == 2)
  88. {
  89. Number_Of_Runs = atoi (argv[1]);
  90. }
  91. else if (Number_Of_Runs <= 0)
  92. #endif
  93. {
  94. Number_Of_Runs = NUMBER_OF_RUNS;
  95. }
  96. /* Initializations */
  97. Next_Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
  98. Ptr_Glob = (Rec_Pointer) alloca (sizeof (Rec_Type));
  99. Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
  100. Ptr_Glob->Discr = Ident_1;
  101. Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
  102. Ptr_Glob->variant.var_1.Int_Comp = 40;
  103. strcpy (Ptr_Glob->variant.var_1.Str_Comp,
  104. "DHRYSTONE PROGRAM, SOME STRING");
  105. strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
  106. Arr_2_Glob [8][7] = 10;
  107. /* Was missing in published program. Without this statement, */
  108. /* Arr_2_Glob [8][7] would have an undefined value. */
  109. /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
  110. /* overflow may occur for this array element. */
  111. debug_printf("\n");
  112. debug_printf("Dhrystone Benchmark, Version %s\n", Version);
  113. if (Reg)
  114. {
  115. debug_printf("Program compiled with 'register' attribute\n");
  116. }
  117. else
  118. {
  119. debug_printf("Program compiled without 'register' attribute\n");
  120. }
  121. debug_printf("Using %s, HZ=%d\n", CLOCK_TYPE, HZ);
  122. debug_printf("\n");
  123. Done = false;
  124. while (!Done) {
  125. debug_printf("Trying %d runs through Dhrystone:\n", Number_Of_Runs);
  126. /***************/
  127. /* Start timer */
  128. /***************/
  129. setStats(1);
  130. Start_Timer();
  131. for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)
  132. {
  133. Proc_5();
  134. Proc_4();
  135. /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
  136. Int_1_Loc = 2;
  137. Int_2_Loc = 3;
  138. strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
  139. Enum_Loc = Ident_2;
  140. Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
  141. /* Bool_Glob == 1 */
  142. while (Int_1_Loc < Int_2_Loc) /* loop body executed once */
  143. {
  144. Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
  145. /* Int_3_Loc == 7 */
  146. Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
  147. /* Int_3_Loc == 7 */
  148. Int_1_Loc += 1;
  149. } /* while */
  150. /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  151. Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
  152. /* Int_Glob == 5 */
  153. Proc_1 (Ptr_Glob);
  154. for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
  155. /* loop body executed twice */
  156. {
  157. if (Enum_Loc == Func_1 (Ch_Index, 'C'))
  158. /* then, not executed */
  159. {
  160. Proc_6 (Ident_1, &Enum_Loc);
  161. strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
  162. Int_2_Loc = Run_Index;
  163. Int_Glob = Run_Index;
  164. }
  165. }
  166. /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  167. Int_2_Loc = Int_2_Loc * Int_1_Loc;
  168. Int_1_Loc = Int_2_Loc / Int_3_Loc;
  169. Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
  170. /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
  171. Proc_2 (&Int_1_Loc);
  172. /* Int_1_Loc == 5 */
  173. } /* loop "for Run_Index" */
  174. /**************/
  175. /* Stop timer */
  176. /**************/
  177. Stop_Timer();
  178. setStats(0);
  179. User_Time = End_Time - Begin_Time;
  180. if (User_Time < Too_Small_Time)
  181. {
  182. printf("Measured time too small to obtain meaningful results\n");
  183. Number_Of_Runs = Number_Of_Runs * 10;
  184. printf("\n");
  185. } else Done = true;
  186. }
  187. debug_printf("Final values of the variables used in the benchmark:\n");
  188. debug_printf("\n");
  189. debug_printf("Int_Glob: %d\n", Int_Glob);
  190. debug_printf(" should be: %d\n", 5);
  191. debug_printf("Bool_Glob: %d\n", Bool_Glob);
  192. debug_printf(" should be: %d\n", 1);
  193. debug_printf("Ch_1_Glob: %c\n", Ch_1_Glob);
  194. debug_printf(" should be: %c\n", 'A');
  195. debug_printf("Ch_2_Glob: %c\n", Ch_2_Glob);
  196. debug_printf(" should be: %c\n", 'B');
  197. debug_printf("Arr_1_Glob[8]: %d\n", Arr_1_Glob[8]);
  198. debug_printf(" should be: %d\n", 7);
  199. debug_printf("Arr_2_Glob[8][7]: %d\n", Arr_2_Glob[8][7]);
  200. debug_printf(" should be: Number_Of_Runs + 10\n");
  201. debug_printf("Ptr_Glob->\n");
  202. debug_printf(" Ptr_Comp: %d\n", (long) Ptr_Glob->Ptr_Comp);
  203. debug_printf(" should be: (implementation-dependent)\n");
  204. debug_printf(" Discr: %d\n", Ptr_Glob->Discr);
  205. debug_printf(" should be: %d\n", 0);
  206. debug_printf(" Enum_Comp: %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
  207. debug_printf(" should be: %d\n", 2);
  208. debug_printf(" Int_Comp: %d\n", Ptr_Glob->variant.var_1.Int_Comp);
  209. debug_printf(" should be: %d\n", 17);
  210. debug_printf(" Str_Comp: %s\n", Ptr_Glob->variant.var_1.Str_Comp);
  211. debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
  212. debug_printf("Next_Ptr_Glob->\n");
  213. debug_printf(" Ptr_Comp: %d\n", (long) Next_Ptr_Glob->Ptr_Comp);
  214. debug_printf(" should be: (implementation-dependent), same as above\n");
  215. debug_printf(" Discr: %d\n", Next_Ptr_Glob->Discr);
  216. debug_printf(" should be: %d\n", 0);
  217. debug_printf(" Enum_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
  218. debug_printf(" should be: %d\n", 1);
  219. debug_printf(" Int_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
  220. debug_printf(" should be: %d\n", 18);
  221. debug_printf(" Str_Comp: %s\n",
  222. Next_Ptr_Glob->variant.var_1.Str_Comp);
  223. debug_printf(" should be: DHRYSTONE PROGRAM, SOME STRING\n");
  224. debug_printf("Int_1_Loc: %d\n", Int_1_Loc);
  225. debug_printf(" should be: %d\n", 5);
  226. debug_printf("Int_2_Loc: %d\n", Int_2_Loc);
  227. debug_printf(" should be: %d\n", 13);
  228. debug_printf("Int_3_Loc: %d\n", Int_3_Loc);
  229. debug_printf(" should be: %d\n", 7);
  230. debug_printf("Enum_Loc: %d\n", Enum_Loc);
  231. debug_printf(" should be: %d\n", 1);
  232. debug_printf("Str_1_Loc: %s\n", Str_1_Loc);
  233. debug_printf(" should be: DHRYSTONE PROGRAM, 1'ST STRING\n");
  234. debug_printf("Str_2_Loc: %s\n", Str_2_Loc);
  235. debug_printf(" should be: DHRYSTONE PROGRAM, 2'ND STRING\n");
  236. debug_printf("\n");
  237. Microseconds = ((User_Time / Number_Of_Runs) * Mic_secs_Per_Second) / HZ;
  238. Dhrystones_Per_Second = (HZ * Number_Of_Runs) / User_Time;
  239. printf("Microseconds for one run through Dhrystone: %ld\n", Microseconds);
  240. printf("Dhrystones per Second: %ld\n", Dhrystones_Per_Second);
  241. return 0;
  242. }
  243. Proc_1 (Ptr_Val_Par)
  244. /******************/
  245. REG Rec_Pointer Ptr_Val_Par;
  246. /* executed once */
  247. {
  248. REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
  249. /* == Ptr_Glob_Next */
  250. /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
  251. /* corresponds to "rename" in Ada, "with" in Pascal */
  252. structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
  253. Ptr_Val_Par->variant.var_1.Int_Comp = 5;
  254. Next_Record->variant.var_1.Int_Comp
  255. = Ptr_Val_Par->variant.var_1.Int_Comp;
  256. Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
  257. Proc_3 (&Next_Record->Ptr_Comp);
  258. /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
  259. == Ptr_Glob->Ptr_Comp */
  260. if (Next_Record->Discr == Ident_1)
  261. /* then, executed */
  262. {
  263. Next_Record->variant.var_1.Int_Comp = 6;
  264. Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
  265. &Next_Record->variant.var_1.Enum_Comp);
  266. Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
  267. Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
  268. &Next_Record->variant.var_1.Int_Comp);
  269. }
  270. else /* not executed */
  271. structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
  272. } /* Proc_1 */
  273. Proc_2 (Int_Par_Ref)
  274. /******************/
  275. /* executed once */
  276. /* *Int_Par_Ref == 1, becomes 4 */
  277. One_Fifty *Int_Par_Ref;
  278. {
  279. One_Fifty Int_Loc;
  280. Enumeration Enum_Loc;
  281. Int_Loc = *Int_Par_Ref + 10;
  282. do /* executed once */
  283. if (Ch_1_Glob == 'A')
  284. /* then, executed */
  285. {
  286. Int_Loc -= 1;
  287. *Int_Par_Ref = Int_Loc - Int_Glob;
  288. Enum_Loc = Ident_1;
  289. } /* if */
  290. while (Enum_Loc != Ident_1); /* true */
  291. } /* Proc_2 */
  292. Proc_3 (Ptr_Ref_Par)
  293. /******************/
  294. /* executed once */
  295. /* Ptr_Ref_Par becomes Ptr_Glob */
  296. Rec_Pointer *Ptr_Ref_Par;
  297. {
  298. if (Ptr_Glob != Null)
  299. /* then, executed */
  300. *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
  301. Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
  302. } /* Proc_3 */
  303. Proc_4 () /* without parameters */
  304. /*******/
  305. /* executed once */
  306. {
  307. Boolean Bool_Loc;
  308. Bool_Loc = Ch_1_Glob == 'A';
  309. Bool_Glob = Bool_Loc | Bool_Glob;
  310. Ch_2_Glob = 'B';
  311. } /* Proc_4 */
  312. Proc_5 () /* without parameters */
  313. /*******/
  314. /* executed once */
  315. {
  316. Ch_1_Glob = 'A';
  317. Bool_Glob = false;
  318. } /* Proc_5 */