mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-13 16:18:05 +00:00
test: setup common stability and robustness properties tracking
This commit is contained in:
@@ -91,25 +91,51 @@ public sealed class RingBuffer : IEnumerable<double>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recalculates the sum by iterating over all elements.
|
||||
/// Recalculates the sum by iterating over all elements using SIMD acceleration.
|
||||
/// Useful for correcting floating-point drift after many updates.
|
||||
/// Uses GetSequencedSpans to avoid allocation when buffer wraps.
|
||||
/// </summary>
|
||||
public double RecalculateSum()
|
||||
{
|
||||
double sum = 0;
|
||||
GetSequencedSpans(out var first, out var second);
|
||||
_sum = SumSpanSimd(first) + SumSpanSimd(second);
|
||||
return _sum;
|
||||
}
|
||||
|
||||
for (int i = 0; i < first.Length; i++)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static double SumSpanSimd(ReadOnlySpan<double> span)
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
sum += first[i];
|
||||
return 0.0;
|
||||
}
|
||||
for (int i = 0; i < second.Length; i++)
|
||||
|
||||
int vectorSize = Vector<double>.Count;
|
||||
var acc = Vector<double>.Zero;
|
||||
int i = 0;
|
||||
|
||||
if (span.Length >= vectorSize)
|
||||
{
|
||||
sum += second[i];
|
||||
ref double spanRef = ref MemoryMarshal.GetReference(span);
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize)
|
||||
{
|
||||
acc += Unsafe.As<double, Vector<double>>(ref Unsafe.Add(ref spanRef, i));
|
||||
}
|
||||
}
|
||||
|
||||
// Horizontal sum of SIMD accumulator
|
||||
double sum = 0.0;
|
||||
for (int j = 0; j < vectorSize; j++)
|
||||
{
|
||||
sum += acc[j];
|
||||
}
|
||||
|
||||
// Scalar tail
|
||||
for (; i < span.Length; i++)
|
||||
{
|
||||
sum += span[i];
|
||||
}
|
||||
|
||||
_sum = sum;
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user