-
-
Notifications
You must be signed in to change notification settings - Fork 51
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 #44 from KoziLord/master
Add a Remainder method
- Loading branch information
Showing
8 changed files
with
270 additions
and
21 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
samples/MathSharp.Interactive/Benchmarks/Vector/Double/RemainderBenchmark.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,43 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using OpenTK; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace MathSharp.Interactive.Benchmarks.Vector.Double | ||
{ | ||
using Vector = MathSharp.Vector; | ||
public class RemainderBenchmark | ||
{ | ||
private Vector4d vec; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
vec = new Vector4d(0, 1.2f, 1.6f, 3.2f); | ||
} | ||
[Benchmark] | ||
public void Normal() | ||
{ | ||
var result = normal(vec, 2); | ||
} | ||
|
||
[Benchmark] | ||
public void MathSharp() | ||
{ | ||
var result = mathSharp(vec, 2); | ||
} | ||
|
||
//Prevents the JIT from compiling Normal() away. | ||
private unsafe Vector4d mathSharp(Vector4d vector, double divisor) | ||
{ | ||
var v = Avx.LoadVector256((double*)&vector); | ||
v = Vector.Remainder(v, divisor); | ||
|
||
Vector4d result; | ||
Avx.Store((double*)&result, v); | ||
return result; | ||
} | ||
|
||
private Vector4d normal(Vector4d vector, double divisor) | ||
=> new Vector4d(vector.X % divisor, vector.Y % divisor, vector.Z % divisor, vector.W % divisor); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
samples/MathSharp.Interactive/Benchmarks/Vector/Single/RemainderBenchmark.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,39 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System.Numerics; | ||
namespace MathSharp.Interactive.Benchmarks.Vector.Single | ||
{ | ||
using Vector = MathSharp.Vector; | ||
public class RemainderBenchmark | ||
{ | ||
private Vector4 vec; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
vec = new Vector4(0, 1.2f, 1.6f, 3.2f); | ||
} | ||
[Benchmark] | ||
public void Normal() | ||
{ | ||
var result = normal(vec, 2); | ||
} | ||
|
||
[Benchmark] | ||
public void MathSharp() | ||
{ | ||
var result = mathSharp(vec, 2); | ||
} | ||
|
||
//Prevents the JIT from compiling Normal() away. | ||
private Vector4 mathSharp(Vector4 vector, float divisor) | ||
{ | ||
var v = vector.Load(); | ||
v = Vector.Remainder(v, divisor); | ||
Vector.Store(v, out Vector4 result); | ||
return result; | ||
} | ||
|
||
private Vector4 normal(Vector4 vector, float divisor) | ||
=> new Vector4(vector.X % divisor, vector.Y % divisor, vector.Z % divisor, vector.W % divisor); | ||
} | ||
} |
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
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
41 changes: 41 additions & 0 deletions
41
tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Remainder.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,41 @@ | ||
using OpenTK; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.Intrinsics; | ||
using System.Runtime.Intrinsics.X86; | ||
using System.Text; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests | ||
{ | ||
public class Remainder | ||
{ | ||
public static IEnumerable<object[]> Data => | ||
new[] | ||
{ | ||
new object[] { new Vector4d(10.4f), new Vector4d(2f) }, | ||
new object[] { new Vector4d(-10.4f), new Vector4d(2f) }, | ||
new object[] { new Vector4d(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4d(3f) }, | ||
new object[] { new Vector4d(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4d(2.4f) }, | ||
new object[] { new Vector4d(23.45f), new Vector4d(219f) }, | ||
new object[] { new Vector4d(12), new Vector4d(float.NaN) }, | ||
new object[] { new Vector4d(-10000000f), new Vector4d(2.2f) }, | ||
new object[] { new Vector4d(10000000f), new Vector4d(2.2f) }, | ||
new object[] { new Vector4d(float.NegativeInfinity), new Vector4d(3f) } | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(Data))] | ||
public unsafe void Remainder_Theory(Vector4d left, Vector4d right) | ||
{ | ||
var l = Avx.LoadAlignedVector256((double*)&left); | ||
var r = Avx.LoadAlignedVector256((double*)&right); | ||
|
||
var remainder = Vector.Remainder(l, r); | ||
var expected = new Vector4d(left.X % right.X, left.Y % right.Y, left.Z % right.Z, left.W % right.W); | ||
|
||
TestHelpers.AreEqual(expected, remainder); | ||
} | ||
} | ||
} |
Oops, something went wrong.