feat(big): implement ToStream and EncodeDataBytes ops

This commit is contained in:
Adam Heinermann 2024-11-17 03:17:16 -08:00 committed by superp00t
parent 8e96181679
commit 6a88f93619
3 changed files with 68 additions and 1 deletions

View file

@ -131,6 +131,15 @@ void Div(BigBuffer& a, BigBuffer& b, const BigBuffer& c, const BigBuffer& d, Big
stack.Free(allocCount); stack.Free(allocCount);
} }
void EncodeDataBytes(TSGrowableArray<uint8_t>& output, uint32_t value) {
uint32_t v = value;
while (v != 0) {
*output.New() = v % 255u;
v /= 255u;
}
*output.New() = 255;
}
uint32_t ExtractLowPart(uint64_t& value) { uint32_t ExtractLowPart(uint64_t& value) {
auto low = static_cast<uint32_t>(value); auto low = static_cast<uint32_t>(value);
value >>= 32; value >>= 32;
@ -467,3 +476,9 @@ void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
output.SetCount(0); output.SetCount(0);
ToBinaryAppend(output, buffer); ToBinaryAppend(output, buffer);
} }
void ToStream(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
ToBinary(output, buffer);
EncodeDataBytes(output, output.Count());
ToBinaryAppend(output, buffer);
}

View file

@ -17,6 +17,8 @@ void Div(BigBuffer& a, uint32_t* b, const BigBuffer& c, uint64_t d);
void Div(BigBuffer& a, BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack); void Div(BigBuffer& a, BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack);
void EncodeDataBytes(TSGrowableArray<uint8_t>& output, uint32_t value);
uint32_t ExtractLowPart(uint64_t& value); uint32_t ExtractLowPart(uint64_t& value);
uint32_t ExtractLowPartLargeSum(uint64_t& value, uint64_t add); uint32_t ExtractLowPartLargeSum(uint64_t& value, uint64_t add);
@ -71,4 +73,6 @@ void Sub(BigBuffer& a, const BigBuffer& b, uint32_t c);
void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer); void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer);
void ToStream(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer);
#endif #endif

View file

@ -1,6 +1,7 @@
#include "storm/Big.hpp" #include "storm/Big.hpp"
#include "storm/big/Ops.hpp" #include "storm/big/Ops.hpp"
#include "test/Test.hpp" #include "test/Test.hpp"
#include <vector>
TEST_CASE("Add", "[big]") { TEST_CASE("Add", "[big]") {
BigDataTest a, b; BigDataTest a, b;
@ -280,6 +281,28 @@ TEST_CASE("Div", "[big]") {
} }
} }
TEST_CASE("EncodeDataBytes", "[big]") {
struct EncodeTestCase {
uint32_t input;
std::vector<uint8_t> output;
};
SECTION("encodes values to array") {
auto v = GENERATE(
EncodeTestCase{ 0, { 255 } },
EncodeTestCase{ 1, { 1, 255 } },
EncodeTestCase{ 255, { 0, 1, 255 } },
EncodeTestCase{ UINT32_MAX, { 0, 4, 6, 4, 1, 255 } }
);
TSGrowableArray<uint8_t> arr;
EncodeDataBytes(arr, v.input);
CHECK(arr.Count() == v.output.size());
CHECK(std::vector<uint8_t>(arr.m_data, arr.m_data + arr.Count()) == v.output);
}
}
TEST_CASE("ExtractLowPart", "[big]") { TEST_CASE("ExtractLowPart", "[big]") {
SECTION("extracts low part of 0") { SECTION("extracts low part of 0") {
uint64_t value = 0; uint64_t value = 0;
@ -1045,5 +1068,30 @@ TEST_CASE("Sub", "[big]") {
CHECK(a->Primary()[0] == 0xDB975320); CHECK(a->Primary()[0] == 0xDB975320);
CHECK(a->Primary()[1] == 0xECA8641F); CHECK(a->Primary()[1] == 0xECA8641F);
} }
}
TEST_CASE("ToStream", "[big]") {
BigDataTest a;
struct ToStreamTestCase {
uint64_t input;
std::vector<uint8_t> output;
};
SECTION("streams buffers to array") {
auto v = GENERATE(
ToStreamTestCase{ 0, { 255 } },
ToStreamTestCase{ 1, { 1, 1, 255, 1 } },
ToStreamTestCase{ 255, { 0xFF, 1, 255, 0xFF } },
ToStreamTestCase{ UINT32_MAX, { 0xFF, 0xFF, 0xFF, 0xFF, 4, 255, 0xFF, 0xFF, 0xFF, 0xFF } },
ToStreamTestCase{ 0x123456789ABCDEF0, { 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 8, 255, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12 } }
);
TSGrowableArray<uint8_t> arr;
SBigFromStr(a, std::to_string(v.input).c_str());
ToStream(arr, a->Primary());
CHECK(std::vector<uint8_t>(arr.m_data, arr.m_data + arr.Count()) == v.output);
}
} }