The OpenD Programming Language

BigInt

Stack-allocated big signed integer.

@serdeScoped
@serdeProxy!(const(char)[])
struct BigInt (
uint size64
) if (
size64 &&
size64 <= ushort.max
) {}

Constructors

this
this(UInt!size fixedInt)
this
this(size_t[N] data)
this
this(ulong data)
this
this(long data)
this
this(int data)
this
this(uint data)
this
this(const(char)[] str)

Members

Functions

bitLength
size_t bitLength()
coefficients
inout(size_t)[] coefficients()
copy
BigInt copy()
copyFrom
bool copyFrom(const(W)[] coefficients, bool sign)
copyFromBigEndian
bool copyFromBigEndian(const(ubyte)[] data, bool sign)
ctlz
size_t ctlz()
divMod
ref divMod(BigInt!divisorSize64 divisor, BigInt!size64 quotient, BigInt!remainderSize remainder)
fromBinaryStringImpl
bool fromBinaryStringImpl(const(C)[] str)
fromHexStringImpl
bool fromHexStringImpl(const(C)[] str)
fromStringImpl
bool fromStringImpl(const(C)[] str)
mulPow5
bool mulPow5(ulong degree)
multiply
ref multiply(BigInt!aSize64 a, BigInt!bSize64 b)
normalize
void normalize()
opAssign
ref opAssign(ulong data)
opAssign
ref opAssign(long data)
opAssign
ref opAssign(uint data)
opAssign
ref opAssign(int data)
opAssign
ref opAssign(UInt!rhsSize data)
opAssign
ref opAssign(BigInt!rhsSize64 rhs)
opCast
T opCast()
opCast
T opCast()
opCmp
auto opCmp(BigInt rhs)
opEquals
bool opEquals(BigInt rhs)
opEquals
bool opEquals(ulong rhs, bool rhsSign)
opEquals
bool opEquals(long rhs)
opEquals
bool opEquals(uint rhs)
opEquals
bool opEquals(int rhs)
opOpAssign
size_t opOpAssign(size_t rhs, size_t overflow)

Performs size_t overflow = (big += overflow) *= scalar operatrion.

opOpAssign
uint opOpAssign(uint rhs, uint overflow)

Performs uint remainder = (overflow$big) /= scalar operatrion, where $ denotes big-endian concatenation. Precondition: overflow < rhs

opOpAssign
UInt!size opOpAssign(UInt!size rhs, UInt!size overflow)

Performs size_t overflow = (big += overflow) *= fixed operatrion.

opOpAssign
ref opOpAssign(UInt!size rhs)
ref opOpAssign(ulong rhs)
ref opOpAssign(long rhs)
opOpAssign
ref opOpAssign(BigInt!rhsSize64 rhs)

Performs this %= rhs and this /= rhs operations.

opOpAssign
ref opOpAssign(BigInt!rhsSize64 rhs)

Performs this %= rhs and this /= rhs operations.

opOpAssign
bool opOpAssign(BigInt!rhsSize64 rhs)
bool opOpAssign(BigIntView!(const size_t) rhs)
bool opOpAssign(ulong rhs)
bool opOpAssign(uint rhs)
bool opOpAssign(long rhs)
bool opOpAssign(int rhs)

Performs size_t overflow = big *= fixed operatrion.

opOpAssign
BigInt opOpAssign(size_t shift)
pow
ref pow(ulong degree)
powMod
ref powMod(BigInt!expSize exponent, BigInt modulus)
ref powMod(BigUIntView!(const size_t) exponent, BigInt modulus)
putCoefficient
void putCoefficient(size_t value)
toString
immutable(C)[] toString()
toString
void toString(W w)
view
BigIntView!size_t view()
view
BigIntView!(const size_t) view()

Static functions

fromBigEndian
BigInt fromBigEndian(const(ubyte)[] data, bool sign)
fromBinaryString
BigInt fromBinaryString(const(char)[] str)
fromHexString
BigInt fromHexString(const(char)[] str)

Variables

data
size_t[ulong.sizeof / size_t.sizeof * size64] data;
length
uint length;
sign
bool sign;

Parameters

size64

count of 64bit words in coefficient

Examples

Check @nogc toString impl

import mir.format;
auto str = "-34010447314490204552169750449563978034784726557588085989975288830070948234680";
auto integer = BigInt!4(str);
auto buffer = stringBuf;
buffer << integer;
assert(buffer.data == str);
1 import mir.test;
2 import mir.bignum.fixed;
3 import mir.bignum.low_level_view;
4 
5 {
6     auto a = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
7     auto b = UInt!128.fromHexString("f79a222050aaeaaa417fa25a2ac93291");
8 
9     // ca3d7e25aebe687b 168dcef32d0bb2f0
10     auto c = BigInt!4.fromHexString("bf4c87424431d21563f23b1fc00d75ac");
11     a %= b;
12     a.should == c;
13     a = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
14     a /= b;
15     assert(a == BigInt!4.fromHexString("ca3d7e25aebe687b7cc1b250b44690fb"));
16 }
17 
18 {
19     auto a = BigInt!4.fromHexString("7fff000080000000000000000000");
20     auto b = UInt!128.fromHexString("80000000000000000001");
21 
22     auto c = BigInt!4.fromHexString("fffe0000");
23     a /= b;
24     a.should == c;
25 
26     a = BigInt!4.fromHexString("7fff000080000000000000000000");
27     assert((a %= b) == BigInt!4.fromHexString("7fffffffffff00020000"));
28 }
29 
30 {
31     auto a = BigInt!16.fromHexString("76d053cdcc87ec8c9455c375d6a08c799fad73cf07415e70af5dfacaff4bd306647a7cceb98839cce89ae65900938821564fd2af3c9d881c172264bb17e3530ce79b938d5eb7ffec558be43ab5b684978417c5053fb8df63fc65c9efd8b2e86469c53259509eb597f81647930f24ef05a79bfecf04e5ec52414c6a3f7481d533");
32     auto b = UInt!128.fromHexString("9c5c1aa6ad7ad18065a3a74598e27bee");
33 
34     assert((a /= b) == BigInt!16.fromHexString("c2871f2b07522bda1e63de12850d2208bb242c716b5739d6744ee1d9c937b8d765d3742e18785d08c2405e5c83f3c875d5726d09dfaee29e813675a4f91bfee01e8cbbbca9588325d54cf2a625faffde4d8709e0517f786f609d8ce6997e0e71d2f976ae169b0c4be7a7dba3135af96c"));
35     a = BigInt!16.fromHexString("76d053cdcc87ec8c9455c375d6a08c799fad73cf07415e70af5dfacaff4bd306647a7cceb98839cce89ae65900938821564fd2af3c9d881c172264bb17e3530ce79b938d5eb7ffec558be43ab5b684978417c5053fb8df63fc65c9efd8b2e86469c53259509eb597f81647930f24ef05a79bfecf04e5ec52414c6a3f7481d533");
36     assert((a %= b) == BigInt!16.fromHexString("85d81587a8b62af1874315d26ebf0ecb"));
37 }
38 
39 {
40     auto a = BigInt!4.fromHexString("DEADBEEF");
41     auto b = UInt!256.fromHexString("18aeff9fa4aace484a9f8f9002cdf38fa6e53fc0f6c035051dc86931c1c08316");
42 
43     assert((a /= b) == 0);
44     a = BigInt!4.fromHexString("DEADBEEF");
45     assert((a %= b) == 0xDEADBEEF);
46 }
47 
48 void test(const long av, const long bv)
49 {
50     auto a = BigInt!4(av);
51     const b = BigInt!4(bv);
52     a /= b;
53     assert(a == av / bv);
54     a = BigInt!4(av);
55     a %= b;
56     assert(a == av % bv);
57 }
58 
59 {
60     auto av = 0xDEADBEEF;
61     auto bv = 0xDEAD;
62     test(+av, +bv);
63     // test(+av, -bv);
64     // test(-av, +bv);
65     // test(+av, +bv);
66 }
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.coefficients, b.sign));
assert(c == b);
b >>= 18;
auto bView = cast(BigIntView!ushort)b.view;
assert(!c.copyFrom(bView.coefficients[0 .. $ - 1], bView.sign));
assert(c == b);

Meta