summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.cs
blob: d4994418228b86f66133eda8c20832e67610d5cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]

namespace Benchstone.BenchI
{
public static class EightQueens
{

#if DEBUG
    public const int Iterations = 1;
#else
    public const int Iterations = 100000;
#endif

    static int[] m_c = new int[15];
    static int[] m_x = new int[9];

    static void TryMe(int i, ref int q, int[] a, int[] b)
    {
        int j = 0;
        q = 0;
        while ((q == 0) && (j != 8)) {
            j = j + 1;
            q = 0;
            if ((b[j] == 1) && (a[i + j] == 1) && (m_c[i - j + 7] == 1)) {
                m_x[i] = j;
                b[j] = 0;
                a[i + j] = 0;
                m_c[i - j + 7] = 0;
                if (i < 8) {
                    TryMe(i + 1, ref q, a, b);
                    if (q == 0) {
                        b[j] = 1;
                        a[i + j] = 1;
                        m_c[i - j + 7] = 1;
                    }
                }
                else {
                    q = 1;
                }
            }
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Bench() {
        int[] a = new int[9];
        int[] b = new int[17];
        int q = 0;
        int i = 0;
        while (i <= 16) {
            if ((i >= 1) && (i <= 8)) {
                a[i] = 1;
            }
            if (i >= 2) {
                b[i] = 1;
            }
            if (i <= 14) {
                m_c[i] = 1;
            }
            i = i + 1;
        }

        TryMe(1, ref q, b, a);

        return (q == 1);
    }

    [Benchmark]
    public static void Test() {
        foreach (var iteration in Benchmark.Iterations) {
            using (iteration.StartMeasurement()) {
                for (int i = 0; i < Iterations; i++) {
                    Bench();
                }
            }
        }
    }

    static bool TestBase() {
        bool result = true;
        for (int i = 0; i < Iterations; i++) {
            result &= Bench();
        }
        return result;
    }

    public static int Main() {
        bool result = TestBase();
        return (result ? 100 : -1);
    }
}
}