Fix #10513 - powmod is slow for ulong (#10688)

Co-authored-by: Iain Buclaw <ibuclaw@gdcproject.org>
This commit is contained in:
Aditya Chincholkar 2025-03-27 15:24:57 +05:30 committed by GitHub
parent 89f5914683
commit dfc3d021c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -830,28 +830,39 @@ if (isUnsigned!F && isUnsigned!G && isUnsigned!H)
{
static if (T.sizeof == 8)
{
static T addmod(T a, T b, T c)
if (c <= 0x100000000)
{
b = c - b;
if (a >= b)
return a - b;
T result = a * b;
return result % c;
}
else
return c - b + a;
}
T result = 0, tmp;
b %= c;
while (a > 0)
{
if (a & 1)
result = addmod(result, b, c);
import core.int128 : Cent, mul, udivmod;
a >>= 1;
b = addmod(b, b, c);
auto product = mul(a, b);
if (product.hi >= c)
{
product.hi %= c;
}
return result;
T remainder = void;
udivmod(product, c, remainder);
return remainder;
}
}
else static if (T.sizeof == 4)
{
if (c <= 0x10000)
{
T result = a * b;
return result % c;
}
else
{
DoubleT result = cast(DoubleT) (cast(DoubleT) a * cast(DoubleT) b);
return result % c;
}
}
else
{