MethodImplOptions.AggressiveOptimization & MethodImplOptions.AggressiveInlining optimized. Small optimizations.

This commit is contained in:
mlemanczyk committed 2025-12-10 19:59:51 +01:00
1 parent 7a242b3bfa
commit 05d3061c47
23 files changed
+628 -559

No files matched your search

@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using ILGPU;
using ILGPU.Runtime;
@@ -6,6 +7,7 @@ namespace PerfectNumbers.Core.Gpu;
internal static class Pow2ModKernels
{
/// This kernel always sets the result of the corresponding element. Callers don't need to clear the output buffers.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static void Pow2ModKernelScan(
Index1D index,
ulong exponent,
@@ -107,6 +109,7 @@ internal static class Pow2ModKernels
/// This kernel doesn't always set the result of the found array. It sets the value only when it needs to. The callers
/// must clean the output buffer before the call to get a deterministic result.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static void Pow2ModOrderKernelScan(
Index1D index,
ulong exponent,
@@ -117,7 +117,7 @@ public static class Pow2MontgomeryKernels
return sumHigh;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
private static void MultiplyPartsGpu(ulong left, ulong right, out ulong high, out ulong low)
{
ulong leftLow = (uint)left;
@@ -135,13 +135,13 @@ public static class Pow2MontgomeryKernels
high = unchecked(highProduct + (cross1 >> 32) + (cross2 >> 32) + (carry >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
internal static ulong Pow2MontgomeryModWindowedGpuKeepMontgomery(in MontgomeryDivisorDataGpu divisor, ulong exponent)
{
return Pow2MontgomeryModWindowedGpuMontgomeryResult(divisor, exponent);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static ulong Pow2MontgomeryModWindowedGpuConvertToStandard(this MontgomeryDivisorDataGpu divisor, ulong exponent)
{
ulong modulus = divisor.Modulus;
@@ -150,7 +150,7 @@ public static class Pow2MontgomeryKernels
return MontgomeryMultiplyGpu(montgomeryResult, 1UL, modulus, nPrime);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static ulong Pow2MontgomeryModWindowedGpuMontgomeryResult(in MontgomeryDivisorDataGpu divisor, ulong exponent)
{
ulong modulus = divisor.Modulus;
@@ -1182,6 +1182,7 @@ internal static partial class PrimeOrderKernels
return divisor.Pow2MontgomeryModWindowedGpuConvertToStandard(exponent) == 1UL;
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static ulong CalculateByDoublingKernel(ulong prime)
{
ulong order = 1UL;
@@ -1203,6 +1204,7 @@ internal static partial class PrimeOrderKernels
/// This kernel always sets the result of the corresponding element. Callers don't need to clear the output buffers.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
internal static void Pow2ModKernel(Index1D index, ArrayView1D<ulong, Stride1D.Dense> exponents, ulong divisorModulus, ulong divisorNPrime, ulong divisorMontgomeryOne, ulong divisorMontgomeryTwo, ulong divisorMontgomeryTwoSquared, ArrayView1D<ulong, Stride1D.Dense> remainders)
{
ulong exponent = exponents[index];
@@ -1211,12 +1213,14 @@ internal static partial class PrimeOrderKernels
}
/// This kernel always sets the result of the corresponding element. Callers don't need to clear the output buffers.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
internal static void Pow2ModKernelWide(Index1D index, ArrayView1D<GpuUInt128, Stride1D.Dense> exponents, GpuUInt128 modulus, ArrayView1D<GpuUInt128, Stride1D.Dense> remainders)
{
GpuUInt128 exponent = exponents[index];
remainders[index] = Pow2ModKernelCore(exponent, modulus);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static GpuUInt128[] InitializeOddPowersTable(GpuUInt128 baseValue, GpuUInt128 modulus, int oddPowerCount)
{
GpuUInt128[] result = new GpuUInt128[PerfectNumberConstants.MaxOddPowersCount];
@@ -1242,6 +1246,7 @@ internal static partial class PrimeOrderKernels
return result;
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
internal static GpuUInt128 Pow2ModKernelCore(GpuUInt128 exponent, GpuUInt128 modulus)
{
// This should never happen in production code.