swiss ok, needs more benchmark

This commit is contained in:
Youmu Liu
2026-03-06 18:22:30 +08:00
parent 83997f64d0
commit f6e897ed06
70 changed files with 1889 additions and 3371 deletions
+33 -22
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixPanel"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -25,14 +25,14 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics
namespace StgSharp.Mathematics.Numeric
{
public unsafe struct MatrixPanel
{
@@ -42,37 +42,41 @@ namespace StgSharp.Mathematics
private byte Head;
// Allocate aligned memory for one panel; caller must free via Destroy.
public static unsafe MatrixPanel<T>* Create<T>() where T: unmanaged, INumber<T>
public static unsafe MatrixPanel<T>* Create<T>() where T : unmanaged, INumber<T>
{
return (MatrixPanel<T>*)NativeMemory.AlignedAlloc(Size<T>(), AlignmentBytes);
}
public static unsafe void Destroy<T>(MatrixPanel<T>* panel) where T: unmanaged, INumber<T>
public static unsafe void Destroy<T>(
MatrixPanel<T>* panel
) where T : unmanaged, INumber<T>
{
NativeMemory.AlignedFree(panel);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Span<T> Elements<T>(ref T head) where T: unmanaged, INumber<T>
public static unsafe Span<T> Elements<T>(
ref T head
) where T : unmanaged, INumber<T>
{
void* p = Unsafe.AsPointer(ref head);
return new Span<T>(p, MaxElementCount<T>());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ElementSideLength<T>() where T: unmanaged, INumber<T>
public static int ElementSideLength<T>() where T : unmanaged, INumber<T>
{
return PanelSideLength<T>();
}
public static int MaxElementCount<T>() where T: unmanaged, INumber<T>
public static int MaxElementCount<T>() where T : unmanaged, INumber<T>
{
int side = ElementSideLength<T>();
return side * side;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nuint Size<T>() where T: unmanaged, INumber<T>
public static nuint Size<T>() where T : unmanaged, INumber<T>
{
return (nuint)(MaxElementCount<T>() * sizeof(T));
}
@@ -81,7 +85,7 @@ namespace StgSharp.Mathematics
/// Number of 4x4 kernels that fit in a panel for the given ISA and element T.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int KernelCapacity<T>() where T: unmanaged, INumber<T>
internal static int KernelCapacity<T>() where T : unmanaged, INumber<T>
{
int perSide = KernelSideCount<T>();
return perSide * perSide;
@@ -91,9 +95,9 @@ namespace StgSharp.Mathematics
/// How many 4x4 kernels fit along a single panel dimension for the given ISA.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int KernelSideCount<T>() where T: unmanaged, INumber<T>
internal static int KernelSideCount<T>() where T : unmanaged, INumber<T>
{
SIMDID id = NumericalModule.IntrinsicMask;
SIMDID id = NumericalModule.GlobalIntrinsicMask;
switch (id.MaskByte[0] & 0b_00001111)
{
case 1:
@@ -110,31 +114,35 @@ namespace StgSharp.Mathematics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe nuint Align64(void* p)
private static unsafe nuint Align64(
void* p
)
{
return ((nuint)p + (AlignmentBytes - 1)) & ~(nuint)(AlignmentBytes - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int KernelSideCountAvx2<T>() where T: unmanaged, INumber<T>
private static int KernelSideCountAvx2<T>() where T : unmanaged, INumber<T>
{
return PanelSideFromBits<T>(256) / 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int KernelSideCountAvx512<T>() where T: unmanaged, INumber<T>
private static int KernelSideCountAvx512<T>() where T : unmanaged, INumber<T>
{
return PanelSideFromBits<T>(512) / 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int KernelSideCountSse<T>() where T: unmanaged, INumber<T>
private static int KernelSideCountSse<T>() where T : unmanaged, INumber<T>
{
return PanelSideFromBits<T>(128) / 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int PanelSideFromBits<T>(int vectorBits) where T: unmanaged, INumber<T>
private static int PanelSideFromBits<T>(
int vectorBits
) where T : unmanaged, INumber<T>
{
int elemBits = sizeof(T) * 8;
@@ -146,9 +154,9 @@ namespace StgSharp.Mathematics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int PanelSideLength<T>() where T: unmanaged, INumber<T>
private static int PanelSideLength<T>() where T : unmanaged, INumber<T>
{
SIMDID id = NumericalModule.IntrinsicMask;
SIMDID id = NumericalModule.GlobalIntrinsicMask;
switch (id.MaskByte[0] & 0b_00001111)
{
case 1:
@@ -167,12 +175,15 @@ namespace StgSharp.Mathematics
}
// Single POD-style panel: ZMM-compatible size. Alignment handled by allocator, not by the struct.
public unsafe struct MatrixPanel<T> where T: unmanaged, INumber<T>
public unsafe struct MatrixPanel<T> where T : unmanaged, INumber<T>
{
internal T Head; // first element of the panel buffer
public ref T this[int column, int row]
public ref T this[
int column,
int row
]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref Unsafe.Add(ref Head, row * MatrixPanel.ElementSideLength<T>() + column);