c82b1e1e8b1a227c9516c23e151297b5961fafcf
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / LogicArray / LogicArray.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
6 using Microsoft.Xunit.Performance;
7 using System;
8 using System.Runtime.CompilerServices;
9 using Xunit;
10
11 [assembly: OptimizeForBenchmarks]
12
13 namespace Benchstone.BenchI
14 {
15 public static class LogicArray
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 3000;
22 #endif
23
24     const int ArraySize = 50;
25
26     struct Workarea
27     {
28         public int X;
29         public int[][] A;
30     }
31
32     static T[][] AllocArray<T>(int n1, int n2) {
33         T[][] a = new T[n1][];
34         for (int i = 0; i < n1; ++i) {
35             a[i] = new T[n2];
36         }
37         return a;
38     }
39
40     static bool Inner(ref Workarea cmn) {
41         int i, j, k;
42         cmn.X = 0;
43         for (i = 1; i <= 50; i++) {
44             for (j = 1; j <= 50; j++) {
45                 cmn.A[i][j] = 1;
46             }
47         }
48         for (k = 1; k <= 50; k++) {
49             for (j = 1; j <= 50; j++) {
50                 i = 1;
51                 do {
52                     cmn.X = cmn.X | cmn.A[i][j] & cmn.A[i + 1][k];
53                     i = i + 2;
54                 } while (i <= 50);
55             }
56         }
57         if (cmn.X != 1) {
58             return false;
59         }
60         else {
61             return true;
62         }
63     }
64
65     [MethodImpl(MethodImplOptions.NoInlining)]
66     static bool Bench() {
67         Workarea cmn = new Workarea();
68         cmn.X = 0;
69         cmn.A = AllocArray<int>(51, 51);
70         for (int n = 1; n <= Iterations; n++) {
71             bool result = Inner(ref cmn);
72             if (!result) {
73                 return false;
74             }
75         }
76
77         return true;
78     }
79
80     [Benchmark]
81     public static void Test() {
82         foreach (var iteration in Benchmark.Iterations) {
83             using (iteration.StartMeasurement()) {
84                 Bench();
85             }
86         }
87     }
88
89     static bool TestBase() {
90         bool result = Bench();
91         return result;
92     }
93
94     public static int Main() {
95         bool result = TestBase();
96         return (result ? 100 : -1);
97     }
98 }
99 }