mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(big): add Mul
This commit is contained in:
parent
33585cb36f
commit
e74654800d
3 changed files with 75 additions and 0 deletions
|
|
@ -49,6 +49,19 @@ uint64_t MakeLarge(uint32_t low, uint32_t high) {
|
||||||
return low + (static_cast<uint64_t>(high) << 32);
|
return low + (static_cast<uint64_t>(high) << 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c) {
|
||||||
|
// TODO assertion c <= SMALL_BOUND
|
||||||
|
|
||||||
|
uint64_t carry = 0;
|
||||||
|
uint32_t i = 0;
|
||||||
|
for (i = 0; carry || b.IsUsed(i); i++) {
|
||||||
|
carry += b[i] * c;
|
||||||
|
a[i] = ExtractLowPart(carry);
|
||||||
|
}
|
||||||
|
|
||||||
|
a.SetCount(i);
|
||||||
|
}
|
||||||
|
|
||||||
void ToBinaryAppend(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
|
void ToBinaryAppend(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
|
||||||
for (uint32_t i = 0; i < buffer.Count() * 4; i++) {
|
for (uint32_t i = 0; i < buffer.Count() * 4; i++) {
|
||||||
auto byte = buffer[i / 4] >> (8 * (i & 3));
|
auto byte = buffer[i / 4] >> (8 * (i & 3));
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ void FromUnsigned(BigBuffer& buffer, uint32_t value);
|
||||||
|
|
||||||
uint64_t MakeLarge(uint32_t low, uint32_t high);
|
uint64_t MakeLarge(uint32_t low, uint32_t high);
|
||||||
|
|
||||||
|
void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c);
|
||||||
|
|
||||||
void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer);
|
void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
60
test/Big.cpp
60
test/Big.cpp
|
|
@ -54,6 +54,66 @@ TEST_CASE("ExtractLowPartSx", "[big]") {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Mul", "[big]") {
|
||||||
|
SECTION("multiplies 0 and 1") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 0);
|
||||||
|
|
||||||
|
uint64_t c = 1;
|
||||||
|
|
||||||
|
Mul(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 1);
|
||||||
|
CHECK(a->Primary()[0] == 0);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("multiplies 2 and 4") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 2);
|
||||||
|
|
||||||
|
uint64_t c = 4;
|
||||||
|
|
||||||
|
Mul(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 1);
|
||||||
|
CHECK(a->Primary()[0] == 8);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("multiplies 0xFFFFFFFF and 0x100") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 0xFFFFFFFF);
|
||||||
|
|
||||||
|
uint64_t c = 0x100;
|
||||||
|
|
||||||
|
Mul(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 2);
|
||||||
|
CHECK(a->Primary()[0] == 0xFFFFFF00);
|
||||||
|
CHECK(a->Primary()[1] == 0xFF);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("MakeLarge", "[big]") {
|
TEST_CASE("MakeLarge", "[big]") {
|
||||||
SECTION("creates uint64_t out of 0xAABBCCDD and 0x11223344") {
|
SECTION("creates uint64_t out of 0xAABBCCDD and 0x11223344") {
|
||||||
uint64_t value = MakeLarge(0xAABBCCDD, 0x11223344);
|
uint64_t value = MakeLarge(0xAABBCCDD, 0x11223344);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue