Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / DMath / DMath.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.BenchF
14 {
15 public static class DMath
16 {
17 #if DEBUG
18     public const int Iterations = 1;
19 #else
20     public const int Iterations = 100000;
21 #endif
22
23     private const double Deg2Rad = 57.29577951;
24     private static volatile object s_volatileObject;
25
26     private static void Escape(object obj)
27     {
28         s_volatileObject = obj;
29     }
30
31     private static double Fact(double n)
32     {
33         double res;
34         res = 1.0;
35         while (n > 0.0)
36         {
37             res *= n;
38             n -= 1.0;
39         }
40
41         return res;
42     }
43
44     private static double Power(double n, double p)
45     {
46         double res;
47         res = 1.0;
48         while (p > 0.0)
49         {
50             res *= n;
51             p -= 1.0;
52         }
53
54         return res;
55     }
56
57     [MethodImpl(MethodImplOptions.NoInlining)]
58     private static bool Bench(int loop)
59     {
60         double[] sines = new double[91];
61         double angle, radians, sine, worksine, temp, k;
62         double diff;
63
64         for (int iter = 1; iter <= loop; iter++)
65         {
66             for (angle = 0.0; angle <= 90.0; angle += 1.0)
67             {
68                 radians = angle / Deg2Rad;
69                 k = 0.0;
70                 worksine = 0.0;
71                 do
72                 {
73                     sine = worksine;
74                     temp = (2.0 * k) + 1.0;
75                     worksine += (Power(-1.0, k) / Fact(temp)) * Power(radians, temp);
76                     k += 1.0;
77                     diff = Math.Abs(sine - worksine);
78                 } while (diff > 1E-8);
79
80                 sines[(int)angle] = worksine;
81             }
82         }
83
84         // Escape sines array so that its elements appear live-out
85         Escape(sines);
86
87         return true;
88     }
89
90     [Benchmark]
91     public static void Test()
92     {
93         foreach (var iteration in Benchmark.Iterations)
94         {
95             using (iteration.StartMeasurement())
96             {
97                 Bench(Iterations);
98             }
99         }
100     }
101
102     private static bool TestBase()
103     {
104         bool result = Bench(Iterations);
105         return result;
106     }
107
108     public static int Main()
109     {
110         bool result = TestBase();
111         return (result ? 100 : -1);
112     }
113 }
114 }
115