home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / demos / programme / StormC / Fast-ANSI-Lib / Examples / Dhrystone / dhry_storm_1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  11.5 KB  |  397 lines

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