7a6bed7dd74b212f068a4722b38f4df4fdf11858
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / Pi / Pi.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 Pi
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 100;
22 #endif
23
24     static int[] ComputePi(int[] a) {
25
26         int d = 4;
27         int r = 10000;
28         int n = 251;
29         int m = (int)(3.322 * n * d);
30         int[] digits = new int[n];
31         int i, k, q;
32
33         for (i = 0; i <= m; i++) {
34             a[i] = 2;
35         }
36
37         a[m] = 4;
38
39         for (i = 1; i <= n; i++) {
40             q = 0;
41             for (k = m; k > 0L; k--) {
42                 a[k] = a[k] * r + q;
43                 q = a[k] / (2 * k + 1);
44                 a[k] -= (2 * k + 1) * q;
45                 q *= k;
46             }
47             a[0] = a[0] * r + q;
48             q = a[0] / r;
49             a[0] -= q * r;
50             digits[i-1] = q;
51         }
52
53         return digits;
54     }
55
56     [MethodImpl(MethodImplOptions.NoInlining)]
57     static bool Bench(int[] a) {
58         int[] digits = ComputePi(a);
59         return (digits[0] == 3 && digits[1] == 1415 && digits[2] == 9265 && digits[250] == 1989);
60     }
61
62     [Benchmark]
63     public static void Test() {
64         int[] a = new int[3340];
65         foreach (var iteration in Benchmark.Iterations) {
66             using (iteration.StartMeasurement()) {
67                 for (int i = 0; i < Iterations; i++) {
68                     Bench(a);
69                 }
70             }
71         }
72     }
73
74     static bool TestBase() {
75         bool result = true;
76         int[] a = new int[3340];
77         for (int i = 0; i < Iterations; i++) {
78             result &= Bench(a);
79         }
80         return result;
81     }
82
83     public static int Main() {
84         bool result = TestBase();
85         return (result ? 100 : -1);
86     }
87 }
88 }