Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchF / SqMtx / SqMtx.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.BenchF
14 {
15 public static class SqMtx
16 {
17 #if DEBUG
18     public const int Iterations = 1;
19 #else
20     public const int Iterations = 4000;
21 #endif
22
23     private const int MatrixSize = 40;
24
25     private static T[][] AllocArray<T>(int n1, int n2)
26     {
27         T[][] a = new T[n1][];
28         for (int i = 0; i < n1; ++i)
29         {
30             a[i] = new T[n2];
31         }
32         return a;
33     }
34
35     [MethodImpl(MethodImplOptions.NoInlining)]
36     private static bool Bench()
37     {
38         double[][] a = AllocArray<double>(41, 41);
39         double[][] c = AllocArray<double>(41, 41);
40
41         int i, j;
42
43         for (i = 1; i <= MatrixSize; i++)
44         {
45             for (j = 1; j <= MatrixSize; j++)
46             {
47                 a[i][j] = i + j;
48             }
49         }
50
51         for (i = 1; i <= Iterations; i++)
52         {
53             Inner(a, c, MatrixSize);
54         }
55
56         if (c[1][1] == 23820.0)
57         {
58             return true;
59         }
60         else
61         {
62             return false;
63         }
64     }
65
66     private static void Inner(double[][] a, double[][] c, int n)
67     {
68         for (int i = 1; i <= n; i++)
69         {
70             for (int j = 1; j <= n; j++)
71             {
72                 c[i][j] = 0.0;
73                 for (int k = 1; k <= n; k++)
74                 {
75                     c[i][j] = c[i][j] + a[i][k] * a[k][j];
76                 }
77             }
78         }
79     }
80
81     [Benchmark]
82     public static void Test()
83     {
84         foreach (var iteration in Benchmark.Iterations)
85         {
86             using (iteration.StartMeasurement())
87             {
88                 Bench();
89             }
90         }
91     }
92
93     private static bool TestBase()
94     {
95         bool result = Bench();
96         return result;
97     }
98
99     public static int Main()
100     {
101         bool result = TestBase();
102         return (result ? 100 : -1);
103     }
104 }
105 }