Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.bignum.integer

Note The module doesn't provide full arithmetic API for now.

struct BigInt(size_t maxSize64) if (maxSize64 && (maxSize64 <= (ushort).max));
Stack-allocated big signed integer.
Parameters:
maxSize64 count of 64bit words in coefficient
Examples:
import mir.bignum.fixed;
import mir.bignum.low_level_view;

auto a = BigInt!4.fromHexString("4b313b23aa560e1b0985f89cbe6df5460860e39a64ba92b4abdd3ee77e4e05b8");
auto b = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
auto c = BigInt!4.fromHexString("7869dd864619cace5953a09910327b3971413e6aa5f417fa25a2ac93291b941f");
c.sign = true;
assert(a != b);
assert(a < b);
a -= b;
assert(a.sign);
assert(a == c);
a -= a;
assert(!a.sign);
assert(!a.length);

auto d = BigInt!4.fromHexString("0de1a911c6dc8f90a7169a148e65d22cf34f6a8254ae26362b064f26ac44218a");
assert((b *= 0x7869dd86) == 0x5c019770);
assert(b == d);

d = BigInt!4.fromHexString("856eeb23e68cc73f2a517448862cdc97e83f9dfa23768296724bf00fda7df32a");
auto o = b *= UInt!128.fromHexString("f79a222050aaeaaa417fa25a2ac93291");
assert(o == UInt!128.fromHexString("d6d15b99499b73e68c3331eb0f7bf16"));
assert(b == d);

d = BigInt!4.fromHexString("d"); // initial value
d.mulPow5(60);
c = BigInt!4.fromHexString("81704fcef32d3bd8117effd5c4389285b05d");
assert(d == c);

d >>= 80;
c = BigInt!4.fromHexString("81704fcef32d3bd8");
assert(d == c);

c = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
d = BigInt!4.fromHexString("9935cea0707f79a222050aaeaaaed17feb7aa76999d700000000000000000000");
c <<= 80;
assert(d == c);
c >>= 80;
c <<= 84;
d <<= 4;
assert(d == c);
assert(c != b);
b.sign = true;
assert(!c.copyFrom(b.view));
assert(c == b);
b >>= 18;
auto bView = cast(BigIntView!ushort)b.view;
assert(!c.copyFrom(bView.topLeastSignificantPart(bView.unsigned.coefficients.length - 1)));
assert(c == b);
bool sign;
uint length;
size_t[(ulong).sizeof / size_t.sizeof * maxSize64] data;
this(size_t size)(UInt!size fixedInt);
this(size_t N)(size_t[N] data)
if (N <= this.data.length);
this(ulong data);
@property BigInt copy();
const pure nothrow @nogc @safe bool opEquals(ref const BigInt rhs);
const pure nothrow @nogc @safe auto opCmp(ref const BigInt rhs);
@property BigIntView!size_t view()();
const @property BigIntView!(const(size_t)) view()();
void normalize()();
void putCoefficient(size_t value);
pure nothrow @nogc @safe size_t opOpAssign(string op : "*")(size_t rhs, size_t overflow = 0u);
Performs size_t overflow = (big += overflow) *= scalar operatrion.

Precondition non-empty coefficients

Parameters:
size_t rhs unsigned value to multiply by
size_t overflow initial overflow value
Returns:
unsigned overflow value
pure nothrow @nogc @safe UInt!size opOpAssign(string op : "*", size_t size)(UInt!size rhs, UInt!size overflow = 0);
Performs size_t overflow = (big += overflow) *= fixed operatrion.

Precondition non-empty coefficients

Parameters:
UInt!size rhs unsigned value to multiply by
UInt!size overflow initial overflow value
Returns:
unsigned overflow value
pure nothrow @nogc @safe bool opOpAssign(string op)(ref const BigInt rhs)
if (op == "+" || op == "-");
Performs size_t overflow = big *= fixed operatrion.

Precondition non-empty coefficients

Parameters:
BigInt rhs unsigned value to multiply by
Returns:
overflow
pure @trusted BigInt fromHexString()(scope const(char)[] str);
bool mulPow5(size_t degree);
pure nothrow @nogc ref @safe BigInt opOpAssign(string op)(size_t shift) return
if (op == "<<" || op == ">>");
const T opCast(T, bool wordNormalized = false, bool nonZero = false)()
if (isFloatingPoint!T && isMutable!T);
bool copyFrom(W, WordEndian endian)(BigIntView!(const(W), endian) view);

bool copyFrom(W, WordEndian endian)(BigUIntView!(const(W), endian) view);