9dcc1feb3fabc48efe1065d62c788dbe83df9256
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Secant / Secant.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 // The secant algorithm adapted from Conte and DeBoor
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 Secant
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 3000000;
22 #endif
23
24     public static volatile object VolatileObject;
25
26     [MethodImpl(MethodImplOptions.NoInlining)]
27     private static void Escape(object obj)
28     {
29         VolatileObject = obj;
30     }
31
32     [MethodImpl(MethodImplOptions.NoInlining)]
33     private static bool Bench()
34     {
35         int idbg, iflag;
36         double x0, x1, fx1;
37
38         iflag = 0;
39         idbg = 0;
40         x1 = 0;
41         fx1 = 0.0;
42
43         for (int i = 1; i <= Iterations; i++)
44         {
45             x0 = 1.0;
46             x1 = 2.0;
47             Inner(ref x0, ref x1, 0.0000001, 0.0000001, 30, out iflag);
48             if (iflag > 1)
49             {
50                 goto L888;
51             }
52
53             fx1 = FF(x1);
54             if (idbg != 0)
55             {
56                 System.Console.WriteLine(" the root is {0:E}, F(ROOT):= {1:E}\n", x1, fx1);
57             }
58         L888:
59             {
60             }
61         }
62
63         // Escape iflag, x1, and fx1 so that they appear live
64         Escape(iflag);
65         Escape(x1);
66         Escape(fx1);
67
68         return true;
69     }
70
71     private static double FF(double x)
72     {
73         return (-1.0 - (x * (1.0 - (x * x))));
74     }
75
76     private static void Inner(ref double x0, ref double x1, double xtol, double ftol, int ntol, out int iflag)
77     {
78         double deltax, deltaf, f0, f1;
79
80         iflag = 0;
81         f0 = FF(x0);
82         deltax = x1 - x0;
83
84         for (int n = 1; n <= ntol; n++)
85         {
86             f1 = FF(x1);
87
88             if (System.Math.Abs(f1) <= ftol)
89             {
90                 goto L30;
91             }
92
93             deltaf = f0 - f1;
94             if (deltaf == 0.0)
95             {
96                 goto L999;
97             }
98
99             deltax = f1 / deltaf * deltax;
100             x0 = x1;
101             x1 = x1 + deltax;
102             if (System.Math.Abs(deltax) <= xtol)
103             {
104                 goto L88;
105             }
106
107             f0 = f1;
108         }
109
110     L999:
111         iflag = 2;
112         goto L88;
113     L30:
114         iflag = 1;
115     L88:
116         {
117         }
118     }
119
120     [Benchmark]
121     public static void Test()
122     {
123         foreach (var iteration in Benchmark.Iterations)
124         {
125             using (iteration.StartMeasurement())
126             {
127                 Bench();
128             }
129         }
130     }
131
132     private static bool TestBase()
133     {
134         bool result = Bench();
135         return result;
136     }
137
138     public static int Main()
139     {
140         bool result = TestBase();
141         return (result ? 100 : -1);
142     }
143 }
144 }