home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xco212p.zip / SAMPLES / DHRY / puredhry.mod < prev    next >
Text File  |  1996-02-08  |  7KB  |  231 lines

  1. MODULE puredhry;
  2.  
  3. IMPORT dry1, dry2, InOut;
  4. FROM Storage IMPORT ALLOCATE;
  5.  
  6. TYPE longint = INTEGER; (* CARDINAL for some compilers *)
  7.  
  8. (* InOut replacement *)
  9.  
  10. PROCEDURE wl; BEGIN InOut.WriteLn; END wl;
  11. PROCEDURE wc(c: CHAR); BEGIN InOut.Write(c); END wc;
  12.  
  13. PROCEDURE ws (s: ARRAY OF CHAR);
  14. VAR i: CARDINAL;
  15. BEGIN
  16.   FOR i := 0 TO HIGH(s) DO
  17.     IF s[i]=0C THEN RETURN; END;
  18.     wc (s[i]);
  19.   END;
  20. END ws;
  21.  
  22. PROCEDURE wb(b: BOOLEAN);
  23. BEGIN
  24.   IF b THEN InOut.WriteString ("TRUE");
  25.   ELSE      InOut.WriteString ("FALSE");
  26.   END;
  27. END wb;
  28.  
  29. PROCEDURE we(e: dry2.Enumeration);
  30. BEGIN
  31.   CASE e OF
  32.     |dry2.Ident1: ws ("Ident1");
  33.     |dry2.Ident2: ws ("Ident2");
  34.     |dry2.Ident3: ws ("Ident3");
  35.     |dry2.Ident4: ws ("Ident4");
  36.     |dry2.Ident5: ws ("Ident5");
  37.     |ELSE ws ("Error - unexpected value");
  38.   END;
  39. END we;
  40.  
  41. PROCEDURE wli (n: longint);
  42.   VAR i: INTEGER;
  43.     buf: ARRAY [0..31] OF CHAR;
  44. BEGIN
  45.   i := 0;
  46.   WHILE n > 0 DO
  47.     buf[i] := CHR(ORD('0') + VAL(CARDINAL,n MOD 10)); INC(i); n := n DIV 10;
  48.   END;
  49.   IF i = 0 THEN buf[0] := '0'; INC(i); END;
  50.   REPEAT DEC(i); wc (buf[i]); UNTIL i=0;
  51. END wli;
  52.  
  53. PROCEDURE wi (i: dry2.integer);
  54. BEGIN
  55.   wli (VAL(longint, i));
  56. END wi;
  57.  
  58. PROCEDURE rli (VAR n: longint);
  59. VAR
  60.   c: CHAR;
  61.   t: longint;
  62. BEGIN
  63.   n := 0;
  64.   LOOP
  65.     InOut.Read (c);
  66.     IF (c<'0') OR (c>'9') THEN RETURN END;
  67.     t := VAL (longint, ORD(c) - ORD('0'));
  68.     IF (MAX(longint)-t) DIV 10 < n THEN
  69.       wl; wl; ws ("Error: value is too big"); wl; wl; HALT;
  70.     END;
  71.     n := n*10 + t;
  72.   END;
  73. END rli;
  74.  
  75. PROCEDURE Proc0;
  76.  
  77.   (* main program, corresponds to procedures        *)
  78.   (* Main and Proc0 in the Ada version              *)
  79.  
  80. VAR
  81.  
  82.   Int1Loc,
  83.   Int2Loc,
  84.   Int3Loc: dry2.integer;
  85.   ChIndex: CHAR;
  86.   EnumLoc: dry2.Enumeration;
  87.   Str1Loc: dry2.Str30;
  88.   Str2Loc: dry2.Str30;
  89.   RunIndex,
  90.   NumberOfRuns,
  91.   n: longint;
  92.  
  93. BEGIN
  94.  
  95.   (* Initializations *)
  96.  
  97.   NEW(dry2.NextPtrGlob);
  98.   NEW(dry2.PtrGlob);
  99.  
  100.   dry2.PtrGlob^.PtrComp  := dry2.NextPtrGlob;
  101.   dry2.PtrGlob^.Discr    := dry2.Ident1;
  102.   dry2.PtrGlob^.EnumComp := dry2.Ident3;
  103.   dry2.PtrGlob^.IntComp  := 40;
  104.   dry2.PtrGlob^.StrComp  := "DHRYSTONE PROGRAM, SOME STRING";
  105.   Str1Loc                := "DHRYSTONE PROGRAM, 1'ST STRING";
  106.  
  107.   dry2.Arr2Glob [8][7] := 10;
  108.         (* Was missing in published program. Without this statement,    *)
  109.         (* Arr2Glob [8][7] would have an undefined value.               *)
  110.         (* Warning: With 16-Bit processors and NumberOfRuns > 32000,    *)
  111.         (* overflow may occur for this array element.                   *)
  112.  
  113.   wl;
  114.   ws ("Dhrystone Benchmark, Version 2.1 (Language: Modula-2)"); wl; wl;
  115.  
  116.   ws ("Please give the number of runs through the benchmark: ");
  117.   rli (n); wl; NumberOfRuns := n;
  118.  
  119.   ws ("Execution starts, "); wli (NumberOfRuns);
  120.   ws (" runs through Dhrystone"); wl;
  121.  
  122.   RunIndex := 0;
  123.  
  124.   (***************)
  125.   (* Start timer *)
  126.   (***************)
  127.  
  128.   WHILE RunIndex < NumberOfRuns DO INC (RunIndex);
  129.     dry1.Proc5;
  130.     dry1.Proc4;
  131.       (* Ch1Glob == 'A', Ch2Glob == 'B', BoolGlob == true *)
  132.     Int1Loc := 2;
  133.     Int2Loc := 3;
  134.     Str2Loc := "DHRYSTONE PROGRAM, 2'ND STRING";
  135.     EnumLoc := dry2.Ident2;
  136.     dry2.BoolGlob := NOT dry2.Func2 (Str1Loc, Str2Loc);
  137.       (* BoolGlob == 1 *)
  138.  
  139.     WHILE Int1Loc < Int2Loc DO  (* loop body executed once *)
  140.       Int3Loc := 5 * Int1Loc - Int2Loc;
  141.         (* Int3Loc == 7 *)
  142.       dry2.Proc7 (Int1Loc, Int2Loc, Int3Loc);
  143.         (* Int3Loc == 7 *)
  144.       INC (Int1Loc);
  145.     END;
  146.       (* Int1Loc == 3, Int2Loc == 3, Int3Loc == 7 *)
  147.     dry2.Proc8 (dry2.Arr1Glob, dry2.Arr2Glob, Int1Loc, Int3Loc);
  148.       (* IntGlob == 5 *)
  149.     dry1.Proc1 (dry2.PtrGlob);
  150.     FOR ChIndex := 'A' TO dry2.Ch2Glob DO
  151.         (* loop body executed twice *)
  152.       IF EnumLoc = dry2.Func1 (ChIndex, 'C') THEN
  153.           (* then, not executed *)
  154.         dry2.Proc6 (dry2.Ident1, EnumLoc);
  155.         Str2Loc := "DHRYSTONE PROGRAM, 3'RD STRING";
  156.         Int2Loc := Int1Loc;
  157.         dry2.IntGlob := Int1Loc;
  158.        END;
  159.     END;
  160.       (* Int1Loc == 3, Int2Loc == 3, Int3Loc == 7 *)
  161.     Int2Loc := Int2Loc * Int1Loc;
  162.     Int1Loc := Int2Loc DIV Int3Loc;
  163.     Int2Loc := 7 * (Int2Loc - Int3Loc) - Int1Loc;
  164.       (* Int1Loc == 1, Int2Loc == 13, Int3Loc == 7 *)
  165.     dry1.Proc2 (Int1Loc);
  166.       (* Int1Loc == 5 *)
  167.   END; (* loop "for RunIndex" *)
  168.  
  169.   (**************)
  170.   (* Stop timer *)
  171.   (**************)
  172.  
  173.   ws ("Execution ends"); wl; wl;
  174.   ws ("Final values of the variables used in the benchmark:"); wl; wl;
  175.   ws ("IntGlob:           "); wi(dry2.IntGlob); wl;
  176.   ws ("        should be: 5"); wl;
  177.   ws ("BoolGlob:          "); wb (dry2.BoolGlob); wl;
  178.   ws ("        should be: TRUE"); wl;
  179.   ws ("Ch1Glob:           "); wc (dry2.Ch1Glob); wl;
  180.   ws ("        should be: A"); wl;
  181.   ws ("Ch2Glob:           "); wc (dry2.Ch2Glob); wl;
  182.   ws ("        should be: B"); wl;
  183.   ws ("Arr1Glob[8]:       "); wi (dry2.Arr1Glob[8]); wl;
  184.   ws ("        should be: 7"); wl;
  185.   ws ("Arr2Glob[8][7]:    "); wi (dry2.Arr2Glob[8][7]); wl;
  186.   ws ("        should be: NumberOfRuns + 10"); wl;
  187.  
  188.   ws ("PtrGlob^.PtrComp must be equal to dry2.NextPtrGlob^.PtrComp -- ");
  189.   IF dry2.PtrGlob^.PtrComp=dry2.NextPtrGlob^.PtrComp THEN ws ("OK");
  190.   ELSE ws ("error!");
  191.   END;
  192.   wl;
  193.  
  194.   ws ("PtrGlob"); wl;
  195.   ws ("  Discr:           "); we (dry2.PtrGlob^.Discr); wl;
  196.   ws ("        should be: Ident1"); wl;
  197.   ws ("  EnumComp:        "); we (dry2.PtrGlob^.EnumComp); wl;
  198.   ws ("        should be: Ident3"); wl;
  199.   ws ("  IntComp:         "); wi (dry2.PtrGlob^.IntComp); wl;
  200.   ws ("        should be: 17"); wl;
  201.   ws ("  StrComp:         "); ws (dry2.PtrGlob^.StrComp); wl;
  202.   ws ("        should be: DHRYSTONE PROGRAM, SOME STRING"); wl;
  203.   ws ("NextPtrGlob"); wl;
  204.   ws ("  Discr:           "); we (dry2.NextPtrGlob^.Discr); wl;
  205.   ws ("        should be: Ident1"); wl;
  206.   ws ("  EnumComp:        "); we (dry2.NextPtrGlob^.EnumComp); wl;
  207.   ws ("        should be: Ident2"); wl;
  208.   ws ("  IntComp:         "); wi (dry2.NextPtrGlob^.IntComp); wl;
  209.   ws ("        should be: 18"); wl;
  210.   ws ("  StrComp:         "); ws (dry2.NextPtrGlob^.StrComp); wl;
  211.   ws ("        should be: DHRYSTONE PROGRAM, SOME STRING"); wl;
  212.   ws ("Int1Loc:           "); wi (Int1Loc); wl;
  213.   ws ("        should be: 5"); wl;
  214.   ws ("Int2Loc:           "); wi (Int2Loc); wl;
  215.   ws ("        should be: 13"); wl;
  216.   ws ("Int3Loc:           "); wi (Int3Loc); wl;
  217.   ws ("        should be: 7"); wl;
  218.   ws ("EnumLoc:           "); we (EnumLoc); wl;
  219.   ws ("        should be: Ident2"); wl;
  220.   ws ("Str1Loc:           "); ws (Str1Loc); wl;
  221.   ws ("        should be: DHRYSTONE PROGRAM, 1'ST STRING"); wl;
  222.   ws ("Str2Loc:           "); ws (Str2Loc); wl;
  223.   ws ("        should be: DHRYSTONE PROGRAM, 2'ND STRING"); wl;
  224.   wl;
  225. END Proc0;
  226.  
  227.  
  228. BEGIN
  229.   Proc0;
  230. END puredhry.
  231.