summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchF/Regula/Regula.cs
blob: 0011288e552e0613188072eea85d0f89dfc6f387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// The modified regula-falsi routine adapted from Conte and De Boor

using Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]
[assembly: MeasureInstructionsRetired]

namespace Benchstone.BenchF
{
public static class Regula
{
#if DEBUG
    public const int Iterations = 1;
#else
    public const int Iterations = 4000000;
#endif

    public static volatile object VolatileObject;

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Escape(object obj)
    {
        VolatileObject = obj;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static bool Bench()
    {
        double error, fxi;
        double a, b, xi;
        int idbg, iflag;

        iflag = 0;
        idbg = 0;
        xi = 0;
        error = 0.0;
        fxi = 0.0;

        for (int i = 1; i <= Iterations; i++)
        {
            a = 1.0;
            b = 2.0;
            Inner(ref a, ref b, 0.0000001, 0.0000000001, 30, out iflag);
            if (iflag > 2)
            {
                goto L999;
            }

            xi = (a + b) / 2.0;
            error = System.Math.Abs(b - a) / 2.0;
            fxi = FG(xi);

            if (idbg != 0)
            {
                System.Console.WriteLine(" the root is  {0:E}", xi);
                System.Console.WriteLine(" plus/minus {0}\n", error);
                System.Console.WriteLine(" fg(root):= {0:E}\n", fxi);
            }

        L999:
            {
            }
        }

        // Escape iflag, xi, error, and fxi so that they appear live
        Escape(iflag);
        Escape(xi);
        Escape(error);
        Escape(fxi);

        return true;
    }

    private static double FG(double x)
    {
        return (-1.0 - (x * (1.0 - (x * x))));
    }

    private static void Inner(ref double a, ref double b, double xtol, double ftol, int ntol, out int iflag)
    {
        double signfa, prevfw, fa, fb, fw, w;

        iflag = 0;
        fa = FG(a);
        if (fa < 0.0)
        {
            signfa = -1.0;
        }
        else
        {
            signfa = 1.0;
        }

        fb = FG(b);
        if (signfa * fb <= 0.0)
        {
            goto L5;
        }

        iflag = 3;
        goto L99;

    L5:
        w = a;
        fw = fa;
        for (int i = 1; i <= ntol; i++)
        {
            if (System.Math.Abs(b - a) / 2.0 <= xtol)
            {
                goto L99;
            }
            if (System.Math.Abs(fw) > ftol)
            {
                goto L9;
            }

            a = w;
            b = w;
            iflag = 1;
            goto L99;

        L9:
            w = (fa * b - fb * a) / (fa - fb);
            if (fw < 0.0)
            {
                prevfw = -1.0;
            }
            else
            {
                prevfw = 1.0;
            }
            fw = FG(w);

            if (signfa * fw < 0.0)
            {
                goto L10;
            }
            a = w;
            fa = fw;
            if (fw * prevfw > 0.0)
            {
                fb = fb / 2.0;
            }
            goto L20;

        L10:
            b = w;
            fb = fw;
            if (fw * prevfw > 0.0)
            {
                fa = fa / 2.0;
            }

        L20:
            {
            }
        }

        iflag = 2;
    L99:
        {
        }
    }

    [Benchmark]
    public static void Test()
    {
        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                Bench();
            }
        }
    }

    private static bool TestBase()
    {
        bool result = Bench();
        return result;
    }

    public static int Main()
    {
        bool result = TestBase();
        return (result ? 100 : -1);
    }
}
}