Files
Misaki.HighPerformance/Misaki.HighPerformance.Test/Benchmark/MathematicsBenchmark.cs
Misaki 49e1171781
Some checks failed
Publish NuGet Packages / publish (push) Failing after 3m12s
Refactor and enhance math and utility libraries
Refactored `sincos` usage across `quaternion` and `random` to use `out` parameters for improved performance. Enhanced `random` struct with updated random direction generation methods.

Added new benchmarks in `MathematicsBenchmark` for vector operations, including SIMD-based `f4` struct. Downgraded target framework to `net9.0` for compatibility.

Introduced `ReadOnlyUnsafeCollection` for low-level memory management. Added utility methods in `CollectionUtility` for span creation and optimized list operations.

Renamed `MemoryUtilities` to `MemoryUtility` and updated all references. Enhanced `ObjectPool` with `Rent` and `TryRent` methods. Enabled `AllowUnsafeBlocks` and AOT compatibility in project configuration.

Performed general code cleanup, including removal of unused methods, improved formatting, and alignment with modern coding practices.
2025-11-04 14:53:01 +09:00

124 lines
2.4 KiB
C#

using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Mathematics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace Misaki.HighPerformance.Test.Benchmark;
public unsafe class MathematicsBenchmark
{
public struct f4
{
private Vector128<float> _vec;
public f4(float x, float y, float z, float w)
{
_vec = Vector128.Create(x, y, z, w);
}
public f4(Vector128<float> vec)
{
_vec = vec;
}
public static f4 operator +(f4 a, f4 b)
{
var result = a._vec + b._vec;
return Unsafe.As<Vector128<float>, f4>(ref result);
}
}
[Params(100)]
public int count;
[Benchmark]
public Vector2 Vector2Add()
{
var a = new Vector2(1, 2);
var b = new Vector2(3, 4);
var c = new Vector2(5, 6);
for (var i = 0; i < count; i++)
{
c += a + b;
}
return c;
}
[Benchmark]
public float2 Float2Add()
{
var a = new float2(1, 2);
var b = new float2(3, 4);
var c = new float2(5, 6);
for (var i = 0; i < count; i++)
{
c += a + b;
}
return c;
}
[Benchmark]
public Vector4 Vector4Add()
{
var a = new Vector4(1, 2, 3, 4);
var b = new Vector4(5, 6, 7, 8);
var result = new Vector4();
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
}
[Benchmark]
public float4 Float4Add()
{
var a = new float4(1, 2, 3, 4);
var b = new float4(5, 6, 7, 8);
var result = new float4();
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
}
[Benchmark]
public f4 f4Add()
{
var a = new f4(1, 2, 3, 4);
var b = new f4(5, 6, 7, 8);
var result = new f4(0, 0, 0, 0);
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
}
[Benchmark]
public unsafe Vector128<float> v128Add()
{
var a = Vector128.Create(1f, 2f, 3f, 4f);
var b = Vector128.Create(5f, 6f, 7f, 8f);
var result = Vector128<float>.Zero;
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
}
}