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 // FFT benchmark adapted from a Fortran routine from the book
6 // Digital Signal Analysis, Samuel Stearns, Hayden Book Co.
8 using Microsoft.Xunit.Performance;
10 using System.Runtime.CompilerServices;
13 [assembly: OptimizeForBenchmarks]
15 namespace Benchstone.BenchF
17 public static class FFT
20 public const int Iterations = 1;
22 public const int Iterations = 300000;
25 private static readonly int s_points = 16;
26 public static volatile object VolatileObject;
28 [MethodImpl(MethodImplOptions.NoInlining)]
29 private static void Escape(object obj)
34 [MethodImpl(MethodImplOptions.NoInlining)]
35 private static bool Bench()
37 double[] fr = new double[17];
38 double[] fi = new double[17];
43 for (int iter = 1; iter <= Iterations; iter++)
45 for (i = 1; i <= s_points; ++i)
47 t = ((double)0.375) * ((double)(i - 1));
48 fr[i] = System.Math.Exp(-t) * System.Math.Sin(t);
51 FastFourierT(fr, fi, s_points);
54 // Escape the results to live-out.
61 private static void FastFourierT(double[] fr, double[] fi, int n)
65 double a, el, tr, ti, wr, wi;
74 for (l = l / 2; ((mr + l) > nn); l = l / 2)
78 // mr <= (mr % l) + l ==> mr <= (l - 1) + l = 2l - 1
84 // Accessing upto m + 1 ==> nn + 1 ==> n - 1 + 1 ==> n
86 // Accessing upto mr + 1 ==> n - 1 + 1 ==> n
87 fr[m + 1] = fr[mr + 1];
90 fi[m + 1] = fi[mr + 1];
96 for (l = 1; l < n; l = istep)
104 a = ((double)3.1415926535) * (((double)(1 - m)) / el);
105 wr = System.Math.Cos(a);
106 wi = System.Math.Sin(a);
110 // l can have a maximum value of 2^x where 2^x < n and 2^(x+1) = n, since n is even
111 // ==> istep <= 2^(x+1) ==> i can only take the value of m and m <= l
112 // Therefore, j <= l + l
113 // or j <= 2^x + 2^x = 2^(x+1) = n
117 // Accessing upto j <= n, i <= n
118 tr = wr * fr[j] - wi * fi[j];
119 ti = wr * fi[j] + wi * fr[j];
132 public static void Test()
134 foreach (var iteration in Benchmark.Iterations)
136 using (iteration.StartMeasurement())
143 private static bool TestBase()
145 bool result = Bench();
149 public static int Main()
151 bool result = TestBase();
152 return (result ? 100 : -1);