SIMD Refactor: Merge simd-dev into dev (#55)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
Co-authored-by: Warp <agent@warp.dev>
This commit is contained in:
Miha Kralj
2026-01-18 19:02:03 -08:00
committed by GitHub
co-authored by Claude Opus 4.5 aider Warp
parent 5bcdf8d614
commit 86fe32a682
1750 changed files with 198235 additions and 80539 deletions
+372
View File
@@ -0,0 +1,372 @@
namespace QuanTAlib.Tests;
public class TValueTests
{
[Fact]
public void Constructor_WithLongTime_SetsPropertiesCorrectly()
{
long time = DateTime.UtcNow.Ticks;
const double value = 123.45;
var tValue = new TValue(time, value);
Assert.Equal(time, tValue.Time);
Assert.Equal(value, tValue.Value);
}
[Fact]
public void Constructor_WithDateTime_SetsPropertiesCorrectly()
{
var dateTime = new DateTime(2024, 6, 15, 10, 30, 0, DateTimeKind.Utc);
double value = 123.45;
var tValue = new TValue(dateTime, value);
Assert.Equal(dateTime.Ticks, tValue.Time);
Assert.Equal(value, tValue.Value);
}
[Fact]
public void AsDateTime_ReturnsCorrectDateTime()
{
var dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, 100.0);
Assert.Equal(dt, tValue.AsDateTime);
Assert.Equal(DateTimeKind.Utc, tValue.AsDateTime.Kind);
}
[Fact]
public void ToString_FormatsCorrectly()
{
var dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, 123.456);
string result = tValue.ToString();
Assert.Contains("2023-01-01", result, StringComparison.Ordinal);
Assert.Contains("12:00:00", result, StringComparison.Ordinal);
Assert.Contains("123.46", result, StringComparison.Ordinal);
}
[Fact]
public void ExplicitConversion_ToDouble_ReturnsValue()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, 42.0);
double val = (double)tValue;
Assert.Equal(42.0, val);
}
[Fact]
public void ImplicitConversion_ToDateTime_ReturnsCorrectDateTime()
{
var dateTime = new DateTime(2024, 6, 15, 10, 30, 0, DateTimeKind.Utc);
var tValue = new TValue(dateTime.Ticks, 100.0);
DateTime result = tValue;
Assert.Equal(dateTime, result);
Assert.Equal(DateTimeKind.Utc, result.Kind);
}
[Fact]
public void Equals_TValue_SameValues_ReturnsTrue()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12345, 100.0);
Assert.True(tv1.Equals(tv2));
}
[Fact]
public void Equals_TValue_DifferentTime_ReturnsFalse()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12346, 100.0);
Assert.False(tv1.Equals(tv2));
}
[Fact]
public void Equals_TValue_DifferentValue_ReturnsFalse()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12345, 101.0);
Assert.False(tv1.Equals(tv2));
}
[Fact]
public void Equals_Object_SameTValue_ReturnsTrue()
{
var tv1 = new TValue(12345, 100.0);
object tv2 = new TValue(12345, 100.0);
Assert.True(tv1.Equals(tv2));
}
[Fact]
public void Equals_Object_DifferentType_ReturnsFalse()
{
var tv = new TValue(12345, 100.0);
object other = "not a TValue";
Assert.False(tv.Equals(other));
}
[Fact]
public void Equals_Object_Null_ReturnsFalse()
{
var tv = new TValue(12345, 100.0);
Assert.False(tv.Equals(null));
}
[Fact]
public void GetHashCode_SameValues_ReturnsSameHashCode()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12345, 100.0);
Assert.Equal(tv1.GetHashCode(), tv2.GetHashCode());
}
[Fact]
public void GetHashCode_DifferentValues_ReturnsDifferentHashCode()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12346, 100.0);
Assert.NotEqual(tv1.GetHashCode(), tv2.GetHashCode());
}
[Fact]
public void EqualityOperator_SameValues_ReturnsTrue()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12345, 100.0);
Assert.True(tv1 == tv2);
}
[Fact]
public void EqualityOperator_DifferentValues_ReturnsFalse()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12346, 100.0);
Assert.False(tv1 == tv2);
}
[Fact]
public void InequalityOperator_SameValues_ReturnsFalse()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12345, 100.0);
Assert.False(tv1 != tv2);
}
[Fact]
public void InequalityOperator_DifferentValues_ReturnsTrue()
{
var tv1 = new TValue(12345, 100.0);
var tv2 = new TValue(12346, 100.0);
Assert.True(tv1 != tv2);
}
[Fact]
public void Constructor_WithDateTimeLocal_ConvertsToUtc()
{
var localTime = new DateTime(2024, 6, 15, 10, 30, 0, DateTimeKind.Local);
double value = 123.45;
var tValue = new TValue(localTime, value);
// Time should be stored as UTC ticks
var expectedUtc = localTime.ToUniversalTime();
Assert.Equal(expectedUtc.Ticks, tValue.Time);
}
[Fact]
public void Constructor_WithDateTimeUnspecified_ConvertsToUtc()
{
var unspecifiedTime = new DateTime(2024, 6, 15, 10, 30, 0, DateTimeKind.Unspecified);
double value = 123.45;
var tValue = new TValue(unspecifiedTime, value);
// Unspecified is treated as local and converted to UTC
var expectedUtc = unspecifiedTime.ToUniversalTime();
Assert.Equal(expectedUtc.Ticks, tValue.Time);
}
[Fact]
public void Constructor_WithDateTimeUtc_PreservesTicks()
{
var utcTime = new DateTime(2024, 6, 15, 10, 30, 0, DateTimeKind.Utc);
double value = 123.45;
var tValue = new TValue(utcTime, value);
Assert.Equal(utcTime.Ticks, tValue.Time);
}
[Fact]
public void Default_TValue_HasZeroTimeAndValue()
{
var defaultTValue = default(TValue);
Assert.Equal(0, defaultTValue.Time);
Assert.Equal(0.0, defaultTValue.Value);
}
[Fact]
public void Constructor_WithNaN_PreservesNaN()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.NaN);
Assert.True(double.IsNaN(tValue.Value));
}
[Fact]
public void Constructor_WithPositiveInfinity_PreservesInfinity()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.PositiveInfinity);
Assert.True(double.IsPositiveInfinity(tValue.Value));
}
[Fact]
public void Constructor_WithNegativeInfinity_PreservesInfinity()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.NegativeInfinity);
Assert.True(double.IsNegativeInfinity(tValue.Value));
}
[Fact]
public void Constructor_WithMaxValue_PreservesMaxValue()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.MaxValue);
Assert.Equal(double.MaxValue, tValue.Value);
}
[Fact]
public void Constructor_WithMinValue_PreservesMinValue()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.MinValue);
Assert.Equal(double.MinValue, tValue.Value);
}
[Fact]
public void Constructor_WithEpsilon_PreservesEpsilon()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.Epsilon);
Assert.Equal(double.Epsilon, tValue.Value);
}
[Fact]
public void ExplicitConversion_ToDouble_WithNaN_ReturnsNaN()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, double.NaN);
double val = (double)tValue;
Assert.True(double.IsNaN(val));
}
[Fact]
public void ToString_WithNaN_FormatsCorrectly()
{
var dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, double.NaN);
string result = tValue.ToString();
Assert.Contains("NaN", result, StringComparison.Ordinal);
}
[Fact]
public void ToString_WithInfinity_FormatsCorrectly()
{
var dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, double.PositiveInfinity);
string result = tValue.ToString();
Assert.Contains("∞", result, StringComparison.Ordinal);
}
[Fact]
public void ToString_WithNegativeValue_FormatsCorrectly()
{
var dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, -123.456);
string result = tValue.ToString();
Assert.Contains("-123.46", result, StringComparison.Ordinal);
}
[Fact]
public void AsDateTime_ReturnsUtcKind()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, 100.0);
Assert.Equal(DateTimeKind.Utc, tValue.AsDateTime.Kind);
}
[Fact]
public void Equals_WithNaN_BothNaN_ReturnsFalse()
{
// NaN != NaN in IEEE 754
var tv1 = new TValue(12345, double.NaN);
var tv2 = new TValue(12345, double.NaN);
// Record struct equality compares fields directly
// double.NaN.Equals(double.NaN) returns true in .NET
Assert.True(tv1.Equals(tv2));
}
[Fact]
public void GetHashCode_WithNaN_DoesNotThrow()
{
var tv = new TValue(12345, double.NaN);
var hash = tv.GetHashCode();
Assert.True(hash != 0 || hash == 0); // Just verify it doesn't throw
}
[Fact]
public void Constructor_WithZeroTime_Allowed()
{
var tValue = new TValue(0, 100.0);
Assert.Equal(0, tValue.Time);
Assert.Equal(100.0, tValue.Value);
}
[Fact]
public void Constructor_WithNegativeTime_Allowed()
{
var tValue = new TValue(-12345, 100.0);
Assert.Equal(-12345, tValue.Time);
}
[Fact]
public void Constructor_WithMaxLongTime_Allowed()
{
var tValue = new TValue(long.MaxValue, 100.0);
Assert.Equal(long.MaxValue, tValue.Time);
}
}
+99
View File
@@ -0,0 +1,99 @@
# TValue: Time-Value Pair
## What It Does
`TValue` is the fundamental atomic unit of data in QuanTAlib. It represents a single point in a time series, consisting of a timestamp and a double-precision floating-point value. It serves as the standard input and output format for all indicators and data streams.
## Design Philosophy
In high-frequency trading and quantitative analysis, memory allocation is a critical bottleneck. `TValue` is designed as a **lightweight, immutable struct** to ensure:
* **Zero Heap Allocation**: Being a struct, it lives on the stack or embedded in arrays, avoiding Garbage Collector (GC) pressure.
* **Thread Safety**: Immutability guarantees safe concurrent access.
* **Minimal Footprint**: Occupies exactly 16 bytes (8 bytes for `long` Time + 8 bytes for `double` Value), fitting efficiently in CPU cache lines.
## How It Works
`TValue` is implemented as a `readonly record struct`. It encapsulates:
* **Time**: A `long` representing ticks (UTC).
* **Value**: A `double` representing the data magnitude.
It supports implicit conversions to `double` (extracting the value) and `DateTime` (extracting the time), making it syntactically fluid to use in calculations.
## Structure
### Definition
```csharp
public readonly record struct TValue(long Time, double Value);
```
### Properties
| Property | Type | Description |
| ------ | ------ | ------ |
| `Time` | `long` | Timestamp in ticks (UTC). |
| `Value` | `double` | The data value. |
| `AsDateTime` | `DateTime` | Helper to view `Time` as a `DateTime` object. |
### Constructors
| Constructor | Description |
| ------ | ------ |
| `new TValue(long time, double value)` | Creates a TValue from raw ticks. |
| `new TValue(DateTime time, double value)` | Creates a TValue from a DateTime object. |
## Usage
### Creating TValues
```csharp
// From DateTime
var t1 = new TValue(DateTime.UtcNow, 100.5);
// From Ticks
var t2 = new TValue(DateTime.UtcNow.Ticks, 100.5);
```
### Implicit Conversions
```csharp
TValue tv = new TValue(DateTime.UtcNow, 42.0);
// Implicitly converts to double
double val = tv; // 42.0
// Implicitly converts to DateTime
DateTime dt = tv; // DateTime object
```
### String Representation
```csharp
Console.WriteLine(tv); // Output: "[2024-01-01 12:00:00, 42.00]"
```
## Performance Profile
* **Memory**: 16 bytes per instance.
* **Allocation**: 0 bytes (Stack allocated).
* **Copying**: Cheap (fits in two 64-bit registers).
## Integration
`TValue` is the primary currency of the library:
* **Indicators**: `Update(TValue input)` accepts it.
* **Series**: `TSeries` stores collections of it.
* **Events**: `ITValuePublisher` broadcasts it.
## Architecture Notes
* **SkipLocalsInit**: The struct is marked with `[SkipLocalsInit]` to suppress zero-initialization of locals, squeezing out nanoseconds in tight loops.
* **AggressiveInlining**: All accessors and operators are inlined to ensure zero abstraction penalty.
## References
* [Structure of Arrays (SoA)](https://en.wikipedia.org/wiki/AOS_and_SOA)
* [C# Struct Performance](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct)
+40
View File
@@ -0,0 +1,40 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// A lightweight struct representing a time-value pair.
/// Pure data type: 16 bytes (long + double).
/// </summary>
[SkipLocalsInit]
[StructLayout(LayoutKind.Auto)]
public readonly record struct TValue(long Time, double Value)
{
public DateTime AsDateTime => new(Time, DateTimeKind.Utc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue(DateTime time, double value)
: this(time.Kind == DateTimeKind.Utc ? time.Ticks : time.ToUniversalTime().Ticks, value)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator double(TValue tv) => tv.Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator DateTime(TValue tv) => new(tv.Time, DateTimeKind.Utc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString()
{
string valueStr = Value switch
{
double.PositiveInfinity => ((char)0x221E).ToString(),
double.NegativeInfinity => "-" + (char)0x221E,
_ when double.IsNaN(Value) => "NaN",
_ => Value.ToString("F2"),
};
return $"[{AsDateTime:yyyy-MM-dd HH:mm:ss}, {valueStr}]";
}
}