cc698f44dd7a6d5f3215d6c910f90f4f8df6ec63
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / NewtE / NewtE.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 // Simultaneous equations by Newton's method adapted from Conte and De Boor
6 // to solve F(X,Y)=0 and G(X,Y)=0
7
8 using Microsoft.Xunit.Performance;
9 using System;
10 using System.Runtime.CompilerServices;
11 using Xunit;
12
13 [assembly: OptimizeForBenchmarks]
14
15 namespace Benchstone.BenchF
16 {
17 public static class NewtE
18 {
19 #if DEBUG
20     public const int Iterations = 1;
21 #else
22     public const int Iterations = 1000000;
23 #endif
24
25     [MethodImpl(MethodImplOptions.NoInlining)]
26     private static bool Bench()
27     {
28         double idgb, a, b, x, y, deltaX, deltaY;
29         a = 0;
30         b = 0;
31         x = 0;
32         y = 0;
33         idgb = 0;
34
35         if (idgb != 0)
36         {
37             System.Console.WriteLine("{0}, {1}, F(x,y) , G(x,y) \n", x, y);
38         }
39
40         for (int j = 1; j <= Iterations; j++)
41         {
42             x = 1.0;
43             y = (-2.0);
44             a = F(x, y);
45             b = G(x, y);
46             if (idgb != 0)
47             {
48                 System.Console.WriteLine(" {0}, {1}, {2}, {3}\n", x, y, a, b);
49             }
50
51             for (int i = 1; i <= 20; i++)
52             {
53                 deltaX = (-F(x, y) * GY(x, y) + G(x, y) * FY(x, y)) / (FX(x, y) * GY(x, y) - FY(x, y) * GX(x, y));
54                 deltaY = (-G(x, y) * FX(x, y) + F(x, y) * GX(x, y)) / (FX(x, y) * GY(x, y) - FY(x, y) * GX(x, y));
55                 x = x + deltaX;
56                 y = y + deltaY;
57                 a = F(x, y);
58                 b = G(x, y);
59                 if (idgb != 0)
60                 {
61                     System.Console.WriteLine("{0}, {1}, {2}, {3}, {4}\n", i, x, y, a, b);
62                 }
63
64                 if ((System.Math.Abs(deltaX) < 0.000001) && (System.Math.Abs(deltaY) < 0.000001) &&
65                    (System.Math.Abs(a) < 0.000001) && (System.Math.Abs(b) < 0.000001))
66                 {
67                     goto L11;
68                 }
69             }
70             if (idgb != 0)
71             {
72                 System.Console.WriteLine("FAILED TO CONVERGE IN 20 ITERATIONS\n");
73             }
74
75         L11:
76             {
77             }
78         }
79
80         return true;
81     }
82
83     private static double F(double x, double y)
84     {
85         return ((x) + 3 * System.Math.Log(x) / System.Math.Log(10.0) - (y) * (y));
86     }
87
88     private static double G(double x, double y)
89     {
90         return (2 * (x) * (x) - (x) * (y) - 5 * (x) + 1);
91     }
92
93     private static double FX(double x, double y)
94     {
95         return (1 + 3 / (System.Math.Log(10.0) * (x)));
96     }
97
98     private static double FY(double x, double y)
99     {
100         return ((-2) * (y));
101     }
102
103     private static double GX(double x, double y)
104     {
105         return (4 * (x) - (y) - 5);
106     }
107
108     private static double GY(double x, double y)
109     {
110         return (-(x));
111     }
112
113     [Benchmark]
114     public static void Test()
115     {
116         foreach (var iteration in Benchmark.Iterations)
117         {
118             using (iteration.StartMeasurement())
119             {
120                 Bench();
121             }
122         }
123     }
124
125     private static bool TestBase()
126     {
127         bool result = Bench();
128         return result;
129     }
130
131     public static int Main()
132     {
133         bool result = TestBase();
134         return (result ? 100 : -1);
135     }
136 }
137 }