Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / Ackermann / Ackermann.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 Ackermann
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 Acker(int m, int n) {
25         if (m == 0) {
26             return n + 1;
27         }
28         else if (n == 0) {
29             return Acker(m - 1, 1);
30         }
31         else {
32             return Acker(m - 1, Acker(m, n - 1));
33         }
34     }
35
36     [MethodImpl(MethodImplOptions.NoInlining)]
37     static bool Bench() {
38         int a00 = Acker(0, 0);
39         int a11 = Acker(1, 1);
40         int a22 = Acker(2, 2);
41         int a33 = Acker(3, 3);
42         return (a00 == 1) && (a11 == 3) && (a22 == 7) & (a33 == 61);
43     }
44
45     [Benchmark]
46     public static void Test() {
47         foreach (var iteration in Benchmark.Iterations) {
48             using (iteration.StartMeasurement()) {
49                 for (int i = 0; i < Iterations; i++) {
50                     Bench();
51                 }
52             }
53         }
54     }
55
56     static bool TestBase() {
57         bool result = true;
58         for (int i = 0; i < Iterations; i++) {
59             result &= Bench();
60         }
61         return result;
62     }
63
64     public static int Main() {
65         bool result = TestBase();
66         return (result ? 100 : -1);
67     }
68 }
69 }