Compare commits

..

4 Commits

Author SHA1 Message Date
Oliver Booth 31eeaec94e
Merge db96c9e6fb into 8eaa01b505 2024-06-12 13:27:43 +01:00
Oliver Booth db96c9e6fb
refactor(test): exclude platform-specific Unpack impl
can't have coverage on SSE3/AVX2 instructions if CPU doesn't support it.
2024-06-12 13:27:14 +01:00
Oliver Booth d022a71ce6
test: add tests for INumber<T>.Sign 2024-06-12 13:15:54 +01:00
Oliver Booth e51296d285
Merge branch 'release/4.0.0' into feature/generic_math 2024-06-12 12:10:27 +01:00
2 changed files with 55 additions and 14 deletions

View File

@ -0,0 +1,32 @@
#if NET7_0_OR_GREATER
using NUnit.Framework;
using X10D.Math;
namespace X10D.Tests.Numerics;
[TestFixture]
internal class NumberTests
{
[Test]
public void Sign_ShouldReturn1_GivenPositiveNumber()
{
Assert.That(NumberExtensions.Sign(2), Is.Positive);
Assert.That(NumberExtensions.Sign(2), Is.EqualTo(1));
}
[Test]
public void Sign_Should0_GivenZero()
{
Assert.That(NumberExtensions.Sign(0), Is.Not.Positive);
Assert.That(NumberExtensions.Sign(0), Is.Not.Negative);
Assert.That(NumberExtensions.Sign(0), Is.EqualTo(0));
}
[Test]
public void Sign_ShouldReturnNegative1_GivenNegativeNumber()
{
Assert.That(NumberExtensions.Sign(-2), Is.Negative);
Assert.That(NumberExtensions.Sign(-2), Is.EqualTo(-1));
}
}
#endif

View File

@ -1,4 +1,5 @@
#if NET7_0_OR_GREATER #if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -48,6 +49,28 @@ public static class BinaryIntegerExtensions
} }
} }
UnpackInternal(value, destination);
}
[MethodImpl(CompilerResources.MaxOptimization)]
private static void UnpackInternal_Fallback<TInteger>(this TInteger value, Span<bool> destination)
where TInteger : unmanaged, IBinaryInteger<TInteger>
{
unsafe
{
int bitCount = sizeof(TInteger) * 8;
for (var index = 0; index < bitCount; index++)
{
destination[index] = (value & (TInteger.One << index)) != TInteger.Zero;
}
}
}
[ExcludeFromCodeCoverage]
[MethodImpl(CompilerResources.MaxOptimization)]
private static void UnpackInternal<TInteger>(TInteger value, Span<bool> destination)
where TInteger : unmanaged, IBinaryInteger<TInteger>
{
switch (value) switch (value)
{ {
case byte valueByte when Sse3.IsSupported: case byte valueByte when Sse3.IsSupported:
@ -71,19 +94,5 @@ public static class BinaryIntegerExtensions
break; break;
} }
} }
[MethodImpl(CompilerResources.MaxOptimization)]
internal static void UnpackInternal_Fallback<TInteger>(this TInteger value, Span<bool> destination)
where TInteger : unmanaged, IBinaryInteger<TInteger>
{
unsafe
{
int bitCount = sizeof(TInteger) * 8;
for (var index = 0; index < bitCount; index++)
{
destination[index] = (value & (TInteger.One << index)) != TInteger.Zero;
}
}
}
} }
#endif #endif