Add optimized modulo helpers for unsigned integers
This commit is contained in:
1 parent
2c2bc62cb2
commit
95a0b030f6
7 files changed
+425
-84
No files matched your search
@@ -209,6 +209,30 @@ public class UInt128ExtensionsTests
|
||||
value.Mod5().Should().Be((ulong)(value % 5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Category", "Fast")]
|
||||
public void Mod7_matches_operator()
|
||||
{
|
||||
UInt128 value = (UInt128.One << 100) + 9876543210987654321UL;
|
||||
value.Mod7().Should().Be((ulong)(value % 7));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Category", "Fast")]
|
||||
public void Mod11_matches_operator()
|
||||
{
|
||||
UInt128 value = (UInt128.One << 120) + 9876543210987654321UL;
|
||||
value.Mod11().Should().Be((ulong)(value % 11));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Category", "Fast")]
|
||||
public void Mod128_matches_operator()
|
||||
{
|
||||
UInt128 value = (UInt128.One << 120) + 9876543210987654321UL;
|
||||
value.Mod128().Should().Be((ulong)(value % 128));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Category", "Fast")]
|
||||
public void Mul64_matches_expected_product()
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace PerfectNumbers.Core.Tests;
|
||||
|
||||
public class UIntExtensionsTests
|
||||
{
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod3_matches_operator(uint value)
|
||||
{
|
||||
value.Mod3().Should().Be(value % 3U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod5_matches_operator(uint value)
|
||||
{
|
||||
value.Mod5().Should().Be(value % 5U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod7_matches_operator(uint value)
|
||||
{
|
||||
value.Mod7().Should().Be(value % 7U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod8_matches_operator(uint value)
|
||||
{
|
||||
value.Mod8().Should().Be(value % 8U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod10_matches_operator(uint value)
|
||||
{
|
||||
value.Mod10().Should().Be(value % 10U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod11_matches_operator(uint value)
|
||||
{
|
||||
value.Mod11().Should().Be(value % 11U);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0U)]
|
||||
[InlineData(1U)]
|
||||
[InlineData(1234567890U)]
|
||||
[InlineData(uint.MaxValue)]
|
||||
public void Mod128_matches_operator(uint value)
|
||||
{
|
||||
value.Mod128().Should().Be(value % 128U);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,86 @@ public class ULongExtensionsTests
|
||||
ULongExtensions.Mod10(value).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod3_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod3().Should().Be(value % 3UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod5_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod5().Should().Be(value % 5UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod7_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod7().Should().Be(value % 7UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod8_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod8().Should().Be(value % 8UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod11_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod11().Should().Be(value % 11UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
[InlineData(1234567890123456789UL)]
|
||||
public void Mod128_matches_operator(ulong value)
|
||||
{
|
||||
value.Mod128().Should().Be(value % 128UL);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(1UL)]
|
||||
[InlineData(123456789UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
public void Mod10_8_5_3Steps_matches_manual_formula(ulong value)
|
||||
{
|
||||
value.Mod10_8_5_3Steps(out ulong step10, out ulong step8, out ulong step5, out ulong step3);
|
||||
|
||||
ulong expected10 = ((value % 10UL) << 1) % 10UL;
|
||||
ulong expected8 = ((value & 7UL) << 1) & 7UL;
|
||||
ulong expected5 = ((value % 5UL) << 1) % 5UL;
|
||||
ulong expected3 = ((value % 3UL) << 1) % 3UL;
|
||||
|
||||
step10.Should().Be(expected10);
|
||||
step8.Should().Be(expected8);
|
||||
step5.Should().Be(expected5);
|
||||
step3.Should().Be(expected3);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Trait("Category", "Fast")]
|
||||
[InlineData(2UL, true)]
|
||||
|
||||
Reference in new issue
Block a user