cd44b1c83ebadc9cb157c11d68c6554a75e7fc96
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Bisect / Bisect.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 Bisect algorithm 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 Bisect
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 400000;
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 a, b, error, p1, xi;
37
38         iflag = 0;
39         error = 0.0;
40         xi = 0.0;
41         idbg = 0;
42         for (int i = 1; i <= Iterations; i++)
43         {
44             for (int j = 1; j <= 10; j++)
45             {
46                 a = 1.0;
47                 b = 2.0;
48                 p1 = 0.000001;
49                 Inner(ref a, ref b, ref p1, out iflag);
50                 if (iflag > 1)
51                 {
52                     goto L999;
53                 }
54
55                 xi = (a + b) / 2.0;
56                 if (a > b)
57                 {
58                     error = (a - b) / 2.0;
59                 }
60                 else
61                 {
62                     error = (b - a) / 2.0;
63                 }
64
65                 if (idbg != 0)
66                 {
67                     System.Console.WriteLine(" the root is {0:E} plus/minus {1:E}\n", xi, error);
68                 }
69             }
70         }
71     L999:
72         {
73         }
74
75         // Escape iflag, error, xi so that they appear live
76         Escape(iflag);
77         Escape(error);
78         Escape(xi);
79
80         return true;
81     }
82
83     private static double FF(double x)
84     {
85         return ((-1.0 - (x * (1.0 - (x * x)))));
86     }
87
88     private static void Inner(ref double a, ref double b, ref double xtol, out int iflag)
89     {
90         double fa, error;
91         double xm, fm;
92
93         iflag = 0;
94         fa = FF(a);
95         /*      check for sign change  */
96         if (((fa) * FF(b)) < 0.0)
97         {
98             goto L5;
99         }
100
101         iflag = 2;
102         goto L99;
103
104     L5:
105         {
106             error = System.Math.Abs(b - a);
107         }
108     L6:
109         error = error / 2.0;
110         /*      check for sufficiently small interval  */
111         if (error < xtol)
112         {
113             goto L99;
114         }
115         xm = (a + b) / 2.0;
116         /*      check for unreasonable error requirement */
117         if (xm + error == xm)
118         {
119             goto L20;
120         }
121
122         fm = FF(xm);
123         /*      change to new interval  */
124         if (fa * fm < 0.0)
125         {
126             goto L9;
127         }
128         a = xm;
129         fa = fm;
130         goto L6;
131     L9:
132         b = xm;
133         goto L6;
134     L20:
135         iflag = 1;
136     L99:
137         {
138         }
139     }
140
141     [Benchmark]
142     public static void Test()
143     {
144         foreach (var iteration in Benchmark.Iterations)
145         {
146             using (iteration.StartMeasurement())
147             {
148                 Bench();
149             }
150         }
151     }
152
153     private static bool TestBase()
154     {
155         bool result = Bench();
156         return result;
157     }
158
159     public static int Main()
160     {
161         bool result = TestBase();
162         return (result ? 100 : -1);
163     }
164 }
165 }