d4994418228b86f66133eda8c20832e67610d5cf
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / 8Queens / 8Queens.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 EightQueens
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 100000;
22 #endif
23
24     static int[] m_c = new int[15];
25     static int[] m_x = new int[9];
26
27     static void TryMe(int i, ref int q, int[] a, int[] b)
28     {
29         int j = 0;
30         q = 0;
31         while ((q == 0) && (j != 8)) {
32             j = j + 1;
33             q = 0;
34             if ((b[j] == 1) && (a[i + j] == 1) && (m_c[i - j + 7] == 1)) {
35                 m_x[i] = j;
36                 b[j] = 0;
37                 a[i + j] = 0;
38                 m_c[i - j + 7] = 0;
39                 if (i < 8) {
40                     TryMe(i + 1, ref q, a, b);
41                     if (q == 0) {
42                         b[j] = 1;
43                         a[i + j] = 1;
44                         m_c[i - j + 7] = 1;
45                     }
46                 }
47                 else {
48                     q = 1;
49                 }
50             }
51         }
52     }
53
54     [MethodImpl(MethodImplOptions.NoInlining)]
55     static bool Bench() {
56         int[] a = new int[9];
57         int[] b = new int[17];
58         int q = 0;
59         int i = 0;
60         while (i <= 16) {
61             if ((i >= 1) && (i <= 8)) {
62                 a[i] = 1;
63             }
64             if (i >= 2) {
65                 b[i] = 1;
66             }
67             if (i <= 14) {
68                 m_c[i] = 1;
69             }
70             i = i + 1;
71         }
72
73         TryMe(1, ref q, b, a);
74
75         return (q == 1);
76     }
77
78     [Benchmark]
79     public static void Test() {
80         foreach (var iteration in Benchmark.Iterations) {
81             using (iteration.StartMeasurement()) {
82                 for (int i = 0; i < Iterations; i++) {
83                     Bench();
84                 }
85             }
86         }
87     }
88
89     static bool TestBase() {
90         bool result = true;
91         for (int i = 0; i < Iterations; i++) {
92             result &= Bench();
93         }
94         return result;
95     }
96
97     public static int Main() {
98         bool result = TestBase();
99         return (result ? 100 : -1);
100     }
101 }
102 }