From 15bc2686ccfa7178c99bf94220d3a6f1d16b74f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Wed, 24 Sep 2025 00:34:13 +0200 Subject: [PATCH] Tiny optimization --- PerfectNumbers.Core/ULongExtensions.cs | 54 ++++++++++++-------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/PerfectNumbers.Core/ULongExtensions.cs b/PerfectNumbers.Core/ULongExtensions.cs index 7fadfa1..fd67bf7 100644 --- a/PerfectNumbers.Core/ULongExtensions.cs +++ b/PerfectNumbers.Core/ULongExtensions.cs @@ -85,27 +85,23 @@ public static class ULongExtensions } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong Mod10(this ulong value) - { - ulong mod5 = value % 5UL; - return (value & 1UL) == 0UL - ? mod5 switch - { - 0UL => 0UL, - 1UL => 6UL, - 2UL => 2UL, - 3UL => 8UL, - _ => 4UL, - } - : mod5 switch - { - 0UL => 5UL, - 1UL => 1UL, - 2UL => 7UL, - 3UL => 3UL, - _ => 9UL, - }; - } + public static ulong Mod10(this ulong value) => (value & 1UL) == 0UL + ? (value % 5UL) switch + { + 0UL => 0UL, + 1UL => 6UL, + 2UL => 2UL, + 3UL => 8UL, + _ => 4UL, + } + : (value % 5UL) switch + { + 0UL => 5UL, + 1UL => 1UL, + 2UL => 7UL, + 3UL => 3UL, + _ => 9UL, + }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong Mod128(this ulong value) => value & 127UL; @@ -147,14 +143,14 @@ public static class ULongExtensions } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Mod10_8_5_3Steps(this ulong value, out ulong step10, out ulong step8, out ulong step5, out ulong step3) - { - value.Mod10_8_5_3(out step10, out step8, out step5, out step3); + public static void Mod10_8_5_3Steps(this ulong value, out ulong step10, out ulong step8, out ulong step5, out ulong step3) + { + value.Mod10_8_5_3(out step10, out step8, out step5, out step3); - step10 = (step10 << 1).Mod10(); - step8 = (step8 << 1) & 7UL; - step5 = (step5 << 1) % 5UL; - step3 = (step3 << 1) % 3UL; + step10 = (step10 << 1).Mod10(); + step8 = (step8 << 1) & 7UL; + step5 = (step5 << 1) % 5UL; + step3 = (step3 << 1) % 3UL; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -394,7 +390,7 @@ public static class ULongExtensions // Reusing result after resetting it for the rotation-driven accumulation phase. result = one; UInt128 rotationCount = exponent % cycleLength; - + // We're reusing "zero" as rotation index for just a little better performance while (zero < rotationCount) {