1e7c6879ad9e1388db1064af538fe1d0d0d51d16
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Romber / Romber.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 // Integration by romberg method 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 Romber
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 640000;
22 #endif
23
24     private static T[][] AllocArray<T>(int n1, int n2)
25     {
26         T[][] a = new T[n1][];
27         for (int i = 0; i < n1; ++i)
28         {
29             a[i] = new T[n2];
30         }
31         return a;
32     }
33
34     [MethodImpl(MethodImplOptions.NoInlining)]
35     private static bool Bench()
36     {
37         double[][] r = AllocArray<double>(11, 11);
38         double[][] t = AllocArray<double>(11, 11);
39
40         int idbg, m, n, i, kmax, fourj, j, kmaxm2, l, k, mm1;
41         double sum, ratio, t1, h, a, b;
42
43         for (l = 1; l <= Iterations; l++)
44         {
45             idbg = 0;
46             m = 2;
47             kmax = 6;
48             a = 0;
49             b = 1;
50             h = (b - a) / (m);
51             sum = (F(a) + F(b)) / 2;
52
53             mm1 = m - 1;
54             if (mm1 < 0)
55             {
56                 goto L40;
57             }
58             if (mm1 == 0)
59             {
60                 goto L10;
61             }
62             for (i = 1; i <= mm1; i++)
63             {
64                 t1 = a + i * h;
65                 sum = sum + F(t1);
66             }
67
68         L10:
69             t[1][1] = sum * h;
70             if (idbg != 0)
71             {
72                 System.Console.WriteLine(" romberg t-table \n");
73                 System.Console.WriteLine("{0}\n", t[1][1]);
74             }
75
76             for (k = 2; k <= kmax; k++)
77             {
78                 h = h / 2;
79                 n = m * 2;
80                 sum = 0;
81                 for (i = 1; i <= n / 2; i++)
82                 {
83                     r[k][1] = r[k - 1][1] * System.Math.Sqrt(b * mm1);
84                     t1 = a + i * h;
85                     sum = sum + F(t1);
86                 }
87
88                 t[k][1] = t[k - 1][1] / 2 + sum * h;
89                 fourj = 1;
90                 for (j = 2; j <= k; j++)
91                 {
92                     fourj = fourj * 4;
93                     t[k - 1][j - 1] = t[k][j - 1] - t[k - 1][j - 1];
94                     t[k][j] = t[k][j - 1] + t[k - 1][j - 1] / (fourj - 1);
95                 }
96
97                 if (idbg != 0)
98                 {
99                     j = 1;
100                     System.Console.WriteLine("{0} {1} {2}d\n", t[k][j], j, k);
101                 }
102             }
103
104             kmaxm2 = kmax - 2;
105             if (kmaxm2 <= 0)
106             {
107                 goto L40;
108             }
109
110             if (idbg != 0)
111             {
112                 System.Console.WriteLine(" table of ratios \n");
113             }
114
115             for (k = 1; k <= kmaxm2; k++)
116             {
117                 for (j = 1; j <= k; j++)
118                 {
119                     ratio = 0;
120                     if (System.Math.Abs(t[k + 1][j]) > 0)
121                     {
122                         ratio = t[k][j] / t[k + 1][j];
123                     }
124                     t[k][j] = ratio;
125                 }
126             }
127
128             if (idbg != 0)
129             {
130                 j = 1;
131                 System.Console.WriteLine("{0} {1} {2}\n", t[k][j], j, k);
132             }
133
134         L40:
135             {
136             }
137         }
138
139         return true;
140     }
141
142     private static double F(double x)
143     {
144         return (System.Math.Exp((-(x)) * (x)));
145     }
146
147     [Benchmark]
148     public static void Test()
149     {
150         foreach (var iteration in Benchmark.Iterations)
151         {
152             using (iteration.StartMeasurement())
153             {
154                 Bench();
155             }
156         }
157     }
158
159     private static bool TestBase()
160     {
161         bool result = Bench();
162         return result;
163     }
164
165     public static int Main()
166     {
167         bool result = TestBase();
168         return (result ? 100 : -1);
169     }
170 }
171 }