Smaller optimizations. Division by zero fixed?

This commit is contained in:
mlemanczyk committed 2025-10-17 14:18:49 +02:00
1 parent 5fcd082fff
commit 43cf4a978f
3 files changed
+55 -46

No files matched your search

+26 -20
View File
@@ -77,11 +77,17 @@ internal static partial class PrimeOrderCalculator
private static bool TrySpecialMaxCpu(ulong phi, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData) private static bool TrySpecialMaxCpu(ulong phi, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
{ {
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
int length = factors.Count; int length = factors.Count;
ReadOnlySpan<FactorEntry> factorSpan = new(factors.Factors, 0, length);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
ulong factor = factorSpan[i].Value; ulong factor = factorSpan[i].Value;
// Don't uncomment this. If there is division by zero, that means something wrong.
// if (factor == 0UL)
// {
// continue;
// }
ulong reduced = phi / factor; ulong reduced = phi / factor;
if (Pow2EqualsOneCpu(reduced, prime, divisorData)) if (Pow2EqualsOneCpu(reduced, prime, divisorData))
{ {
@@ -638,7 +644,7 @@ internal static partial class PrimeOrderCalculator
private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData) private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData)
{ {
// if (IsGpuPow2Allowed) if (IsGpuPow2Allowed)
{ {
ulong remainder = exponent.Pow2MontgomeryModWindowedGpu(divisorData, false); ulong remainder = exponent.Pow2MontgomeryModWindowedGpu(divisorData, false);
// GpuPow2ModStatus status = PrimeOrderGpuHeuristics.TryPow2Mod(exponent, prime, out ulong remainder, divisorData); // GpuPow2ModStatus status = PrimeOrderGpuHeuristics.TryPow2Mod(exponent, prime, out ulong remainder, divisorData);
@@ -741,26 +747,26 @@ internal static partial class PrimeOrderCalculator
remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts); remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts);
// } // }
// int dictionaryCount = counts.Count; int dictionaryCount = counts.Count;
// if (dictionaryCount <= FactorSlotCount) if (dictionaryCount <= FactorSlotCount)
// { {
// int copyIndex = 0; int copyIndex = 0;
// foreach (KeyValuePair<ulong, int> entry in counts) foreach (KeyValuePair<ulong, int> entry in counts)
// { {
// primeSlots[copyIndex] = entry.Key; primeSlots[copyIndex] = entry.Key;
// exponentSlots[copyIndex] = entry.Value; exponentSlots[copyIndex] = entry.Value;
// copyIndex++; copyIndex++;
// } }
// factorCount = copyIndex; factorCount = copyIndex;
// ThreadStaticPools.ReturnUlongIntDictionary(counts); ThreadStaticPools.ReturnUlongIntDictionary(counts);
// counts = null; counts = null;
// } }
// else else
// { {
useDictionary = true; useDictionary = true;
// factorCount = dictionaryCount; factorCount = dictionaryCount;
// } }
} }
+6 -5
View File
@@ -330,11 +330,12 @@ public static class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModWindowedCpu(this ulong exponent, in MontgomeryDivisorData divisor, bool keepMontgomery) public static ulong Pow2MontgomeryModWindowedCpu(this ulong exponent, in MontgomeryDivisorData divisor, bool keepMontgomery)
{ {
ulong modulus = divisor.Modulus; ulong modulus = divisor.Modulus;
if (exponent == 0UL) // This should never happen in production code. If it does, we need to fix it.
{ // if (exponent == 0UL)
return keepMontgomery ? divisor.MontgomeryOne : 1UL % modulus; // {
} // return keepMontgomery ? divisor.MontgomeryOne : 1UL % modulus;
// }
if (exponent <= Pow2WindowFallbackThreshold) if (exponent <= Pow2WindowFallbackThreshold)
{ {
+23 -21
View File
@@ -5,30 +5,32 @@ namespace PerfectNumbers.Core;
public sealed class UnboundedTaskScheduler : TaskScheduler public sealed class UnboundedTaskScheduler : TaskScheduler
{ {
public static UnboundedTaskScheduler Instance { get; } = new(); public static UnboundedTaskScheduler Instance { get; } = new();
private UnboundedTaskScheduler() private UnboundedTaskScheduler()
{ {
} ThreadPool.SetMaxThreads(100_000, 100_000);
ThreadPool.SetMinThreads(10_300, 100);
}
public override int MaximumConcurrencyLevel => int.MaxValue; public override int MaximumConcurrencyLevel => int.MaxValue;
protected override void QueueTask(Task task) protected override void QueueTask(Task task)
{ {
ThreadPool.UnsafeQueueUserWorkItem(static state => ThreadPool.UnsafeQueueUserWorkItem(static state =>
{ {
(UnboundedTaskScheduler Scheduler, Task Task) tuple = ((UnboundedTaskScheduler, Task))state!; (UnboundedTaskScheduler Scheduler, Task Task) tuple = ((UnboundedTaskScheduler, Task))state!;
tuple.Scheduler.TryExecuteTask(tuple.Task); tuple.Scheduler.TryExecuteTask(tuple.Task);
}, (this, task)); }, (this, task));
} }
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{ {
return TryExecuteTask(task); return TryExecuteTask(task);
} }
protected override IEnumerable<Task>? GetScheduledTasks() protected override IEnumerable<Task>? GetScheduledTasks()
{ {
return null; return null;
} }
} }