Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / NDhrystone / NDhrystone.cs
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.
4 //
5 // Adapted from
6 //
7 // Dhrystone: a synthetic systems programming benchmark
8 // Reinhold P. Weicker
9 // Communications of the ACM, Volume 27 Issue 10, Oct 1984, Pages 1013-1030
10
11 using Microsoft.Xunit.Performance;
12 using System;
13 using System.Runtime.CompilerServices;
14 using Xunit;
15
16 [assembly: OptimizeForBenchmarks]
17
18 namespace Benchstone.BenchI
19 {
20 public static class NDhrystone
21 {
22
23 #if DEBUG
24     public const int Iterations = 1;
25 #else
26     public const int Iterations = 7000000;
27 #endif
28
29     static T[][] AllocArray<T>(int n1, int n2) {
30         T[][] a = new T[n1][];
31         for (int i = 0; i < n1; ++i) {
32             a[i] = new T[n2];
33         }
34         return a;
35     }
36
37     enum Enumeration
38     {
39         Ident1 = 1, Ident2, Ident3, Ident4, Ident5
40     }
41
42     sealed class Record
43     {
44         public Record PtrComp;
45         public Enumeration Discr;
46         public Enumeration EnumComp;
47         public int IntComp;
48         public char[] StringComp;
49     }
50
51     static int s_intGlob;
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;
61
62     static void Proc0() {
63         int intLoc1;
64         int intLoc2;
65         int intLoc3 = 0;
66         Enumeration enumLoc;
67
68         int i;   /* modification */
69
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 */
77
78         for (i = 0; i < Iterations; ++i) {
79             Proc5();
80             Proc4();
81             intLoc1 = 2;
82             intLoc2 = 3;
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);
89                 ++intLoc1;
90             }
91             Proc8(m_array1Glob, m_array2Glob, intLoc1, intLoc3);
92             Proc1(ref m_ptrGlb);
93             for (char charIndex = 'A'; charIndex <= s_char2Glob; ++charIndex) {
94                 if (enumLoc == Func1(charIndex, 'C')) {
95                     Proc6(Enumeration.Ident1, ref enumLoc);
96                 }
97             }
98             intLoc3 = intLoc2 * intLoc1;
99             intLoc2 = intLoc3 / intLoc1;
100             intLoc2 = 7 * (intLoc3 - intLoc2) - intLoc1;
101             Proc2(ref intLoc1);
102         }
103     }
104
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);
116         }
117         else {
118             ptrParIn = ptrParIn.PtrComp;
119         }
120     }
121
122     static void Proc2(ref int intParIO) {
123         int intLoc;
124         Enumeration enumLoc = Enumeration.Ident2;
125         intLoc = intParIO + 10;
126
127         for (;;) {
128             if (s_char1Glob == 'A') {
129                 --intLoc;
130                 intParIO = intLoc - s_intGlob;
131                 enumLoc = Enumeration.Ident1;
132             }
133             if (enumLoc == Enumeration.Ident1) {
134                 break;
135             }
136         }
137     }
138
139     static void Proc3(ref Record ptrParOut) {
140         if (m_ptrGlb != null) {
141             ptrParOut = m_ptrGlb.PtrComp;
142         }
143         else {
144             s_intGlob = 100;
145         }
146
147         Proc7(10, s_intGlob, ref m_ptrGlb.IntComp);
148     }
149
150     static void Proc4() {
151         bool boolLoc;
152         boolLoc = s_char1Glob == 'A';
153         boolLoc |= s_boolGlob;
154         s_char2Glob = 'B';
155     }
156
157     static void Proc5() {
158         s_char1Glob = 'A';
159         s_boolGlob = false;
160     }
161
162     static void Proc6(Enumeration enumParIn, ref Enumeration enumParOut) {
163         enumParOut = enumParIn;
164         if (!Func3(enumParIn)) {
165             enumParOut = Enumeration.Ident4;
166         }
167
168         switch (enumParIn) {
169             case Enumeration.Ident1:
170                 enumParOut = Enumeration.Ident1;
171                 break;
172             case Enumeration.Ident2:
173                 if (s_intGlob > 100) {
174                     enumParOut = Enumeration.Ident1;
175                 }
176                 else {
177                     enumParOut = Enumeration.Ident4;
178                 }
179                 break;
180             case Enumeration.Ident3:
181                 enumParOut = Enumeration.Ident2;
182                 break;
183             case Enumeration.Ident4:
184                 break;
185             case Enumeration.Ident5:
186                 enumParOut = Enumeration.Ident3;
187                 break;
188         }
189     }
190
191     static void Proc7(int intParI1, int intParI2, ref int intParOut) {
192         int intLoc;
193         intLoc = intParI1 + 2;
194         intParOut = intParI2 + intLoc;
195     }
196
197     static void Proc8(int[] array1Par, int[][] array2Par, int intParI1, int intParI2) {
198         int intLoc;
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;
205         }
206         ++array2Par[intLoc][intLoc - 1];
207         array2Par[intLoc + 20][intLoc] = array1Par[intLoc];
208         s_intGlob = 5;
209     }
210
211     static Enumeration Func1(char charPar1, char charPar2) {
212         char charLoc1;
213         char charLoc2;
214         charLoc1 = charPar1;
215         charLoc2 = charLoc1;
216         if (charLoc2 != charPar2) {
217             return (Enumeration.Ident1);
218         }
219         else {
220             return (Enumeration.Ident2);
221         }
222     }
223
224     static bool Func2(char[] strParI1, char[] strParI2) {
225         int intLoc;
226         char charLoc = '\0';
227         intLoc = 1;
228         while (intLoc <= 1) {
229             if (Func1(strParI1[intLoc], strParI2[intLoc + 1]) == Enumeration.Ident1) {
230                 charLoc = 'A';
231                 ++intLoc;
232             }
233         }
234         if (charLoc >= 'W' && charLoc <= 'Z') {
235             intLoc = 7;
236         }
237         if (charLoc == 'X') {
238             return true;
239         }
240         else {
241             for (int i = 0; i < 30; i++) {
242                 if (strParI1[i] > strParI2[i]) {
243                     intLoc += 7;
244                     return true;
245                 }
246             }
247
248             return false;
249         }
250     }
251
252     static bool Func3(Enumeration enumParIn) {
253         Enumeration enumLoc;
254         enumLoc = enumParIn;
255         if (enumLoc == Enumeration.Ident3) {
256             return true;
257         }
258
259         return false;
260     }
261
262     [MethodImpl(MethodImplOptions.NoInlining)]
263     static bool Bench() {
264         m_array2Glob = AllocArray<int>(51, 51);
265         Proc0();
266         return true;
267     }
268
269     [Benchmark]
270     public static void Test() {
271         foreach (var iteration in Benchmark.Iterations) {
272             using (iteration.StartMeasurement()) {
273                 Bench();
274             }
275         }
276     }
277
278     static bool TestBase() {
279         bool result = Bench();
280         return result;
281     }
282
283     public static int Main() {
284         bool result = TestBase();
285         return (result ? 100 : -1);
286     }
287 }
288 }