Initial BitContradiction implementation on GPU

This commit is contained in:
mlemanczyk committed 2026-01-25 16:37:22 +01:00
1 parent e61d166eb1
commit 17763033e1
13 files changed
+1969 -23

No files matched your search

@@ -5,6 +5,7 @@ using Xunit;
namespace PerfectNumbers.Core.Tests;
[Trait("Category", "Fast")]
public class BitContradictionIntegrationTests
{
// 2^11 - 1 = 2047 = 23 * 89
@@ -39,11 +40,21 @@ public class BitContradictionIntegrationTests
divides.Should().BeFalse();
}
[Fact]
public void BitContradictionAcceptsKnownLargeDivisor()
{
BigInteger divisor = BigInteger.Parse("1209708008767");
ulong p = 138000001;
bool decided = Invoke(divisor, p, out bool divides);
decided.Should().BeTrue();
divides.Should().BeTrue();
}
[Fact]
public void BitContradictionRejectsNonDivisorWithDecision()
{
// 2^11 - 1 is not divisible by 25
BigInteger divisor = 25;
// 2^11 - 1 is not divisible by 45 (k=2, q=22*2+1).
BigInteger divisor = 45;
ulong p = 11;
bool decided = Invoke(divisor, p, out bool divides);
decided.Should().BeTrue("small p should be decided exactly");
@@ -0,0 +1,80 @@
using System;
using System.Numerics;
using FluentAssertions;
using PerfectNumbers.Core.Gpu.Kernels;
using Xunit;
namespace PerfectNumbers.Core.Tests;
public class BitContradictionKernelsTests
{
[Theory]
[Trait("Category", "Fast")]
[InlineData(31UL, 0UL, 0)]
[InlineData(31UL, 0UL, 5)]
[InlineData(61UL, 7UL, 3)]
[InlineData(36721UL, 1UL, 0)]
public void TryBuildQWordsFromBatchIndex_matches_BigInteger(ulong exponent, ulong batchIndex, int batchOffset)
{
int wordCount = BitContradictionKernels.MaxQWordCount;
ulong[] batchIndexWords = new ulong[wordCount];
batchIndexWords[0] = batchIndex;
ulong[] qWords = new ulong[wordCount];
bool ok = BitContradictionKernels.TryBuildQWordsFromBatchIndex(
exponent,
batchIndexWords,
wordCount,
batchOffset,
qWords,
out int qBitLen);
ok.Should().BeTrue();
BigInteger expectedK = ((BigInteger)batchIndex * BitContradictionKernels.BatchCount) + BigInteger.One + batchOffset;
BigInteger expectedQ = (expectedK * (exponent << 1)) + BigInteger.One;
BigInteger actualQ = ReadBigIntegerFromWords(qWords);
actualQ.Should().Be(expectedQ);
qBitLen.Should().Be(GetBitLength(expectedQ));
}
private static BigInteger ReadBigIntegerFromWords(ReadOnlySpan<ulong> words)
{
byte[] bytes = new byte[words.Length * sizeof(ulong)];
for (int w = 0; w < words.Length; w++)
{
ulong word = words[w];
int byteIndex = w << 3;
bytes[byteIndex] = (byte)word;
bytes[byteIndex + 1] = (byte)(word >> 8);
bytes[byteIndex + 2] = (byte)(word >> 16);
bytes[byteIndex + 3] = (byte)(word >> 24);
bytes[byteIndex + 4] = (byte)(word >> 32);
bytes[byteIndex + 5] = (byte)(word >> 40);
bytes[byteIndex + 6] = (byte)(word >> 48);
bytes[byteIndex + 7] = (byte)(word >> 56);
}
return new BigInteger(bytes, isUnsigned: true, isBigEndian: false);
}
private static int GetBitLength(BigInteger value)
{
if (value.IsZero)
{
return 0;
}
byte[] bytes = value.ToByteArray(isUnsigned: true, isBigEndian: false);
int last = bytes[^1];
int bits = (bytes.Length - 1) * 8;
while (last != 0)
{
bits++;
last >>= 1;
}
return bits;
}
}
@@ -16,7 +16,7 @@ public class BitContradictionSolverTests
ColumnBounds bounds = ComputeColumnBounds(a, q, 1);
bounds.ForcedOnes.Should().Be(1);
bounds.PossibleOnes.Should().Be(1);
bounds.PossibleOnes.Should().Be(2);
}
[Fact]