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 // The modified regula-falsi routine adapted from Conte and De Boor
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class Regula
19 public const int Iterations = 1;
21 public const int Iterations = 4000000;
24 public static volatile object VolatileObject;
26 [MethodImpl(MethodImplOptions.NoInlining)]
27 private static void Escape(object obj)
32 [MethodImpl(MethodImplOptions.NoInlining)]
33 private static bool Bench()
45 for (int i = 1; i <= Iterations; i++)
49 Inner(ref a, ref b, 0.0000001, 0.0000000001, 30, out iflag);
56 error = System.Math.Abs(b - a) / 2.0;
61 System.Console.WriteLine(" the root is {0:E}", xi);
62 System.Console.WriteLine(" plus/minus {0}\n", error);
63 System.Console.WriteLine(" fg(root):= {0:E}\n", fxi);
71 // Escape iflag, xi, error, and fxi so that they appear live
80 private static double FG(double x)
82 return (-1.0 - (x * (1.0 - (x * x))));
85 private static void Inner(ref double a, ref double b, double xtol, double ftol, int ntol, out int iflag)
87 double signfa, prevfw, fa, fb, fw, w;
101 if (signfa * fb <= 0.0)
112 for (int i = 1; i <= ntol; i++)
114 if (System.Math.Abs(b - a) / 2.0 <= xtol)
118 if (System.Math.Abs(fw) > ftol)
129 w = (fa * b - fb * a) / (fa - fb);
140 if (signfa * fw < 0.0)
146 if (fw * prevfw > 0.0)
155 if (fw * prevfw > 0.0)
172 public static void Test()
174 foreach (var iteration in Benchmark.Iterations)
176 using (iteration.StartMeasurement())
183 private static bool TestBase()
185 bool result = Bench();
189 public static int Main()
191 bool result = TestBase();
192 return (result ? 100 : -1);