summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchF/Romber/Romber.cs
blob: 1e7c6879ad9e1388db1064af538fe1d0d0d51d16 (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
// 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.
//
// Integration by romberg method adapted from Conte and de Boor

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

[assembly: OptimizeForBenchmarks]

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

    private static T[][] AllocArray<T>(int n1, int n2)
    {
        T[][] a = new T[n1][];
        for (int i = 0; i < n1; ++i)
        {
            a[i] = new T[n2];
        }
        return a;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static bool Bench()
    {
        double[][] r = AllocArray<double>(11, 11);
        double[][] t = AllocArray<double>(11, 11);

        int idbg, m, n, i, kmax, fourj, j, kmaxm2, l, k, mm1;
        double sum, ratio, t1, h, a, b;

        for (l = 1; l <= Iterations; l++)
        {
            idbg = 0;
            m = 2;
            kmax = 6;
            a = 0;
            b = 1;
            h = (b - a) / (m);
            sum = (F(a) + F(b)) / 2;

            mm1 = m - 1;
            if (mm1 < 0)
            {
                goto L40;
            }
            if (mm1 == 0)
            {
                goto L10;
            }
            for (i = 1; i <= mm1; i++)
            {
                t1 = a + i * h;
                sum = sum + F(t1);
            }

        L10:
            t[1][1] = sum * h;
            if (idbg != 0)
            {
                System.Console.WriteLine(" romberg t-table \n");
                System.Console.WriteLine("{0}\n", t[1][1]);
            }

            for (k = 2; k <= kmax; k++)
            {
                h = h / 2;
                n = m * 2;
                sum = 0;
                for (i = 1; i <= n / 2; i++)
                {
                    r[k][1] = r[k - 1][1] * System.Math.Sqrt(b * mm1);
                    t1 = a + i * h;
                    sum = sum + F(t1);
                }

                t[k][1] = t[k - 1][1] / 2 + sum * h;
                fourj = 1;
                for (j = 2; j <= k; j++)
                {
                    fourj = fourj * 4;
                    t[k - 1][j - 1] = t[k][j - 1] - t[k - 1][j - 1];
                    t[k][j] = t[k][j - 1] + t[k - 1][j - 1] / (fourj - 1);
                }

                if (idbg != 0)
                {
                    j = 1;
                    System.Console.WriteLine("{0} {1} {2}d\n", t[k][j], j, k);
                }
            }

            kmaxm2 = kmax - 2;
            if (kmaxm2 <= 0)
            {
                goto L40;
            }

            if (idbg != 0)
            {
                System.Console.WriteLine(" table of ratios \n");
            }

            for (k = 1; k <= kmaxm2; k++)
            {
                for (j = 1; j <= k; j++)
                {
                    ratio = 0;
                    if (System.Math.Abs(t[k + 1][j]) > 0)
                    {
                        ratio = t[k][j] / t[k + 1][j];
                    }
                    t[k][j] = ratio;
                }
            }

            if (idbg != 0)
            {
                j = 1;
                System.Console.WriteLine("{0} {1} {2}\n", t[k][j], j, k);
            }

        L40:
            {
            }
        }

        return true;
    }

    private static double F(double x)
    {
        return (System.Math.Exp((-(x)) * (x)));
    }

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