d72e58789db521778d4cb355323580e0514f9bb5
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Whetsto / Whetsto.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 // C# translation of Whetstone Double Precision Benchmark
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 Whetsto
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 50000;
22 #endif
23
24     private static int s_j, s_k, s_l;
25     private static double s_t, s_t2;
26
27     public static volatile int Volatile_out;
28
29     private static void Escape(int n, int j, int k, double x1, double x2, double x3, double x4)
30     {
31         Volatile_out = n;
32         Volatile_out = j;
33         Volatile_out = k;
34         Volatile_out = (int)x1;
35         Volatile_out = (int)x2;
36         Volatile_out = (int)x3;
37         Volatile_out = (int)x4;
38     }
39
40     [MethodImpl(MethodImplOptions.NoInlining)]
41     private static bool Bench()
42     {
43         double[] e1 = new double[4];
44         double x1, x2, x3, x4, x, y, z, t1;
45         int i, n1, n2, n3, n4, n6, n7, n8, n9, n10, n11;
46
47         s_t = 0.499975;
48         t1 = 0.50025;
49         s_t2 = 2.0;
50         n1 = 0 * Iterations;
51         n2 = 12 * Iterations;
52         n3 = 14 * Iterations;
53         n4 = 345 * Iterations;
54         n6 = 210 * Iterations;
55         n7 = 32 * Iterations;
56         n8 = 899 * Iterations;
57         n9 = 616 * Iterations;
58         n10 = 0 * Iterations;
59         n11 = 93 * Iterations;
60         x1 = 1.0;
61         x2 = x3 = x4 = -1.0;
62
63         for (i = 1; i <= n1; i += 1)
64         {
65             x1 = (x1 + x2 + x3 - x4) * s_t;
66             x2 = (x1 + x2 - x3 - x4) * s_t;
67             x3 = (x1 - x2 + x3 + x4) * s_t;
68             x4 = (-x1 + x2 + x3 + x4) * s_t;
69         }
70         Escape(n1, n1, n1, x1, x2, x3, x4);
71
72         /* MODULE 2:  array elements */
73         e1[0] = 1.0;
74         e1[1] = e1[2] = e1[3] = -1.0;
75         for (i = 1; i <= n2; i += 1)
76         {
77             e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * s_t;
78             e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * s_t;
79             e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * s_t;
80             e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * s_t;
81         }
82         Escape(n2, n3, n2, e1[0], e1[1], e1[2], e1[3]);
83
84         /* MODULE 3:  array as parameter */
85         for (i = 1; i <= n3; i += 1)
86         {
87             PA(e1);
88         }
89         Escape(n3, n2, n2, e1[0], e1[1], e1[2], e1[3]);
90
91         /* MODULE 4:  conditional jumps */
92         s_j = 1;
93         for (i = 1; i <= n4; i += 1)
94         {
95             if (s_j == 1)
96             {
97                 s_j = 2;
98             }
99             else
100             {
101                 s_j = 3;
102             }
103             if (s_j > 2)
104             {
105                 s_j = 0;
106             }
107             else
108             {
109                 s_j = 1;
110             }
111             if (s_j < 1)
112             {
113                 s_j = 1;
114             }
115             else
116             {
117                 s_j = 0;
118             }
119         }
120         Escape(n4, s_j, s_j, x1, x2, x3, x4);
121
122         /* MODULE 5:  omitted */
123         /* MODULE 6:  integer Math */
124         s_j = 1;
125         s_k = 2;
126         s_l = 3;
127         for (i = 1; i <= n6; i += 1)
128         {
129             s_j = s_j * (s_k - s_j) * (s_l - s_k);
130             s_k = s_l * s_k - (s_l - s_j) * s_k;
131             s_l = (s_l - s_k) * (s_k + s_j);
132             e1[s_l - 2] = s_j + s_k + s_l;
133             e1[s_k - 2] = s_j * s_k * s_l;
134         }
135         Escape(n6, s_j, s_k, e1[0], e1[1], e1[2], e1[3]);
136
137         /* MODULE 7:  trig. functions */
138         x = y = 0.5;
139         for (i = 1; i <= n7; i += 1)
140         {
141             x = s_t * System.Math.Atan(s_t2 * System.Math.Sin(x) * System.Math.Cos(x) / (System.Math.Cos(x + y) + System.Math.Cos(x - y) - 1.0));
142             y = s_t * System.Math.Atan(s_t2 * System.Math.Sin(y) * System.Math.Cos(y) / (System.Math.Cos(x + y) + System.Math.Cos(x - y) - 1.0));
143         }
144         Escape(n7, s_j, s_k, x, x, y, y);
145
146         /* MODULE 8:  procedure calls */
147         x = y = z = 1.0;
148         for (i = 1; i <= n8; i += 1)
149         {
150             P3(x, y, out z);
151         }
152         Escape(n8, s_j, s_k, x, y, z, z);
153
154         /* MODULE9:  array references */
155         s_j = 1;
156         s_k = 2;
157         s_l = 3;
158         e1[0] = 1.0;
159         e1[1] = 2.0;
160         e1[2] = 3.0;
161         for (i = 1; i <= n9; i += 1)
162         {
163             P0(e1);
164         }
165         Escape(n9, s_j, s_k, e1[0], e1[1], e1[2], e1[3]);
166
167         /* MODULE10:  integer System.Math */
168         s_j = 2;
169         s_k = 3;
170         for (i = 1; i <= n10; i += 1)
171         {
172             s_j = s_j + s_k;
173             s_k = s_j + s_k;
174             s_j = s_k - s_j;
175             s_k = s_k - s_j - s_j;
176         }
177         Escape(n10, s_j, s_k, x1, x2, x3, x4);
178
179         /* MODULE11:  standard functions */
180         x = 0.75;
181         for (i = 1; i <= n11; i += 1)
182         {
183             x = System.Math.Sqrt(System.Math.Exp(System.Math.Log(x) / t1));
184         }
185         Escape(n11, s_j, s_k, x, x, x, x);
186
187         return true;
188     }
189
190     private static void PA(double[] e)
191     {
192         int j;
193         j = 0;
194     lab:
195         e[0] = (e[0] + e[1] + e[2] - e[3]) * s_t;
196         e[1] = (e[0] + e[1] - e[2] + e[3]) * s_t;
197         e[2] = (e[0] - e[1] + e[2] + e[3]) * s_t;
198         e[3] = (-e[0] + e[1] + e[2] + e[3]) / s_t2;
199         j += 1;
200         if (j < 6)
201         {
202             goto lab;
203         }
204     }
205
206     private static void P3(double x, double y, out double z)
207     {
208         x = s_t * (x + y);
209         y = s_t * (x + y);
210         z = (x + y) / s_t2;
211     }
212
213     private static void P0(double[] e1)
214     {
215         e1[s_j] = e1[s_k];
216         e1[s_k] = e1[s_l];
217         e1[s_l] = e1[s_j];
218     }
219
220     [Benchmark]
221     public static void Test()
222     {
223         foreach (var iteration in Benchmark.Iterations)
224         {
225             using (iteration.StartMeasurement())
226             {
227                 Bench();
228             }
229         }
230     }
231
232     private static bool TestBase()
233     {
234         bool result = Bench();
235         return result;
236     }
237
238     public static int Main()
239     {
240         bool result = TestBase();
241         return (result ? 100 : -1);
242     }
243 }
244 }