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.
5 // C# translation of Whetstone Double Precision Benchmark
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class Whetsto
19 public const int Iterations = 1;
21 public const int Iterations = 50000;
24 private static int s_j, s_k, s_l;
25 private static double s_t, s_t2;
27 public static volatile int Volatile_out;
29 private static void Escape(int n, int j, int k, double x1, double x2, double x3, double x4)
34 Volatile_out = (int)x1;
35 Volatile_out = (int)x2;
36 Volatile_out = (int)x3;
37 Volatile_out = (int)x4;
40 [MethodImpl(MethodImplOptions.NoInlining)]
41 private static bool Bench()
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;
53 n4 = 345 * Iterations;
54 n6 = 210 * Iterations;
56 n8 = 899 * Iterations;
57 n9 = 616 * Iterations;
59 n11 = 93 * Iterations;
63 for (i = 1; i <= n1; i += 1)
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;
70 Escape(n1, n1, n1, x1, x2, x3, x4);
72 /* MODULE 2: array elements */
74 e1[1] = e1[2] = e1[3] = -1.0;
75 for (i = 1; i <= n2; i += 1)
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;
82 Escape(n2, n3, n2, e1[0], e1[1], e1[2], e1[3]);
84 /* MODULE 3: array as parameter */
85 for (i = 1; i <= n3; i += 1)
89 Escape(n3, n2, n2, e1[0], e1[1], e1[2], e1[3]);
91 /* MODULE 4: conditional jumps */
93 for (i = 1; i <= n4; i += 1)
120 Escape(n4, s_j, s_j, x1, x2, x3, x4);
122 /* MODULE 5: omitted */
123 /* MODULE 6: integer Math */
127 for (i = 1; i <= n6; i += 1)
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;
135 Escape(n6, s_j, s_k, e1[0], e1[1], e1[2], e1[3]);
137 /* MODULE 7: trig. functions */
139 for (i = 1; i <= n7; i += 1)
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));
144 Escape(n7, s_j, s_k, x, x, y, y);
146 /* MODULE 8: procedure calls */
148 for (i = 1; i <= n8; i += 1)
152 Escape(n8, s_j, s_k, x, y, z, z);
154 /* MODULE9: array references */
161 for (i = 1; i <= n9; i += 1)
165 Escape(n9, s_j, s_k, e1[0], e1[1], e1[2], e1[3]);
167 /* MODULE10: integer System.Math */
170 for (i = 1; i <= n10; i += 1)
175 s_k = s_k - s_j - s_j;
177 Escape(n10, s_j, s_k, x1, x2, x3, x4);
179 /* MODULE11: standard functions */
181 for (i = 1; i <= n11; i += 1)
183 x = System.Math.Sqrt(System.Math.Exp(System.Math.Log(x) / t1));
185 Escape(n11, s_j, s_k, x, x, x, x);
190 private static void PA(double[] e)
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;
206 private static void P3(double x, double y, out double z)
213 private static void P0(double[] e1)
221 public static void Test()
223 foreach (var iteration in Benchmark.Iterations)
225 using (iteration.StartMeasurement())
232 private static bool TestBase()
234 bool result = Bench();
238 public static int Main()
240 bool result = TestBase();
241 return (result ? 100 : -1);