Normalize all existing files to LF
This commit is contained in:
@@ -1,280 +1,280 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix44"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit, Size = (16 * sizeof(float)), Pack = 16)]
|
||||
public unsafe struct Matrix44 : IEquatable<Matrix44>
|
||||
{
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix44"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit, Size = (16 * sizeof(float)), Pack = 16)]
|
||||
public unsafe struct Matrix44 : IEquatable<Matrix44>
|
||||
{
|
||||
|
||||
[FieldOffset(0 * sizeof(float))] internal Column column;
|
||||
|
||||
[FieldOffset(0)] internal ColumnSet4 mat;
|
||||
[FieldOffset(0)] internal MatrixKernel<float> kernel;
|
||||
|
||||
internal Matrix44(
|
||||
ColumnSet4 mat
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
this.mat = mat;
|
||||
}
|
||||
|
||||
internal Matrix44(
|
||||
Vector4 vec0,
|
||||
Vector4 vec1,
|
||||
Vector4 vec2,
|
||||
Vector4 vec3
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
mat.colum0 = vec0;
|
||||
mat.colum1 = vec1;
|
||||
mat.colum2 = vec2;
|
||||
mat.colum3 = vec3;
|
||||
}
|
||||
|
||||
public Matrix44()
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
}
|
||||
|
||||
public unsafe Matrix44(
|
||||
float a00,
|
||||
float a01,
|
||||
float a02,
|
||||
float a03,
|
||||
float a10,
|
||||
float a11,
|
||||
float a12,
|
||||
float a13,
|
||||
float a20,
|
||||
float a21,
|
||||
float a22,
|
||||
float a23,
|
||||
float a30,
|
||||
float a31,
|
||||
float a32,
|
||||
float a33
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
mat.colum0 = new Vector4(a00, a10, a20, a30);
|
||||
mat.colum1 = new Vector4(a01, a11, a21, a31);
|
||||
mat.colum2 = new Vector4(a02, a12, a22, a32);
|
||||
mat.colum3 = new Vector4(a03, a13, a23, a33);
|
||||
}
|
||||
|
||||
public unsafe float this[
|
||||
int rowNum,
|
||||
int columNum
|
||||
]
|
||||
{
|
||||
get
|
||||
[FieldOffset(0)] internal ColumnSet4 mat;
|
||||
[FieldOffset(0)] internal MatrixKernel<float> kernel;
|
||||
|
||||
internal Matrix44(
|
||||
ColumnSet4 mat
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
this.mat = mat;
|
||||
}
|
||||
|
||||
internal Matrix44(
|
||||
Vector4 vec0,
|
||||
Vector4 vec1,
|
||||
Vector4 vec2,
|
||||
Vector4 vec3
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
mat.colum0 = vec0;
|
||||
mat.colum1 = vec1;
|
||||
mat.colum2 = vec2;
|
||||
mat.colum3 = vec3;
|
||||
}
|
||||
|
||||
public Matrix44()
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
}
|
||||
|
||||
public unsafe Matrix44(
|
||||
float a00,
|
||||
float a01,
|
||||
float a02,
|
||||
float a03,
|
||||
float a10,
|
||||
float a11,
|
||||
float a12,
|
||||
float a13,
|
||||
float a20,
|
||||
float a21,
|
||||
float a22,
|
||||
float a23,
|
||||
float a30,
|
||||
float a31,
|
||||
float a32,
|
||||
float a33
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
mat.colum0 = new Vector4(a00, a10, a20, a30);
|
||||
mat.colum1 = new Vector4(a01, a11, a21, a31);
|
||||
mat.colum2 = new Vector4(a02, a12, a22, a32);
|
||||
mat.colum3 = new Vector4(a03, a13, a23, a33);
|
||||
}
|
||||
|
||||
public unsafe float this[
|
||||
int rowNum,
|
||||
int columNum
|
||||
]
|
||||
{
|
||||
get
|
||||
{
|
||||
#if DEBUG
|
||||
if (rowNum is > 3 or < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
if (columNum is > 3 or < 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
if (rowNum is > 3 or < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
if (columNum is > 3 or < 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
#endif
|
||||
return kernel[rowNum + 4 * columNum];
|
||||
}
|
||||
set
|
||||
return kernel[rowNum + 4 * columNum];
|
||||
}
|
||||
set
|
||||
{
|
||||
#if DEBUG
|
||||
if (rowNum is > 3 or < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
if (columNum is > 3 or < 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
if (rowNum is > 3 or < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
if (columNum is > 3 or < 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(columNum));
|
||||
}
|
||||
#endif
|
||||
kernel[rowNum + 4 * columNum] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix44 Transpose
|
||||
{
|
||||
get
|
||||
{
|
||||
Matrix44 transpose = new();
|
||||
InternalTranspose(ref transpose);
|
||||
return transpose;
|
||||
}
|
||||
}
|
||||
|
||||
public static Matrix44 Unit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
||||
|
||||
public static Matrix44 WNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, -1);
|
||||
|
||||
public static Matrix44 XNegativeUnit => new Matrix44(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public static Matrix44 YNegativeUnit => new Matrix44(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public static Matrix44 ZNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public ReadOnlySpan<float> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ColumnSet4, float>(ref mat), 16);
|
||||
}
|
||||
|
||||
public override bool Equals(
|
||||
object? obj
|
||||
)
|
||||
{
|
||||
return obj is Matrix44 x && Equals(x);
|
||||
}
|
||||
|
||||
public bool Equals(
|
||||
Matrix44 other
|
||||
)
|
||||
{
|
||||
return mat.Equals(other.mat);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(mat.colum0.GetHashCode(), mat.colum1.GetHashCode(),
|
||||
mat.colum2.GetHashCode(), mat.colum3.GetHashCode());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe void InternalTranspose(
|
||||
ref Matrix44 transpose
|
||||
)
|
||||
{
|
||||
const int f32 = 0;
|
||||
fixed (MatrixKernel<float>* source = &this.kernel, target = &transpose.kernel) {
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Transpose,
|
||||
null, (MatrixKernel*)source,
|
||||
(MatrixKernel*)target, 0);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator -(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Sub,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 1);
|
||||
return ret;
|
||||
kernel[rowNum + 4 * columNum] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
return !(left == right);
|
||||
public Matrix44 Transpose
|
||||
{
|
||||
get
|
||||
{
|
||||
Matrix44 transpose = new();
|
||||
InternalTranspose(ref transpose);
|
||||
return transpose;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator *(
|
||||
Matrix44 mat,
|
||||
float value
|
||||
)
|
||||
{
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
return new Matrix44(mat.mat.colum0 * value, mat.mat.colum1 * value,
|
||||
mat.mat.colum2 * value, mat.mat.colum3 * value);
|
||||
public static Matrix44 Unit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
|
||||
|
||||
public static Matrix44 WNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, -1);
|
||||
|
||||
public static Matrix44 XNegativeUnit => new Matrix44(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public static Matrix44 YNegativeUnit => new Matrix44(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public static Matrix44 ZNegativeUnit => new Matrix44(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0,
|
||||
0, 0, 1);
|
||||
|
||||
public ReadOnlySpan<float> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ColumnSet4, float>(ref mat), 16);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix44 operator *(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Fma,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 0);
|
||||
return ret;
|
||||
public override bool Equals(
|
||||
object? obj
|
||||
)
|
||||
{
|
||||
return obj is Matrix44 x && Equals(x);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator +(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Add,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 1);
|
||||
return ret;
|
||||
public bool Equals(
|
||||
Matrix44 other
|
||||
)
|
||||
{
|
||||
return mat.Equals(other.mat);
|
||||
}
|
||||
|
||||
public static bool operator ==(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
return left.Equals(right);
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(mat.colum0.GetHashCode(), mat.colum1.GetHashCode(),
|
||||
mat.colum2.GetHashCode(), mat.colum3.GetHashCode());
|
||||
}
|
||||
|
||||
[InlineArray(4)]
|
||||
internal struct Column
|
||||
{
|
||||
|
||||
private Vec4 value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe void InternalTranspose(
|
||||
ref Matrix44 transpose
|
||||
)
|
||||
{
|
||||
const int f32 = 0;
|
||||
fixed (MatrixKernel<float>* source = &this.kernel, target = &transpose.kernel) {
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Transpose,
|
||||
null, (MatrixKernel*)source,
|
||||
(MatrixKernel*)target, 0);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator -(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Sub,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator *(
|
||||
Matrix44 mat,
|
||||
float value
|
||||
)
|
||||
{
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
return new Matrix44(mat.mat.colum0 * value, mat.mat.colum1 * value,
|
||||
mat.mat.colum2 * value, mat.mat.colum3 * value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix44 operator *(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Fma,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Matrix44 operator +(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
Matrix44 ret = new();
|
||||
int f32 = MatrixElementType.F32.IntrinsicNode;
|
||||
NumericalModule.GlobalContext.mat_sk[f32].Call(MatrixIntrinsicHandle.Add,
|
||||
(MatrixKernel*)&left,
|
||||
(MatrixKernel*)&right,
|
||||
(MatrixKernel*)&ret, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static bool operator ==(
|
||||
Matrix44 left,
|
||||
Matrix44 right
|
||||
)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
[InlineArray(4)]
|
||||
internal struct Column
|
||||
{
|
||||
|
||||
private Vec4 value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,162 +1,162 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec2"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec2"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
/// <summary>
|
||||
/// A two dimension vector defined by two elements. Vec2 in World are default used as colum
|
||||
/// vector.
|
||||
/// <summary>
|
||||
/// A two dimension vector defined by two elements. Vec2 in World are default used as colum
|
||||
/// vector.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public struct Vec2 : IUnmanagedVector<Vec2>
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public struct Vec2 : IUnmanagedVector<Vec2>
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal unsafe fixed float num[2];
|
||||
|
||||
[FieldOffset(0)]
|
||||
[FieldOffset(0)]
|
||||
internal M128 reg;
|
||||
|
||||
[FieldOffset(0)]
|
||||
[FieldOffset(0)]
|
||||
internal Vector2 v;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public float X;
|
||||
[FieldOffset(4)]
|
||||
public float Y;
|
||||
|
||||
internal Vec2(Vector2 vec)
|
||||
{
|
||||
Unsafe.SkipInit(out reg);
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
v = vec;
|
||||
}
|
||||
|
||||
internal Vec2(M128 vec)
|
||||
{
|
||||
Unsafe.SkipInit(out reg);
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
reg = vec;
|
||||
reg.Member<ulong>(1) = 0;
|
||||
}
|
||||
|
||||
public Vec2(float x, float y)
|
||||
{
|
||||
v = new Vector2(x, y);
|
||||
}
|
||||
|
||||
public static Vec2 Unit => new Vec2(1, 1);
|
||||
|
||||
public static Vec2 Zero => new Vec2(0, 0);
|
||||
|
||||
public static Vec2 One
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec2(1, 1);
|
||||
}
|
||||
|
||||
public Vec2 XY
|
||||
{
|
||||
readonly get => this;
|
||||
set => this = value;
|
||||
}
|
||||
|
||||
public Vec3 XYZ
|
||||
{
|
||||
readonly get => new(reg);
|
||||
set
|
||||
{
|
||||
reg = value.reg;
|
||||
reg.Member<ulong>(1) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public float Cross(Vec2 right)
|
||||
{
|
||||
return X * right.Y - Y * right.X;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public float Dot(Vec2 vec)
|
||||
{
|
||||
return Vector2.Dot(v, vec.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec2 operator -(Vec2 left, Vec2 right)
|
||||
{
|
||||
return new Vec2(left.v - right.v);
|
||||
[FieldOffset(0)]
|
||||
public float X;
|
||||
[FieldOffset(4)]
|
||||
public float Y;
|
||||
|
||||
internal Vec2(Vector2 vec)
|
||||
{
|
||||
Unsafe.SkipInit(out reg);
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
v = vec;
|
||||
}
|
||||
|
||||
public static bool operator !=(Vec2 left, Vec2 right)
|
||||
{
|
||||
return !(left == right);
|
||||
internal Vec2(M128 vec)
|
||||
{
|
||||
Unsafe.SkipInit(out reg);
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
reg = vec;
|
||||
reg.Member<ulong>(1) = 0;
|
||||
}
|
||||
|
||||
public static Vec2 operator *(Vec2 vec, float value)
|
||||
{
|
||||
return new Vec2(vec.v * value);
|
||||
}
|
||||
public static Vec2 operator *(float value, Vec2 vec)
|
||||
{
|
||||
return new Vec2(vec.v * value);
|
||||
public Vec2(float x, float y)
|
||||
{
|
||||
v = new Vector2(x, y);
|
||||
}
|
||||
|
||||
public static Vec2 operator /(Vec2 vec, float value)
|
||||
{
|
||||
return new Vec2(vec.v / value);
|
||||
public static Vec2 Unit => new Vec2(1, 1);
|
||||
|
||||
public static Vec2 Zero => new Vec2(0, 0);
|
||||
|
||||
public static Vec2 One
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec2(1, 1);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec2 operator +(Vec2 left, Vec2 right)
|
||||
{
|
||||
return new Vec2(left.v + right.v);
|
||||
public Vec2 XY
|
||||
{
|
||||
readonly get => this;
|
||||
set => this = value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Vec2 left, Vec2 right)
|
||||
{
|
||||
return left.v == right.v;
|
||||
public Vec3 XYZ
|
||||
{
|
||||
readonly get => new(reg);
|
||||
set
|
||||
{
|
||||
reg = value.reg;
|
||||
reg.Member<ulong>(1) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator Vec2((float, float) tuple)
|
||||
{
|
||||
return new Vec2(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public float Cross(Vec2 right)
|
||||
{
|
||||
return X * right.Y - Y * right.X;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public float Dot(Vec2 vec)
|
||||
{
|
||||
return Vector2.Dot(v, vec.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec2 operator -(Vec2 left, Vec2 right)
|
||||
{
|
||||
return new Vec2(left.v - right.v);
|
||||
}
|
||||
|
||||
public static bool operator !=(Vec2 left, Vec2 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static Vec2 operator *(Vec2 vec, float value)
|
||||
{
|
||||
return new Vec2(vec.v * value);
|
||||
}
|
||||
public static Vec2 operator *(float value, Vec2 vec)
|
||||
{
|
||||
return new Vec2(vec.v * value);
|
||||
}
|
||||
|
||||
public static Vec2 operator /(Vec2 vec, float value)
|
||||
{
|
||||
return new Vec2(vec.v / value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec2 operator +(Vec2 left, Vec2 right)
|
||||
{
|
||||
return new Vec2(left.v + right.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Vec2 left, Vec2 right)
|
||||
{
|
||||
return left.v == right.v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator Vec2((float, float) tuple)
|
||||
{
|
||||
return new Vec2(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,236 +1,236 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec3"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[CollectionBuilder(typeof(Vec3), nameof(FromSpan))]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public struct Vec3 : IEquatable<Vec3>, IUnmanagedVector<Vec3>, IEnumerable<float>
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal unsafe fixed float num[3];
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec3"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[CollectionBuilder(typeof(Vec3), nameof(FromSpan))]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public struct Vec3 : IEquatable<Vec3>, IUnmanagedVector<Vec3>, IEnumerable<float>
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal unsafe fixed float num[3];
|
||||
[FieldOffset(0)] internal M128 reg;
|
||||
|
||||
[FieldOffset(0)] internal Vector3 v;
|
||||
|
||||
[FieldOffset(0)] public float X;
|
||||
[FieldOffset(4)] public float Y;
|
||||
[FieldOffset(8)] public float Z;
|
||||
|
||||
internal Vec3(M128 vector)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
reg = vector;
|
||||
reg.Member<uint>(3) = 0;
|
||||
}
|
||||
|
||||
internal Vec3(Vector3 vector)
|
||||
{
|
||||
v = vector;
|
||||
}
|
||||
|
||||
public Vec3()
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
}
|
||||
|
||||
public Vec3(Vec2 xy, float z)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
reg = xy.reg;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
public Vec3(float x, float y, float z)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
v = new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
public unsafe Vec2 XY
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get => new(reg);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => Unsafe.As<Vector3, Vector2>(ref v) = value.v;
|
||||
}
|
||||
|
||||
public Vec3 XYZ
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get => new(v);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => this = value;
|
||||
}
|
||||
|
||||
public static Vec3 Zero => new(0, 0, 0);
|
||||
|
||||
public static Vec3 One => new(1, 1, 1);
|
||||
|
||||
public static Vec3 UnitX => new(1, 0, 0);
|
||||
|
||||
public static Vec3 UnitY => new(0, 1, 0);
|
||||
|
||||
public static Vec3 UnitZ => new(0, 0, 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 Cross(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Linear.Cross(left, right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly float Dot(Vec3 right)
|
||||
{
|
||||
return Vector3.Dot(v, right.v);
|
||||
}
|
||||
|
||||
public readonly bool Equals(Vec3 other)
|
||||
{
|
||||
return reg == other.reg;
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object obj)
|
||||
{
|
||||
return obj is not null && obj is Vec3 vec && this == vec;
|
||||
}
|
||||
|
||||
public static Vec3 FromSpan(ReadOnlySpan<float> span)
|
||||
{
|
||||
Vec3 v = new();
|
||||
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
|
||||
return v;
|
||||
}
|
||||
|
||||
public unsafe IEnumerator<float> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < 3; i++) {
|
||||
yield return reg.Member<float>(i);
|
||||
}
|
||||
}
|
||||
|
||||
public override unsafe int GetHashCode()
|
||||
{
|
||||
fixed (float* add = &X) {
|
||||
return HashCode.Combine(X, Y, Z, (ulong)add);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly float GetMaxValue()
|
||||
{
|
||||
float ret = X;
|
||||
ret = ret > Y ? ret : Y;
|
||||
ret = ret > Z ? ret : Z;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static bool IsParallel(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Cross(left, right) == Zero;
|
||||
}
|
||||
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return $"[{X},{Y},{Z}]";
|
||||
}
|
||||
|
||||
public readonly string ToString(string sample)
|
||||
{
|
||||
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator -(Vec3 vec)
|
||||
{
|
||||
return new Vec3(-vec.v);
|
||||
[FieldOffset(0)] public float X;
|
||||
[FieldOffset(4)] public float Y;
|
||||
[FieldOffset(8)] public float Z;
|
||||
|
||||
internal Vec3(M128 vector)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
reg = vector;
|
||||
reg.Member<uint>(3) = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator -(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.v - right.v);
|
||||
internal Vec3(Vector3 vector)
|
||||
{
|
||||
v = vector;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Vec3 left, Vec3 right)
|
||||
{
|
||||
return left.v != right.v;
|
||||
public Vec3()
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator *(Vec3 vec, float value)
|
||||
{
|
||||
return new Vec3(vec.v * value);
|
||||
public Vec3(Vec2 xy, float z)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
reg = xy.reg;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float operator *(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Vector3.Dot(left.v, right.v);
|
||||
public Vec3(float x, float y, float z)
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
v = new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator /(Vec3 vec, float value)
|
||||
{
|
||||
return new Vec3(vec.v / value);
|
||||
public unsafe Vec2 XY
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get => new(reg);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => Unsafe.As<Vector3, Vector2>(ref v) = value.v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator +(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.v + right.v);
|
||||
public Vec3 XYZ
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get => new(v);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => this = value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Vec3 left, Vec3 right)
|
||||
{
|
||||
return left.v == right.v;
|
||||
public static Vec3 Zero => new(0, 0, 0);
|
||||
|
||||
public static Vec3 One => new(1, 1, 1);
|
||||
|
||||
public static Vec3 UnitX => new(1, 0, 0);
|
||||
|
||||
public static Vec3 UnitY => new(0, 1, 0);
|
||||
|
||||
public static Vec3 UnitZ => new(0, 0, 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 Cross(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Linear.Cross(left, right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe implicit operator Vec3((float, float, float) tuple)
|
||||
{
|
||||
return Unsafe.As<(float,float,float), Vec3>(ref tuple);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly float Dot(Vec3 right)
|
||||
{
|
||||
return Vector3.Dot(v, right.v);
|
||||
}
|
||||
|
||||
public readonly bool Equals(Vec3 other)
|
||||
{
|
||||
return reg == other.reg;
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object obj)
|
||||
{
|
||||
return obj is not null && obj is Vec3 vec && this == vec;
|
||||
}
|
||||
|
||||
public static Vec3 FromSpan(ReadOnlySpan<float> span)
|
||||
{
|
||||
Vec3 v = new();
|
||||
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
|
||||
return v;
|
||||
}
|
||||
|
||||
public unsafe IEnumerator<float> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < 3; i++) {
|
||||
yield return reg.Member<float>(i);
|
||||
}
|
||||
}
|
||||
|
||||
public override unsafe int GetHashCode()
|
||||
{
|
||||
fixed (float* add = &X) {
|
||||
return HashCode.Combine(X, Y, Z, (ulong)add);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly float GetMaxValue()
|
||||
{
|
||||
float ret = X;
|
||||
ret = ret > Y ? ret : Y;
|
||||
ret = ret > Z ? ret : Z;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static bool IsParallel(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Cross(left, right) == Zero;
|
||||
}
|
||||
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return $"[{X},{Y},{Z}]";
|
||||
}
|
||||
|
||||
public readonly string ToString(string sample)
|
||||
{
|
||||
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator -(Vec3 vec)
|
||||
{
|
||||
return new Vec3(-vec.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator -(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.v - right.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Vec3 left, Vec3 right)
|
||||
{
|
||||
return left.v != right.v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator *(Vec3 vec, float value)
|
||||
{
|
||||
return new Vec3(vec.v * value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float operator *(Vec3 left, Vec3 right)
|
||||
{
|
||||
return Vector3.Dot(left.v, right.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator /(Vec3 vec, float value)
|
||||
{
|
||||
return new Vec3(vec.v / value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec3 operator +(Vec3 left, Vec3 right)
|
||||
{
|
||||
return new Vec3(left.v + right.v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Vec3 left, Vec3 right)
|
||||
{
|
||||
return left.v == right.v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe implicit operator Vec3((float, float, float) tuple)
|
||||
{
|
||||
return Unsafe.As<(float,float,float), Vec3>(ref tuple);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,251 +1,251 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec4"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[CollectionBuilder(builderType:typeof(Vec4), methodName: nameof(Vec4.FromSpan))]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public unsafe struct Vec4 : IEquatable<Vec4>, IUnmanagedVector<Vec4>
|
||||
{
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Vec4"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Graphics
|
||||
{
|
||||
[CollectionBuilder(builderType:typeof(Vec4), methodName: nameof(Vec4.FromSpan))]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
|
||||
public unsafe struct Vec4 : IEquatable<Vec4>, IUnmanagedVector<Vec4>
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal unsafe fixed float num[4];
|
||||
|
||||
[FieldOffset(0)] internal M128 reg;
|
||||
|
||||
[FieldOffset(0)] internal Vector4 vec;
|
||||
[FieldOffset(0)] internal Vector4 vec;
|
||||
[FieldOffset(12)] public float W;
|
||||
|
||||
[FieldOffset(0)] public float X;
|
||||
[FieldOffset(4)] public float Y;
|
||||
[FieldOffset(8)] public float Z;
|
||||
|
||||
internal Vec4(
|
||||
Vector4 v
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
[FieldOffset(0)] public float X;
|
||||
[FieldOffset(4)] public float Y;
|
||||
[FieldOffset(8)] public float Z;
|
||||
|
||||
internal Vec4(
|
||||
Vector4 v
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
Unsafe.SkipInit(out reg);
|
||||
|
||||
vec = v;
|
||||
}
|
||||
|
||||
internal Vec4(
|
||||
M128 v
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
vec = v;
|
||||
}
|
||||
|
||||
internal Vec4(
|
||||
M128 v
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
Unsafe.SkipInit(out vec);
|
||||
|
||||
reg = v;
|
||||
}
|
||||
|
||||
public Vec4(
|
||||
Vec3 v3,
|
||||
float w
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
reg = v3.reg;
|
||||
W = w;
|
||||
}
|
||||
|
||||
public Vec4(
|
||||
float x,
|
||||
float y,
|
||||
float z,
|
||||
float w
|
||||
)
|
||||
{
|
||||
vec = new Vector4(x, y, z, w);
|
||||
}
|
||||
|
||||
public unsafe Vec2 XY
|
||||
{
|
||||
get => new Vec2(reg);
|
||||
set
|
||||
{
|
||||
X = value.X;
|
||||
Y = value.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe Vec3 XYZ
|
||||
{
|
||||
get => new Vec3(reg);
|
||||
set
|
||||
{
|
||||
float w = W;
|
||||
reg = value.reg;
|
||||
W = w;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vec4 One
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec4(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
public static Vec4 Zero
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec4(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 Add(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left + right;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<float> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref reg.Member<float>(0), 4);
|
||||
}
|
||||
|
||||
public override bool Equals(
|
||||
object obj
|
||||
)
|
||||
{
|
||||
return obj is Vec4 v && v == this;
|
||||
}
|
||||
|
||||
public static Vec4 FromSpan(
|
||||
ReadOnlySpan<float> span
|
||||
)
|
||||
{
|
||||
if (span.Length < 4) {
|
||||
throw new ArgumentException("Span length must be at least 4.", nameof(span));
|
||||
reg = v;
|
||||
}
|
||||
|
||||
public Vec4(
|
||||
Vec3 v3,
|
||||
float w
|
||||
)
|
||||
{
|
||||
Unsafe.SkipInit(out X);
|
||||
Unsafe.SkipInit(out Y);
|
||||
Unsafe.SkipInit(out Z);
|
||||
Unsafe.SkipInit(out W);
|
||||
reg = v3.reg;
|
||||
W = w;
|
||||
}
|
||||
|
||||
public Vec4(
|
||||
float x,
|
||||
float y,
|
||||
float z,
|
||||
float w
|
||||
)
|
||||
{
|
||||
vec = new Vector4(x, y, z, w);
|
||||
}
|
||||
|
||||
public unsafe Vec2 XY
|
||||
{
|
||||
get => new Vec2(reg);
|
||||
set
|
||||
{
|
||||
X = value.X;
|
||||
Y = value.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe Vec3 XYZ
|
||||
{
|
||||
get => new Vec3(reg);
|
||||
set
|
||||
{
|
||||
float w = W;
|
||||
reg = value.reg;
|
||||
W = w;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vec4 One
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec4(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
public static Vec4 Zero
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Vec4(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 Add(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left + right;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<float> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref reg.Member<float>(0), 4);
|
||||
}
|
||||
|
||||
public override bool Equals(
|
||||
object obj
|
||||
)
|
||||
{
|
||||
return obj is Vec4 v && v == this;
|
||||
}
|
||||
|
||||
public static Vec4 FromSpan(
|
||||
ReadOnlySpan<float> span
|
||||
)
|
||||
{
|
||||
if (span.Length < 4) {
|
||||
throw new ArgumentException("Span length must be at least 4.", nameof(span));
|
||||
}
|
||||
|
||||
// Read 4 floats as a Vector4 in one operation without per-element copy.
|
||||
Vector4 v = MemoryMarshal.Read<Vector4>(MemoryMarshal.AsBytes(span));
|
||||
return new Vec4(v);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<float>.Enumerator GetEnumerator()
|
||||
{
|
||||
return AsSpan().GetEnumerator();
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return vec.GetHashCode();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 Subtract(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left - right;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return vec.ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator -(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return new Vec4(left.vec - right.vec);
|
||||
Vector4 v = MemoryMarshal.Read<Vector4>(MemoryMarshal.AsBytes(span));
|
||||
return new Vec4(v);
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return !(left == right);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<float>.Enumerator GetEnumerator()
|
||||
{
|
||||
return AsSpan().GetEnumerator();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator *(
|
||||
Matrix44 mat,
|
||||
Vec4 vec
|
||||
)
|
||||
{
|
||||
Matrix44 transpose = mat.Transpose;
|
||||
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum1, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum2, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum3, vec.vec));
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return vec.GetHashCode();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator +(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return new Vec4(left.vec + right.vec);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 Subtract(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left - right;
|
||||
}
|
||||
|
||||
public static bool operator ==(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left.vec == right.vec;
|
||||
}
|
||||
|
||||
bool IEquatable<Vec4>.Equals(
|
||||
Vec4 other
|
||||
)
|
||||
{
|
||||
return vec == other.vec;
|
||||
}
|
||||
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return vec.ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator -(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return new Vec4(left.vec - right.vec);
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator *(
|
||||
Matrix44 mat,
|
||||
Vec4 vec
|
||||
)
|
||||
{
|
||||
Matrix44 transpose = mat.Transpose;
|
||||
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum1, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum2, vec.vec),
|
||||
Vector4.Dot(transpose.mat.colum3, vec.vec));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vec4 operator +(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return new Vec4(left.vec + right.vec);
|
||||
}
|
||||
|
||||
public static bool operator ==(
|
||||
Vec4 left,
|
||||
Vec4 right
|
||||
)
|
||||
{
|
||||
return left.vec == right.vec;
|
||||
}
|
||||
|
||||
bool IEquatable<Vec4>.Equals(
|
||||
Vec4 other
|
||||
)
|
||||
{
|
||||
return vec == other.vec;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,101 +1,101 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix.Create"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T>
|
||||
{
|
||||
|
||||
private const int KernelSideLength = 4;
|
||||
|
||||
public static Matrix<T> Create(
|
||||
int columnLength,
|
||||
int rowLength,
|
||||
MatrixLayout layout,
|
||||
HybridLayerSegregatedFitAllocator allocator)
|
||||
{
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix.Create"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T>
|
||||
{
|
||||
|
||||
private const int KernelSideLength = 4;
|
||||
|
||||
public static Matrix<T> Create(
|
||||
int columnLength,
|
||||
int rowLength,
|
||||
MatrixLayout layout,
|
||||
HybridLayerSegregatedFitAllocator allocator)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(allocator);
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(columnLength);
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(rowLength);
|
||||
|
||||
int kernelColumns = DivideRoundUp(columnLength, KernelSideLength);
|
||||
int kernelColumns = DivideRoundUp(columnLength, KernelSideLength);
|
||||
int kernelRows = DivideRoundUp(rowLength, KernelSideLength);
|
||||
|
||||
if (layout is MatrixLayout.UpperTriangle or MatrixLayout.LowerTriangle)
|
||||
{
|
||||
if (kernelColumns != kernelRows) {
|
||||
throw new ArgumentException("Triangular layouts require square matrices.");
|
||||
}
|
||||
if (layout is MatrixLayout.UpperTriangle or MatrixLayout.LowerTriangle)
|
||||
{
|
||||
if (kernelColumns != kernelRows) {
|
||||
throw new ArgumentException("Triangular layouts require square matrices.");
|
||||
}
|
||||
}
|
||||
|
||||
long kernelCount = layout switch
|
||||
{
|
||||
MatrixLayout.DenseRectangle => (long)kernelColumns * kernelRows,
|
||||
MatrixLayout.UpperTriangle => TriangularKernelCount(kernelColumns),
|
||||
MatrixLayout.LowerTriangle => TriangularKernelCount(kernelRows),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(layout))
|
||||
long kernelCount = layout switch
|
||||
{
|
||||
MatrixLayout.DenseRectangle => (long)kernelColumns * kernelRows,
|
||||
MatrixLayout.UpperTriangle => TriangularKernelCount(kernelColumns),
|
||||
MatrixLayout.LowerTriangle => TriangularKernelCount(kernelRows),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(layout))
|
||||
};
|
||||
|
||||
long requiredBytes = kernelCount * MatrixKernel<T>.Size;
|
||||
long requiredBytes = kernelCount * MatrixKernel<T>.Size;
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(requiredBytes, nameof(requiredBytes));
|
||||
|
||||
HybridLayerSegregatedFitAllocationHandle handle = allocator.Alloc((uint)requiredBytes);
|
||||
HybridLayerSegregatedFitAllocationHandle handle = allocator.Alloc((uint)requiredBytes);
|
||||
MatrixStorageHLSF<T> storage = new MatrixStorageHLSF<T>(allocator, handle);
|
||||
|
||||
Matrix<T> matrix = new Matrix<T>(MatrixKernelBoarder.Create(layout), new MatrixSize(columnLength, rowLength,
|
||||
kernelColumns,
|
||||
Matrix<T> matrix = new Matrix<T>(MatrixKernelBoarder.Create(layout), new MatrixSize(columnLength, rowLength,
|
||||
kernelColumns,
|
||||
kernelRows), storage);
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int DivideRoundUp(int value, int divisor)
|
||||
{
|
||||
return (value + (divisor - 1)) / divisor;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static long TriangularKernelCount(int order)
|
||||
{
|
||||
long n = order;
|
||||
return (n * (n + 1L)) / 2L;
|
||||
}
|
||||
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int DivideRoundUp(int value, int divisor)
|
||||
{
|
||||
return (value + (divisor - 1)) / divisor;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static long TriangularKernelCount(int order)
|
||||
{
|
||||
long n = order;
|
||||
return (n * (n + 1L)) / 2L;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,204 +1,204 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T> where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private protected static readonly int t_count = 16 / sizeof(T);
|
||||
|
||||
private protected static readonly int t_size = sizeof(T);
|
||||
private readonly MatrixKernelBoarder _boarder;
|
||||
private readonly MatrixSize _size;
|
||||
private readonly MatrixStorage<T> _buffer;
|
||||
|
||||
private protected Matrix(
|
||||
MatrixKernelBoarder boarder,
|
||||
MatrixSize size,
|
||||
MatrixStorage<T> buffer
|
||||
|
||||
)
|
||||
{
|
||||
_boarder = boarder;
|
||||
_size = size;
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
public T this[
|
||||
int x,
|
||||
int y
|
||||
]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return T.Zero;
|
||||
}
|
||||
return ((T*)Buffer)[offset];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return;
|
||||
}
|
||||
((T*)Buffer)[offset] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matlab style indexer to generate a new matrix
|
||||
/// </summary>
|
||||
/// <param name="column">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="row">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
/// <exception cref="NotImplementedException">
|
||||
///
|
||||
/// </exception>
|
||||
public unsafe Matrix<T> this[
|
||||
ReadOnlySpan<MatrixIndex> column,
|
||||
ReadOnlySpan<MatrixIndex> row
|
||||
]
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in columns.
|
||||
/// </summary>
|
||||
public int ColumnLength => _size.ElementColumnLength;
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in rows.
|
||||
/// </summary>
|
||||
public int RowLength => _size.ElementRowLength;
|
||||
|
||||
public MatrixLayout Layout { get; init; }
|
||||
|
||||
internal MatrixKernel<T>* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (MatrixKernel<T>*)_buffer.BufferPointer;
|
||||
}
|
||||
|
||||
protected internal int KernelColumnLength => _size.KernelColumnLength;
|
||||
|
||||
protected internal int KernelRowLength => _size.KernelRowLength;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe ref MatrixKernel<T> GetMatrixKernel(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return ref _boarder.IsInBuffer(x, y, in _size) ?
|
||||
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
|
||||
ref Unsafe.NullRef<MatrixKernel<T>>();
|
||||
}
|
||||
|
||||
internal long GetElementOffset(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
int
|
||||
k_c = y / 4,
|
||||
c = y % 4,
|
||||
k_r = x / 4,
|
||||
r = x % 4;
|
||||
if (!_boarder.IsInBuffer(k_r, k_c, in _size)) {
|
||||
return -1;
|
||||
}
|
||||
long kernelOffset = _boarder.GetIndexOffset(k_r, k_c, in _size);
|
||||
|
||||
return kernelOffset * 16 + 4 * r + c;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref T GetElementUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
|
||||
ref MatrixKernel<T> kernel = ref _buffer[_boarder.GetIndexOffset(k_r, k_c, in _size)];
|
||||
return ref kernel[c, r];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe MatrixKernel<T>* GetKernelAddressUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return Buffer + _boarder.GetIndexOffset(x, y, in _size);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref MatrixKernel<T> GetKernelUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
return ref _buffer[_boarder.GetIndexOffset(k_c, k_r, in _size)];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T> where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private protected static readonly int t_count = 16 / sizeof(T);
|
||||
|
||||
private protected static readonly int t_size = sizeof(T);
|
||||
private readonly MatrixKernelBoarder _boarder;
|
||||
private readonly MatrixSize _size;
|
||||
private readonly MatrixStorage<T> _buffer;
|
||||
|
||||
private protected Matrix(
|
||||
MatrixKernelBoarder boarder,
|
||||
MatrixSize size,
|
||||
MatrixStorage<T> buffer
|
||||
|
||||
)
|
||||
{
|
||||
_boarder = boarder;
|
||||
_size = size;
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
public T this[
|
||||
int x,
|
||||
int y
|
||||
]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return T.Zero;
|
||||
}
|
||||
return ((T*)Buffer)[offset];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return;
|
||||
}
|
||||
((T*)Buffer)[offset] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matlab style indexer to generate a new matrix
|
||||
/// </summary>
|
||||
/// <param name="column">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="row">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
/// <exception cref="NotImplementedException">
|
||||
///
|
||||
/// </exception>
|
||||
public unsafe Matrix<T> this[
|
||||
ReadOnlySpan<MatrixIndex> column,
|
||||
ReadOnlySpan<MatrixIndex> row
|
||||
]
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in columns.
|
||||
/// </summary>
|
||||
public int ColumnLength => _size.ElementColumnLength;
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in rows.
|
||||
/// </summary>
|
||||
public int RowLength => _size.ElementRowLength;
|
||||
|
||||
public MatrixLayout Layout { get; init; }
|
||||
|
||||
internal MatrixKernel<T>* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (MatrixKernel<T>*)_buffer.BufferPointer;
|
||||
}
|
||||
|
||||
protected internal int KernelColumnLength => _size.KernelColumnLength;
|
||||
|
||||
protected internal int KernelRowLength => _size.KernelRowLength;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe ref MatrixKernel<T> GetMatrixKernel(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return ref _boarder.IsInBuffer(x, y, in _size) ?
|
||||
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
|
||||
ref Unsafe.NullRef<MatrixKernel<T>>();
|
||||
}
|
||||
|
||||
internal long GetElementOffset(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
int
|
||||
k_c = y / 4,
|
||||
c = y % 4,
|
||||
k_r = x / 4,
|
||||
r = x % 4;
|
||||
if (!_boarder.IsInBuffer(k_r, k_c, in _size)) {
|
||||
return -1;
|
||||
}
|
||||
long kernelOffset = _boarder.GetIndexOffset(k_r, k_c, in _size);
|
||||
|
||||
return kernelOffset * 16 + 4 * r + c;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref T GetElementUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
|
||||
ref MatrixKernel<T> kernel = ref _buffer[_boarder.GetIndexOffset(k_r, k_c, in _size)];
|
||||
return ref kernel[c, r];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe MatrixKernel<T>* GetKernelAddressUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return Buffer + _boarder.GetIndexOffset(x, y, in _size);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref MatrixKernel<T> GetKernelUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
return ref _buffer[_boarder.GetIndexOffset(k_c, k_r, in _size)];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +1,72 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixStorage"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixStorage"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using HLSFAllocator = global::StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
|
||||
using HLSFHandle = global::StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public abstract class MatrixStorage<T> : IDisposable where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal unsafe ref MatrixKernel<T> this[long index]
|
||||
{
|
||||
get { return ref Unsafe.AsRef<MatrixKernel<T>>(BufferPointer + MatrixKernel<T>.Size * index); }
|
||||
}
|
||||
|
||||
protected internal abstract unsafe T* BufferPointer { get; }
|
||||
|
||||
public abstract void Dispose();
|
||||
|
||||
}
|
||||
|
||||
internal class MatrixStorageHLSF<T>(HLSFAllocator allocator, HLSFHandle handle) : MatrixStorage<T>
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal HLSFAllocator _allocator = allocator;
|
||||
internal HLSFHandle _handle = handle;
|
||||
|
||||
protected internal override unsafe T* BufferPointer => (T*)_handle.Pointer;
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
allocator.Free(_handle);
|
||||
}
|
||||
|
||||
}
|
||||
using HLSFAllocator = global::StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
|
||||
using HLSFHandle = global::StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public abstract class MatrixStorage<T> : IDisposable where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal unsafe ref MatrixKernel<T> this[long index]
|
||||
{
|
||||
get { return ref Unsafe.AsRef<MatrixKernel<T>>(BufferPointer + MatrixKernel<T>.Size * index); }
|
||||
}
|
||||
|
||||
protected internal abstract unsafe T* BufferPointer { get; }
|
||||
|
||||
public abstract void Dispose();
|
||||
|
||||
}
|
||||
|
||||
internal class MatrixStorageHLSF<T>(HLSFAllocator allocator, HLSFHandle handle) : MatrixStorage<T>
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal HLSFAllocator _allocator = allocator;
|
||||
internal HLSFHandle _handle = handle;
|
||||
|
||||
protected internal override unsafe T* BufferPointer => (T*)_handle.Pointer;
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
allocator.Free(_handle);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,90 +1,90 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumericalModule"
|
||||
// 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 StgSharp.Mathematics.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class NumericalModule : IStaticModule
|
||||
{
|
||||
|
||||
public static unsafe int DefaultSIMDAlignment
|
||||
{
|
||||
get
|
||||
{
|
||||
SIMDID id = GlobalIntrinsicMask;
|
||||
return (id.MaskByte[0] & 0b_00001111) switch
|
||||
{
|
||||
1 => id.MaskByte[1] switch
|
||||
{
|
||||
1 => 16,
|
||||
2 => 32,
|
||||
3 => 64,
|
||||
4 => 64,
|
||||
5 => 64,
|
||||
_ => 8,
|
||||
},
|
||||
_ => 8,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public string ModuleName => "Math.Numerical";
|
||||
|
||||
internal static IntrinsicContext GlobalContext
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
} = new();
|
||||
|
||||
internal static SIMDID GlobalIntrinsicMask { get; set; }
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InitializeModule(
|
||||
IModuleInitializeProfile profile
|
||||
)
|
||||
{
|
||||
MatrixParallel.Init();
|
||||
}
|
||||
|
||||
public void UninitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumericalModule"
|
||||
// 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 StgSharp.Mathematics.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class NumericalModule : IStaticModule
|
||||
{
|
||||
|
||||
public static unsafe int DefaultSIMDAlignment
|
||||
{
|
||||
get
|
||||
{
|
||||
SIMDID id = GlobalIntrinsicMask;
|
||||
return (id.MaskByte[0] & 0b_00001111) switch
|
||||
{
|
||||
1 => id.MaskByte[1] switch
|
||||
{
|
||||
1 => 16,
|
||||
2 => 32,
|
||||
3 => 64,
|
||||
4 => 64,
|
||||
5 => 64,
|
||||
_ => 8,
|
||||
},
|
||||
_ => 8,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public string ModuleName => "Math.Numerical";
|
||||
|
||||
internal static IntrinsicContext GlobalContext
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
} = new();
|
||||
|
||||
internal static SIMDID GlobalIntrinsicMask { get; set; }
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InitializeModule(
|
||||
IModuleInitializeProfile profile
|
||||
)
|
||||
{
|
||||
MatrixParallel.Init();
|
||||
}
|
||||
|
||||
public void UninitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish"
|
||||
// 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 StgSharp.Collections;
|
||||
using StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private const int MIN_TASK_PER_THREAD = 256;
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish"
|
||||
// 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 StgSharp.Collections;
|
||||
using StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private const int MIN_TASK_PER_THREAD = 256;
|
||||
private const int REC_TASK_PER_THREAD = 1024;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
internal static unsafe MatrixParallelWrap ScheduleTask(
|
||||
MatrixParallelTask* package
|
||||
)
|
||||
{
|
||||
return ScheduleBufferTask(package);
|
||||
}
|
||||
|
||||
private static unsafe MatrixParallelWrap ScheduleBufferTask(
|
||||
MatrixParallelTask* package
|
||||
)
|
||||
{
|
||||
/*
|
||||
int primCount = package->PrimCount;
|
||||
int secCount = package->SecCount;
|
||||
internal static unsafe MatrixParallelWrap ScheduleTask(
|
||||
MatrixParallelTask* package
|
||||
)
|
||||
{
|
||||
return ScheduleBufferTask(package);
|
||||
}
|
||||
|
||||
private static unsafe MatrixParallelWrap ScheduleBufferTask(
|
||||
MatrixParallelTask* package
|
||||
)
|
||||
{
|
||||
/*
|
||||
int primCount = package->PrimCount;
|
||||
int secCount = package->SecCount;
|
||||
long count = (long)primCount * secCount;
|
||||
/**/
|
||||
/**/
|
||||
|
||||
// Console.WriteLine($"amount of kernels is {count}");
|
||||
|
||||
long recommendCount = /*count / /**/ 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(pool);
|
||||
wrap.TaskQueue = MatrixParallelQueue.Rent(package);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
}
|
||||
long recommendCount = /*count / /**/ 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(pool);
|
||||
wrap.TaskQueue = MatrixParallelQueue.Rent(package);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,134 +1,134 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel"
|
||||
// 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 StgSharp.Collections;
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
private static ConcurrentDictionary<SIMDID, nint> contextMap;
|
||||
|
||||
private static SlabAllocator<IntrinsicContext> contextAllocator;
|
||||
|
||||
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
internal static unsafe IntrinsicContext* GetContext(
|
||||
SIMDID simd
|
||||
)
|
||||
{
|
||||
if (!IsParallelSupported) {
|
||||
return null;
|
||||
}
|
||||
if (contextMap.TryGetValue(simd, out nint handle))
|
||||
{
|
||||
return (IntrinsicContext*)handle;
|
||||
} else
|
||||
{
|
||||
IntrinsicContext* context = (IntrinsicContext*)contextAllocator.Allocate();
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
if (!IsParallelSupported)
|
||||
{
|
||||
IsInitialized = true;
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
contextAllocator = SlabAllocator<IntrinsicContext>.Create(4, SlabBufferLayout.Chunked, false);
|
||||
contextMap = [];
|
||||
int count = Environment.ProcessorCount;
|
||||
_threads = new(count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
MatrixParallelThread thread = new MatrixParallelThread(i);
|
||||
_threads.Push(thread);
|
||||
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
|
||||
}
|
||||
|
||||
// TODO 构建全局的保底上下文
|
||||
foreach (MatrixParallelThread thread in _threads) { }
|
||||
if (contextMap.Keys.Count == 1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
MatrixParallelThread thread = _threads.Pop();
|
||||
thread.Dispose();
|
||||
}
|
||||
} else
|
||||
{
|
||||
// TODO 排序并丢弃四个能力最弱线程
|
||||
}
|
||||
_awaitingLeaderHandle = new AwaitingQueue();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial ulong CompareSIMDID(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial void LoadIntrinsicFunction(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel"
|
||||
// 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 StgSharp.Collections;
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
private static ConcurrentDictionary<SIMDID, nint> contextMap;
|
||||
|
||||
private static SlabAllocator<IntrinsicContext> contextAllocator;
|
||||
|
||||
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
internal static unsafe IntrinsicContext* GetContext(
|
||||
SIMDID simd
|
||||
)
|
||||
{
|
||||
if (!IsParallelSupported) {
|
||||
return null;
|
||||
}
|
||||
if (contextMap.TryGetValue(simd, out nint handle))
|
||||
{
|
||||
return (IntrinsicContext*)handle;
|
||||
} else
|
||||
{
|
||||
IntrinsicContext* context = (IntrinsicContext*)contextAllocator.Allocate();
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
if (!IsParallelSupported)
|
||||
{
|
||||
IsInitialized = true;
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
contextAllocator = SlabAllocator<IntrinsicContext>.Create(4, SlabBufferLayout.Chunked, false);
|
||||
contextMap = [];
|
||||
int count = Environment.ProcessorCount;
|
||||
_threads = new(count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
MatrixParallelThread thread = new MatrixParallelThread(i);
|
||||
_threads.Push(thread);
|
||||
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
|
||||
}
|
||||
|
||||
// TODO 构建全局的保底上下文
|
||||
foreach (MatrixParallelThread thread in _threads) { }
|
||||
if (contextMap.Keys.Count == 1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
MatrixParallelThread thread = _threads.Pop();
|
||||
thread.Dispose();
|
||||
}
|
||||
} else
|
||||
{
|
||||
// TODO 排序并丢弃四个能力最弱线程
|
||||
}
|
||||
_awaitingLeaderHandle = new AwaitingQueue();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial ulong CompareSIMDID(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial void LoadIntrinsicFunction(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixTaskFactory"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixTaskFactory"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using hlsfAllocator = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static unsafe class MatrixParallelFactory
|
||||
{
|
||||
|
||||
using hlsfAllocator = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static unsafe class MatrixParallelFactory
|
||||
{
|
||||
|
||||
private static hlsfAllocator _intermediateResult;
|
||||
|
||||
private static
|
||||
@@ -58,60 +58,60 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
private static SlabAllocator<MatrixParallelTask> _wrapAllocator;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTask* CreateBaseTask()
|
||||
{
|
||||
MatrixParallelTask* p = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTask));
|
||||
s.Fill(0);
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ScalarPacket* CreateScalarPacket()
|
||||
{
|
||||
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTask* FromExistPackage(
|
||||
MatrixParallelTask* exist
|
||||
)
|
||||
{
|
||||
MatrixParallelTask* ptr = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTask.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTask* CreateBaseTask()
|
||||
{
|
||||
MatrixParallelTask* p = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTask));
|
||||
s.Fill(0);
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ScalarPacket* CreateScalarPacket()
|
||||
{
|
||||
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTask* FromExistPackage(
|
||||
MatrixParallelTask* exist
|
||||
)
|
||||
{
|
||||
MatrixParallelTask* ptr = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTask.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
public static void Release(
|
||||
ScalarPacket* source
|
||||
)
|
||||
{
|
||||
_scalarAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release(
|
||||
MatrixParallelTask* source
|
||||
)
|
||||
{
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void Init()
|
||||
{
|
||||
if (_intermediateResult is null)
|
||||
{
|
||||
_intermediateResult = new hlsfAllocator(64 * 16 * 16);
|
||||
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64 * 1024, SlabBufferLayout.Chunked);
|
||||
public static void Release(
|
||||
ScalarPacket* source
|
||||
)
|
||||
{
|
||||
_scalarAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release(
|
||||
MatrixParallelTask* source
|
||||
)
|
||||
{
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void Init()
|
||||
{
|
||||
if (_intermediateResult is null)
|
||||
{
|
||||
_intermediateResult = new hlsfAllocator(64 * 16 * 16);
|
||||
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64 * 1024, SlabBufferLayout.Chunked);
|
||||
_wrapAllocator = SlabAllocator<MatrixParallelTask>.
|
||||
Create(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Create(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user