mirror of
https://github.com/dlang/phobos.git
synced 2025-04-26 13:10:35 +03:00
Co-authored-by: Iain Buclaw <ibuclaw@gdcproject.org>
This commit is contained in:
parent
89f5914683
commit
dfc3d021c6
1 changed files with 28 additions and 17 deletions
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue