Graphic mat 44 is now generic; TextRegex source gen will use goto instead of do{}while()

This commit is contained in:
Youmu Liu
2026-09-07 17:51:15 +08:00
parent ee67dfbde1
commit 90c7538639
27 changed files with 805 additions and 713 deletions
@@ -15,13 +15,61 @@ namespace Nitload.Mathematics.Numeric.Graphics
public static class Vec3
{
public static bool IsParallel<T>(Vec3<T> left, Vec3<T> right) where T: unmanaged,INumber<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<double> ConvertToDouble(
Vec3<long> source
)
{
return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<double> ConvertToDouble(
Vec3<ulong> source
)
{
return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<float> ConvertToSingle(
Vec3<int> source
)
{
return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<float> ConvertToSingle(
Vec3<uint> source
)
{
return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
}
public static Vec3<T> FromSpan<T>(
ReadOnlySpan<T> span
) where T : unmanaged, INumber<T>
{
if (span.Length < 3) {
throw new ArgumentException("Span length must be at least 3.", nameof(span));
}
return new(span[0], span[1], span[2]);
}
public static bool IsParallel<T>(
Vec3<T> left,
Vec3<T> right
) where T : unmanaged,INumber<T>
{
return Cross(left, right).Equals(T.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<T> AsVector128Safe<T>(this Vec3<T> source) where T: unmanaged, INumber<T>
internal static Vector128<T> AsVector128Safe<T>(
this Vec3<T> source
) where T : unmanaged, INumber<T>
{
Vector128<T> result = Vector128<T>.Zero;
Unsafe.WriteUnaligned(ref Unsafe.As<Vector128<T>, byte>(ref result), source);
@@ -29,7 +77,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<T> AsVector128Unsafe<T>(this Vec3<T> source) where T: unmanaged, INumber<T>
internal static Vector128<T> AsVector128Unsafe<T>(
this Vec3<T> source
) where T : unmanaged, INumber<T>
{
Unsafe.SkipInit(out Vector128<T> result);
Unsafe.WriteUnaligned(ref Unsafe.As<Vector128<T>, byte>(ref result), source);
@@ -37,7 +87,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector256<T> AsVector256Safe<T>(this Vec3<T> source) where T: unmanaged, INumber<T>
internal static Vector256<T> AsVector256Safe<T>(
this Vec3<T> source
) where T : unmanaged, INumber<T>
{
Vector256<T> result = Vector256<T>.Zero;
Unsafe.WriteUnaligned(ref Unsafe.As<Vector256<T>, byte>(ref result), source);
@@ -45,7 +97,9 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector256<T> AsVector256Unsafe<T>(this Vec3<T> source) where T: unmanaged, INumber<T>
internal static Vector256<T> AsVector256Unsafe<T>(
this Vec3<T> source
) where T : unmanaged, INumber<T>
{
Unsafe.SkipInit(out Vector256<T> result);
Unsafe.WriteUnaligned(ref Unsafe.As<Vector256<T>, byte>(ref result), source);
@@ -53,45 +107,28 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vec3<T> FromVector128Unsafe<T>(Vector128<T> source) where T: unmanaged, INumber<T>
internal static Vec3<T> FromVector128Unsafe<T>(
Vector128<T> source
) where T : unmanaged, INumber<T>
{
return Unsafe.As<Vector128<T>, Vec3<T>>(ref source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vec3<T> FromVector256Unsafe<T>(Vector256<T> source) where T: unmanaged, INumber<T>
internal static Vec3<T> FromVector256Unsafe<T>(
Vector256<T> source
) where T : unmanaged, INumber<T>
{
return Unsafe.As<Vector256<T>, Vec3<T>>(ref source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<float> ConvertToSingle(Vec3<int> source)
{
return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<float> ConvertToSingle(Vec3<uint> source)
{
return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<double> ConvertToDouble(Vec3<long> source)
{
return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<double> ConvertToDouble(Vec3<ulong> source)
{
return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe()));
}
#region cross
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3<T> Cross<T>(Vec3<T> left, Vec3<T> right) where T: unmanaged, INumber<T>
public static Vec3<T> Cross<T>(
Vec3<T> left,
Vec3<T> right
) where T : unmanaged, INumber<T>
{
if (typeof(T) == typeof(float))
{
@@ -140,7 +177,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<double> CrossDouble(Vec3<double> left, Vec3<double> right)
private static Vec3<double> CrossDouble(
Vec3<double> left,
Vec3<double> right
)
{
Vector256<double> l = left.AsVector256Safe();
Vector256<double> r = right.AsVector256Safe();
@@ -151,7 +191,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<int> CrossInt32(Vec3<int> left, Vec3<int> right)
private static Vec3<int> CrossInt32(
Vec3<int> left,
Vec3<int> right
)
{
Vector128<int> l = left.AsVector128Safe();
Vector128<int> r = right.AsVector128Safe();
@@ -162,7 +205,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<float> CrossSingle(Vec3<float> left, Vec3<float> right)
private static Vec3<float> CrossSingle(
Vec3<float> left,
Vec3<float> right
)
{
Vector128<float> l = left.AsVector128Safe();
Vector128<float> r = right.AsVector128Safe();
@@ -173,7 +219,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<uint> CrossUInt32(Vec3<uint> left, Vec3<uint> right)
private static Vec3<uint> CrossUInt32(
Vec3<uint> left,
Vec3<uint> right
)
{
Vector128<uint> l = left.AsVector128Safe();
Vector128<uint> r = right.AsVector128Safe();
@@ -184,7 +233,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<ulong> CrossUInt64(Vec3<ulong> left, Vec3<ulong> right)
private static Vec3<ulong> CrossUInt64(
Vec3<ulong> left,
Vec3<ulong> right
)
{
Vector256<ulong> l = left.AsVector256Safe();
Vector256<ulong> r = right.AsVector256Safe();
@@ -195,7 +247,10 @@ namespace Nitload.Mathematics.Numeric.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vec3<long> CrossInt64(Vec3<long> left, Vec3<long> right)
private static Vec3<long> CrossInt64(
Vec3<long> left,
Vec3<long> right
)
{
Vector256<long> l = left.AsVector256Safe();
Vector256<long> r = right.AsVector256Safe();