bfb6546a388d2bf403ead104ac6667eb8258b712
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / NewtR / NewtR.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 // Newton's method 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 NewtR
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 80000000;
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, fx0;
37
38         iflag = 0;
39         idbg = 0;
40         fx0 = 0.0;
41         x0 = 1.0;
42
43         for (int i = 1; i <= Iterations; i++)
44         {
45             Inner(ref x0, 0.0000001, 0.0000001, 10, out iflag);
46             if (iflag > 1)
47             {
48                 goto L888;
49             }
50
51             fx0 = FF(x0);
52             if (idbg != 0)
53             {
54                 System.Console.WriteLine(" THE ROOT IS {0:e} F(ROOT) := {1:E}\n", x0, fx0);
55             }
56
57         L888:
58             {
59             }
60         }
61
62         // Escape iflag, x0, and fx0 so that they appear live
63         Escape(iflag);
64         Escape(x0);
65         Escape(fx0);
66
67         return true;
68     }
69
70     private static double FF(double x)
71     {
72         return (-1.0 - ((x) * (1.0 - ((x) * (x)))));
73     }
74
75     private static double FFDer(double x)
76     {
77         return (3.0 * (x) * (x) - 1.0);
78     }
79
80     private static void Inner(ref double x0, double xtol, double ftol, int ntol, out int iflag)
81     {
82         double fx0, deriv, deltax;
83
84         iflag = 0;
85         for (int n = 1; n <= ntol; n++)
86         {
87             fx0 = FF(x0);
88             if (System.Math.Abs(fx0) < ftol)
89             {
90                 goto L999;
91             }
92             deriv = FFDer(x0);
93
94             if (deriv == 0.0)
95             {
96                 goto L999;
97             }
98             deltax = fx0 / deriv;
99             x0 = x0 - deltax;
100             if (System.Math.Abs(deltax) < xtol)
101             {
102                 goto L999;
103             }
104         }
105     L999:
106         iflag = 2;
107     }
108
109     [Benchmark]
110     public static void Test()
111     {
112         foreach (var iteration in Benchmark.Iterations)
113         {
114             using (iteration.StartMeasurement())
115             {
116                 Bench();
117             }
118         }
119     }
120
121     private static bool TestBase()
122     {
123         bool result = Bench();
124         return result;
125     }
126
127     public static int Main()
128     {
129         bool result = TestBase();
130         return (result ? 100 : -1);
131     }
132 }
133 }