Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Simpsn / Simpsn.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 // Integration by Simpson's rule adapted from Conte and de Boor
6
7 using Microsoft.Xunit.Performance;
8 using System;
9 using System.Runtime.CompilerServices;
10 using Xunit;
11
12 [assembly: OptimizeForBenchmarks]
13
14 namespace Benchstone.BenchF
15 {
16 public static class Simpsn
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 90000;
22 #endif
23
24     [MethodImpl(MethodImplOptions.NoInlining)]
25     private static bool Bench()
26     {
27         double a, b, x, s, c, h, hov2, half, t1;
28         int idbg, n, nm1;
29
30         s = 0;
31         idbg = 0;
32         if (idbg != 0)
33         {
34             System.Console.WriteLine("simpsons rule\n");
35         }
36
37         for (int j = 1; j <= Iterations; j++)
38         {
39             a = 0;
40             b = 1;
41             c = 4;
42             n = 100;
43             h = (b - a) / n;
44             hov2 = h / System.Math.Sqrt(c);
45             s = 0;
46             t1 = a + hov2;
47             half = F(t1);
48             nm1 = n - 1;
49             for (int i = 1; i <= nm1; i++)
50             {
51                 x = a + i * h;
52                 s = s + F(x);
53                 t1 = x + hov2;
54                 half = half + F(t1);
55                 s = (h / 6) * (F(a) + 4 * half + 2 * s + F(b));
56                 if (idbg != 0)
57                 {
58                     System.Console.WriteLine(" integral from a = {0} to b = {1} for n = {2} is {3}\n", a, b, n, s);
59                 }
60             }
61         }
62
63         return true;
64     }
65
66     private static double F(double x)
67     {
68         return (System.Math.Exp((-(x)) * 2));
69     }
70
71     [Benchmark]
72     public static void Test()
73     {
74         foreach (var iteration in Benchmark.Iterations)
75         {
76             using (iteration.StartMeasurement())
77             {
78                 Bench();
79             }
80         }
81     }
82
83     private static bool TestBase()
84     {
85         bool result = Bench();
86         return result;
87     }
88
89     public static int Main()
90     {
91         bool result = TestBase();
92         return (result ? 100 : -1);
93     }
94 }
95 }