mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 13:40:20 +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 if (T.sizeof == 8)
|
||||||
{
|
{
|
||||||
static T addmod(T a, T b, T c)
|
if (c <= 0x100000000)
|
||||||
{
|
{
|
||||||
b = c - b;
|
T result = a * b;
|
||||||
if (a >= b)
|
return result % c;
|
||||||
return a - b;
|
|
||||||
else
|
|
||||||
return c - b + a;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
T result = 0, tmp;
|
|
||||||
|
|
||||||
b %= c;
|
|
||||||
while (a > 0)
|
|
||||||
{
|
{
|
||||||
if (a & 1)
|
import core.int128 : Cent, mul, udivmod;
|
||||||
result = addmod(result, b, c);
|
|
||||||
|
|
||||||
a >>= 1;
|
auto product = mul(a, b);
|
||||||
b = addmod(b, b, c);
|
|
||||||
|
if (product.hi >= c)
|
||||||
|
{
|
||||||
|
product.hi %= c;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue