HLSF use long as aloloc size

This commit is contained in:
Nitload
2026-02-23 12:32:15 +08:00
parent 4bd88ccbca
commit c155cab510
552 changed files with 154266 additions and 154330 deletions
+306 -306
View File
@@ -1,306 +1,306 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Camera"
// 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.Graphics.OpenGL;
using StgSharp.Graphics.ShaderEdit;
using System;
namespace StgSharp.Mathematics.Graphic
{
public sealed class Camera : IglConvertable
{
private bool _isLookAtAvailable;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
internal Matrix44 cameraAtt;
internal Matrix44 rotation;
internal Matrix44 rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
internal Uniform<Matrix44> convertedUniform;
internal Vec3 _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
}
public Camera(Vec3 position, Vec3 target, Vec3 up)
: this()
{
rotationAtt = Matrix44.Unit;
cameraAtt = new Matrix44();
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
_yaw = Radius.Zero;
}
public Matrix44 Projection => _projection;
public Matrix44 View
{
get
{
if (_isLookAtAvailable) {
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
move.column[3].vec -= cameraAtt.column[3].vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
public Matrix44 CameraMatrix()
{
return Projection * View;
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
/// Get all uniform related to this <see cref="Camera" />.
/// </summary>
/// <param _label="source">
/// Shader program requires this camera.
/// </param>
/// <param _label="uniformName">
/// ContextName of all related uniforms. If you fallow form from <see
/// cref="IglConvertable.DisplayGLtypeDefinition" />
/// </param>
/// <exception cref="ArgumentException">
///
/// </exception>
public unsafe void GainAllUniforms(ShaderProgram source, params string[] uniformName)
{
if (uniformName is null || uniformName.Length != 1) {
throw new ArgumentException(
"Camera needs exactly one uniform.", nameof(uniformName));
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
public void MoveNear(float distance)
{
cameraAtt.column[3].Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
cameraAtt.column[3].X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
cameraAtt.column[3].Y -= distance;
_isLookAtAvailable = false;
}
public unsafe void SetAllUniforms()
{
// Console.WriteLine(Projection * ViewBase);
OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View);
}
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
if ((position.reg == cameraAtt.column[3].reg) &&
(this.up == up) &&
(cameraAtt.column[3].reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
{
// deadlock
throw new ArgumentException("Direction bust is not zero.");
}
if (Vec3.IsParallel(direction, up)) {
throw new ArgumentException("Direction and UP is on on same way");
}
_target = direction;
this.up = up;
_isLookAtAvailable = false;
direction.Orthogonalize(ref up);
Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction));
cameraAtt.column[0].reg = right.reg;
cameraAtt.column[1].reg = up.reg;
cameraAtt.column[2].reg = direction.reg;
cameraAtt.column[3].reg = position.reg;
InternalPitch();
InternalRow();
InternalYaw();
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) depthRange)
{
float
distance = _target.GetLength(),
near = distance - depthRange.front,
far = distance + depthRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scalar.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
_projection.column[0].X = 2 * near / width;
_projection.column[1].Y = 2 * near / height;
_projection.column[2].X = 2 * offsetX / width;
_projection.column[2].Y = 2 * offsetY / height;
_projection.column[2].Z = (far + near) / (near - far);
_projection.column[2].W = -1;
_projection.column[3].Z = 2 * near * far / (near - far);
}
public void Test(params Vec4[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
Console.WriteLine(View);
foreach (Vec4 item in vec) {
Console.Write($"{Projection * View * item};");
}
}
ShaderStruct IglConvertable.GetConvertedGLtype()
{
throw new NotImplementedException();
}
~Camera() { }
#region rotation
public void Yaw(Radius r)
{
_yaw -= r;
// Console.WriteLine(_yaw._radius);
// Console.WriteLine(rotationAtt);
InternalYaw();
}
public void Pitch(Radius r)
{
_pitch -= r;
// Console.WriteLine(_pitch._radius);
// Console.WriteLine(rotationAtt);
InternalPitch();
}
public void Row(Radius r)
{
_row -= r;
// Console.WriteLine(_row._radius);
// Console.WriteLine(rotationAtt);
InternalRow();
}
internal void InternalPitch()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum1.vec, cameraAtt.colum2.vec);
float angle = _pitch._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum1.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
/**/
}
internal void InternalRow()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum1.vec);
float angle = _row._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum1.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
internal void InternalYaw()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum2.vec);
float angle = _yaw._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
#endregion
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Camera"
// 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.Graphics.OpenGL;
using StgSharp.Graphics.ShaderEdit;
using System;
namespace StgSharp.Mathematics.Graphic
{
public sealed class Camera : IglConvertable
{
private bool _isLookAtAvailable;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
internal Matrix44 cameraAtt;
internal Matrix44 rotation;
internal Matrix44 rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
internal Uniform<Matrix44> convertedUniform;
internal Vec3 _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
}
public Camera(Vec3 position, Vec3 target, Vec3 up)
: this()
{
rotationAtt = Matrix44.Unit;
cameraAtt = new Matrix44();
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
_yaw = Radius.Zero;
}
public Matrix44 Projection => _projection;
public Matrix44 View
{
get
{
if (_isLookAtAvailable) {
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
move.column[3].vec -= cameraAtt.column[3].vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
public Matrix44 CameraMatrix()
{
return Projection * View;
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
/// Get all uniform related to this <see cref="Camera" />.
/// </summary>
/// <param _label="source">
/// Shader program requires this camera.
/// </param>
/// <param _label="uniformName">
/// ContextName of all related uniforms. If you fallow form from <see
/// cref="IglConvertable.DisplayGLtypeDefinition" />
/// </param>
/// <exception cref="ArgumentException">
///
/// </exception>
public unsafe void GainAllUniforms(ShaderProgram source, params string[] uniformName)
{
if (uniformName is null || uniformName.Length != 1) {
throw new ArgumentException(
"Camera needs exactly one uniform.", nameof(uniformName));
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
public void MoveNear(float distance)
{
cameraAtt.column[3].Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
cameraAtt.column[3].X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
cameraAtt.column[3].Y -= distance;
_isLookAtAvailable = false;
}
public unsafe void SetAllUniforms()
{
// Console.WriteLine(Projection * ViewBase);
OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View);
}
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
if ((position.reg == cameraAtt.column[3].reg) &&
(this.up == up) &&
(cameraAtt.column[3].reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
{
// deadlock
throw new ArgumentException("Direction bust is not zero.");
}
if (Vec3.IsParallel(direction, up)) {
throw new ArgumentException("Direction and UP is on on same way");
}
_target = direction;
this.up = up;
_isLookAtAvailable = false;
direction.Orthogonalize(ref up);
Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction));
cameraAtt.column[0].reg = right.reg;
cameraAtt.column[1].reg = up.reg;
cameraAtt.column[2].reg = direction.reg;
cameraAtt.column[3].reg = position.reg;
InternalPitch();
InternalRow();
InternalYaw();
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) depthRange)
{
float
distance = _target.GetLength(),
near = distance - depthRange.front,
far = distance + depthRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scalar.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
_projection.column[0].X = 2 * near / width;
_projection.column[1].Y = 2 * near / height;
_projection.column[2].X = 2 * offsetX / width;
_projection.column[2].Y = 2 * offsetY / height;
_projection.column[2].Z = (far + near) / (near - far);
_projection.column[2].W = -1;
_projection.column[3].Z = 2 * near * far / (near - far);
}
public void Test(params Vec4[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
Console.WriteLine(View);
foreach (Vec4 item in vec) {
Console.Write($"{Projection * View * item};");
}
}
ShaderStruct IglConvertable.GetConvertedGLtype()
{
throw new NotImplementedException();
}
~Camera() { }
#region rotation
public void Yaw(Radius r)
{
_yaw -= r;
// Console.WriteLine(_yaw._radius);
// Console.WriteLine(rotationAtt);
InternalYaw();
}
public void Pitch(Radius r)
{
_pitch -= r;
// Console.WriteLine(_pitch._radius);
// Console.WriteLine(rotationAtt);
InternalPitch();
}
public void Row(Radius r)
{
_row -= r;
// Console.WriteLine(_row._radius);
// Console.WriteLine(rotationAtt);
InternalRow();
}
internal void InternalPitch()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum1.vec, cameraAtt.colum2.vec);
float angle = _pitch._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum1.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
/**/
}
internal void InternalRow()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum1.vec);
float angle = _row._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum1.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
internal void InternalYaw()
{
/*
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(cameraAtt.colum0.vec, cameraAtt.colum2.vec);
float angle = _yaw._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle), Scaler.Sin(angle), Scaler.Cos(angle));
partialCoord *= rotation;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
#endregion
}
}
+262 -262
View File
@@ -1,262 +1,262 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.HighPerformance;
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.Graphic
{
[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
{
#if DEBUG
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
{
#if DEBUG
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) {
NativeIntrinsic.Context.mat[f32].buffer_transpose(source, target);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_sub(&left, &right, &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
)
{
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 = (int)MatrixElementType.F32;
NativeIntrinsic.Context.mat[f32].kernel_fma(&left, &right, &ret);
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_add(&left, &right, &ret, 1);
return ret;
}
public static bool operator ==(
Matrix44 left,
Matrix44 right
)
{
return left.Equals(right);
}
[InlineArray(4)]
internal struct Column
{
private Vec4 value;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.HighPerformance;
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.Graphic
{
[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
{
#if DEBUG
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
{
#if DEBUG
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) {
NativeIntrinsic.Context.mat[f32].buffer_transpose(source, target);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_sub(&left, &right, &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
)
{
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 = (int)MatrixElementType.F32;
NativeIntrinsic.Context.mat[f32].kernel_fma(&left, &right, &ret);
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_add(&left, &right, &ret, 1);
return ret;
}
public static bool operator ==(
Matrix44 left,
Matrix44 right
)
{
return left.Equals(right);
}
[InlineArray(4)]
internal struct Column
{
private Vec4 value;
}
}
}
+162 -162
View File
@@ -1,162 +1,162 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec2"
// 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
/// <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>
{
[FieldOffset(0)] internal unsafe fixed float num[2];
[FieldOffset(0)]
internal M128 reg;
[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);
}
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);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec2"
// 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
/// <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>
{
[FieldOffset(0)] internal unsafe fixed float num[2];
[FieldOffset(0)]
internal M128 reg;
[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);
}
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);
}
}
}
+236 -236
View File
@@ -1,236 +1,236 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec3"
// 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.HighPerformance;
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.Graphic
{
[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);
}
[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();
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec3"
// 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.HighPerformance;
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.Graphic
{
[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);
}
[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();
}
}
}
+211 -211
View File
@@ -1,212 +1,212 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.HighPerformance;
using StgSharp.Internal.Intrinsic;
using StgSharp.Mathematics.Numeric;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[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(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);
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);
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));
}
// 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);
}
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;
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.HighPerformance;
using StgSharp.Internal.Intrinsic;
using StgSharp.Mathematics.Numeric;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[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(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);
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);
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));
}
// 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);
}
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,49 +1,49 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Cross"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float Cross(Vec2 vec1, Vec2 vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
public static Vec3 Cross(Vec3 vec1, Vec3 vec2)
{
return new Vec3(
(vec1.Y * vec2.Z) - (vec1.Z * vec2.Y),
(vec1.X * vec2.Z) - (vec1.Z * vec2.X),
(vec1.X * vec2.Y) - (vec1.Y * vec2.X));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Cross"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float Cross(Vec2 vec1, Vec2 vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
public static Vec3 Cross(Vec3 vec1, Vec3 vec2)
{
return new Vec3(
(vec1.Y * vec2.Z) - (vec1.Z * vec2.Y),
(vec1.X * vec2.Z) - (vec1.Z * vec2.X),
(vec1.X * vec2.Y) - (vec1.Y * vec2.X));
}
}
}
@@ -1,47 +1,47 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetAngle"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetAngle(Vec2 vec1, Vec2 vec2)
{
return Scalar.ACos(
1 / (vec1.GetLength() * vec2.GetLength()));
}
public static float GetAngle(Vec3 vec1, Vec3 vec2)
{
return Scalar.ACos((vec1 * vec2) / (vec1.GetLength() * vec2.GetLength()));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetAngle"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetAngle(Vec2 vec1, Vec2 vec2)
{
return Scalar.ACos(
1 / (vec1.GetLength() * vec2.GetLength()));
}
public static float GetAngle(Vec3 vec1, Vec3 vec2)
{
return Scalar.ACos((vec1 * vec2) / (vec1.GetLength() * vec2.GetLength()));
}
}
}
@@ -1,46 +1,46 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetLength"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetLength(this Vec3 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
public static float GetLength(this Vec2 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetLength"
// 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.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetLength(this Vec3 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
public static float GetLength(this Vec2 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
}
}
@@ -1,82 +1,82 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Normalize"
// 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.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Normalize(Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Normalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Normalize(ref Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
public static unsafe void Normalize(ref Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Normalize"
// 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.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Normalize(Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Normalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Normalize(ref Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
public static unsafe void Normalize(ref Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
@@ -1,92 +1,92 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Orthogonalize"
// 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.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Orthogonalize(this Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Orthogonalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Orthogonalize(this Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
/// <summary>
/// Orthogonalize two <see cref="Vec3" />s, let their length be 1 and perpendicular to
/// each other.
/// </summary>
/// <param _label="source">
///
/// </param>
/// <param _label="target">
///
/// </param>
public static unsafe void Orthogonalize(this Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Orthogonalize"
// 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.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Orthogonalize(this Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Orthogonalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Orthogonalize(this Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
/// <summary>
/// Orthogonalize two <see cref="Vec3" />s, let their length be 1 and perpendicular to
/// each other.
/// </summary>
/// <param _label="source">
///
/// </param>
/// <param _label="target">
///
/// </param>
public static unsafe void Orthogonalize(this Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
@@ -1,88 +1,88 @@
# Matrix Coordinate & Naming Conventions (StgSharp)
This document defines the coordinate terminology and naming rules used across `Matrix<T>`, `MatrixKernel<T>`, and packed/triangular storage layouts.
Goal: eliminate ambiguity around `column`/`row` names (e.g., “index **of** a column” vs “index **along** a column”).
---
## 1) The key rule: encode prepositions into identifiers (`Of` vs `In`)
### `...OfX` — “which X” (object identity / ordinal)
Use **`Of`** when the value identifies **which object** (which column, which row, which kernel, etc.).
Examples:
- `columnOfElement`: the element is located in **which column**
- `rowOfKernel`: the kernel block is located in **which row**
### `...InX` / `...WithinX` — “inside X” (local coordinate)
Use **`In`** (or `Within`) when the value is a **local coordinate inside** another object.
Examples:
- `columnInKernel`: local column coordinate **inside** a kernel block
- `rowInKernel`: local row coordinate **inside** a kernel block
> Quick mental model: **`Of` = identity**, **`In` = inside**.
---
## 2) Coordinate levels: `Element`, `KernelGrid`, `InKernel`
StgSharp commonly uses three coordinate levels:
1. **Element-level**: final matrix element coordinates (`Matrix<T>[col,row]`)
2. **Kernel-grid level**: coordinates of a kernel block in the kernel grid
3. **In-kernel level**: local element coordinates within a kernel (e.g., 4×4)
Recommended names:
| Level | Column name | Row name | Meaning |
|---|---|---|---|
| Element | `columnOfElement` | `rowOfElement` | element located in which column/row (0-based) |
| Kernel grid | `columnOfKernel` | `rowOfKernel` | kernel block located in which kernel-column/kernel-row (0-based) |
| In-kernel | `columnInKernel` | `rowInKernel` | local coordinate inside the kernel (0..KernelSize-1) |
---
## 3) Index base: 0-based (mandatory)
Unless explicitly documented otherwise, all coordinates are **0-based**:
- `columnOfElement ∈ [0, ColumnLength-1]`
- `rowOfElement ∈ [0, RowLength-1]`
- `columnOfKernel ∈ [0, KernelColumnLength-1]`
- `rowOfKernel ∈ [0, KernelRowLength-1]`
- `columnInKernel ∈ [0, KernelSize-1]`
- `rowInKernel ∈ [0, KernelSize-1]`
---
## 4) Mapping element coordinates to kernel coordinates (KernelSize = 4 example)
For a fixed kernel size `KernelSize = 4`:
```csharp
int columnOfKernel = columnOfElement / 4;
int rowOfKernel = rowOfElement / 4;
int columnInKernel = columnOfElement % 4;
int rowInKernel = rowOfElement % 4;
```
If `columnOfElement` and `rowOfElement` are guaranteed non-negative, bit operations can be used:
```csharp
int columnOfKernel = columnOfElement >> 2;
int rowOfKernel = rowOfElement >> 2;
int columnInKernel = columnOfElement & 3;
int rowInKernel = rowOfElement & 3;
```
---
## 5) Packed storage: distinguish `Index` from `Offset`
Packed layouts do **not** store “empty blocks”; stored blocks are placed contiguously in a packed buffer.
### Use the correct terms
- **`packedKernelIndex`**: “the N-th kernel
# Matrix Coordinate & Naming Conventions (StgSharp)
This document defines the coordinate terminology and naming rules used across `Matrix<T>`, `MatrixKernel<T>`, and packed/triangular storage layouts.
Goal: eliminate ambiguity around `column`/`row` names (e.g., “index **of** a column” vs “index **along** a column”).
---
## 1) The key rule: encode prepositions into identifiers (`Of` vs `In`)
### `...OfX` — “which X” (object identity / ordinal)
Use **`Of`** when the value identifies **which object** (which column, which row, which kernel, etc.).
Examples:
- `columnOfElement`: the element is located in **which column**
- `rowOfKernel`: the kernel block is located in **which row**
### `...InX` / `...WithinX` — “inside X” (local coordinate)
Use **`In`** (or `Within`) when the value is a **local coordinate inside** another object.
Examples:
- `columnInKernel`: local column coordinate **inside** a kernel block
- `rowInKernel`: local row coordinate **inside** a kernel block
> Quick mental model: **`Of` = identity**, **`In` = inside**.
---
## 2) Coordinate levels: `Element`, `KernelGrid`, `InKernel`
StgSharp commonly uses three coordinate levels:
1. **Element-level**: final matrix element coordinates (`Matrix<T>[col,row]`)
2. **Kernel-grid level**: coordinates of a kernel block in the kernel grid
3. **In-kernel level**: local element coordinates within a kernel (e.g., 4×4)
Recommended names:
| Level | Column name | Row name | Meaning |
|---|---|---|---|
| Element | `columnOfElement` | `rowOfElement` | element located in which column/row (0-based) |
| Kernel grid | `columnOfKernel` | `rowOfKernel` | kernel block located in which kernel-column/kernel-row (0-based) |
| In-kernel | `columnInKernel` | `rowInKernel` | local coordinate inside the kernel (0..KernelSize-1) |
---
## 3) Index base: 0-based (mandatory)
Unless explicitly documented otherwise, all coordinates are **0-based**:
- `columnOfElement ∈ [0, ColumnLength-1]`
- `rowOfElement ∈ [0, RowLength-1]`
- `columnOfKernel ∈ [0, KernelColumnLength-1]`
- `rowOfKernel ∈ [0, KernelRowLength-1]`
- `columnInKernel ∈ [0, KernelSize-1]`
- `rowInKernel ∈ [0, KernelSize-1]`
---
## 4) Mapping element coordinates to kernel coordinates (KernelSize = 4 example)
For a fixed kernel size `KernelSize = 4`:
```csharp
int columnOfKernel = columnOfElement / 4;
int rowOfKernel = rowOfElement / 4;
int columnInKernel = columnOfElement % 4;
int rowInKernel = rowOfElement % 4;
```
If `columnOfElement` and `rowOfElement` are guaranteed non-negative, bit operations can be used:
```csharp
int columnOfKernel = columnOfElement >> 2;
int rowOfKernel = rowOfElement >> 2;
int columnInKernel = columnOfElement & 3;
int rowInKernel = rowOfElement & 3;
```
---
## 5) Packed storage: distinguish `Index` from `Offset`
Packed layouts do **not** store “empty blocks”; stored blocks are placed contiguously in a packed buffer.
### Use the correct terms
- **`packedKernelIndex`**: “the N-th kernel
@@ -1,38 +1,38 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Cordination"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics.Numeric
{
public enum CoordinatePlain
{
XY,
YZ,
XZ,
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Cordination"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics.Numeric
{
public enum CoordinatePlain
{
XY,
YZ,
XZ,
}
}
@@ -1,39 +1,39 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ElementType"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics.Numeric
{
public enum MatrixElementType : int
{
F32 = 0,
F64 = 1,
I32 = 2,
I64 = 3,
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ElementType"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics.Numeric
{
public enum MatrixElementType : int
{
F32 = 0,
F64 = 1,
I32 = 2,
I64 = 3,
}
}
@@ -1,46 +1,46 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="IUnmanagedVector"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public interface IUnmanagedVector<TSelf> where TSelf: unmanaged, IUnmanagedVector<TSelf>
{
public static abstract TSelf Zero { get; }
public static abstract TSelf One { get; }
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="IUnmanagedVector"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public interface IUnmanagedVector<TSelf> where TSelf: unmanaged, IUnmanagedVector<TSelf>
{
public static abstract TSelf Zero { get; }
public static abstract TSelf One { get; }
}
}
@@ -1,104 +1,101 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix.Create"
// 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.HighPerformance;
using StgSharp.HighPerformance.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 kernelRows = DivideRoundUp(rowLength, KernelSideLength);
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 requiredBytes = kernelCount * MatrixKernel<T>.Size;
if (requiredBytes <= 0 || requiredBytes > uint.MaxValue) {
throw new ArgumentOutOfRangeException(nameof(requiredBytes), "Requested matrix exceeds allocator capacity.");
}
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,
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;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.HighPerformance;
using StgSharp.HighPerformance.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 kernelRows = DivideRoundUp(rowLength, KernelSideLength);
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 requiredBytes = kernelCount * MatrixKernel<T>.Size;
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(requiredBytes, nameof(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,
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;
}
}
}
+204 -204
View File
@@ -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.HighPerformance;
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.HighPerformance;
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,70 +1,70 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixIndex"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric
{
[StructLayout(LayoutKind.Sequential)]
public readonly struct MatrixIndex
{
public readonly int Begin;
public readonly int End;
private MatrixIndex(int begin, int end, bool safe = false)
{
Begin = begin;
End = end;
}
internal MatrixIndex(int begin, int end)
{
if (begin > end) {
throw new ArgumentException("Begin index cannot be greater than end index.");
}
Begin = begin;
End = end;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator MatrixIndex((int, int) range)
{
return new(range.Item1, range.Item2, false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator MatrixIndex(int index)
{
return new(index, -1, false);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixIndex"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric
{
[StructLayout(LayoutKind.Sequential)]
public readonly struct MatrixIndex
{
public readonly int Begin;
public readonly int End;
private MatrixIndex(int begin, int end, bool safe = false)
{
Begin = begin;
End = end;
}
internal MatrixIndex(int begin, int end)
{
if (begin > end) {
throw new ArgumentException("Begin index cannot be greater than end index.");
}
Begin = begin;
End = end;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator MatrixIndex((int, int) range)
{
return new(range.Item1, range.Item2, false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator MatrixIndex(int index)
{
return new(index, -1, false);
}
}
}
@@ -1,252 +1,252 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixMetadata"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric
{
public enum MatrixLayout
{
DenseRectangle,
UpperTriangle,
LowerTriangle,
}
public abstract class MatrixKernelBoarder
{
private static readonly MatrixKernelBoarder DenseSquareInstance = new DenseSquareKernelBoarder();
private static readonly MatrixKernelBoarder LowerTriangleInstance = new LowerTriangleKernelBoarder();
private static readonly MatrixKernelBoarder UpperTriangleInstance = new UpperTriangleKernelBoarder();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixKernelBoarder Create(
MatrixLayout layout
)
{
return layout switch
{
MatrixLayout.DenseRectangle => DenseSquareInstance,
MatrixLayout.UpperTriangle => UpperTriangleInstance,
MatrixLayout.LowerTriangle => LowerTriangleInstance,
_ => throw new ArgumentOutOfRangeException(nameof(layout)),
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public abstract long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize size
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public abstract bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static long GetTriangularOrder(
in MatrixSize enumerator
)
{
return Math.Min(enumerator.KernelColumnLength, enumerator.KernelRowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static bool IsWithinKernelDomain(
in MatrixSize enumerator,
int xKernel,
int yKernel
)
{
return xKernel >= 0 &&
yKernel >= 0 &&
xKernel < enumerator.KernelColumnLength &&
yKernel < enumerator.KernelRowLength;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static bool IsWithinTriangularDomain(
in MatrixSize enumerator,
int xKernel,
int yKernel
)
{
long order = GetTriangularOrder(enumerator);
return xKernel >= 0 && yKernel >= 0 && xKernel < order && yKernel < order;
}
private sealed class DenseSquareKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long x = xKernel;
long y = yKernel;
long col = enumerator.KernelColumnLength;
// Console.Write($"Attempt to get offset at ({x},{y}), offset is {x * col + y} ");
return x * col + y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinKernelDomain(enumerator, xKernel, yKernel);
}
}
private sealed class LowerTriangleKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long order = GetTriangularOrder(enumerator);
long x = xKernel;
long y = yKernel;
long prefixColumns = x * order - (x * (x - 1L)) / 2L;
return prefixColumns + (y - x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel >= xKernel;
}
}
private sealed unsafe class UpperTriangleKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long x = xKernel;
long y = yKernel;
long prefixColumns = (x * (x + 1L)) / 2L;
return prefixColumns + y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel <= xKernel;
}
}
}
[StructLayout(LayoutKind.Explicit)]
public struct MatrixSize
{
[FieldOffset(0)] public readonly int ElementColumnLength;
[FieldOffset(4)] public readonly int ElementRowLength;
[FieldOffset(8)] public readonly int KernelColumnLength;
[FieldOffset(12)] public readonly int KernelRowLength;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MatrixSize(
int elementColumnLength,
int elementRowLength,
int kernelColumnLength,
int kernelRowLength
)
{
ElementColumnLength = elementColumnLength;
ElementRowLength = elementRowLength;
KernelColumnLength = kernelColumnLength;
KernelRowLength = kernelRowLength;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct MatrixKernelEnumerator
{
[FieldOffset(0)] public int CurrentColumn;
[FieldOffset(4)] public int CurrentRow;
[FieldOffset(8)] public readonly int KernelColumnLength;
[FieldOffset(12)] public readonly int KernelRowLength;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MatrixKernelEnumerator(
int kernelColumnLength,
int kernelRowLength,
int currentColumn = 0,
int currentRow = 0
)
{
CurrentColumn = currentColumn;
CurrentRow = currentRow;
KernelColumnLength = kernelColumnLength;
KernelRowLength = kernelRowLength;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixMetadata"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric
{
public enum MatrixLayout
{
DenseRectangle,
UpperTriangle,
LowerTriangle,
}
public abstract class MatrixKernelBoarder
{
private static readonly MatrixKernelBoarder DenseSquareInstance = new DenseSquareKernelBoarder();
private static readonly MatrixKernelBoarder LowerTriangleInstance = new LowerTriangleKernelBoarder();
private static readonly MatrixKernelBoarder UpperTriangleInstance = new UpperTriangleKernelBoarder();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixKernelBoarder Create(
MatrixLayout layout
)
{
return layout switch
{
MatrixLayout.DenseRectangle => DenseSquareInstance,
MatrixLayout.UpperTriangle => UpperTriangleInstance,
MatrixLayout.LowerTriangle => LowerTriangleInstance,
_ => throw new ArgumentOutOfRangeException(nameof(layout)),
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public abstract long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize size
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public abstract bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static long GetTriangularOrder(
in MatrixSize enumerator
)
{
return Math.Min(enumerator.KernelColumnLength, enumerator.KernelRowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static bool IsWithinKernelDomain(
in MatrixSize enumerator,
int xKernel,
int yKernel
)
{
return xKernel >= 0 &&
yKernel >= 0 &&
xKernel < enumerator.KernelColumnLength &&
yKernel < enumerator.KernelRowLength;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static bool IsWithinTriangularDomain(
in MatrixSize enumerator,
int xKernel,
int yKernel
)
{
long order = GetTriangularOrder(enumerator);
return xKernel >= 0 && yKernel >= 0 && xKernel < order && yKernel < order;
}
private sealed class DenseSquareKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long x = xKernel;
long y = yKernel;
long col = enumerator.KernelColumnLength;
// Console.Write($"Attempt to get offset at ({x},{y}), offset is {x * col + y} ");
return x * col + y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinKernelDomain(enumerator, xKernel, yKernel);
}
}
private sealed class LowerTriangleKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long order = GetTriangularOrder(enumerator);
long x = xKernel;
long y = yKernel;
long prefixColumns = x * order - (x * (x - 1L)) / 2L;
return prefixColumns + (y - x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel >= xKernel;
}
}
private sealed unsafe class UpperTriangleKernelBoarder : MatrixKernelBoarder
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetIndexOffset(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
long x = xKernel;
long y = yKernel;
long prefixColumns = (x * (x + 1L)) / 2L;
return prefixColumns + y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool IsInBuffer(
int xKernel,
int yKernel,
in MatrixSize enumerator
)
{
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel <= xKernel;
}
}
}
[StructLayout(LayoutKind.Explicit)]
public struct MatrixSize
{
[FieldOffset(0)] public readonly int ElementColumnLength;
[FieldOffset(4)] public readonly int ElementRowLength;
[FieldOffset(8)] public readonly int KernelColumnLength;
[FieldOffset(12)] public readonly int KernelRowLength;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MatrixSize(
int elementColumnLength,
int elementRowLength,
int kernelColumnLength,
int kernelRowLength
)
{
ElementColumnLength = elementColumnLength;
ElementRowLength = elementRowLength;
KernelColumnLength = kernelColumnLength;
KernelRowLength = kernelRowLength;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct MatrixKernelEnumerator
{
[FieldOffset(0)] public int CurrentColumn;
[FieldOffset(4)] public int CurrentRow;
[FieldOffset(8)] public readonly int KernelColumnLength;
[FieldOffset(12)] public readonly int KernelRowLength;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MatrixKernelEnumerator(
int kernelColumnLength,
int kernelRowLength,
int currentColumn = 0,
int currentRow = 0
)
{
CurrentColumn = currentColumn;
CurrentRow = currentRow;
KernelColumnLength = kernelColumnLength;
KernelRowLength = kernelRowLength;
}
}
}
@@ -1,72 +1,72 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixStorage"
// 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.HighPerformance;
using StgSharp.HighPerformance.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.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using HLSFHandle = global::StgSharp.HighPerformance.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);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixStorage"
// 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.HighPerformance;
using StgSharp.HighPerformance.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.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using HLSFHandle = global::StgSharp.HighPerformance.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);
}
}
}
+107 -107
View File
@@ -1,107 +1,107 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ACos"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ACos(float x)
{
return MathF.Acos(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
vec = hpiby2_head - (p - q);
}
else
{
vec = y + y * u;
}
vec = sign * vec;
return piby2 - vec;
*/
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ACos"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ACos(float x)
{
return MathF.Acos(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
vec = hpiby2_head - (p - q);
}
else
{
vec = y + y * u;
}
vec = sign * vec;
return piby2 - vec;
*/
}
}
}
+119 -119
View File
@@ -1,119 +1,119 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ASin"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
private const float
piby2_tail = 7.5497894159e-08f, /* 0x33a22168 */
hpiby2_head = 7.8539812565e-01f, /* 0x3f490fda */
piby2 = 1.5707963705e+00f; /* 0x3fc90fdb */
/// <summary>
/// Reurns the value of ArcSin(x)
/// </summary>
/// <param _label="x">
/// The sine value of the angle
/// </param>
/// <returns>
///
/// </returns>
/// <exception cref="ArgumentException">
///
/// </exception>
public static float ASin(float x)
{
return MathF.Asin(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
vec = hpiby2_head - (p - q);
}
else
{
vec = y + y * u;
}/*
return sign * vec;
*/
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ASin"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
private const float
piby2_tail = 7.5497894159e-08f, /* 0x33a22168 */
hpiby2_head = 7.8539812565e-01f, /* 0x3f490fda */
piby2 = 1.5707963705e+00f; /* 0x3fc90fdb */
/// <summary>
/// Reurns the value of ArcSin(x)
/// </summary>
/// <param _label="x">
/// The sine value of the angle
/// </param>
/// <returns>
///
/// </returns>
/// <exception cref="ArgumentException">
///
/// </exception>
public static float ASin(float x)
{
return MathF.Asin(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
vec = hpiby2_head - (p - q);
}
else
{
vec = y + y * u;
}/*
return sign * vec;
*/
}
}
}
+116 -116
View File
@@ -1,116 +1,116 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ATan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ATan(float x)
{
return MathF.Atan(x);
/*
float sign = 1, angle = 0;
if (x < 0)
{
sign = -1;
x = -x;
}
uint xBit = *(uint*)&x;
int xExp = (int)(xBit >> 23) - 127;
if (xExp >= 26)
{
return 1;
}
if (xExp <= -15)
{
return x;
}
if (xExp >= 0)
{
x = (x - 1) / (x + 1);
angle = 0.7853981634f;
}
float vec = 0.7853981634f * x + x * (1 - x) * (0.2447f + 0.0663f * x);
vec += angle;
vec *= sign;
return vec;
}
public static unsafe float ATan(float x, float y)
{
float z = y / x;
float sign = 1, angle = 0, angle2 = 0;
if (x < 0)
{
angle = -3.1415926535f;
}
if (z < 0)
{
sign = -1;
z = -z;
}
uint zBit = *(uint*)&z;
int zExp = (int)(zBit >> 23) - 127;
if (zExp >= 26)
{
return 1;
}
if (zExp <= -15)
{
return z;
}
if (zExp >= 0)
{
z = (z - 1) / (z + 1);
angle2 = 0.7853981634f;
}
float vec = 0.7853981634f * z + z * (1 - z) * (0.2447f + 0.0663f * z);
vec *= 57.29577951f;
vec += angle2;
vec += angle;
vec *= sign;
return vec;
*/
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ATan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ATan(float x)
{
return MathF.Atan(x);
/*
float sign = 1, angle = 0;
if (x < 0)
{
sign = -1;
x = -x;
}
uint xBit = *(uint*)&x;
int xExp = (int)(xBit >> 23) - 127;
if (xExp >= 26)
{
return 1;
}
if (xExp <= -15)
{
return x;
}
if (xExp >= 0)
{
x = (x - 1) / (x + 1);
angle = 0.7853981634f;
}
float vec = 0.7853981634f * x + x * (1 - x) * (0.2447f + 0.0663f * x);
vec += angle;
vec *= sign;
return vec;
}
public static unsafe float ATan(float x, float y)
{
float z = y / x;
float sign = 1, angle = 0, angle2 = 0;
if (x < 0)
{
angle = -3.1415926535f;
}
if (z < 0)
{
sign = -1;
z = -z;
}
uint zBit = *(uint*)&z;
int zExp = (int)(zBit >> 23) - 127;
if (zExp >= 26)
{
return 1;
}
if (zExp <= -15)
{
return z;
}
if (zExp >= 0)
{
z = (z - 1) / (z + 1);
angle2 = 0.7853981634f;
}
float vec = 0.7853981634f * z + z * (1 - z) * (0.2447f + 0.0663f * z);
vec *= 57.29577951f;
vec += angle2;
vec += angle;
vec *= sign;
return vec;
*/
}
}
}
+66 -66
View File
@@ -1,67 +1,67 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="BasicOps"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Abs(float x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(float*)a;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int Abs(int x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(int*)a;
}
internal static string GetListSeparator(IFormatProvider formatProvider)
{
if (formatProvider is CultureInfo cultureInfo) {
return cultureInfo.TextInfo.ListSeparator;
}
if (formatProvider?.GetFormat(typeof(TextInfo)) is TextInfo textInfo) {
return textInfo.ListSeparator;
}
return CultureInfo.CurrentCulture.TextInfo.ListSeparator;
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="BasicOps"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Abs(float x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(float*)a;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int Abs(int x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(int*)a;
}
internal static string GetListSeparator(IFormatProvider formatProvider)
{
if (formatProvider is CultureInfo cultureInfo) {
return cultureInfo.TextInfo.ListSeparator;
}
if (formatProvider?.GetFormat(typeof(TextInfo)) is TextInfo textInfo) {
return textInfo.ListSeparator;
}
return CultureInfo.CurrentCulture.TextInfo.ListSeparator;
}
}
}
+64 -64
View File
@@ -1,64 +1,64 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Compare"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public static float MaxOf(params float[] stream)
{
float ret = stream[0];
foreach (float num in stream)
{
if (ret < num) {
ret = num;
}
}
return ret;
}
public static float MinOf(params float[] stream)
{
float ret = stream[0];
foreach (float num in stream)
{
if (ret > num) {
ret = num;
}
}
return ret;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Compare"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public static float MaxOf(params float[] stream)
{
float ret = stream[0];
foreach (float num in stream)
{
if (ret < num) {
ret = num;
}
}
return ret;
}
public static float MinOf(params float[] stream)
{
float ret = stream[0];
foreach (float num in stream)
{
if (ret > num) {
ret = num;
}
}
return ret;
}
}
}
+57 -57
View File
@@ -1,57 +1,57 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Cos"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(float x)
{
/*
float refer = x / 360.0f;
refer -= (int)refer;
refer *= 40;
int a = (int)refer; //大分度,查表
refer -= a; //小分度,拟合
float refer2 = refer * refer;
return ((-sinData[a]) * ((0.1570796327f * refer) - (6.459640975e-4f * refer * refer2)))
+ (cosData[a] * ((1 - (0.0123370055f * refer2)) + (2.536695709e-5f * refer2 * refer2)));
*/
return MathF.Cos(x);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Cos"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(float x)
{
/*
float refer = x / 360.0f;
refer -= (int)refer;
refer *= 40;
int a = (int)refer; //大分度,查表
refer -= a; //小分度,拟合
float refer2 = refer * refer;
return ((-sinData[a]) * ((0.1570796327f * refer) - (6.459640975e-4f * refer * refer2)))
+ (cosData[a] * ((1 - (0.0123370055f * refer2)) + (2.536695709e-5f * refer2 * refer2)));
*/
return MathF.Cos(x);
}
}
}
+52 -52
View File
@@ -1,52 +1,52 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Ctor"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics
{
public unsafe delegate float ScalarCalcHandler(float* x);
public static unsafe partial class Scalar
{
private const uint MEM_COMMIT = 0x1000;
private const uint MEM_RELEASE = 0x8000;
private const uint MEM_RESERVE = 0x2000;
private const uint PAGE_EXECUTE_READWRITE = 0x40;
static Scalar() { }
[DllImport("Kernel32.dll", EntryPoint = "VirtualAlloc")]
private static extern IntPtr VirtualAlloc(IntPtr address, int size, uint allocType, uint protect);
[DllImport("Kernel32.dll", EntryPoint = "VirtualFree")]
private static extern bool VirtualFree(IntPtr address, int size, uint freeType);
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Ctor"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics
{
public unsafe delegate float ScalarCalcHandler(float* x);
public static unsafe partial class Scalar
{
private const uint MEM_COMMIT = 0x1000;
private const uint MEM_RELEASE = 0x8000;
private const uint MEM_RESERVE = 0x2000;
private const uint PAGE_EXECUTE_READWRITE = 0x40;
static Scalar() { }
[DllImport("Kernel32.dll", EntryPoint = "VirtualAlloc")]
private static extern IntPtr VirtualAlloc(IntPtr address, int size, uint allocType, uint protect);
[DllImport("Kernel32.dll", EntryPoint = "VirtualFree")]
private static extern bool VirtualFree(IntPtr address, int size, uint freeType);
}
}
+109 -109
View File
@@ -1,109 +1,109 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Exp"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
public const float E = 2.718281828f;
public const double EDouble = System.Math.E;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Exp(float x)
{
return MathF.Exp(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Exp(double x)
{
return System.Math.Exp(x);
}
/// <summary>
/// Get the value of E ^ x;
/// </summary>
/// <param _label="x">
///
/// </param>
/// <returns>
///
/// </returns>
public static unsafe float ExpSlow(float x)
{
if (x == 1) {
return E;
}
if (x == 0) {
return 1;
}
bool sign = false;
if (x < 0)
{
sign = true;
x = -x;
}
float z = x, t;
if (x < 0.5f)
{
t = 1.0f + (x * (1.0f + (x * (0.5f + (x * (0.1666666667f + (x * (0.0416666666667f + (x * 8.333333333e-3f)))))))));
} else
{
/*
* define z = n * 2 ^ m
* n is between 0 and 0.5
*/
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111) + 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (2.718281828f + (n * (1.359140914f + (n * (0.4530469714f + (n * (0.1132617429f + (n * 0.02265234857f)))))))));
for (int i = 0; i < m; i++) {
t = t * t;
}
}
if (sign) {
t = 1 / t;
}
return t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Exp"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
public const float E = 2.718281828f;
public const double EDouble = System.Math.E;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Exp(float x)
{
return MathF.Exp(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Exp(double x)
{
return System.Math.Exp(x);
}
/// <summary>
/// Get the value of E ^ x;
/// </summary>
/// <param _label="x">
///
/// </param>
/// <returns>
///
/// </returns>
public static unsafe float ExpSlow(float x)
{
if (x == 1) {
return E;
}
if (x == 0) {
return 1;
}
bool sign = false;
if (x < 0)
{
sign = true;
x = -x;
}
float z = x, t;
if (x < 0.5f)
{
t = 1.0f + (x * (1.0f + (x * (0.5f + (x * (0.1666666667f + (x * (0.0416666666667f + (x * 8.333333333e-3f)))))))));
} else
{
/*
* define z = n * 2 ^ m
* n is between 0 and 0.5
*/
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111) + 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (2.718281828f + (n * (1.359140914f + (n * (0.4530469714f + (n * (0.1132617429f + (n * 0.02265234857f)))))))));
for (int i = 0; i < m; i++) {
t = t * t;
}
}
if (sign) {
t = 1 / t;
}
return t;
}
}
}
+68 -68
View File
@@ -1,68 +1,68 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Factorial"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
private static readonly uint[] factorialResult = [
1,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600
];
public static unsafe ulong Factorial(int n)
{
ArgumentOutOfRangeException.ThrowIfLessThan(n, 0);
if (n < 13) {
return factorialResult[n];
}
if (n < 30) {
return NativeIntrinsic.Context.factorial_simd(n);
}
double
dn = n,
stir = Sqrt(2 * PiDouble * dn) * Pow(dn / EDouble, dn),
correction = Exp(1 / (12 * dn));
return (ulong)(stir * correction);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Factorial"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
private static readonly uint[] factorialResult = [
1,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600
];
public static unsafe ulong Factorial(int n)
{
ArgumentOutOfRangeException.ThrowIfLessThan(n, 0);
if (n < 13) {
return factorialResult[n];
}
if (n < 30) {
return NativeIntrinsic.Context.factorial_simd(n);
}
double
dn = n,
stir = Sqrt(2 * PiDouble * dn) * Pow(dn / EDouble, dn),
correction = Exp(1 / (12 * dn));
return (ulong)(stir * correction);
}
}
}
+31 -31
View File
@@ -1,31 +1,31 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Function"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics
{
public class Function { }
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Function"
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics
{
public class Function { }
}
+80 -80
View File
@@ -1,80 +1,80 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Log"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
private static readonly float[] logArray = {
0f, 0.00995033085316809f, 0.0198026272961797f, 0.0295588022415444f, 0.0392207131532813f, 0.048790164169432f, 0.0582689081239758f, 0.0676586484738149f, 0.0769610411361284f, 0.0861776962410524f, 0.0953101798043249f, 0.104360015324243f, 0.113328685307003f, 0.122217632724249f, 0.131028262406404f, 0.139761942375159f, 0.148420005118273f, 0.157003748809665f, 0.165514438477573f, 0.173953307123438f, 0.182321556793955f, 0.19062035960865f, 0.198850858745165f, 0.207014169384326f, 0.215111379616945f, 0.22314355131421f, 0.231111720963387f, 0.2390169004705f, 0.246860077931526f, 0.254642218373581f, 0.262364264467491f, 0.27002713721306f, 0.27763173659828f, 0.285178942233662f, 0.29266961396282f, 0.300104592450338f, 0.307484699747961f, 0.314810739840034f, 0.322083499169113f, 0.3293037471426f, 0.336472236621213f, 0.343589704390077f, 0.350656871613169f, 0.357674444271816f, 0.364643113587909f, 0.371563556432483f, 0.378436435720245f, 0.385262400790645f, 0.392042087776024f, 0.398776119957368f, 0.405465108108164f, 0.412109650826833f, 0.418710334858185f, 0.425267735404344f, 0.431782416425538f, 0.438254930931155f, 0.444685821261446f, 0.451075619360217f, 0.457424847038875f, 0.46373401623214f, 0.470003629245736f, 0.476234178996372f, 0.482426149244293f, 0.488580014818671f, 0.494696241836107f, 0.500775287912489f, 0.506817602368452f, 0.512823626428664f, 0.518793793415168f, 0.524728528934982f, 0.53062825106217f, 0.536493370514568f, 0.542324290825362f, 0.548121408509688f, 0.553885113226438f, 0.559615787935423f, 0.56531380905006f, 0.570979546585738f, 0.576613364303994f, 0.582215619852664f, 0.587786664902119f, 0.593326845277734f, 0.598836501088704f, 0.60431596685333f, 0.609765571620894f, 0.615185639090233f, 0.62057648772511f, 0.625938430866495f, 0.631271776841858f, 0.636576829071551f, 0.641853886172395f, 0.647103242058538f, 0.65232518603969f, 0.657520002916794f, 0.662687973075237f, 0.667829372575655f, 0.672944473242426f, 0.678033542749897f, 0.683096844706444f, 0.688134638736401f,
};
/// <summary>
///
/// </summary>
/// <param _label="x">
///
/// </param>
/// <returns>
/// Returns the value of ln(x).
/// </returns>
public static unsafe float Log(float x)
{
if (x < 0) {
throw new ArgumentOutOfRangeException();
}
uint input = *(uint*)&x;
int f = (int)input;
f >>= 23;
f -= 127;
input <<= 9;
input >>= 9;
input += 0b_0011_1111_1000_0000_0000_0000_0000_0000;
float result = *(float*)&input;
uint order = ((uint)(result * 100)) - 100;
float cache = (result / (1f + (0.01f * order))) - 1;
result = (cache * (-0.25f)) + 0.3333333333333f;
result = (result * cache) - 0.5f;
result = (result * cache) + 1.0f;
result *= cache;
return
(f * 0.6931471806f) + result + logArray[order];
}
public static float Log(float x, float y)
{
return Log(x) / Log(y);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Log"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
private static readonly float[] logArray = {
0f, 0.00995033085316809f, 0.0198026272961797f, 0.0295588022415444f, 0.0392207131532813f, 0.048790164169432f, 0.0582689081239758f, 0.0676586484738149f, 0.0769610411361284f, 0.0861776962410524f, 0.0953101798043249f, 0.104360015324243f, 0.113328685307003f, 0.122217632724249f, 0.131028262406404f, 0.139761942375159f, 0.148420005118273f, 0.157003748809665f, 0.165514438477573f, 0.173953307123438f, 0.182321556793955f, 0.19062035960865f, 0.198850858745165f, 0.207014169384326f, 0.215111379616945f, 0.22314355131421f, 0.231111720963387f, 0.2390169004705f, 0.246860077931526f, 0.254642218373581f, 0.262364264467491f, 0.27002713721306f, 0.27763173659828f, 0.285178942233662f, 0.29266961396282f, 0.300104592450338f, 0.307484699747961f, 0.314810739840034f, 0.322083499169113f, 0.3293037471426f, 0.336472236621213f, 0.343589704390077f, 0.350656871613169f, 0.357674444271816f, 0.364643113587909f, 0.371563556432483f, 0.378436435720245f, 0.385262400790645f, 0.392042087776024f, 0.398776119957368f, 0.405465108108164f, 0.412109650826833f, 0.418710334858185f, 0.425267735404344f, 0.431782416425538f, 0.438254930931155f, 0.444685821261446f, 0.451075619360217f, 0.457424847038875f, 0.46373401623214f, 0.470003629245736f, 0.476234178996372f, 0.482426149244293f, 0.488580014818671f, 0.494696241836107f, 0.500775287912489f, 0.506817602368452f, 0.512823626428664f, 0.518793793415168f, 0.524728528934982f, 0.53062825106217f, 0.536493370514568f, 0.542324290825362f, 0.548121408509688f, 0.553885113226438f, 0.559615787935423f, 0.56531380905006f, 0.570979546585738f, 0.576613364303994f, 0.582215619852664f, 0.587786664902119f, 0.593326845277734f, 0.598836501088704f, 0.60431596685333f, 0.609765571620894f, 0.615185639090233f, 0.62057648772511f, 0.625938430866495f, 0.631271776841858f, 0.636576829071551f, 0.641853886172395f, 0.647103242058538f, 0.65232518603969f, 0.657520002916794f, 0.662687973075237f, 0.667829372575655f, 0.672944473242426f, 0.678033542749897f, 0.683096844706444f, 0.688134638736401f,
};
/// <summary>
///
/// </summary>
/// <param _label="x">
///
/// </param>
/// <returns>
/// Returns the value of ln(x).
/// </returns>
public static unsafe float Log(float x)
{
if (x < 0) {
throw new ArgumentOutOfRangeException();
}
uint input = *(uint*)&x;
int f = (int)input;
f >>= 23;
f -= 127;
input <<= 9;
input >>= 9;
input += 0b_0011_1111_1000_0000_0000_0000_0000_0000;
float result = *(float*)&input;
uint order = ((uint)(result * 100)) - 100;
float cache = (result / (1f + (0.01f * order))) - 1;
result = (cache * (-0.25f)) + 0.3333333333333f;
result = (result * cache) - 0.5f;
result = (result * cache) + 1.0f;
result *= cache;
return
(f * 0.6931471806f) + result + logArray[order];
}
public static float Log(float x, float y)
{
return Log(x) / Log(y);
}
}
}
+121 -121
View File
@@ -1,121 +1,121 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Pow"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Pow(float x, float y)
{
return System.MathF.Pow(x, y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe double Pow(double x, double y)
{
return System.Math.Pow(x, y);
}
/// <summary>
/// calculate the value of x^y
/// </summary>
/// <param _label="x">
///
/// </param>
/// <param _label="y">
///
/// </param>
/// <returns>
/// Returns the value of x^y
/// </returns>
public static unsafe float PowSlow(float x, float y)
{
/*
* x^y = E ^ ( log( x ) * y )
* Now define z = | log(x) * y | = m * n
* and m is an integer, and 0.5 < |n| < 1
* then E ^ z = ( E ^ n ) ^ m
*
*
* As a float number can be expressed in (1+e)*(2^f)
* n = (1+e)/2
* m = 2 ^ (f + 1)
*
*/
float z, t;
if ((x == 1) || (y == 0)) {
return 1;
}
if ((x == 1) && (y == 0)) {
return float.PositiveInfinity;
}
if ((x == 0) && (y != 0)) {
return 0;
}
z = Scalar.Log(x) * y;
bool sign = false;
if (z < 0)
{
sign = true;
z = -z;
}
if (z < 0.5f)
{
t = 1.0f + (z * (1.0f + (z * (0.5f + (z * (0.1666666667f + (z * (0.0416666666667f + (z * 8.333333333e-3f)))))))));
} else
{
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111) + 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (2.718281828f + (n * (1.359140914f + (n * (0.4530469714f + (n * (0.1132617429f + (n * 0.02265234857f)))))))));
for (int i = 0; i < m; i++) {
t = t * t;
}
}
if (sign) {
t = 1 / t;
}
return t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Pow"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Pow(float x, float y)
{
return System.MathF.Pow(x, y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe double Pow(double x, double y)
{
return System.Math.Pow(x, y);
}
/// <summary>
/// calculate the value of x^y
/// </summary>
/// <param _label="x">
///
/// </param>
/// <param _label="y">
///
/// </param>
/// <returns>
/// Returns the value of x^y
/// </returns>
public static unsafe float PowSlow(float x, float y)
{
/*
* x^y = E ^ ( log( x ) * y )
* Now define z = | log(x) * y | = m * n
* and m is an integer, and 0.5 < |n| < 1
* then E ^ z = ( E ^ n ) ^ m
*
*
* As a float number can be expressed in (1+e)*(2^f)
* n = (1+e)/2
* m = 2 ^ (f + 1)
*
*/
float z, t;
if ((x == 1) || (y == 0)) {
return 1;
}
if ((x == 1) && (y == 0)) {
return float.PositiveInfinity;
}
if ((x == 0) && (y != 0)) {
return 0;
}
z = Scalar.Log(x) * y;
bool sign = false;
if (z < 0)
{
sign = true;
z = -z;
}
if (z < 0.5f)
{
t = 1.0f + (z * (1.0f + (z * (0.5f + (z * (0.1666666667f + (z * (0.0416666666667f + (z * 8.333333333e-3f)))))))));
} else
{
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111) + 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (2.718281828f + (n * (1.359140914f + (n * (0.4530469714f + (n * (0.1132617429f + (n * 0.02265234857f)))))))));
for (int i = 0; i < m; i++) {
t = t * t;
}
}
if (sign) {
t = 1 / t;
}
return t;
}
}
}
+126 -126
View File
@@ -1,126 +1,126 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Random"
// 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 System;
using System.Collections.Generic;
using System.Data;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
private static byte[] randomSeed;
internal static float currentSeed
#if DEBUG
= 0.5f;
#else
= Abs(System.DateTime.UtcNow.GetHashCode());
#endif
/// <summary>
/// returns a random float number between 0 and 1.
/// </summary>
/// <returns>
///
/// </returns>
public static unsafe float LowProceisionRandom()
{
float seed = currentSeed;
float sample = Scalar.Pow(Scalar.SeedSqrt(seed), 2);
float sample2 = (sample - seed) / seed;
sample2 = Scalar.Abs(sample2);
if (sample == 1) {
sample = sample2;
}
currentSeed = sample;
sample2 *= 1000000;
float result = sample2 - ((int)sample2);
if (result == 0)
{
currentSeed = sample2 + 0.1f;
result = Scalar.LowProceisionRandom();
}
return result;
}
/// <summary>
///
/// </summary>
/// <returns>
///
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Random<T>() where T : unmanaged, IConvertible, INumber<T>
{
if (randomSeed == null)
{
randomSeed = new byte[64];
string originSeed =
($"{((Assembly.GetEntryAssembly() == null) ? (Assembly.GetAssembly(typeof(World))!) :
Assembly.GetEntryAssembly())}") + ($"{DateTime.UtcNow.ToString("o")}");
randomSeed = SHA256.HashData(Encoding.UTF8.GetBytes(originSeed));
}
randomSeed = SHA256.HashData(randomSeed);
fixed (byte* bptr = randomSeed) {
return *(T*)bptr;
}
}
/// <summary>
/// Generates a random seed in int value.
/// </summary>
/// <returns>
///
/// </returns>
public static unsafe int RandomSeed()
{
float seedValue1 = LowProceisionRandom();
float seedValue2 = LowProceisionRandom();
uint seedValueBits = *(uint*)&seedValue1;
float seedValue = seedValueBits * seedValue2;
return *(int*)&seedValue;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Random"
// 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 System;
using System.Collections.Generic;
using System.Data;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
private static byte[] randomSeed;
internal static float currentSeed
#if DEBUG
= 0.5f;
#else
= Abs(System.DateTime.UtcNow.GetHashCode());
#endif
/// <summary>
/// returns a random float number between 0 and 1.
/// </summary>
/// <returns>
///
/// </returns>
public static unsafe float LowProceisionRandom()
{
float seed = currentSeed;
float sample = Scalar.Pow(Scalar.SeedSqrt(seed), 2);
float sample2 = (sample - seed) / seed;
sample2 = Scalar.Abs(sample2);
if (sample == 1) {
sample = sample2;
}
currentSeed = sample;
sample2 *= 1000000;
float result = sample2 - ((int)sample2);
if (result == 0)
{
currentSeed = sample2 + 0.1f;
result = Scalar.LowProceisionRandom();
}
return result;
}
/// <summary>
///
/// </summary>
/// <returns>
///
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Random<T>() where T : unmanaged, IConvertible, INumber<T>
{
if (randomSeed == null)
{
randomSeed = new byte[64];
string originSeed =
($"{((Assembly.GetEntryAssembly() == null) ? (Assembly.GetAssembly(typeof(World))!) :
Assembly.GetEntryAssembly())}") + ($"{DateTime.UtcNow.ToString("o")}");
randomSeed = SHA256.HashData(Encoding.UTF8.GetBytes(originSeed));
}
randomSeed = SHA256.HashData(randomSeed);
fixed (byte* bptr = randomSeed) {
return *(T*)bptr;
}
}
/// <summary>
/// Generates a random seed in int value.
/// </summary>
/// <returns>
///
/// </returns>
public static unsafe int RandomSeed()
{
float seedValue1 = LowProceisionRandom();
float seedValue2 = LowProceisionRandom();
uint seedValueBits = *(uint*)&seedValue1;
float seedValue = seedValueBits * seedValue2;
return *(int*)&seedValue;
}
}
}
+65 -65
View File
@@ -1,65 +1,65 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="SignOf"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(float x)
{
if (x == 0) {
return 0;
}
if (x > 0) {
return 1;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(int x)
{
if (x == 0) {
return 0;
}
if (x > 0) {
return 1;
}
return -1;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="SignOf"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
public static unsafe partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(float x)
{
if (x == 0) {
return 0;
}
if (x > 0) {
return 1;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(int x)
{
if (x == 0) {
return 0;
}
if (x > 0) {
return 1;
}
return -1;
}
}
}
+47 -47
View File
@@ -1,47 +1,47 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Sin"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public const float Pi = 3.1415926f;
public const double PiDouble = System.Math.PI;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Sin(float x)
{
return MathF.Sin(x);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Sin"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public const float Pi = 3.1415926f;
public const double PiDouble = System.Math.PI;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Sin(float x)
{
return MathF.Sin(x);
}
}
}
+74 -74
View File
@@ -1,74 +1,74 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Sqrt"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public static unsafe float SeedSqrt(float x)
{
ArgumentOutOfRangeException.ThrowIfNegative(x);
if (x <= 1e-12) {
return 0;
}
uint a = *(uint*)&x;
uint f = a;
f >>= 23;
f /= 2;
f += 64;
a -= 0b_0011_1111_0000_0000_0000_0000_0000_0000;
uint e = a << 8;
e >>= 8;
e *= 0b_1101_0001;
e >>= 24;
e += 0b_0000_0000_0001_0000_1010_1000_1110_0010;
f <<= 23;
e += f;
float result = *(float*)&e;
result = (result + (x / result)) / 2;
return (result + (x / result)) / 2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sqrt(float x)
{
return MathF.Sqrt(x);
/**/
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Sqrt(double x)
{
return System.Math.Sqrt(x);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Sqrt"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
public static unsafe float SeedSqrt(float x)
{
ArgumentOutOfRangeException.ThrowIfNegative(x);
if (x <= 1e-12) {
return 0;
}
uint a = *(uint*)&x;
uint f = a;
f >>= 23;
f /= 2;
f += 64;
a -= 0b_0011_1111_0000_0000_0000_0000_0000_0000;
uint e = a << 8;
e >>= 8;
e *= 0b_1101_0001;
e >>= 24;
e += 0b_0000_0000_0001_0000_1010_1000_1110_0010;
f <<= 23;
e += f;
float result = *(float*)&e;
result = (result + (x / result)) / 2;
return (result + (x / result)) / 2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sqrt(float x)
{
return MathF.Sqrt(x);
/**/
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Sqrt(double x)
{
return System.Math.Sqrt(x);
}
}
}
+43 -43
View File
@@ -1,43 +1,43 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Tan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Tan(float x)
{
return MathF.Tan(x);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Tan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics
{
public static partial class Scalar
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Tan(float x)
{
return MathF.Tan(x);
}
}
}