summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchF/NewtR/NewtR.cs
blob: cf945bc11092aada7cbad0667155a7953ade9d09 (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
// 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.
//
// Newton's method adapted from Conte and De Boor

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

[assembly: OptimizeForBenchmarks]
[assembly: MeasureInstructionsRetired]

public static class NewtR
{
#if DEBUG
    public const int Iterations = 1;
#else
    public const int Iterations = 80000000;
#endif

    public static volatile object VolatileObject;

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

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static bool Bench()
    {
        int idbg, iflag;
        double x0, fx0;

        iflag = 0;
        idbg = 0;
        fx0 = 0.0;
        x0 = 1.0;

        for (int i = 1; i <= Iterations; i++)
        {
            Inner(ref x0, 0.0000001, 0.0000001, 10, out iflag);
            if (iflag > 1)
            {
                goto L888;
            }

            fx0 = FF(x0);
            if (idbg != 0)
            {
                System.Console.WriteLine(" THE ROOT IS {0:e} F(ROOT) := {1:E}\n", x0, fx0);
            }

        L888:
            {
            }
        }

        // Escape iflag, x0, and fx0 so that they appear live
        Escape(iflag);
        Escape(x0);
        Escape(fx0);

        return true;
    }

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

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

    private static void Inner(ref double x0, double xtol, double ftol, int ntol, out int iflag)
    {
        double fx0, deriv, deltax;

        iflag = 0;
        for (int n = 1; n <= ntol; n++)
        {
            fx0 = FF(x0);
            if (System.Math.Abs(fx0) < ftol)
            {
                goto L999;
            }
            deriv = FFDer(x0);

            if (deriv == 0.0)
            {
                goto L999;
            }
            deltax = fx0 / deriv;
            x0 = x0 - deltax;
            if (System.Math.Abs(deltax) < xtol)
            {
                goto L999;
            }
        }
    L999:
        iflag = 2;
    }

    [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);
    }
}