-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from NoiseStudio/feature/279/operators-in-nesl
Operators in NESL
- Loading branch information
Showing
28 changed files
with
506 additions
and
125 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
NoiseEngine.Tests/Nesl/CompilerTools/Execute/Methods/Call/CallOpCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using NoiseEngine.Tests.Environments; | ||
using NoiseEngine.Tests.Fixtures; | ||
|
||
namespace NoiseEngine.Tests.Nesl.CompilerTools.Execute.Methods.Call; | ||
|
||
public class CallOpCode : NeslExecuteTestEnvironment { | ||
|
||
public CallOpCode(ApplicationFixture fixture) : base(fixture) { | ||
} | ||
|
||
[Fact] | ||
public void Add() { | ||
RunKernelWithSingleBuffer(@" | ||
using System; | ||
uniform RwBuffer<f32> buffer; | ||
[Kernel(1, 1, 1)] | ||
void Main() { | ||
buffer[0] = 1.0 + 2.0; | ||
} | ||
", null, 3f); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
NoiseEngine.Tests/Nesl/CompilerTools/Execute/Methods/Operators.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using NoiseEngine.Tests.Environments; | ||
using NoiseEngine.Tests.Fixtures; | ||
using System; | ||
|
||
namespace NoiseEngine.Tests.Nesl.CompilerTools.Execute.Methods; | ||
|
||
public class Operators : NeslExecuteTestEnvironment { | ||
|
||
public Operators(ApplicationFixture fixture) : base(fixture) { | ||
} | ||
|
||
[Theory] | ||
[InlineData(new uint[] { | ||
32, 51, 44, 91, | ||
97, 21, 90, 35, | ||
129, 72, 134, 126 | ||
})] | ||
[InlineData(new int[] { | ||
89, -90, 10, 28, | ||
91, 18, -31, 75, | ||
180, -72, -21, 103 | ||
})] | ||
[InlineData(new float[] { | ||
-3.51834f, 4.60347f, 5.26284f, 4.37281f, | ||
1.30167f, 6.68611f, -7.74374f, 9.32226f, | ||
-2.21667f, 11.2895793914794921875f, -2.480900287628173828125f, 13.69507f | ||
})] | ||
public void Add(object values) { | ||
Invoker(values, "+"); | ||
} | ||
|
||
private void Invoker(object values, string op) { | ||
// Use typeof instead of is keyword to avoid implicit conversion. | ||
Type type = values.GetType(); | ||
if (type == typeof(uint[])) | ||
InvokerImpl((uint[])values, "u32", op); | ||
else if (type == typeof(ulong[])) | ||
InvokerImpl((ulong[])values, "u64", op); | ||
else if (type == typeof(int[])) | ||
InvokerImpl((int[])values, "i32", op); | ||
else if (type == typeof(long[])) | ||
InvokerImpl((long[])values, "i64", op); | ||
else if (type == typeof(float[])) | ||
InvokerImpl((float[])values, "f32", op); | ||
else if (type == typeof(double[])) | ||
InvokerImpl((double[])values, "f64", op); | ||
else | ||
throw new ArgumentException("Invalid type", nameof(values)); | ||
} | ||
|
||
private void InvokerImpl<T>(T[] values, string type, string op) where T : unmanaged { | ||
for (int i = 1; i <= 1; i++) { | ||
int block = values.Length / 3; | ||
int initialLength = block * 2; | ||
T[] initialValues = values.AsSpan(0, initialLength).ToArray(); | ||
T[] expectedValues = values.AsSpan(initialLength).ToArray(); | ||
|
||
RunKernelWithSingleBuffer($@" | ||
using System; | ||
uniform RwBuffer<{type}> buffer; | ||
[Kernel({block}, 1, 1)] | ||
void Main() {{ | ||
buffer[ComputeUtils.GlobalInvocation3.X] = buffer[ComputeUtils.GlobalInvocation3.X] {op} | ||
buffer[ComputeUtils.GlobalInvocation3.X + {block}]; | ||
}} | ||
", initialValues, expectedValues); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
using System.Operators; | ||
|
||
[Size(32)] | ||
[PlatformDependentTypeRepresentation("OpTypeFloat`32")] | ||
public struct Float32 { | ||
public struct Float32 : IAdd<Float32, Float32, Float32> { | ||
|
||
[CallOpCode(1)] | ||
public static Float32 Add(Float32 lhs, Float32 rhs) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
using System.Operators; | ||
|
||
[Size(64)] | ||
[PlatformDependentTypeRepresentation("OpTypeFloat`64")] | ||
public struct Float64 { | ||
public struct Float64 : IAdd<Float64, Float64, Float64> { | ||
|
||
[CallOpCode(1)] | ||
public static Float64 Add(Float64 lhs, Float64 rhs) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
using System.Operators; | ||
|
||
[Size(32)] | ||
[PlatformDependentTypeRepresentation("OpTypeInt`32`1")] | ||
public struct Int32 { | ||
public struct Int32 : IAdd<Int32, Int32, Int32> { | ||
|
||
[CallOpCode(1)] | ||
public static Int32 Add(Int32 lhs, Int32 rhs) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public interface IAdd<TLhs, TRhs, TResult> { | ||
|
||
static TResult Add(TLhs lhs, TRhs rhs); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
using System.Operators; | ||
|
||
[Size(32)] | ||
[PlatformDependentTypeRepresentation("OpTypeInt`32`0")] | ||
public struct UInt32 { | ||
public struct UInt32 : IAdd<UInt32, UInt32, UInt32> { | ||
|
||
[CallOpCode(1)] | ||
public static UInt32 Add(UInt32 lhs, UInt32 rhs) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.