Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions source/mir/bignum/integer.d
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,38 @@ struct BigInt(uint size64)
}
}

// Division by a single-limb power of two must also work through the
// BigInt divisor path, which uses the low-level division dispatcher.
version(mir_bignum_test)
@safe pure @nogc unittest
{
auto one = BigInt!2(1);
auto divisorOne = BigInt!2(1);
one /= divisorOne;
assert(one == 1);

foreach (shift; 0 .. 32)
{
const divisor = 1u << shift;
auto bigDivisor = BigInt!2(divisor);
foreach (dividend; [1UL, 0x0123_4567_89ab_cdefUL, ulong.max])
{
auto quotient = BigInt!2(dividend);
quotient /= bigDivisor;
assert(quotient == dividend / divisor);

auto remainder = BigInt!2(dividend);
remainder %= bigDivisor;
assert(remainder == dividend % divisor);

auto scalarQuotient = BigInt!2(dividend);
const scalarRemainder = scalarQuotient /= divisor;
assert(scalarQuotient == dividend / divisor);
assert(scalarRemainder == dividend % divisor);
}
}
}

/// Check @nogc toString impl
version(mir_bignum_test) @safe pure @nogc unittest
{
Expand Down
47 changes: 47 additions & 0 deletions source/mir/bignum/internal/phobos_kernel.d
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,61 @@ uint multibyteDivAssign()(uint[] dest, uint divisor, uint overflow)
if (__ctfe)
return std.internal.math.biguintnoasm.multibyteDivAssign(dest, divisor, overflow);
else version (D_InlineAsm_X86)
{
// The x86 assembly divider requires a non-power-of-two divisor.
if ((divisor & (divisor - 1)) == 0)
return std.internal.math.biguintnoasm.multibyteDivAssign(dest, divisor, overflow);
return std.internal.math.biguintx86.multibyteDivAssign(dest, divisor, overflow);
}
else version (LDC_ARM_asm)
return std.internal.math.biguintarm.multibyteDivAssign(dest, divisor, overflow);
else
return std.internal.math.biguintnoasm.multibyteDivAssign(dest, divisor, overflow);
}
else alias multibyteDivAssign = std.internal.math.biguintnoasm.multibyteDivAssign;

version(mir_bignum_test)
@safe unittest
{
// Cover every uint power of two, including 1 and the high bit, and
// non-powers of two that continue to use the x86 assembly divider.
immutable uint[8] otherDivisors =
[3u, 6u, 7u, 10u, 0x7fff_ffffu, 0x8000_0001u, 0xffff_fffeu, uint.max];
immutable uint[4][4] inputs = [
[0u, 0u, 0u, 0u],
[1u, 0u, 0u, 0u],
[uint.max, uint.max, uint.max, uint.max],
[0x0123_4567u, 0x89ab_cdefu, 0xfedc_ba98u, 0x7654_3210u]];
foreach (i; 0 .. 32 + otherDivisors.length)
{
const divisor = i < 32 ? 1u << i : otherDivisors[i - 32];
foreach (overflow; [0u, divisor / 2, divisor - 1])
foreach (input; inputs)
foreach (length; 1 .. input.length + 1)
{
uint[4] actual = input;
uint[4] expected = input;
ulong remainder = overflow;
foreach_reverse (ref limb; expected[0 .. length])
{
const value = (remainder << 32) | limb;
limb = cast(uint)(value / divisor);
remainder = value % divisor;
}
const result = multibyteDivAssign(actual[0 .. length], divisor, overflow);
assert(actual == expected);
assert(result == remainder);
}
}

// The dispatcher's compile-time path must remain usable as well.
static assert(() {
uint[2] value = [1u, 1u];
const remainder = multibyteDivAssign(value[], 2, 1);
return value == [0x8000_0000u, 0x8000_0000u] && remainder == 1;
}());
}

version (HaveAsmVersion)
void multibyteAddDiagonalSquares()(uint[] dest, const(uint)[] src)
{
Expand Down
Loading