mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(big): add SBigMul
This commit is contained in:
parent
e74654800d
commit
6e96e0a767
11 changed files with 179 additions and 0 deletions
|
|
@ -24,6 +24,10 @@ void SBigNew(BigData** num) {
|
|||
*num = new (m) BigData();
|
||||
}
|
||||
|
||||
void SBigMul(BigData* a, BigData* b, BigData* c) {
|
||||
Mul(a->Primary(), b->Primary(), c->Primary(), a->Stack());
|
||||
}
|
||||
|
||||
void SBigToBinaryBuffer(BigData* num, uint8_t* data, uint32_t maxBytes, uint32_t* bytes) {
|
||||
auto& output = num->Output();
|
||||
ToBinary(output, num->Primary());
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ void SBigFromBinary(BigData* num, const void* data, uint32_t bytes);
|
|||
|
||||
void SBigFromUnsigned(BigData* num, uint32_t val);
|
||||
|
||||
void SBigMul(BigData* a, BigData* b, BigData* c);
|
||||
|
||||
void SBigNew(BigData** num);
|
||||
|
||||
void SBigToBinaryBuffer(BigData* num, uint8_t* data, uint32_t maxBytes, uint32_t* bytes);
|
||||
|
|
|
|||
|
|
@ -32,3 +32,15 @@ int32_t BigBuffer::IsUsed(uint32_t index) const {
|
|||
void BigBuffer::SetCount(uint32_t count) {
|
||||
this->m_data.SetCount(this->m_offset + count);
|
||||
}
|
||||
|
||||
void BigBuffer::Trim() const {
|
||||
while (this->Count()) {
|
||||
auto& data = const_cast<TSGrowableArray<uint32_t>&>(this->m_data);
|
||||
|
||||
if (*data.Top()) {
|
||||
break;
|
||||
}
|
||||
|
||||
data.SetCount(data.Count() - 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ class BigBuffer {
|
|||
void GrowToFit(uint32_t index);
|
||||
int32_t IsUsed(uint32_t index) const;
|
||||
void SetCount(uint32_t count);
|
||||
void Trim() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -7,3 +7,7 @@ TSGrowableArray<uint8_t>& BigData::Output() const {
|
|||
BigBuffer& BigData::Primary() {
|
||||
return this->m_primary;
|
||||
}
|
||||
|
||||
BigStack& BigData::Stack() const {
|
||||
return const_cast<BigStack&>(this->m_stack);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class BigData {
|
|||
// Member functions
|
||||
TSGrowableArray<uint8_t>& Output() const;
|
||||
BigBuffer& Primary();
|
||||
BigStack& Stack() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
29
storm/big/BigStack.cpp
Normal file
29
storm/big/BigStack.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "storm/big/BigStack.hpp"
|
||||
|
||||
BigBuffer& BigStack::Alloc(uint32_t* count) {
|
||||
STORM_ASSERT(this->m_used < SIZE);
|
||||
|
||||
if (count) {
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
auto& buffer = this->m_buffer[this->m_used];
|
||||
this->m_used++;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void BigStack::Free(uint32_t count) {
|
||||
this->m_used -= count;
|
||||
}
|
||||
|
||||
BigBuffer& BigStack::MakeDistinct(BigBuffer& orig, int32_t required) {
|
||||
return required ? this->Alloc(nullptr) : orig;
|
||||
}
|
||||
|
||||
void BigStack::UnmakeDistinct(BigBuffer& orig, BigBuffer& distinct) {
|
||||
if (&orig != &distinct) {
|
||||
orig = distinct;
|
||||
this->Free(1);
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,10 @@ class BigStack {
|
|||
uint32_t m_used = 0;
|
||||
|
||||
// Member functions
|
||||
BigBuffer& Alloc(uint32_t* count);
|
||||
void Free(uint32_t count);
|
||||
BigBuffer& MakeDistinct(BigBuffer& orig, int32_t required);
|
||||
void UnmakeDistinct(BigBuffer& orig, BigBuffer& distinct);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -62,6 +62,26 @@ void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c) {
|
|||
a.SetCount(i);
|
||||
}
|
||||
|
||||
void Mul(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, BigStack& stack) {
|
||||
auto& aa = stack.MakeDistinct(a, &a == &b || &a == &c);
|
||||
aa.Clear();
|
||||
|
||||
uint32_t i = 0;
|
||||
for (i = 0; b.IsUsed(i); i++) {
|
||||
uint64_t carry = 0;
|
||||
|
||||
uint32_t j = 0;
|
||||
for (j = 0; c.IsUsed(j); j++) {
|
||||
carry += aa[i + j] + b[i] * static_cast<uint64_t>(c[j]);
|
||||
aa[i + j] = ExtractLowPart(carry);
|
||||
}
|
||||
|
||||
aa[i + j] = ExtractLowPart(carry);
|
||||
}
|
||||
|
||||
stack.UnmakeDistinct(a, aa);
|
||||
}
|
||||
|
||||
void ToBinaryAppend(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
|
||||
for (uint32_t i = 0; i < buffer.Count() * 4; i++) {
|
||||
auto byte = buffer[i / 4] >> (8 * (i & 3));
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define STORM_BIG_OPS_HPP
|
||||
|
||||
#include "storm/big/BigBuffer.hpp"
|
||||
#include "storm/big/BigStack.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c);
|
||||
|
|
@ -18,6 +19,8 @@ uint64_t MakeLarge(uint32_t low, uint32_t high);
|
|||
|
||||
void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c);
|
||||
|
||||
void Mul(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, BigStack& stack);
|
||||
|
||||
void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
99
test/Big.cpp
99
test/Big.cpp
|
|
@ -270,6 +270,105 @@ TEST_CASE("SBigFromUnsigned", "[big]") {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SBigMul", "[big]") {
|
||||
SECTION("multiplies 0 and 1") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 0);
|
||||
|
||||
BigData* c;
|
||||
SBigNew(&c);
|
||||
SBigFromUnsigned(c, 1);
|
||||
|
||||
SBigMul(a, b, c);
|
||||
|
||||
a->Primary().Trim();
|
||||
|
||||
CHECK(a->Primary().Count() == 0);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
SBigDel(c);
|
||||
}
|
||||
|
||||
SECTION("multiplies 2 and 4") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 2);
|
||||
|
||||
BigData* c;
|
||||
SBigNew(&c);
|
||||
SBigFromUnsigned(c, 4);
|
||||
|
||||
SBigMul(a, b, c);
|
||||
|
||||
a->Primary().Trim();
|
||||
|
||||
CHECK(a->Primary().Count() == 1);
|
||||
CHECK(a->Primary()[0] == 8);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
SBigDel(c);
|
||||
}
|
||||
|
||||
SECTION("multiplies 0xFFFFFFFF and 0x100") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 0xFFFFFFFF);
|
||||
|
||||
BigData* c;
|
||||
SBigNew(&c);
|
||||
SBigFromUnsigned(c, 0x100);
|
||||
|
||||
SBigMul(a, b, c);
|
||||
|
||||
a->Primary().Trim();
|
||||
|
||||
CHECK(a->Primary().Count() == 2);
|
||||
CHECK(a->Primary()[0] == 0xFFFFFF00);
|
||||
CHECK(a->Primary()[1] == 0xFF);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
SBigDel(c);
|
||||
}
|
||||
|
||||
SECTION("multiplies 0xFFFFFF and 0x11223344") {
|
||||
BigData* a;
|
||||
SBigNew(&a);
|
||||
|
||||
BigData* b;
|
||||
SBigNew(&b);
|
||||
SBigFromUnsigned(b, 0xFFFFFF);
|
||||
|
||||
BigData* c;
|
||||
SBigNew(&c);
|
||||
SBigFromUnsigned(c, 0x11223344);
|
||||
|
||||
SBigMul(a, b, c);
|
||||
|
||||
a->Primary().Trim();
|
||||
|
||||
CHECK(a->Primary().Count() == 2);
|
||||
CHECK(a->Primary()[0] == 0x32DDCCBC);
|
||||
CHECK(a->Primary()[1] == 0x112233);
|
||||
|
||||
SBigDel(a);
|
||||
SBigDel(b);
|
||||
SBigDel(c);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SBigToBinaryBuffer", "[big]") {
|
||||
SECTION("returns expected buffer for bigdata representing 0") {
|
||||
BigData* num;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue