From c5eaf250ab18f4a5475e0e9ed85edc2148720401 Mon Sep 17 00:00:00 2001
From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com>
Date: Tue, 14 Oct 2025 18:13:18 +0800
Subject: [PATCH] vector and matrix optimization
---
.../Code Templates/Templates.CSharp.xml | 37 ++--
.../Memory/ConcurrentBufferStack.cs | 51 +++--
.../Memory/HLSFAllocator.Alloc.cs | 66 ++-----
.../Memory/HLSFAllocator.BucketNode.cs | 51 ++---
.../HighPerformance/Memory/HLSFAllocator.cs | 47 +++--
StgSharp.Common/Mathematics/Graphic/Camera.cs | 75 ++++----
.../Mathematics/Graphic/Matrix44.cs | 81 ++++----
StgSharp.Common/Mathematics/Graphic/Vec2.cs | 44 +++--
StgSharp.Common/Mathematics/Graphic/Vec3.cs | 108 +++++------
StgSharp.Common/Mathematics/Linear/IMatrix.cs | 52 +++---
.../Threading/SamplingAdaptiveBackoff.cs | 40 ++--
.../Threading/SingleConsumerBell.cs | 175 ++++++++++++++++++
StgSharp.Native/sn-head/sn_intrinsic.h | 8 +-
StgSharp.Native/sn-head/sn_matkernel.h | 15 +-
StgSharp.Native/sn-src/load_intrinsic.c | 11 +-
StgSharp.Native/sn-src/vector.c | 2 +-
16 files changed, 482 insertions(+), 381 deletions(-)
create mode 100644 StgSharp.Common/Threading/SingleConsumerBell.cs
diff --git a/.cr/team/Editor/Code Templates/Templates.CSharp.xml b/.cr/team/Editor/Code Templates/Templates.CSharp.xml
index 3d6c7085..ad4ab5d8 100644
--- a/.cr/team/Editor/Code Templates/Templates.CSharp.xml
+++ b/.cr/team/Editor/Code Templates/Templates.CSharp.xml
@@ -15,26 +15,23 @@
// AuthorGroup: «?GetUserInfo(Company)»
// Copyright (c) «?GetUserInfo(Company)». 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.
+//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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
diff --git a/StgSharp.Common/HighPerformance/Memory/ConcurrentBufferStack.cs b/StgSharp.Common/HighPerformance/Memory/ConcurrentBufferStack.cs
index e56c5e2d..99171b6b 100644
--- a/StgSharp.Common/HighPerformance/Memory/ConcurrentBufferStack.cs
+++ b/StgSharp.Common/HighPerformance/Memory/ConcurrentBufferStack.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="ConcurrentBufferStack.cs"
+// file="ConcurrentBufferStack"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -103,7 +100,7 @@ namespace StgSharp.HighPerformance.Memory
public ulong Count
{
-[MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Volatile.Read(ref _count);
}
@@ -125,7 +122,7 @@ namespace StgSharp.HighPerformance.Memory
public void Dispose()
{
_lock.Dispose();
- Marshal.FreeHGlobal((nint)_buffer);
+ NativeMemory.Free(_buffer);
}
public T Pop()
@@ -266,8 +263,8 @@ namespace StgSharp.HighPerformance.Memory
T* dataStart = (T*)(_buffer + sizeof(Header));
fixed (T* sourcePtr = span) {
- Buffer.MemoryCopy(sourcePtr, dataStart,
- header->Capacity * (nuint)sizeof(T),
+ Buffer.MemoryCopy(sourcePtr, dataStart,
+ header->Capacity * (nuint)sizeof(T),
(nuint)span.Length * (nuint)sizeof(T));
}
diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs
index 91ef8e1e..16c77046 100644
--- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs
+++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="HLSFAllocator.Alloc.cs"
+// file="HLSFAllocator.Alloc"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -125,18 +122,6 @@ namespace StgSharp.HighPerformance.Memory
Entry* old = current;
current = newEntry;
- // update lock and data
- finalOffset += (ulong)s;
- remainSize -= s;
-
-
- newEntry->Level = i;
- PushLevel(i, blockCount - 1, bucket);
-
- // set new entry
- current = newEntry;
-
- // update data
finalOffset += (ulong)s;
remainSize -= s;
}
@@ -181,17 +166,6 @@ namespace StgSharp.HighPerformance.Memory
// update lock and data
finalOffset += (ulong)s;
remainSize -= s;
-
-
- newEntry->Level = i;
- PushLevel(i, blockCount - 1, bucket);
-
- // set new entry
- current = newEntry;
-
- // update data
- finalOffset += (ulong)s;
- remainSize -= s;
}
}
diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs
index 8d2fd64f..cb2cf9b5 100644
--- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs
+++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="HLSFAllocator.BucketNode.cs"
+// file="HLSFAllocator.BucketNode"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -39,14 +36,6 @@ namespace StgSharp.HighPerformance.Memory
public unsafe partial class HybridLayerSegregatedFitAllocator
{
- ///
- /// Initialize bucket system (called in constructor)
- ///
- private void InitializeBucketSystem()
- {
- _bucketHeads = new ulong[levelSizeArray.Length * 3];
- }
-
#region BucketNode Operations
[MethodImpl(MethodImplOptions.AggressiveInlining)]
diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs
index 16c26e9a..b8e3abef 100644
--- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs
+++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="HLSFAllocator.cs"
+// file="HLSFAllocator"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -59,7 +56,7 @@ namespace StgSharp.HighPerformance.Memory
m_Buffer = (byte*)NativeMemory.AlignedAlloc(byteSize, 16);
_align = 16;
- InitializeBucketSystem();
+ _bucketHeads = new ulong[levelSizeArray.Length * 3];
_spareMemory = (Entry*)_entries.Allocate();
_spareMemory->State = EntryState.Empty;
@@ -80,7 +77,7 @@ namespace StgSharp.HighPerformance.Memory
_align = align;
_size = byteSize;
- InitializeBucketSystem();
+ _bucketHeads = new ulong[levelSizeArray.Length * 3];
_spareMemory = (Entry*)_entries.Allocate();
_spareMemory->State = EntryState.Empty;
diff --git a/StgSharp.Common/Mathematics/Graphic/Camera.cs b/StgSharp.Common/Mathematics/Graphic/Camera.cs
index 0321e777..cfdcf701 100644
--- a/StgSharp.Common/Mathematics/Graphic/Camera.cs
+++ b/StgSharp.Common/Mathematics/Graphic/Camera.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="Camera.cs"
+// file="Camera"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -82,7 +79,7 @@ namespace StgSharp.Mathematics.Graphic
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
- move.colum3.vec -= cameraAtt.colum3.vec;
+ move.column3.vec -= cameraAtt.column3.vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
@@ -123,19 +120,19 @@ namespace StgSharp.Mathematics.Graphic
public void MoveNear(float distance)
{
- cameraAtt.colum3.Z -= distance;
+ cameraAtt.column3.Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
- cameraAtt.colum3.X -= distance;
+ cameraAtt.column3.X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
- cameraAtt.colum3.Y -= distance;
+ cameraAtt.column3.Y -= distance;
_isLookAtAvailable = false;
}
@@ -148,7 +145,7 @@ namespace StgSharp.Mathematics.Graphic
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
- if ((position.reg == cameraAtt.colum3.reg) && (this.up == up) && (cameraAtt.colum2.reg == direction.reg)) {
+ if ((position.reg == cameraAtt.column3.reg) && (this.up == up) && (cameraAtt.column2.reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
@@ -168,10 +165,10 @@ namespace StgSharp.Mathematics.Graphic
Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction));
- cameraAtt.colum0.reg = right.reg;
- cameraAtt.colum1.reg = up.reg;
- cameraAtt.colum2.reg = direction.reg;
- cameraAtt.colum3.reg = position.reg;
+ cameraAtt.column0.reg = right.reg;
+ cameraAtt.column1.reg = up.reg;
+ cameraAtt.column2.reg = direction.reg;
+ cameraAtt.column3.reg = position.reg;
InternalPitch();
InternalRow();
@@ -189,13 +186,13 @@ namespace StgSharp.Mathematics.Graphic
width = Scaler.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
- _projection.colum0.X = 2 * near / width;
- _projection.colum1.Y = 2 * near / height;
- _projection.colum2.X = 2 * offsetX / width;
- _projection.colum2.Y = 2 * offsetY / height;
- _projection.colum2.Z = (far + near) / (near - far);
- _projection.colum2.W = -1;
- _projection.colum3.Z = 2 * near * far / (near - far);
+ _projection.column0.X = 2 * near / width;
+ _projection.column1.Y = 2 * near / height;
+ _projection.column2.X = 2 * offsetX / width;
+ _projection.column2.Y = 2 * offsetY / height;
+ _projection.column2.Z = (far + near) / (near - far);
+ _projection.column2.W = -1;
+ _projection.column3.Z = 2 * near * far / (near - far);
}
public void Test(params Vec4[] vec)
diff --git a/StgSharp.Common/Mathematics/Graphic/Matrix44.cs b/StgSharp.Common/Mathematics/Graphic/Matrix44.cs
index 683cfec8..0db3ef2c 100644
--- a/StgSharp.Common/Mathematics/Graphic/Matrix44.cs
+++ b/StgSharp.Common/Mathematics/Graphic/Matrix44.cs
@@ -1,38 +1,32 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="Matrix44.cs"
+// file="Matrix44"
// 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 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.
+// 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 StgSharp.Internal.Intrinsic;
using StgSharp.Mathematics.Numeric;
using System;
-using System.ComponentModel;
-using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -40,17 +34,17 @@ using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[StructLayout(LayoutKind.Explicit, Size = 2 * 16 * sizeof(float) + sizeof(bool), Pack = 16)]
- public unsafe struct Matrix44 : IEquatable, IMatrix
+ public unsafe struct Matrix44 : IEquatable, IMatrix
{
[FieldOffset(2 * 64 * sizeof(float))] internal bool isTransposed;
[FieldOffset(0)] internal ColumnSet4 mat;
[FieldOffset(16 * sizeof(float))] internal ColumnSet4 transpose;
- [FieldOffset(0 * sizeof(float))] internal Vec4 colum0;
- [FieldOffset(4 * sizeof(float))] internal Vec4 colum1;
- [FieldOffset(8 * sizeof(float))] internal Vec4 colum2;
- [FieldOffset(12 * sizeof(float))] internal Vec4 colum3;
+ [FieldOffset(0 * sizeof(float))] internal Vec4 column0;
+ [FieldOffset(4 * sizeof(float))] internal Vec4 column1;
+ [FieldOffset(8 * sizeof(float))] internal Vec4 column2;
+ [FieldOffset(12 * sizeof(float))] internal Vec4 column3;
internal Matrix44(ColumnSet4 mat)
{
@@ -161,6 +155,11 @@ namespace StgSharp.Mathematics.Graphic
public static Matrix44 ZNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
+ public ReadOnlySpan AsSpan()
+ {
+ return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref mat), 16);
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe float Det()
{
@@ -208,9 +207,13 @@ namespace StgSharp.Mathematics.Graphic
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(Matrix44 left, Matrix44 right)
{
- return new Matrix44(
- right.mat.colum0 - left.mat.colum0, right.mat.colum1 - left.mat.colum1,
- right.mat.colum2 - left.mat.colum2, right.mat.colum3 - left.mat.colum3);
+ return new Matrix44
+ {
+ column0 = right.column0 - left.column0,
+ column1 = right.column1 - left.column1,
+ column2 = right.column2 - left.column2,
+ column3 = right.column3 - left.column3
+ };
}
public static bool operator !=(Matrix44 left, Matrix44 right)
@@ -255,9 +258,13 @@ namespace StgSharp.Mathematics.Graphic
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(Matrix44 left, Matrix44 right)
{
- Matrix44 ret = new Matrix44();
-
- // InternalIO.Intrinsic.add_mat_4(&left.mat, &right.mat, &ret.mat);
+ Matrix44 ret = new()
+ {
+ column0 = left.column0 + right.column0,
+ column1 = left.column1 + right.column1,
+ column2 = left.column2 + right.column2,
+ column3 = left.column3 + right.column3
+ };
return ret;
}
diff --git a/StgSharp.Common/Mathematics/Graphic/Vec2.cs b/StgSharp.Common/Mathematics/Graphic/Vec2.cs
index 8abc3e3f..8e90924f 100644
--- a/StgSharp.Common/Mathematics/Graphic/Vec2.cs
+++ b/StgSharp.Common/Mathematics/Graphic/Vec2.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="Vec2.cs"
+// file="Vec2"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -86,6 +83,7 @@ namespace StgSharp.Mathematics.Graphic
public static Vec2 One
{
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(1, 1);
}
diff --git a/StgSharp.Common/Mathematics/Graphic/Vec3.cs b/StgSharp.Common/Mathematics/Graphic/Vec3.cs
index 49876363..4cf53d02 100644
--- a/StgSharp.Common/Mathematics/Graphic/Vec3.cs
+++ b/StgSharp.Common/Mathematics/Graphic/Vec3.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="Vec3.cs"
+// file="Vec3"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -55,10 +52,7 @@ namespace StgSharp.Mathematics.Graphic
internal Vec3(M128 vector)
{
- Unsafe.SkipInit(out X);
- Unsafe.SkipInit(out Y);
- Unsafe.SkipInit(out Z);
- Unsafe.SkipInit(out v);
+ Unsafe.SkipInit(out this);
reg = vector;
reg.Member(3) = 0;
}
@@ -75,49 +69,42 @@ namespace StgSharp.Mathematics.Graphic
public Vec3(Vec2 xy, float z)
{
- Unsafe.SkipInit(out X);
- Unsafe.SkipInit(out Y);
- Unsafe.SkipInit(out Z);
- Unsafe.SkipInit(out v);
+ Unsafe.SkipInit(out this);
reg = xy.reg;
Z = z;
}
public Vec3(float x, float y, float z)
{
- Unsafe.SkipInit(out reg);
- Unsafe.SkipInit(out X);
- Unsafe.SkipInit(out Y);
- Unsafe.SkipInit(out Z);
+ Unsafe.SkipInit(out this);
v = new Vector3(x, y, z);
}
public unsafe Vec2 XY
{
- get => new Vec2(reg);
- set
- {
- fixed (Vector3* vptr = &v) {
- *(Vector2*)vptr = value.v;
- }
- }
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ readonly get => new(reg);
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ set => Unsafe.As(ref v) = value.v;
}
public Vec3 XYZ
{
- get => new(v);
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ readonly get => new(v);
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
set => this = value;
}
- public static Vec3 Zero => new Vec3(0, 0, 0);
+ public static Vec3 Zero => new(0, 0, 0);
- public static Vec3 One => new Vec3(1, 1, 1);
+ public static Vec3 One => new(1, 1, 1);
- public static Vec3 UnitX => new Vec3(1, 0, 0);
+ public static Vec3 UnitX => new(1, 0, 0);
- public static Vec3 UnitY => new Vec3(0, 1, 0);
+ public static Vec3 UnitY => new(0, 1, 0);
- public static Vec3 UnitZ => new Vec3(0, 0, 1);
+ public static Vec3 UnitZ => new(0, 0, 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 Cross(Vec3 left, Vec3 right)
@@ -126,30 +113,24 @@ namespace StgSharp.Mathematics.Graphic
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public float Dot(Vec3 right)
+ public readonly float Dot(Vec3 right)
{
return Vector3.Dot(v, right.v);
}
- public bool Equals(Vec3 other)
+ public readonly bool Equals(Vec3 other)
{
return reg == other.reg;
}
- public override bool Equals(object obj)
+ public override readonly bool Equals(object obj)
{
- if (obj is Vec3)
- {
- return this == (Vec3)obj;
- } else
- {
- return false;
- }
+ return obj is not null && obj is Vec3 vec && this == vec;
}
public static Vec3 FromSpan(ReadOnlySpan span)
{
- Vec3 v = new Vec3();
+ Vec3 v = new();
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
return v;
}
@@ -169,7 +150,7 @@ namespace StgSharp.Mathematics.Graphic
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public float GetMaxValue()
+ public readonly float GetMaxValue()
{
float ret = X;
ret = ret > Y ? ret : Y;
@@ -182,12 +163,12 @@ namespace StgSharp.Mathematics.Graphic
return Cross(left, right) == Zero;
}
- public override string ToString()
+ public override readonly string ToString()
{
return $"[{X},{Y},{Z}]";
}
- public string ToString(string sample)
+ public readonly string ToString(string sample)
{
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
}
@@ -243,8 +224,7 @@ namespace StgSharp.Mathematics.Graphic
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe implicit operator Vec3((float, float, float) tuple)
{
- Vec3 vec = new Vec3(*(Vector3*)&tuple);
- return vec;
+ return Unsafe.As<(float,float,float), Vec3>(ref tuple);
}
IEnumerator IEnumerable.GetEnumerator()
diff --git a/StgSharp.Common/Mathematics/Linear/IMatrix.cs b/StgSharp.Common/Mathematics/Linear/IMatrix.cs
index 0730cc93..e19a0f81 100644
--- a/StgSharp.Common/Mathematics/Linear/IMatrix.cs
+++ b/StgSharp.Common/Mathematics/Linear/IMatrix.cs
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="IMatrix.cs"
+// file="IMatrix"
// 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 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -32,9 +29,16 @@ using StgSharp.Graphics;
using System;
using System.Collections.Generic;
+using System.Numerics;
using System.Text;
namespace StgSharp.Mathematics.Numeric
{
- public interface IMatrix where TSelf: IMatrix { }
+ public interface IMatrix where TElement: unmanaged, INumber
+
+ {
+
+ ReadOnlySpan AsSpan();
+
+ }
}
diff --git a/StgSharp.Common/Threading/SamplingAdaptiveBackoff.cs b/StgSharp.Common/Threading/SamplingAdaptiveBackoff.cs
index b52052fa..71e7994c 100644
--- a/StgSharp.Common/Threading/SamplingAdaptiveBackoff.cs
+++ b/StgSharp.Common/Threading/SamplingAdaptiveBackoff.cs
@@ -5,26 +5,23 @@
// 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.
+// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -39,7 +36,7 @@ using System.Threading;
///
public struct SamplingAdaptiveBackoff
{
-
+ /*
private const int CONFIDENCE_THRESHOLD = 3; // Requires N consecutive sampling confirmations to switch strategies
// Configuration constants
@@ -455,5 +452,6 @@ public struct SamplingAdaptiveBackoff
}
}
+ /**/
}
\ No newline at end of file
diff --git a/StgSharp.Common/Threading/SingleConsumerBell.cs b/StgSharp.Common/Threading/SingleConsumerBell.cs
new file mode 100644
index 00000000..2c6ac362
--- /dev/null
+++ b/StgSharp.Common/Threading/SingleConsumerBell.cs
@@ -0,0 +1,175 @@
+//-----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+// file="SingleConsumerBell"
+// 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.Threading;
+
+namespace StgSharp.Threading
+{
+ ///
+ /// A single-consumer bell/signal primitive.
+ ///
+ ///
+ /// Producers can call multiple times to publish signals. The bell
+ /// keeps a pending count of not-yet-consumed signals. The consumer uses or to consume
+ /// signals. When a producer increments the pending count from 0 to 1, the
+ /// internal event is set to wake the consumer. Consuming via
+ /// or clears the entire pending count
+ /// (aggregate consumption). If new signals arrive while resetting, the event is set again to
+ /// avoid missed signals. Ownership: only the specified owner can call
+ /// the consumer APIs.
+ ///
+ public sealed class SingleConsumerBell : IDisposable
+ {
+
+ private volatile bool _disposed;
+ private int _pending; // not volatile: used with Interlocked/Volatile APIs to avoid CS0420
+ private readonly ManualResetEventSlim _evt;
+ private readonly object _owner;
+
+ ///
+ /// Initializes a new instance of .
+ ///
+ ///
+ /// The unique consumer identity; subsequent calls to Wait/TryWait must pass the same
+ /// reference.
+ ///
+ ///
+ /// Spin count passed to the underlying .
+ ///
+ ///
+ /// Thrown if is null.
+ ///
+ public SingleConsumerBell(object owner, int spinCount = 100)
+ {
+ _owner = owner ?? throw new ArgumentNullException(nameof(owner));
+ _evt = new ManualResetEventSlim(false, spinCount);
+ }
+
+ ///
+ /// Disposes the bell, sets the event to wake any waiting consumer, and releases
+ /// resources.
+ ///
+ public void Dispose()
+ {
+ _disposed = true;
+ _evt.Set();
+ _evt.Dispose();
+ }
+
+ ///
+ /// Publishes a signal by incrementing the not-yet-consumed count and returns the current
+ /// pending count.
+ ///
+ ///
+ /// If the pending count transitions from 0 to 1, the event is set to wake the consumer.
+ ///
+ ///
+ /// The current pending count including this call.
+ ///
+ public int Set()
+ {
+ if (_disposed) {
+ return Volatile.Read(ref _pending);
+ }
+
+ // Increment pending; if we transitioned 0 -> 1, set the event to wake the consumer
+ int newCount = Interlocked.Increment(ref _pending);
+ if (newCount == 1) {
+ _evt.Set();
+ }
+ return newCount;
+ }
+
+ ///
+ /// Attempts to consume any pending signals without blocking.
+ ///
+ ///
+ /// The consumer object; must be the same reference provided at construction.
+ ///
+ ///
+ /// true if a signal was available (pending or event set); otherwise
+ /// false.
+ ///
+ ///
+ /// Thrown if is not the owner.
+ ///
+ public bool TryWait(object consumer)
+ {
+ if (!ReferenceEquals(_owner, consumer)) {
+ throw new InvalidOperationException("This bell is owned by another consumer.");
+ }
+
+ if (!_evt.IsSet && Volatile.Read(ref _pending) == 0) {
+ return false;
+ }
+
+ // Aggregate consumption: clear pending and reset event
+ _ = Interlocked.Exchange(ref _pending, 0);
+ _evt.Reset();
+
+ // If concurrent Set happened during reset, re-set the event to avoid lost signals
+ if (Volatile.Read(ref _pending) != 0)
+ {
+ _evt.Set();
+ }
+ return true;
+ }
+
+ ///
+ /// Blocks until a signal is available, then consumes all pending signals.
+ ///
+ ///
+ /// The consumer object; must be the same reference provided at construction.
+ ///
+ ///
+ /// A token to cancel the wait.
+ ///
+ ///
+ /// Thrown if is not the owner.
+ ///
+ public void Wait(object consumer, CancellationToken cancellationToken = default)
+ {
+ if (!ReferenceEquals(_owner, consumer)) {
+ throw new InvalidOperationException("This bell is owned by another consumer.");
+ }
+
+ _evt.Wait(cancellationToken);
+
+ // Consume this round of signals: clear pending and handle reset/concurrency
+ _ = Interlocked.Exchange(ref _pending, 0);
+
+ _evt.Reset();
+
+ if (Volatile.Read(ref _pending) != 0) {
+ _evt.Set();
+ }
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/StgSharp.Native/sn-head/sn_intrinsic.h b/StgSharp.Native/sn-head/sn_intrinsic.h
index 57dd3ca5..cd6f263f 100644
--- a/StgSharp.Native/sn-head/sn_intrinsic.h
+++ b/StgSharp.Native/sn-head/sn_intrinsic.h
@@ -1,7 +1,3 @@
-#ifdef _MSC_VER
-#pragma once
-#endif
-
#ifndef SSC_INTRIN
#define SSC_INTRIN
@@ -128,7 +124,7 @@ typedef struct sn_intrinsic {
SCALARMATARITHMETIC f32_scalar_mul;
MATARITHMETIC f32_sub;
MATTRANSPOSEPROC f32_transpose;
- VECTORNORMALIZEPROC normalize_3;
+ VECTORNORMALIZEPROC f32_normalize_3;
} sn_intrinsic;
typedef enum most_advanced_instruction {
@@ -145,4 +141,4 @@ typedef enum most_advanced_instruction {
#endif
} most_advanced_instruction;
-#endif // !SSC_INTRIN
+#endif
diff --git a/StgSharp.Native/sn-head/sn_matkernel.h b/StgSharp.Native/sn-head/sn_matkernel.h
index 15498d0f..7ad155eb 100644
--- a/StgSharp.Native/sn-head/sn_matkernel.h
+++ b/StgSharp.Native/sn-head/sn_matkernel.h
@@ -1,15 +1,15 @@
#ifndef SN_MATKERNEL
#define SN_MATKERNEL
-#include
-
#define ELEMENTSIZE(T) sizeof(T)
#define XMMCOUNT(T) sizeof(T)
#define YMMCOUNT(T) sizeof(T) / 2
#define ZMMCOUNT(T) sizeof(T) / 4
+#include
+
#define MAT_KERNEL(T) \
- union mat_kernel_##T##{ \
+ union { \
__m128 xmm[XMMCOUNT(T)]; \
__m256 ymm[YMMCOUNT(T)]; \
__m512 zmm[ZMMCOUNT(T)]; \
@@ -17,18 +17,11 @@
}
#define VEC(T) \
- union vec_##T##{ \
+ union { \
__m128 xmm[XMMCOUNT(T) / 4]; \
__m256 ymm[YMMCOUNT(T) / 4]; \
__m512 zmm[ZMMCOUNT(T) / 4]; \
T v[4]; \
}
-/*
-#undef ELEMENTSIZE
-#undef XMMCOUNT
-#undef YMMCOUNT
-#undef ZMMCOUNT
-/**/
-
#endif
diff --git a/StgSharp.Native/sn-src/load_intrinsic.c b/StgSharp.Native/sn-src/load_intrinsic.c
index 169e1af4..aad0134e 100644
--- a/StgSharp.Native/sn-src/load_intrinsic.c
+++ b/StgSharp.Native/sn-src/load_intrinsic.c
@@ -1,7 +1,6 @@
#include "StgSharpNative.h"
#include "sn_intrinsic.h"
-
#if defined(_MSC_VER) && !defined(__clang__)
#include
#include
@@ -71,7 +70,7 @@ void load_intrinsic_sse(sn_intrinsic *context)
context->f32_scalar_mul = f32_scalar_mul_sse;
context->f32_sub = (MATARITHMETIC)f32_sub_sse;
context->f32_transpose = (MATTRANSPOSEPROC)f32_transpose_sse;
- context->normalize_3 = normalize;
+ context->f32_normalize_3 = f32_normalize;
}
void load_intrinsic_avx(sn_intrinsic *context)
@@ -80,11 +79,11 @@ void load_intrinsic_avx(sn_intrinsic *context)
context->factorial_simd = factorial_simd_sse;
context->index_pair = index_pair_sse;
context->f32_add = (MATARITHMETIC)f32_add_avx;
- context->f32_fma = (void(*)(void*, void*, void*))f32_fma_sse;
+ context->f32_fma = (void (*)(void *, void *, void *))f32_fma_sse;
context->f32_scalar_mul = f32_scalar_mul_avx;
context->f32_sub = (MATARITHMETIC)f32_sub_avx;
context->f32_transpose = (MATTRANSPOSEPROC)f32_transpose_avx;
- context->normalize_3 = normalize;
+ context->f32_normalize_3 = f32_normalize;
}
void load_intrinsic_512(sn_intrinsic *context)
@@ -93,9 +92,9 @@ void load_intrinsic_512(sn_intrinsic *context)
context->factorial_simd = factorial_simd_sse;
context->index_pair = index_pair_sse;
context->f32_add = (MATARITHMETIC)f32_add_512;
- context->f32_fma = (void(*)(void*, void*, void*))f32_fma_sse;
+ context->f32_fma = (void (*)(void *, void *, void *))f32_fma_sse;
context->f32_scalar_mul = f32_scalar_mul_512;
context->f32_sub = (MATARITHMETIC)f32_sub_512;
context->f32_transpose = (MATTRANSPOSEPROC)f32_transpose_512;
- context->normalize_3 = normalize;
+ context->f32_normalize_3 = f32_normalize;
}
diff --git a/StgSharp.Native/sn-src/vector.c b/StgSharp.Native/sn-src/vector.c
index 3a25a09b..3cacaaca 100644
--- a/StgSharp.Native/sn-src/vector.c
+++ b/StgSharp.Native/sn-src/vector.c
@@ -1,7 +1,7 @@
#include "sn_intrinsic.h"
#include "sn_internal.h"
-SN_API void SN_DECL normalize(VEC(float) *restrict source, VEC(float) *restrict target)
+INTERNAL void SN_DECL f32_normalize(VEC(float) *restrict source, VEC(float) *restrict target)
{
register __m128 s = _mm_load_ps(&source->xmm);
register __m128 a_length_sq = _mm_mul_ps(s, s);