1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
7 // Dhrystone: a synthetic systems programming benchmark
9 // Communications of the ACM, Volume 27 Issue 10, Oct 1984, Pages 1013-1030
11 using Microsoft.Xunit.Performance;
13 using System.Runtime.CompilerServices;
16 [assembly: OptimizeForBenchmarks]
18 namespace Benchstone.BenchI
20 public static class NDhrystone
24 public const int Iterations = 1;
26 public const int Iterations = 7000000;
29 static T[][] AllocArray<T>(int n1, int n2) {
30 T[][] a = new T[n1][];
31 for (int i = 0; i < n1; ++i) {
39 Ident1 = 1, Ident2, Ident3, Ident4, Ident5
44 public Record PtrComp;
45 public Enumeration Discr;
46 public Enumeration EnumComp;
48 public char[] StringComp;
52 static bool s_boolGlob;
53 static char s_char1Glob;
54 static char s_char2Glob;
55 static int[] m_array1Glob = new int[51];
56 static int[][] m_array2Glob;
57 static Record m_ptrGlb = new Record();
58 static Record m_ptrGlbNext = new Record();
59 static char[] m_string1Loc;
60 static char[] m_string2Loc;
68 int i; /* modification */
70 m_ptrGlb.PtrComp = m_ptrGlbNext;
71 m_ptrGlb.Discr = Enumeration.Ident1;
72 m_ptrGlb.EnumComp = Enumeration.Ident3;
73 m_ptrGlb.IntComp = 40;
74 m_ptrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING".ToCharArray();
75 m_string1Loc = "DHRYSTONE PROGRAM, 1'ST STRING".ToCharArray();
76 m_array2Glob[8][7] = 10; /* Was missing in published program */
78 for (i = 0; i < Iterations; ++i) {
83 m_string2Loc = "DHRYSTONE PROGRAM, 2'ND STRING".ToCharArray();
84 enumLoc = Enumeration.Ident2;
85 s_boolGlob = !Func2(m_string1Loc, m_string2Loc);
86 while (intLoc1 < intLoc2) {
87 intLoc3 = 5 * intLoc1 - intLoc2;
88 Proc7(intLoc1, intLoc2, ref intLoc3);
91 Proc8(m_array1Glob, m_array2Glob, intLoc1, intLoc3);
93 for (char charIndex = 'A'; charIndex <= s_char2Glob; ++charIndex) {
94 if (enumLoc == Func1(charIndex, 'C')) {
95 Proc6(Enumeration.Ident1, ref enumLoc);
98 intLoc3 = intLoc2 * intLoc1;
99 intLoc2 = intLoc3 / intLoc1;
100 intLoc2 = 7 * (intLoc3 - intLoc2) - intLoc1;
105 static void Proc1(ref Record ptrParIn) {
106 ptrParIn.PtrComp = m_ptrGlb;
107 ptrParIn.IntComp = 5;
108 ptrParIn.PtrComp.IntComp = ptrParIn.IntComp;
109 ptrParIn.PtrComp.PtrComp = ptrParIn.PtrComp;
110 Proc3(ref ptrParIn.PtrComp.PtrComp);
111 if (ptrParIn.PtrComp.Discr == Enumeration.Ident1) {
112 ptrParIn.PtrComp.IntComp = 6;
113 Proc6(ptrParIn.EnumComp, ref ptrParIn.PtrComp.EnumComp);
114 ptrParIn.PtrComp.PtrComp = m_ptrGlb.PtrComp;
115 Proc7(ptrParIn.PtrComp.IntComp, 10, ref ptrParIn.PtrComp.IntComp);
118 ptrParIn = ptrParIn.PtrComp;
122 static void Proc2(ref int intParIO) {
124 Enumeration enumLoc = Enumeration.Ident2;
125 intLoc = intParIO + 10;
128 if (s_char1Glob == 'A') {
130 intParIO = intLoc - s_intGlob;
131 enumLoc = Enumeration.Ident1;
133 if (enumLoc == Enumeration.Ident1) {
139 static void Proc3(ref Record ptrParOut) {
140 if (m_ptrGlb != null) {
141 ptrParOut = m_ptrGlb.PtrComp;
147 Proc7(10, s_intGlob, ref m_ptrGlb.IntComp);
150 static void Proc4() {
152 boolLoc = s_char1Glob == 'A';
153 boolLoc |= s_boolGlob;
157 static void Proc5() {
162 static void Proc6(Enumeration enumParIn, ref Enumeration enumParOut) {
163 enumParOut = enumParIn;
164 if (!Func3(enumParIn)) {
165 enumParOut = Enumeration.Ident4;
169 case Enumeration.Ident1:
170 enumParOut = Enumeration.Ident1;
172 case Enumeration.Ident2:
173 if (s_intGlob > 100) {
174 enumParOut = Enumeration.Ident1;
177 enumParOut = Enumeration.Ident4;
180 case Enumeration.Ident3:
181 enumParOut = Enumeration.Ident2;
183 case Enumeration.Ident4:
185 case Enumeration.Ident5:
186 enumParOut = Enumeration.Ident3;
191 static void Proc7(int intParI1, int intParI2, ref int intParOut) {
193 intLoc = intParI1 + 2;
194 intParOut = intParI2 + intLoc;
197 static void Proc8(int[] array1Par, int[][] array2Par, int intParI1, int intParI2) {
199 intLoc = intParI1 + 5;
200 array1Par[intLoc] = intParI2;
201 array1Par[intLoc + 1] = array1Par[intLoc];
202 array1Par[intLoc + 30] = intLoc;
203 for (int intIndex = intLoc; intIndex <= (intLoc + 1); ++intIndex) {
204 array2Par[intLoc][intIndex] = intLoc;
206 ++array2Par[intLoc][intLoc - 1];
207 array2Par[intLoc + 20][intLoc] = array1Par[intLoc];
211 static Enumeration Func1(char charPar1, char charPar2) {
216 if (charLoc2 != charPar2) {
217 return (Enumeration.Ident1);
220 return (Enumeration.Ident2);
224 static bool Func2(char[] strParI1, char[] strParI2) {
228 while (intLoc <= 1) {
229 if (Func1(strParI1[intLoc], strParI2[intLoc + 1]) == Enumeration.Ident1) {
234 if (charLoc >= 'W' && charLoc <= 'Z') {
237 if (charLoc == 'X') {
241 for (int i = 0; i < 30; i++) {
242 if (strParI1[i] > strParI2[i]) {
252 static bool Func3(Enumeration enumParIn) {
255 if (enumLoc == Enumeration.Ident3) {
262 [MethodImpl(MethodImplOptions.NoInlining)]
263 static bool Bench() {
264 m_array2Glob = AllocArray<int>(51, 51);
270 public static void Test() {
271 foreach (var iteration in Benchmark.Iterations) {
272 using (iteration.StartMeasurement()) {
278 static bool TestBase() {
279 bool result = Bench();
283 public static int Main() {
284 bool result = TestBase();
285 return (result ? 100 : -1);