f04fa9ff6036f918aa7688e6d0fe4581dd3ffaaf
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / Midpoint / Midpoint.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 Midpoint
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 70000;
22 #endif
23
24     static T[][] AllocArray<T>(int n1, int n2) {
25         T[][] a = new T[n1][];
26         for (int i = 0; i < n1; ++i) {
27             a[i] = new T[n2];
28         }
29         return a;
30     }
31
32     static int Inner(ref int x, ref int y, ref int z) {
33         int mid;
34
35         if (x < y) {
36             if (y < z) {
37                 mid = y;
38             }
39             else {
40                 if (x < z) {
41                     mid = z;
42                 }
43                 else {
44                     mid = x;
45                 }
46             }
47         }
48         else {
49             if (x < z) {
50                 mid = x;
51             }
52             else {
53                 if (y < z) {
54                     mid = z;
55                 }
56                 else {
57                     mid = y;
58                 }
59             }
60         }
61
62         return (mid);
63     }
64
65     [MethodImpl(MethodImplOptions.NoInlining)]
66     static bool Bench() {
67         int[][] a = AllocArray<int>(2001, 4);
68         int[] mid = new int[2001];
69         int j = 99999;
70
71         for (int i = 1; i <= 2000; i++) {
72             a[i][1] = j & 32767;
73             a[i][2] = (j + 11111) & 32767;
74             a[i][3] = (j + 22222) & 32767;
75             j = j + 33333;
76         }
77
78         for (int k = 1; k <= Iterations; k++) {
79             for (int l = 1; l <= 2000; l++) {
80                 mid[l] = Inner(ref a[l][1], ref a[l][2], ref a[l][3]);
81             }
82         }
83
84         return (mid[2000] == 17018);
85     }
86
87     [Benchmark]
88     public static void Test() {
89         foreach (var iteration in Benchmark.Iterations) {
90             using (iteration.StartMeasurement()) {
91                 Bench();
92             }
93         }
94     }
95
96     static bool TestBase() {
97         bool result = Bench();
98         return result;
99     }
100
101     public static int Main() {
102         bool result = TestBase();
103         return (result ? 100 : -1);
104     }
105 }
106 }