Improve vector and matrix performance and add swizzle support to .net build-int VectorX type.

This commit is contained in:
2025-12-17 16:55:28 +09:00
parent ef2a3a37bd
commit a1ad0bd2da
15 changed files with 2960 additions and 269 deletions

View File

@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;

View File

@@ -1,9 +1,9 @@
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #> <#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #> <#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #> <#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #> <#@ output extension=".gen.cs" #>
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;

View File

@@ -31,7 +31,7 @@
<ItemGroup> <ItemGroup>
<None Update="Collections\FixedText.tt"> <None Update="Collections\FixedText.tt">
<Generator>TextTemplatingFileGenerator</Generator> <Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>FixedText.cs</LastGenOutput> <LastGenOutput>FixedText.gen.cs</LastGenOutput>
</None> </None>
</ItemGroup> </ItemGroup>
@@ -40,7 +40,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Collections\FixedText.cs"> <Compile Update="Collections\FixedText.gen.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DependentUpon>FixedText.tt</DependentUpon> <DependentUpon>FixedText.tt</DependentUpon>

View File

@@ -19,6 +19,12 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
this.typeInfo = typeInfo; this.typeInfo = typeInfo;
sourceBuilder.Clear(); sourceBuilder.Clear();
var message = Validation();
if (message != null)
{
return message;
}
Initialize(); Initialize();
GenerateHeader(); GenerateHeader();
@@ -45,6 +51,11 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
#endregion"); #endregion");
} }
protected virtual string? Validation()
{
return null;
}
protected virtual void Initialize() protected virtual void Initialize()
{ {
} }

View File

@@ -1,13 +1,14 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Text; using System.Text;
namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
{ {
internal class MatrixGenerator : GeneratorBase internal class MatrixGenerator : GeneratorBase
{ {
private readonly List<(string signature, string assignment)> _constructorSignatures = new(); private readonly List<(string signature, List<string> assignment)> _constructorSignatures = new();
private string GetConversionFromTemplate(string template, int componentIndex) private string GetConversionFromTemplate(string template, int componentIndex)
{ {
@@ -15,6 +16,16 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
.Replace("{c}", s_matrixComponents[componentIndex]); .Replace("{c}", s_matrixComponents[componentIndex]);
} }
protected override string? Validation()
{
if (typeInfo.ElementTypeSymbol == null)
{
return "You must specify 'elementType' in NumericTypeAttribute for matrix types.";
}
return null;
}
protected override void GenerateBody() protected override void GenerateBody()
{ {
GenerateField(); GenerateField();
@@ -142,7 +153,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
_constructorSignatures.Add(( _constructorSignatures.Add((
signature: $"{typeInfo.ElementTypeFullName} value", signature: $"{typeInfo.ElementTypeFullName} value",
assignment: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(_ => $"new {typeInfo.ComponentTypeFullName}(value)")))); assignment: Enumerable.Range(0, typeInfo.Column).Select(_ => $"new {typeInfo.ComponentTypeFullName}(value)").ToList()));
var tempSB = new StringBuilder(); var tempSB = new StringBuilder();
for (var r = 0; r < typeInfo.Row; r++) for (var r = 0; r < typeInfo.Row; r++)
@@ -159,16 +170,16 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
_constructorSignatures.Add(( _constructorSignatures.Add((
signature: tempSB.ToString(), signature: tempSB.ToString(),
assignment: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(c => $"new {typeInfo.ComponentTypeFullName}({string.Join(", ", Enumerable.Range(0, typeInfo.Row).Select(r => $"m{r}{c}"))})")))); assignment: Enumerable.Range(0, typeInfo.Column).Select(c => $"new {typeInfo.ComponentTypeFullName}({string.Join(", ", Enumerable.Range(0, typeInfo.Row).Select(r => $"m{r}{c}"))})").ToList()));
} }
_constructorSignatures.Add(( _constructorSignatures.Add((
signature: $"{typeInfo.ComponentTypeFullName} value", signature: $"{typeInfo.ComponentTypeFullName} value",
assignment: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(i => "value")))); assignment: Enumerable.Range(0, typeInfo.Column).Select(i => "value").ToList()));
_constructorSignatures.Add(( _constructorSignatures.Add((
signature: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(i => $"{typeInfo.ComponentTypeFullName} c{i}")), signature: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(i => $"{typeInfo.ComponentTypeFullName} c{i}")),
assignment: string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select(i => $"c{i}")))); assignment: Enumerable.Range(0, typeInfo.Column).Select(i => $"c{i}").ToList()));
if (typeInfo.ConvertableTypes != null) if (typeInfo.ConvertableTypes != null)
{ {
@@ -177,54 +188,35 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
var targetTemplate = kv.Key; var targetTemplate = kv.Key;
var targetTypes = kv.Value; var targetTypes = kv.Value;
var tempSB = new StringBuilder();
foreach (var type in targetTypes) foreach (var type in targetTypes)
{ {
var assignments = new List<string>();
for (var i = 0; i < typeInfo.Column; i++) for (var i = 0; i < typeInfo.Column; i++)
{ {
tempSB.Append(GetConversionFromTemplate(targetTemplate, i)); assignments.Add(GetConversionFromTemplate(targetTemplate, i));
if (i < typeInfo.Column - 1)
{
tempSB.Append(", ");
}
} }
_constructorSignatures.Add(( _constructorSignatures.Add((
signature: $"{type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} v", signature: $"{type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} v",
assignment: tempSB.ToString())); assignment: assignments));
tempSB.Clear();
} }
} }
} }
foreach (var (signature, assignment) in _constructorSignatures) foreach (var (signature, assignment) in _constructorSignatures)
{ {
sourceBuilder.AppendLine($@"
public {typeInfo.TypeName}({signature})
{{
this = Create({assignment});
}}");
}
sourceBuilder.Append($@" sourceBuilder.Append($@"
{INLINE_METHOD_ATTRIBUTE} public {typeInfo.TypeName}({signature})
public static {typeInfo.TypeName} Create({string.Join(", ", Enumerable.Range(0, typeInfo.Column).Select((_, i) => $"{typeInfo.ComponentTypeFullName} {s_matrixComponents[i]}"))}) {{");
{{
global::System.Runtime.CompilerServices.Unsafe.SkipInit(out {typeInfo.TypeFullName} result);
");
for (var i = 0; i < typeInfo.Column; i++) for (var i = 0; i < typeInfo.Column; i++)
{ {
sourceBuilder.Append($@" sourceBuilder.Append($@"
result.{s_matrixComponents[i]} = {s_matrixComponents[i]};"); this.{s_matrixComponents[i]} = {assignment[i]};");
} }
sourceBuilder.AppendLine($@" sourceBuilder.AppendLine($@"
return result;
}}"); }}");
} }
}
private void GenerateUnsafeMethod() private void GenerateUnsafeMethod()
{ {
@@ -467,7 +459,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
{INLINE_METHOD_ATTRIBUTE} {INLINE_METHOD_ATTRIBUTE}
public static {typeInfo.TypeFullName} {typeInfo.TypeName}({signature}) public static {typeInfo.TypeFullName} {typeInfo.TypeName}({signature})
{{ {{
return {typeInfo.TypeFullName}.Create({assignment}); return new {typeInfo.TypeFullName}({string.Join(", ", assignment)});
}}"); }}");
} }
@@ -766,11 +758,47 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
var rhsVectorType = $"{typePrefix}{lhsCols}"; var rhsVectorType = $"{typePrefix}{lhsCols}";
var resultVectorType = $"{typePrefix}{lhsRows}"; var resultVectorType = $"{typePrefix}{lhsRows}";
sourceBuilder.AppendLine($@" var columnSizeBytes = lhsRows * typeInfo.ComponentSize;
var vectorBits = columnSizeBytes > 16 ? 256 : 128;
bool isFloatingPoint = typeInfo.ElementTypeSymbol!.SpecialType == SpecialType.System_Single||
typeInfo.ElementTypeSymbol!.SpecialType == SpecialType.System_Double;
sourceBuilder.Append($@"
{INLINE_METHOD_ATTRIBUTE} {INLINE_METHOD_ATTRIBUTE}
public static {resultVectorType} mul({lhsType} m, {rhsVectorType} v) public static {resultVectorType} mul({lhsType} m, {rhsVectorType} v)
{{ {{");
return {string.Join(" + ", Enumerable.Range(0, lhsCols).Select(c => $"m.{s_matrixComponents[c]} * v.{s_vectorComponents[c]}"))};
for (int i = 0; i < lhsCols; i++)
{
var component = s_vectorComponents[i];
sourceBuilder.Append($@"
var v{component} = global::System.Runtime.Intrinsics.Vector{vectorBits}.Create(v.{component});");
}
sourceBuilder.Append($@"
var sum = global::System.Runtime.Intrinsics.Vector{vectorBits}.Multiply(m.c0.AsVector{vectorBits}(), vx);");
for (int i = 1; i < lhsCols; i++)
{
var component = s_vectorComponents[i];
var col = s_matrixComponents[i];
if (isFloatingPoint)
{
sourceBuilder.Append($@"
sum = global::System.Runtime.Intrinsics.Vector{vectorBits}.FusedMultiplyAdd(m.{col}.AsVector{vectorBits}(), v{component}, sum);");
}
else
{
sourceBuilder.Append($@"
sum = global::System.Runtime.Intrinsics.Vector{vectorBits}.Add(sum,
global::System.Runtime.Intrinsics.Vector{vectorBits}.Multiply(m.{col}.AsVector{vectorBits}(), v{component}));");
}
}
sourceBuilder.AppendLine($@"
return sum.As{typeInfo.ComponentTypeName}();
}}"); }}");
// Vector-Matrix Multiplication: R-element vector * RxC = C-element vector // Vector-Matrix Multiplication: R-element vector * RxC = C-element vector

View File

@@ -7,7 +7,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
internal class VectorGenerator : GeneratorBase internal class VectorGenerator : GeneratorBase
{ {
private int _vectorBitsSize; private int _vectorBitsSize;
private int _missingComponents; private int _missingComponentsCount;
private string _componentTypePrefix = null!; private string _componentTypePrefix = null!;
private readonly List<(string signature, List<string> assignment)> _constructorSignatures = new(); private readonly List<(string signature, List<string> assignment)> _constructorSignatures = new();
@@ -21,7 +21,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
protected override void Initialize() protected override void Initialize()
{ {
var componentSize = typeInfo.ComponentSize; var componentSize = typeInfo.ComponentSize;
var typeSize = componentSize * typeInfo.Row * typeInfo.Column; var typeSize = componentSize * typeInfo.Row;
var vectorBytesSize = typeSize switch var vectorBytesSize = typeSize switch
{ {
//<= 8 => 8, //<= 8 => 8,
@@ -31,7 +31,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
}; };
_vectorBitsSize = vectorBytesSize * 8; _vectorBitsSize = vectorBytesSize * 8;
_missingComponents = (vectorBytesSize - typeSize) / componentSize; _missingComponentsCount = (vectorBytesSize - typeSize) / componentSize;
_componentTypePrefix = typeInfo.ComponentTypeSymbol.SpecialType switch _componentTypePrefix = typeInfo.ComponentTypeSymbol.SpecialType switch
{ {
@@ -224,6 +224,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
var paramNames = new List<string>(); var paramNames = new List<string>();
var paramList = new List<string>(); var paramList = new List<string>();
var assignments = new List<string>(); var assignments = new List<string>();
var fieldOffset = 0; var fieldOffset = 0;
for (var i = 0; i < perm.Count; i++) for (var i = 0; i < perm.Count; i++)
@@ -426,7 +427,6 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
{INLINE_METHOD_ATTRIBUTE} {INLINE_METHOD_ATTRIBUTE}
public void operator +=({typeName} other) public void operator +=({typeName} other)
{{"); {{");
for (var i = 0; i < typeInfo.Row; i++) for (var i = 0; i < typeInfo.Row; i++)
{ {
sourceBuilder.Append($@" sourceBuilder.Append($@"
@@ -759,7 +759,7 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen.Generators
else else
{ {
sourceBuilder.Append($@" sourceBuilder.Append($@"
return global::System.Runtime.Intrinsics.Vector{_vectorBitsSize}.Create({string.Join(", ", Enumerable.Range(0, typeInfo.Row + _missingComponents).Select(i => i < typeInfo.Row ? $"value.{s_vectorComponents[i]}" : "default"))});"); return global::System.Runtime.Intrinsics.Vector{_vectorBitsSize}.Create({string.Join(", ", Enumerable.Range(0, typeInfo.Row + _missingComponentsCount).Select(i => i < typeInfo.Row ? $"value.{s_vectorComponents[i]}" : "default"))});");
} }
sourceBuilder.Append($@" sourceBuilder.Append($@"
}} }}

View File

@@ -2,6 +2,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Misaki.HighPerformance.Mathematics.CodeGen.Generators; using Misaki.HighPerformance.Mathematics.CodeGen.Generators;
using Misaki.HighPerformance.Mathematics.CodeGen.Models; using Misaki.HighPerformance.Mathematics.CodeGen.Models;
using System;
using System.Linq; using System.Linq;
namespace Misaki.HighPerformance.Mathematics.CodeGen namespace Misaki.HighPerformance.Mathematics.CodeGen
@@ -25,10 +26,22 @@ namespace Misaki.HighPerformance.Mathematics.CodeGen
foreach (var typeInfo in types) foreach (var typeInfo in types)
{ {
if (typeInfo is null) if (typeInfo is null)
{
continue; continue;
}
var generator = GetGenerator(typeInfo.Column); var generator = GetGenerator(typeInfo.Column);
var source = generator.Generate(typeInfo);
var source = string.Empty;
try
{
source = generator.Generate(typeInfo);
}
catch (Exception ex)
{
source = $"{ex.Message}\n{ex.StackTrace}";
}
spc.AddSource($"{typeInfo.TypeSymbol.Name}.g.cs", source); spc.AddSource($"{typeInfo.TypeSymbol.Name}.g.cs", source);
} }
}); });

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.2.6</AssemblyVersion> <AssemblyVersion>1.3.0</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
@@ -21,6 +21,14 @@
<IsAotCompatible>True</IsAotCompatible> <IsAotCompatible>True</IsAotCompatible>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="VectorExtension.gen.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>VectorExtension.tt</DependentUpon>
</None>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Misaki.HighPerformance.Mathematics.CodeGen\Misaki.HighPerformance.Mathematics.CodeGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> <ProjectReference Include="..\Misaki.HighPerformance.Mathematics.CodeGen\Misaki.HighPerformance.Mathematics.CodeGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup> </ItemGroup>
@@ -29,4 +37,19 @@
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" /> <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Update="VectorExtension.gen.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>VectorExtension.tt</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="VectorExtension.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>VectorExtension.gen.cs</LastGenOutput>
</None>
</ItemGroup>
</Project> </Project>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,75 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".gen.cs" #>
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Mathematics;
public static class VectorExtension
{
<# static IEnumerable<(string property, bool canSet)> GenerateSwizzles(
string[] pool,
int maxLen)
{
IEnumerable<(string property, bool canSet)> Recurse(string prefix, int depth)
{
if (depth == 0)
{
bool canSet = prefix.Distinct().Count() == prefix.Length;
yield return (prefix, canSet);
}
else
{
foreach (var c in pool)
foreach (var s in Recurse(prefix + c, depth - 1))
yield return s;
}
}
for (int len = 2; len <= maxLen; len++)
foreach (var s in Recurse("", len))
yield return s;
}
void EmitVector(string typePrefix, string[] components)
{
var swizzles = GenerateSwizzles(components, components.Length);
#>
extension(<#= typePrefix + components.Length #> v)
{
<#
foreach (var (property, canSet) in swizzles)
{
var targetDim = property.Length;
var targetStruct = $"{typePrefix}{targetDim}";
#>
public <#= targetStruct #> <#= property #>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return new(<#= string.Join(", ", property.Select(c => $"v.{c}")) #>); }
<# if (canSet)
{
var assigns = string.Join(" ", property
.Select((c, i) => $"v.{c} = value.{components[i]};"));
#>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set { <#= assigns #> }
<# } #>
}
<# } #>
}
<# } #>
<#
EmitVector("Vector", new[] { "X", "Y" });
EmitVector("Vector", new[] { "X", "Y", "Z" });
EmitVector("Vector", new[] { "X", "Y", "Z", "W" });
#>
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,123 +1,168 @@
#define VECTOR_BENCHMARK
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Mathematics; using Misaki.HighPerformance.Mathematics;
using Misaki.HighPerformance.Test.Jobs;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics; using System.Runtime.Intrinsics;
namespace Misaki.HighPerformance.Test.Benchmark; namespace Misaki.HighPerformance.Test.Benchmark;
public unsafe class MathematicsBenchmark public unsafe class MathematicsBenchmark
{ {
public struct f4 public struct f2
{ {
private Vector128<float> _vec; public float x;
public float y;
public f4(float x, float y, float z, float w) public f2(float x, float y)
{ {
_vec = Vector128.Create(x, y, z, w); //this = Asf2(Vector128.Create(x, y, 0, 0));
this.x = x;
this.y = y;
} }
public f4(Vector128<float> vec) [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> AsVector128Unsafe(f2 value)
{ {
_vec = vec; return Vector128.Create(value.x, value.y, 0, 0);
} }
public static f4 operator +(f4 a, f4 b) [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f2 Asf2(Vector128<float> value)
{ {
var result = a._vec + b._vec; //f2 result;
return new f4(result); //result.x = value.GetElement(0);
//result.y = value.GetElement(1);
//return result;
ref byte address = ref Unsafe.As<Vector128<float>, byte>(ref value);
return Unsafe.ReadUnaligned<f2>(ref address);
}
public static f2 operator +(f2 lhs, f2 rhs)
{
//return Asf2(AsVector128Unsafe(lhs) + AsVector128Unsafe(rhs));
return new f2(lhs.x + rhs.x, lhs.y + rhs.y);
} }
} }
[Params(100)] #if VECTOR_BENCHMARK
public int count; private Vector2 _v2a = new Vector2(1, 2);
private Vector2 _v2b = new Vector2(3, 4);
private f2 _f2a = new f2(1, 2);
private f2 _f2b = new f2(3, 4);
[Benchmark] [Benchmark]
public Vector2 Vector2Add() public Vector2 VectorAdd()
{ {
var a = new Vector2(1, 2); var v = new Vector2(0, 0);
var b = new Vector2(3, 4);
var c = new Vector2(5, 6);
for (var i = 0; i < count; i++) for (var i = 0; i < 10; i++)
{ {
c += a + b; v = _v2a + _v2b;
} }
return c; return v;
} }
[Benchmark] [Benchmark]
public float2 Float2Add() public f2 f2Add()
{ {
var a = new float2(1, 2); var v = new f2(0, 0);
var b = new float2(3, 4);
var c = new float2(5, 6);
for (var i = 0; i < count; i++) for (var i = 0; i < 10; i++)
{ {
c += a + b; v = _f2a + _f2b;
} }
return c; return v;
}
#endif
#if NOISE_BENCHMARK
private const int _SIZE = 32;
[Benchmark]
public void VectorNoise()
{
var buf = stackalloc float[_SIZE * _SIZE];
var job = new Misaki.HighPerformance.Test.Jobs.NoiseJobVector
{
buffers = buf,
width = _SIZE,
height = _SIZE,
};
for (var i = 0; i < _SIZE * _SIZE; i++)
{
job.Execute(i, 0);
}
} }
[Benchmark] [Benchmark]
public Vector4 Vector4Add() public void MathNoise()
{ {
var a = new Vector4(1, 2, 3, 4); var buf = stackalloc float[_SIZE * _SIZE];
var b = new Vector4(5, 6, 7, 8); var job = new Misaki.HighPerformance.Test.Jobs.NoiseJobMath
var result = new Vector4(); {
buffers = buf,
width = _SIZE,
height = _SIZE,
};
for (var i = 0; i < count; i++) for (var i = 0; i < _SIZE * _SIZE; i++)
{ {
result += a + b; job.Execute(i, 0);
} }
}
#endif
return result; #if MATRIX_BENCHMARK
private float4x4 _a;
private float4x4 _b;
private Matrix4x4 _ma;
private Matrix4x4 _mb;
[GlobalSetup]
public void Init()
{
_a = new float4x4(
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16);
_b = new float4x4(
16, 15, 14, 13,
12, 11, 10, 9,
8, 7, 6, 5,
4, 3, 2, 1);
_ma = new Matrix4x4(
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16);
_mb = new Matrix4x4(
16, 15, 14, 13,
12, 11, 10, 9,
8, 7, 6, 5,
4, 3, 2, 1);
} }
[Benchmark] [Benchmark]
public float4 Float4Add() public float4x4 Float4x4Multiplication()
{ {
var a = new float4(1, 2, 3, 4); return math.mul(_a, _b);
var b = new float4(5, 6, 7, 8);
var result = new float4();
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
} }
[Benchmark] [Benchmark]
public f4 f4Add() public Matrix4x4 Matrix4x4Multiplication()
{ {
var a = new f4(1, 2, 3, 4); return Matrix4x4.Multiply(_ma, _mb);
var b = new f4(5, 6, 7, 8);
var result = new f4(0, 0, 0, 0);
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
}
[Benchmark]
public Vector128<float> v128Add()
{
var a = Vector128.Create(1f, 2f, 3f, 4f);
var b = Vector128.Create(5f, 6f, 7f, 8f);
var result = Vector128<float>.Zero;
for (var i = 0; i < count; i++)
{
result += a + b;
}
return result;
} }
#endif
} }

View File

@@ -1,4 +1,4 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Jobs; using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
@@ -32,11 +32,11 @@ public class ParallelNoiseBenchmark
} }
[Benchmark] [Benchmark]
public void JobSystem() public unsafe void JobSystem()
{ {
var job = new NoiseJob() var job = new NoiseJobVector()
{ {
buffers = _buffers, buffers = (float*)_buffers.GetUnsafePtr(),
width = _WIDTH, width = _WIDTH,
height = _HEIGHT height = _HEIGHT
}; };
@@ -53,7 +53,7 @@ public class ParallelNoiseBenchmark
var x = i % _WIDTH; var x = i % _WIDTH;
var y = i / _HEIGHT; var y = i / _HEIGHT;
var uv = new Vector2(x, y); var uv = new Vector2(x, y);
_buffers[i] = NoiseJob.GradientNoise(uv); _buffers[i] = NoiseJobVector.GradientNoise(uv);
}); });
} }
@@ -65,7 +65,7 @@ public class ParallelNoiseBenchmark
var x = i % _WIDTH; var x = i % _WIDTH;
var y = i / _HEIGHT; var y = i / _HEIGHT;
var uv = new Vector2(x, y); var uv = new Vector2(x, y);
_buffers[i] = NoiseJob.GradientNoise(uv); _buffers[i] = NoiseJobVector.GradientNoise(uv);
} }
} }
} }

View File

@@ -1,13 +1,13 @@
using Misaki.HighPerformance.Jobs; using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.Mathematics;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Test.Jobs; namespace Misaki.HighPerformance.Test.Jobs;
internal struct NoiseJob : IJobParallelFor internal unsafe struct NoiseJobVector : IJobParallelFor
{ {
public UnsafeArray<float> buffers; public float* buffers;
public int width; public int width;
public int height; public int height;
@@ -49,3 +49,42 @@ internal struct NoiseJob : IJobParallelFor
buffers[loopIndex] = GradientNoise(uv); buffers[loopIndex] = GradientNoise(uv);
} }
} }
internal unsafe struct NoiseJobMath : IJobParallelFor
{
public float* buffers;
public int width;
public int height;
private static float2 GradientNoiseDirect(float2 uv)
{
uv.x %= 289;
uv.y %= 289;
var x = (34 * uv.x + 1) * uv.x % 289 + uv.y;
x = (34 * x + 1) * x % 289;
x = math.frac(x / 41) * 2 - 1;
return math.normalize(new float2(x - math.floor(x + 0.5f), math.abs(x) - 0.5f));
}
public static float GradientNoise(float2 uv)
{
var ip = new float2(math.floor(uv.x), math.floor(uv.y));
var fp = new float2(math.frac(uv.x), math.frac(uv.y));
var d00 = math.dot(GradientNoiseDirect(ip), fp);
var d01 = math.dot(GradientNoiseDirect(ip + new float2(0, 1)), fp - new float2(0, 1));
var d10 = math.dot(GradientNoiseDirect(ip + new float2(1, 0)), fp - new float2(1, 0));
var d11 = math.dot(GradientNoiseDirect(ip + new float2(1, 1)), fp - new float2(1, 1));
fp = fp * fp * fp * (fp * (fp * new float2(6.0f) - new float2(15.0f)) + new float2(10.0f));
return float.Lerp(float.Lerp(d00, d10, fp.y), float.Lerp(d01, d11, fp.y), fp.x);
}
public void Execute(int loopIndex, int threadIndex)
{
var x = loopIndex % width;
var y = loopIndex / height;
var uv = new float2(x, y) / new float2(width, height);
buffers[loopIndex] = GradientNoise(uv);
}
}

View File

@@ -20,12 +20,42 @@
//using Misaki.HighPerformance.LowLevel; //using Misaki.HighPerformance.LowLevel;
//BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.CollectionBenchmark>(); using System.Runtime.Intrinsics;
using Misaki.HighPerformance.Collections; BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.MathematicsBenchmark>();
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
AllocationManager.EnableDebugLayer(); //using Misaki.HighPerformance.Collections;
using var csm = new UnsafeSlotMap<int>(4, Allocator.Persistent); //using Misaki.HighPerformance.LowLevel.Buffer;
AllocationManager.Dispose(); //using Misaki.HighPerformance.LowLevel.Collections;
//AllocationManager.EnableDebugLayer();
//using var csm = new UnsafeSlotMap<int>(4, Allocator.Persistent);
//AllocationManager.Dispose();
//using Misaki.HighPerformance.Mathematics;
//using System.Numerics;
//var a = new Misaki.HighPerformance.Mathematics.float4x4(
// 1, 2, 3, 4,
// 5, 6, 7, 8,
// 9, 10, 11, 12,
// 13, 14, 15, 16);
//var b = new Misaki.HighPerformance.Mathematics.float4x4(
// 16, 15, 14, 13,
// 12, 11, 10, 9,
// 8, 7, 6, 5,
// 4, 3, 2, 1);
//Console.WriteLine(math.mul(a, b));
//var ma = new Matrix4x4(
// 1, 2, 3, 4,
// 5, 6, 7, 8,
// 9, 10, 11, 12,
// 13, 14, 15, 16);
//var mb = new Matrix4x4(
// 16, 15, 14, 13,
// 12, 11, 10, 9,
// 8, 7, 6, 5,
// 4, 3, 2, 1);
//Console.WriteLine(Matrix4x4.Multiply(ma, mb));