home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / diski117.zip / dhry_1.c next >
C/C++ Source or Header  |  1998-03-22  |  15KB  |  508 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. void Proc_1 (), Proc_2 (), Proc_3 (), Proc_4 (), Proc_5 ();
  35.  
  36. #ifndef REG
  37.         Boolean Reg = false;
  38. #define REG
  39.         /* REG becomes defined as empty */
  40.         /* i.e. no register variables   */
  41. #else
  42.         Boolean Reg = true;
  43. #endif
  44.  
  45. /* variables for time measurement: */
  46.  
  47. #ifdef TIMES
  48. struct tms      time_info;
  49. extern  int     times ();
  50.                 /* see library function "times" */
  51. #define Too_Small_Time (2*HZ)
  52.                 /* Measurements should last at least about 2 seconds */
  53. #endif
  54.  
  55. #ifdef TIME
  56. extern long     time();
  57.                 /* see library function "time"  */
  58. #define Too_Small_Time 2
  59.                 /* Measurements should last at least 2 seconds */
  60. #endif
  61.  
  62. #ifdef CLOCK
  63. extern clock_t    clock();
  64. #define Too_Small_Time (2*HZ)
  65. #define DURATION 5
  66. #endif
  67.  
  68. #ifdef OS2TMR
  69. extern long start_alarm(long), start_timer(void *), stop_timer(void*, long);
  70. char Run_Time[8];
  71. #ifdef HZ
  72. #undef HZ
  73. #endif
  74. #define HZ 1000
  75. #define Too_Small_Time (2*HZ)
  76. #define DURATION 5
  77. #define main dhry_main
  78. #endif
  79.  
  80. #ifdef DURATION
  81. #include <signal.h>
  82. #endif
  83.  
  84. unsigned long   Number_Of_Runs;
  85. long            User_Time;
  86. unsigned long   Microseconds,
  87.                 Totalseconds,
  88.                 Dhrystones_Per_Second;
  89.  
  90. /* end of variables for time measurement */
  91.  
  92. long dhry_stone(void)
  93. {
  94.     One_Fifty       Int_1_Loc;
  95.   REG   One_Fifty       Int_2_Loc;
  96.     One_Fifty       Int_3_Loc;
  97.   REG   char            Ch_Index;
  98.     Enumeration     Enum_Loc;
  99.     Str_30          Str_1_Loc;
  100.     Str_30          Str_2_Loc;
  101.   REG   unsigned long   Run_Index;
  102.  
  103.   long Begin_Time, End_Time;
  104.                 
  105.   /* Initializations */
  106.  
  107.   Next_Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
  108.   Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
  109.  
  110.   Ptr_Glob->Ptr_Comp                    = Next_Ptr_Glob;
  111.   Ptr_Glob->Discr                       = Ident_1;
  112.   Ptr_Glob->variant.var_1.Enum_Comp     = Ident_3;
  113.   Ptr_Glob->variant.var_1.Int_Comp      = 40;
  114.   strcpy (Ptr_Glob->variant.var_1.Str_Comp,
  115.           "DHRYSTONE PROGRAM, SOME STRING");
  116.   strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
  117.  
  118.   Arr_2_Glob [8][7] = 10;
  119.         /* Was missing in published program. Without this statement,    */
  120.         /* Arr_2_Glob [8][7] would have an undefined value.             */
  121.         /* Warning: With 16-Bit processors and Number_Of_Runs > 32000,  */
  122.         /* overflow may occur for this array element.                   */
  123.  
  124.   /***************/
  125.   /* Start timer */
  126.   /***************/
  127.  
  128. #ifdef TIMES
  129.   times (&time_info);
  130.   Begin_Time = (long) time_info.tms_utime;
  131. #endif
  132. #ifdef TIME
  133.   Begin_Time = time ( (long *) 0);
  134. #endif
  135. #ifdef CLOCK
  136.   Begin_Time = clock();
  137. #endif
  138. #ifdef OS2TMR
  139.   Begin_Time = 0;
  140.   start_timer(&Run_Time);
  141. #endif
  142.  
  143.   for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)
  144.   {
  145.  
  146.     Proc_5();
  147.     Proc_4();
  148.       /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
  149.     Int_1_Loc = 2;
  150.     Int_2_Loc = 3;
  151.     strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
  152.     Enum_Loc = Ident_2;
  153.     Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
  154.       /* Bool_Glob == 1 */
  155.     while (Int_1_Loc < Int_2_Loc)  /* loop body executed once */
  156.     {
  157.       Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
  158.         /* Int_3_Loc == 7 */
  159.       Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
  160.         /* Int_3_Loc == 7 */
  161.       Int_1_Loc += 1;
  162.     } /* while */
  163.       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  164.     Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
  165.       /* Int_Glob == 5 */
  166.     Proc_1 (Ptr_Glob);
  167.     for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
  168.                              /* loop body executed twice */
  169.     {
  170.       if (Enum_Loc == Func_1 (Ch_Index, 'C'))
  171.           /* then, not executed */
  172.         {
  173.         Proc_6 (Ident_1, &Enum_Loc);
  174.         strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
  175.         Int_2_Loc = Run_Index;
  176.         Int_Glob = Run_Index;
  177.         }
  178.     }
  179.       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  180.     Int_2_Loc = Int_2_Loc * Int_1_Loc;
  181.     Int_1_Loc = Int_2_Loc / Int_3_Loc;
  182.     Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
  183.       /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
  184.     Proc_2 (&Int_1_Loc);
  185.       /* Int_1_Loc == 5 */
  186.  
  187.   } /* loop "for Run_Index" */
  188.  
  189.   /**************/
  190.   /* Stop timer */
  191.   /**************/
  192.  
  193. #ifdef TIMES
  194.   times (&time_info);
  195.   End_Time = (long) time_info.tms_utime;
  196. #endif
  197. #ifdef TIME
  198.   End_Time = time ( (long *) 0);
  199. #endif
  200. #ifdef CLOCK
  201.   End_Time = clock();
  202. #endif
  203. #ifdef OS2TMR
  204.   End_Time = stop_timer(&Run_Time, HZ);
  205. #endif
  206. #ifdef DURATION
  207.   Number_Of_Runs = Run_Index - 1;
  208. #endif
  209.  
  210.   if (Int_Glob != 5)
  211.     return 0;
  212.   if (Bool_Glob != 1)
  213.     return 0;
  214.   if (Ch_1_Glob != 'A')
  215.     return 0;
  216.   if (Ch_2_Glob != 'B')
  217.     return 0;
  218.   if (Arr_1_Glob[8] != 7)
  219.     return 0;
  220.   if (Arr_2_Glob[8][7] != Number_Of_Runs + 10)
  221.     return 0;
  222.   if (Ptr_Glob->Discr != 0)
  223.     return 0;
  224.   if (Ptr_Glob->variant.var_1.Enum_Comp != 2)
  225.     return 0;
  226.   if (Ptr_Glob->variant.var_1.Int_Comp != 17)
  227.     return 0;
  228.   if (strcmp(Ptr_Glob->variant.var_1.Str_Comp, 
  229.          "DHRYSTONE PROGRAM, SOME STRING") != 0)
  230.     return 0;
  231.   if (Next_Ptr_Glob->Discr != 0)
  232.     return 0;
  233.   if (Next_Ptr_Glob->variant.var_1.Enum_Comp != 1)
  234.     return 0;
  235.   if (Next_Ptr_Glob->variant.var_1.Int_Comp != 18)
  236.     return 0;
  237.   if (strcmp(Next_Ptr_Glob->variant.var_1.Str_Comp, 
  238.          "DHRYSTONE PROGRAM, SOME STRING") != 0)
  239.     return 0;
  240.   if (Int_1_Loc != 5)
  241.     return 0;
  242.   if (Int_2_Loc != 13)
  243.     return 0;
  244.   if (Int_3_Loc != 7)
  245.     return 0;
  246.   if (Enum_Loc != 1)
  247.     return 0;
  248.   if (strcmp(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING") != 0)
  249.     return 0;
  250.   if (strcmp(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING") != 0)
  251.     return 0;
  252.  
  253.   return End_Time - Begin_Time;
  254. }
  255.  
  256. void alarm_handler(int sig)
  257. {
  258.   Number_Of_Runs = 0;
  259. }
  260.  
  261. void main (argc, argv)
  262. int argc;
  263. char **argv;
  264. /*****/
  265.  
  266.   /* main program, corresponds to procedures        */
  267.   /* Main and Proc_0 in the Ada version             */
  268. {
  269.   printf ("\n");
  270.   printf ("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
  271.   printf ("\n");
  272.   if (Reg)
  273.   {
  274.     printf ("Program compiled with 'register' attribute\n");
  275.     printf ("\n");
  276.   }
  277.   else
  278.   {
  279.     printf ("Program compiled without 'register' attribute\n");
  280.     printf ("\n");
  281.   }
  282.  
  283.   if ( argc == 1 )
  284.   {
  285. #ifdef DURATION
  286.     printf ("Execution starts, runs through Dhrystone for %d seconds\n", DURATION);
  287.     Number_Of_Runs = -1;
  288. #ifdef OS2TMR
  289.     start_alarm(DURATION);
  290. #else
  291.     signal(SIGALRM, alarm_handler);
  292.     alarm(DURATION);
  293. #endif
  294. #else
  295.     printf ("Please give the number of runs through the benchmark: ");
  296.     {
  297.       unsigned long n;
  298.       scanf ("%lu", &n);
  299.       Number_Of_Runs = n;
  300.     }
  301.     printf ("\n");
  302.     printf ("Execution starts, %lu runs through Dhrystone\n", Number_Of_Runs);
  303. #endif
  304.   }
  305.   else
  306.   {
  307.     extern long atol();
  308.     Number_Of_Runs = atol(argv[1]);
  309.     printf ("Execution starts, %lu runs through Dhrystone\n", Number_Of_Runs);
  310.   }
  311.  
  312.   User_Time = dhry_stone();
  313.  
  314.   if (User_Time == 0)
  315.   {
  316.     printf ("Data consistency error\n");
  317.     exit(1);
  318.   }
  319.  
  320.   printf ("Execution ends\n");
  321.   printf ("\n");
  322. #if 0
  323.   printf ("Final values of the variables used in the benchmark:\n");
  324.   printf ("\n");
  325.   printf ("Int_Glob:            %d\n", Int_Glob);
  326.   printf ("        should be:   %d\n", 5);
  327.   printf ("Bool_Glob:           %d\n", Bool_Glob);
  328.   printf ("        should be:   %d\n", 1);
  329.   printf ("Ch_1_Glob:           %c\n", Ch_1_Glob);
  330.   printf ("        should be:   %c\n", 'A');
  331.   printf ("Ch_2_Glob:           %c\n", Ch_2_Glob);
  332.   printf ("        should be:   %c\n", 'B');
  333.   printf ("Arr_1_Glob[8]:       %d\n", Arr_1_Glob[8]);
  334.   printf ("        should be:   %d\n", 7);
  335.   printf ("Arr_2_Glob[8][7]:    %u\n", Arr_2_Glob[8][7]);
  336.   printf ("        should be:   Number_Of_Runs + 10\n");
  337.   printf ("Ptr_Glob->\n");
  338.   printf ("  Ptr_Comp:          %d\n", (int) Ptr_Glob->Ptr_Comp);
  339.   printf ("        should be:   (implementation-dependent)\n");
  340.   printf ("  Discr:             %d\n", Ptr_Glob->Discr);
  341.   printf ("        should be:   %d\n", 0);
  342.   printf ("  Enum_Comp:         %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
  343.   printf ("        should be:   %d\n", 2);
  344.   printf ("  Int_Comp:          %d\n", Ptr_Glob->variant.var_1.Int_Comp);
  345.   printf ("        should be:   %d\n", 17);
  346.   printf ("  Str_Comp:          %s\n", Ptr_Glob->variant.var_1.Str_Comp);
  347.   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
  348.   printf ("Next_Ptr_Glob->\n");
  349.   printf ("  Ptr_Comp:          %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
  350.   printf ("        should be:   (implementation-dependent), same as above\n");
  351.   printf ("  Discr:             %d\n", Next_Ptr_Glob->Discr);
  352.   printf ("        should be:   %d\n", 0);
  353.   printf ("  Enum_Comp:         %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
  354.   printf ("        should be:   %d\n", 1);
  355.   printf ("  Int_Comp:          %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
  356.   printf ("        should be:   %d\n", 18);
  357.   printf ("  Str_Comp:          %s\n",
  358.                                 Next_Ptr_Glob->variant.var_1.Str_Comp);
  359.   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
  360.   printf ("Int_1_Loc:           %d\n", Int_1_Loc);
  361.   printf ("        should be:   %d\n", 5);
  362.   printf ("Int_2_Loc:           %d\n", Int_2_Loc);
  363.   printf ("        should be:   %d\n", 13);
  364.   printf ("Int_3_Loc:           %d\n", Int_3_Loc);
  365.   printf ("        should be:   %d\n", 7);
  366.   printf ("Enum_Loc:            %d\n", Enum_Loc);
  367.   printf ("        should be:   %d\n", 1);
  368.   printf ("Str_1_Loc:           %s\n", Str_1_Loc);
  369.   printf ("        should be:   DHRYSTONE PROGRAM, 1'ST STRING\n");
  370.   printf ("Str_2_Loc:           %s\n", Str_2_Loc);
  371.   printf ("        should be:   DHRYSTONE PROGRAM, 2'ND STRING\n");
  372.   printf ("\n");
  373. #endif
  374.  
  375.   if (User_Time < Too_Small_Time)
  376.   {
  377.     printf ("Measured time too small to obtain meaningful results\n");
  378.     printf ("Please increase number of runs\n");
  379.     printf ("\n");
  380.   }
  381.   else
  382.   {
  383. #ifdef TIME
  384.     Totalseconds = User_Time * 10L;
  385.     Microseconds = User_Time * Mic_secs_Per_Second * 10L / Number_Of_Runs;
  386.     Dhrystones_Per_Second = (unsigned long) Number_Of_Runs / User_Time;
  387. #else
  388.     Totalseconds = User_Time * 10L / HZ;
  389.     Microseconds = User_Time * (Mic_secs_Per_Second / HZ) * 10L / Number_Of_Runs;
  390.     Dhrystones_Per_Second = (unsigned long) Number_Of_Runs * HZ / User_Time;
  391. #endif
  392.  
  393.     printf ("Total Seconds for %7lu runs through Dhrystone: ", Number_Of_Runs);
  394.     printf ("%6ld.%1ld \n", Totalseconds / 10L, Totalseconds % 10L);
  395.     printf ("Microseconds for one run through Dhrystone:       ");
  396.     printf ("%6ld.%1ld \n", Microseconds / 10L, Microseconds % 10L);
  397.     printf ("Dhrystones per Second:                            ");
  398.     printf ("%8ld \n", Dhrystones_Per_Second);
  399.   }
  400.  
  401. }
  402.  
  403.  
  404. void Proc_1 (Ptr_Val_Par)
  405. /******************/
  406.  
  407. REG Rec_Pointer Ptr_Val_Par;
  408.     /* executed once */
  409. {
  410.   REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
  411.                                         /* == Ptr_Glob_Next */
  412.   /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp,    */
  413.   /* corresponds to "rename" in Ada, "with" in Pascal           */
  414.  
  415.   structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
  416.   Ptr_Val_Par->variant.var_1.Int_Comp = 5;
  417.   Next_Record->variant.var_1.Int_Comp
  418.         = Ptr_Val_Par->variant.var_1.Int_Comp;
  419.   Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
  420.   Proc_3 (&Next_Record->Ptr_Comp);
  421.     /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
  422.                         == Ptr_Glob->Ptr_Comp */
  423.   if (Next_Record->Discr == Ident_1)
  424.     /* then, executed */
  425.   {
  426.     Next_Record->variant.var_1.Int_Comp = 6;
  427.     Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
  428.            &Next_Record->variant.var_1.Enum_Comp);
  429.     Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
  430.     Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
  431.            &Next_Record->variant.var_1.Int_Comp);
  432.   }
  433.   else /* not executed */
  434.     structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
  435. } /* Proc_1 */
  436.  
  437.  
  438. void Proc_2 (Int_Par_Ref)
  439. /******************/
  440.     /* executed once */
  441.     /* *Int_Par_Ref == 1, becomes 4 */
  442.  
  443. One_Fifty   *Int_Par_Ref;
  444. {
  445.   One_Fifty  Int_Loc;
  446.   Enumeration   Enum_Loc;
  447.  
  448.   Int_Loc = *Int_Par_Ref + 10;
  449.   do /* executed once */
  450.     if (Ch_1_Glob == 'A')
  451.       /* then, executed */
  452.     {
  453.       Int_Loc -= 1;
  454.       *Int_Par_Ref = Int_Loc - Int_Glob;
  455.       Enum_Loc = Ident_1;
  456.     } /* if */
  457.   while (Enum_Loc != Ident_1); /* true */
  458. } /* Proc_2 */
  459.  
  460.  
  461. void Proc_3 (Ptr_Ref_Par)
  462. /******************/
  463.     /* executed once */
  464.     /* Ptr_Ref_Par becomes Ptr_Glob */
  465.  
  466. Rec_Pointer *Ptr_Ref_Par;
  467.  
  468. {
  469.   if (Ptr_Glob != Null)
  470.     /* then, executed */
  471.     *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
  472.   Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
  473. } /* Proc_3 */
  474.  
  475.  
  476. void Proc_4 () /* without parameters */
  477. /*******/
  478.     /* executed once */
  479. {
  480.   Boolean Bool_Loc;
  481.  
  482.   Bool_Loc = Ch_1_Glob == 'A';
  483.   Bool_Glob = Bool_Loc | Bool_Glob;
  484.   Ch_2_Glob = 'B';
  485. } /* Proc_4 */
  486.  
  487.  
  488. void Proc_5 () /* without parameters */
  489. /*******/
  490.     /* executed once */
  491. {
  492.   Ch_1_Glob = 'A';
  493.   Bool_Glob = false;
  494. } /* Proc_5 */
  495.  
  496.  
  497.         /* Procedure for the assignment of structures,          */
  498.         /* if the C compiler doesn't support this feature       */
  499. #ifdef  NOSTRUCTASSIGN
  500. memcpy (d, s, l)
  501. register char   *d;
  502. register char   *s;
  503. register int    l;
  504. {
  505.         while (l--) *d++ = *s++;
  506. }
  507. #endif
  508.