From 75586d07b824cc94d62cafbc5b6626edf9bb5584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sat, 4 Oct 2025 22:40:18 +0200 Subject: [PATCH] Relax ulong multiply-shift guards and refresh tests --- .../ULongExtensionsMultiplyShiftTests.cs | 23 ----------------- PerfectNumbers.Core/ULongExtensions.cs | 25 ------------------- todo.txt | 2 ++ 3 files changed, 2 insertions(+), 48 deletions(-) diff --git a/PerfectNumbers.Core.Tests/ULongExtensionsMultiplyShiftTests.cs b/PerfectNumbers.Core.Tests/ULongExtensionsMultiplyShiftTests.cs index d755e1b..5267de5 100644 --- a/PerfectNumbers.Core.Tests/ULongExtensionsMultiplyShiftTests.cs +++ b/PerfectNumbers.Core.Tests/ULongExtensionsMultiplyShiftTests.cs @@ -39,29 +39,6 @@ public class ULongExtensionsMultiplyShiftTests result.Should().Be((ulong)expected); } - [Fact] - public void MultiplyShiftRightShiftFirst_returns_zero_when_shift_is_large() - { - ULongExtensions.MultiplyShiftRightShiftFirst(ulong.MaxValue, 123UL, 64).Should().Be(0UL); - } - [Theory] - [InlineData(0UL, ulong.MaxValue, -1)] - [InlineData(1UL, 1UL, -3)] - public void MultiplyShiftRight_throws_when_shift_is_negative(ulong value, ulong multiplier, int shift) - { - Action act = () => ULongExtensions.MultiplyShiftRight(value, multiplier, shift); - act.Should().Throw(); - } - - [Theory] - [InlineData(0UL, ulong.MaxValue, -1)] - [InlineData(1UL, 1UL, -3)] - public void MultiplyShiftRightShiftFirst_throws_when_shift_is_negative(ulong value, ulong multiplier, int shift) - { - Action act = () => ULongExtensions.MultiplyShiftRightShiftFirst(value, multiplier, shift); - - act.Should().Throw(); - } } diff --git a/PerfectNumbers.Core/ULongExtensions.cs b/PerfectNumbers.Core/ULongExtensions.cs index 58f2802..ab3b25a 100644 --- a/PerfectNumbers.Core/ULongExtensions.cs +++ b/PerfectNumbers.Core/ULongExtensions.cs @@ -59,22 +59,12 @@ public static class ULongExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBitLength(this ulong value) { - if (value == 0UL) - { - return 0; - } - return 64 - BitOperations.LeadingZeroCount(value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong MultiplyShiftRight(ulong value, ulong multiplier, int shift) { - if (shift < 0) - { - throw new ArgumentOutOfRangeException(nameof(shift), "Shift must be non-negative."); - } - UInt128 product = (UInt128)value * multiplier; return (ulong)(product >> shift); } @@ -82,21 +72,6 @@ public static class ULongExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong MultiplyShiftRightShiftFirst(ulong value, ulong multiplier, int shift) { - if (shift < 0) - { - throw new ArgumentOutOfRangeException(nameof(shift), "Shift must be non-negative."); - } - - if (shift == 0) - { - return MultiplyShiftRight(value, multiplier, 0); - } - - if (shift >= 64) - { - return 0UL; - } - ulong high = value >> shift; ulong mask = (1UL << shift) - 1UL; ulong low = value & mask; diff --git a/todo.txt b/todo.txt index 19278dc..2ef5cb1 100644 --- a/todo.txt +++ b/todo.txt @@ -1 +1,3 @@ +- Inline the PrimeOrderGpuHeuristics capability checks at each call site and delete the temporary SupportsPrime/SupportsExponent helpers once the overflow gating is stable. +- Swap PrimeOrderGpuHeuristics.Pow2ModCpu for the existing Pow2MontgomeryModWindowed helper and plan the same windowed ladder for the GPU kernel implementation. - Extend the GPU numeric type probe to cover proposed custom types such as GpuUInt256 and GpuUInt512 once their definitions are available so we can plan the wide-order heuristics on the device.