Better L4 and array storage design

This commit is contained in:
Youmu Liu
2026-03-04 18:12:58 +08:00
parent 7e970352ab
commit dfa6f4ba92
103 changed files with 853 additions and 6837 deletions
@@ -31,33 +31,16 @@ using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Internal
{
[InlineArray(10)]
internal unsafe struct MatrixIntrinsic
{
private nint handle; //f32_panel_mul
public readonly nint this[
MatrixIntrinsicHandle handle
]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this[(int)handle];
}
}
internal enum MatrixIntrinsicHandle : int
{
BufferAdd = 0,
BufferFill = 1,
BufferScalarMul = 2,
BufferSub = 3,
BufferTranspose = 4,
Add = 0,
Fill = 1,
ScalarMul = 2,
Sub = 3,
Transpose = 4,
ClearPanel = 5,
KerFma = 6,
TileFma = 7,
Fma = 6,
Pivot = 8,
}
@@ -66,18 +49,18 @@ namespace StgSharp.Mathematics.Internal
public struct MatrixElementType
{
public const int IntrinsicCount = 0xa;
public const int IntrinsicCount = 0x0a;
public static readonly MatrixElementType F32 = new(0x00010004);
public static readonly MatrixElementType F64 = new(0x00020008);
public static readonly MatrixElementType I08 = new(0x00030001);
public static readonly MatrixElementType I16 = new(0x00040002);
public static readonly MatrixElementType I32 = new(0x00050004);
public static readonly MatrixElementType I64 = new(0x00060008);
public static readonly MatrixElementType U08 = new(0x00070001);
public static readonly MatrixElementType U16 = new(0x00080002);
public static readonly MatrixElementType U32 = new(0x00090004);
public static readonly MatrixElementType U64 = new(0x000a0008);
public static readonly MatrixElementType F32 = new(0x00000004);
public static readonly MatrixElementType F64 = new(0x00010008);
public static readonly MatrixElementType I08 = new(0x00020001);
public static readonly MatrixElementType I16 = new(0x00030002);
public static readonly MatrixElementType I32 = new(0x00040004);
public static readonly MatrixElementType I64 = new(0x00050008);
public static readonly MatrixElementType U08 = new(0x00060001);
public static readonly MatrixElementType U16 = new(0x00070002);
public static readonly MatrixElementType U32 = new(0x00080004);
public static readonly MatrixElementType U64 = new(0x00090008);
[FieldOffset(0)] internal readonly int Mask;
[FieldOffset(2)] public readonly ushort IntrinsicNode;
@@ -96,14 +79,6 @@ namespace StgSharp.Mathematics.Internal
}
[InlineArray(MatrixElementType.IntrinsicCount)]
internal unsafe struct MatrixIntrinsicArray
{
private MatrixIntrinsic e0;
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct IntrinsicContext
{
@@ -112,7 +87,8 @@ namespace StgSharp.Mathematics.Internal
public delegate* unmanaged[Cdecl]<int, ulong> factorial_simd; // factorial_simd
public delegate* unmanaged[Cdecl]<char*, int, int, int> index_pair; // index_pair
public delegate* unmanaged[Cdecl]<Vector4*, Vector4*, void> vec_f32_normalize_3; // f32_normalize_3
public MatrixIntrinsicArray mat; // mat[4] in native order
public MatrixMultiKernelIntrinsicArray mat_mk; // mat[4] in native order
public MatrixSingleKernelIntrinsicArray mat_sk; // mat[4] in native order
public IntrinsicContext()
{
@@ -120,4 +96,79 @@ namespace StgSharp.Mathematics.Internal
}
}
#region mk
[InlineArray(10)]
internal unsafe struct MatrixMultiKernelIntrinsic
{
private nint handle; //f32_panel_mul
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Call(
MatrixIntrinsicHandle index,
MatrixParallelTask* task
)
{
nint ptr = this[(int)index];
((delegate* unmanaged[Cdecl]<MatrixParallelTask*, void>)ptr)(task);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly nint Get(
MatrixIntrinsicHandle index
)
{
return this[(int)index];
}
}
[InlineArray(MatrixElementType.IntrinsicCount)]
internal unsafe struct MatrixMultiKernelIntrinsicArray
{
private MatrixMultiKernelIntrinsic e0;
}
#endregion
#region sk
[InlineArray(10)]
internal unsafe struct MatrixSingleKernelIntrinsic
{
private nint handle; //f32_panel_mul
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Call(
MatrixIntrinsicHandle index,
MatrixKernel* left,
MatrixKernel* right,
MatrixKernel* ans,
ulong scalar
)
{
nint ptr = this[(int)index];
((delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ulong, void>)ptr)(left,
right,
ans,
scalar);
}
}
[InlineArray(MatrixElementType.IntrinsicCount)]
internal unsafe struct MatrixSingleKernelIntrinsicArray
{
private MatrixSingleKernelIntrinsic e0;
}
#endregion
}
@@ -0,0 +1,62 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelTask"
// Project: StgSharp
// 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Internal
{
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct MatrixParallelTask
{
[FieldOffset(0)]public MatrixKernel* Mat1; // 8
[FieldOffset(8)] public MatrixKernel* Mat2; // 8
[FieldOffset(16)] public MatrixKernel* Mat3; // 16
[FieldOffset(32)] public int Common_i;
[FieldOffset(36)] public int Common_j;
[FieldOffset(40)] public int Common_k;
[FieldOffset(44)] public int i;
[FieldOffset(48)] public int j;
[FieldOffset(52)] public int k;
[FieldOffset(56)] public MatrixElementType ElementType; // 56
[FieldOffset(24)] public nint Span; // 24
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(
MatrixParallelTask* source,
MatrixParallelTask* target
)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask),
sizeof(MatrixParallelTask));
}
}
}