Use GPU helper for partial factor small primes

This commit is contained in:
Marek Lemańczyk committed 2025-10-17 06:02:54 +02:00
1 parent b2689529fa
commit ee4787d244
1 file changed
+29 -3
@@ -729,14 +729,40 @@ internal static partial class PrimeOrderCalculator
// We don't need to worry about leftovers, because we always use indexes within the calculated counts
// primeSlots.Clear();
// exponentSlots.Clear();
if (!TryPopulateSmallPrimeFactorsCpu(value, limit, primeSlots, exponentSlots, out factorCount, out remaining))
counts = ThreadStaticPools.RentUlongIntDictionary(Math.Max(FactorSlotCount, 8));
counts.Clear();
bool gpuPopulated = TryPopulateSmallPrimeFactorsGpu(value, limit, counts, out remaining);
if (!gpuPopulated)
{
useDictionary = true;
counts = ThreadStaticPools.RentUlongIntDictionary(Math.Max(factorCount, 8));
counts.Clear();
remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts);
}
int dictionaryCount = counts.Count;
if (dictionaryCount <= FactorSlotCount)
{
int copyIndex = 0;
foreach (KeyValuePair<ulong, int> entry in counts)
{
primeSlots[copyIndex] = entry.Key;
exponentSlots[copyIndex] = entry.Value;
copyIndex++;
}
factorCount = copyIndex;
ThreadStaticPools.ReturnUlongIntDictionary(counts);
counts = null;
}
else
{
useDictionary = true;
factorCount = dictionaryCount;
}
}
if (remaining > 1UL)
{
pending.Add(remaining);