feat(numeric): add scalar conversions and quaternion fix

Added [NumericConvertable] attributes for scalar type conversions to all vector structs. Fixed bitwise mask calculation in quaternion.cs for improved correctness. Updated Program.cs with quaternion test code and bumped project version to 1.3.2.
This commit is contained in:
2026-04-07 20:09:37 +09:00
parent 9824c1ed19
commit 81eb5cb4cf
7 changed files with 28 additions and 8 deletions

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.3.1</AssemblyVersion> <AssemblyVersion>1.3.2</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl> <RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>

View File

@@ -2,6 +2,7 @@ namespace Misaki.HighPerformance.Mathematics;
[NumericType(typeof(double), sizeof(double), 2, 1, "global::Misaki.HighPerformance.Mathematics.double")] [NumericType(typeof(double), sizeof(double), 2, 1, "global::Misaki.HighPerformance.Mathematics.double")]
[NumericConvertable("(double){v}.{c}", typeof(int2), typeof(uint2), typeof(float2), typeof(bool2))] [NumericConvertable("(double){v}.{c}", typeof(int2), typeof(uint2), typeof(float2), typeof(bool2))]
[NumericConvertable("(double){v}", typeof(int), typeof(uint), typeof(float))]
public partial struct double2 public partial struct double2
{ {
} }
@@ -23,6 +24,7 @@ public partial struct double2x4
[NumericType(typeof(double), sizeof(double), 3, 1, "global::Misaki.HighPerformance.Mathematics.double")] [NumericType(typeof(double), sizeof(double), 3, 1, "global::Misaki.HighPerformance.Mathematics.double")]
[NumericConvertable("(double){v}.{c}", typeof(int3), typeof(uint3), typeof(float3), typeof(bool3))] [NumericConvertable("(double){v}.{c}", typeof(int3), typeof(uint3), typeof(float3), typeof(bool3))]
[NumericConvertable("(double){v}", typeof(int), typeof(uint), typeof(float))]
public partial struct double3 public partial struct double3
{ {
} }
@@ -44,6 +46,7 @@ public partial struct double3x4
[NumericType(typeof(double), sizeof(double), 4, 1, "global::Misaki.HighPerformance.Mathematics.double")] [NumericType(typeof(double), sizeof(double), 4, 1, "global::Misaki.HighPerformance.Mathematics.double")]
[NumericConvertable("(double){v}.{c}", typeof(int4), typeof(uint4), typeof(float4), typeof(bool4))] [NumericConvertable("(double){v}.{c}", typeof(int4), typeof(uint4), typeof(float4), typeof(bool4))]
[NumericConvertable("(double){v}", typeof(int), typeof(uint), typeof(float))]
public partial struct double4 public partial struct double4
{ {
} }

View File

@@ -2,6 +2,7 @@ namespace Misaki.HighPerformance.Mathematics;
[NumericType(typeof(float), sizeof(float), 2, 1, "global::Misaki.HighPerformance.Mathematics.float")] [NumericType(typeof(float), sizeof(float), 2, 1, "global::Misaki.HighPerformance.Mathematics.float")]
[NumericConvertable("(float){v}.{c}", typeof(int2), typeof(uint2), typeof(double2), typeof(bool2))] [NumericConvertable("(float){v}.{c}", typeof(int2), typeof(uint2), typeof(double2), typeof(bool2))]
[NumericConvertable("(float){v}", typeof(int), typeof(uint), typeof(double))]
public partial struct float2 public partial struct float2
{ {
} }
@@ -26,6 +27,7 @@ public partial struct float2x4
[NumericType(typeof(float), sizeof(float), 3, 1, "global::Misaki.HighPerformance.Mathematics.float")] [NumericType(typeof(float), sizeof(float), 3, 1, "global::Misaki.HighPerformance.Mathematics.float")]
[NumericConvertable("(float){v}.{c}", typeof(int3), typeof(uint3), typeof(double3), typeof(bool3))] [NumericConvertable("(float){v}.{c}", typeof(int3), typeof(uint3), typeof(double3), typeof(bool3))]
[NumericConvertable("(float){v}", typeof(int), typeof(uint), typeof(double))]
public partial struct float3 public partial struct float3
{ {
} }
@@ -62,6 +64,7 @@ public partial struct float3x4
[NumericType(typeof(float), sizeof(float), 4, 1, "global::Misaki.HighPerformance.Mathematics.float")] [NumericType(typeof(float), sizeof(float), 4, 1, "global::Misaki.HighPerformance.Mathematics.float")]
[NumericConvertable("(float){v}.{c}", typeof(int4), typeof(uint4), typeof(double4), typeof(bool4))] [NumericConvertable("(float){v}.{c}", typeof(int4), typeof(uint4), typeof(double4), typeof(bool4))]
[NumericConvertable("(float){v}", typeof(int), typeof(uint), typeof(double))]
public partial struct float4 public partial struct float4
{ {
} }

View File

@@ -2,6 +2,7 @@ namespace Misaki.HighPerformance.Mathematics;
[NumericType(typeof(int), sizeof(int), 2, 1, "global::Misaki.HighPerformance.Mathematics.int")] [NumericType(typeof(int), sizeof(int), 2, 1, "global::Misaki.HighPerformance.Mathematics.int")]
[NumericConvertable("(int){v}.{c}", typeof(uint2), typeof(float2), typeof(double2), typeof(bool2))] [NumericConvertable("(int){v}.{c}", typeof(uint2), typeof(float2), typeof(double2), typeof(bool2))]
[NumericConvertable("(int){v}", typeof(uint), typeof(float), typeof(double))]
public partial struct int2 public partial struct int2
{ {
} }
@@ -23,6 +24,7 @@ public partial struct int2x4
[NumericType(typeof(int), sizeof(int), 3, 1, "global::Misaki.HighPerformance.Mathematics.int")] [NumericType(typeof(int), sizeof(int), 3, 1, "global::Misaki.HighPerformance.Mathematics.int")]
[NumericConvertable("(int){v}.{c}", typeof(uint3), typeof(float3), typeof(double3), typeof(bool3))] [NumericConvertable("(int){v}.{c}", typeof(uint3), typeof(float3), typeof(double3), typeof(bool3))]
[NumericConvertable("(int){v}", typeof(uint), typeof(float), typeof(double))]
public partial struct int3 public partial struct int3
{ {
} }
@@ -44,6 +46,7 @@ public partial struct int3x4
[NumericType(typeof(int), sizeof(int), 4, 1, "global::Misaki.HighPerformance.Mathematics.int")] [NumericType(typeof(int), sizeof(int), 4, 1, "global::Misaki.HighPerformance.Mathematics.int")]
[NumericConvertable("(int){v}.{c}", typeof(uint4), typeof(float4), typeof(double4), typeof(bool4))] [NumericConvertable("(int){v}.{c}", typeof(uint4), typeof(float4), typeof(double4), typeof(bool4))]
[NumericConvertable("(int){v}", typeof(uint), typeof(float), typeof(double))]
public partial struct int4 public partial struct int4
{ {
} }

View File

@@ -57,14 +57,14 @@ public partial struct quaternion : IEquatable<quaternion>
var u_sign = (asuint(u.x) & 0x80000000); var u_sign = (asuint(u.x) & 0x80000000);
var t = v.y + asfloat(asuint(w.z) ^ u_sign); var t = v.y + asfloat(asuint(w.z) ^ u_sign);
var u_mask = new uint4(u_sign >> 31); var u_mask = new uint4((int)u_sign >> 31);
var t_mask = new uint4(asuint(t) >> 31); var t_mask = new uint4(asint(t) >> 31);
var tr = 1.0f + abs(u.x); float tr = 1.0f + abs(u.x);
var sign_flips = new uint4(0x00000000, 0x80000000, 0x80000000, 0x80000000) ^ (u_mask & new uint4(0x00000000, 0x80000000, 0x00000000, 0x80000000)) ^ (t_mask & new uint4(0x80000000, 0x80000000, 0x80000000, 0x00000000)); var sign_flips = new uint4(0x00000000, 0x80000000, 0x80000000, 0x80000000) ^ (u_mask & new uint4(0x00000000, 0x80000000, 0x00000000, 0x80000000)) ^ (t_mask & new uint4(0x80000000, 0x80000000, 0x80000000, 0x00000000));
value = new float4(tr, u.y, w.x, v.z) + asfloat(asuint(new float4(t, v.x, u.z, w.y)) ^ sign_flips); // +---, +++-, ++-+, +-++ value = float4(tr, u.y, w.x, v.z) + asfloat(asuint(float4(t, v.x, u.z, w.y)) ^ sign_flips); // +---, +++-, ++-+, +-++
value = asfloat((asuint(value) & ~u_mask) | (asuint(value.zwxy) & u_mask)); value = asfloat((asuint(value) & ~u_mask) | (asuint(value.zwxy) & u_mask));
value = asfloat((asuint(value.wzyx) & ~t_mask) | (asuint(value) & t_mask)); value = asfloat((asuint(value.wzyx) & ~t_mask) | (asuint(value) & t_mask));
@@ -81,8 +81,8 @@ public partial struct quaternion : IEquatable<quaternion>
var u_sign = (asuint(u.x) & 0x80000000); var u_sign = (asuint(u.x) & 0x80000000);
var t = v.y + asfloat(asuint(w.z) ^ u_sign); var t = v.y + asfloat(asuint(w.z) ^ u_sign);
var u_mask = new uint4(u_sign >> 31); var u_mask = new uint4((int)u_sign >> 31);
var t_mask = new uint4(asuint(t) >> 31); var t_mask = new uint4(asint(t) >> 31);
var tr = 1.0f + abs(u.x); var tr = 1.0f + abs(u.x);

View File

@@ -2,6 +2,7 @@ namespace Misaki.HighPerformance.Mathematics;
[NumericType(typeof(uint), sizeof(uint), 2, 1, "global::Misaki.HighPerformance.Mathematics.uint")] [NumericType(typeof(uint), sizeof(uint), 2, 1, "global::Misaki.HighPerformance.Mathematics.uint")]
[NumericConvertable("(uint){v}.{c}", typeof(int2), typeof(float2), typeof(double2), typeof(bool2))] [NumericConvertable("(uint){v}.{c}", typeof(int2), typeof(float2), typeof(double2), typeof(bool2))]
[NumericConvertable("(uint){v}", typeof(int), typeof(float), typeof(double))]
public partial struct uint2 public partial struct uint2
{ {
} }
@@ -23,6 +24,7 @@ public partial struct uint2x4
[NumericType(typeof(uint), sizeof(uint), 3, 1, "global::Misaki.HighPerformance.Mathematics.uint")] [NumericType(typeof(uint), sizeof(uint), 3, 1, "global::Misaki.HighPerformance.Mathematics.uint")]
[NumericConvertable("(uint){v}.{c}", typeof(int3), typeof(float3), typeof(double3), typeof(bool3))] [NumericConvertable("(uint){v}.{c}", typeof(int3), typeof(float3), typeof(double3), typeof(bool3))]
[NumericConvertable("(uint){v}", typeof(int), typeof(float), typeof(double))]
public partial struct uint3 public partial struct uint3
{ {
} }
@@ -44,6 +46,7 @@ public partial struct uint3x4
[NumericType(typeof(uint), sizeof(uint), 4, 1, "global::Misaki.HighPerformance.Mathematics.uint")] [NumericType(typeof(uint), sizeof(uint), 4, 1, "global::Misaki.HighPerformance.Mathematics.uint")]
[NumericConvertable("(uint){v}.{c}", typeof(int4), typeof(float4), typeof(double4), typeof(bool4))] [NumericConvertable("(uint){v}.{c}", typeof(int4), typeof(float4), typeof(double4), typeof(bool4))]
[NumericConvertable("(uint){v}", typeof(int), typeof(float), typeof(double))]
public partial struct uint4 public partial struct uint4
{ {
} }

View File

@@ -1,4 +1,12 @@
using BenchmarkDotNet.Running; using BenchmarkDotNet.Running;
using Misaki.HighPerformance.Mathematics;
using Misaki.HighPerformance.Test.Benchmark; using Misaki.HighPerformance.Test.Benchmark;
BenchmarkRunner.Run<SPMDBenchmark>(); //BenchmarkRunner.Run<SPMDBenchmark>();
var faceDirection = math.normalize(float3.zero - new float3(0.0f, 0.0f, 5.0f));
var test = quaternion.LookRotation(faceDirection, math.up());
var rotation = quaternion.EulerXYZ(new float3(0, math.radians(180.0f), 0));
Console.WriteLine(test);
Console.WriteLine(rotation);