From 328bbf61d8552f32882f43042e1c70e02cacbfe4 Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Fri, 15 Nov 2024 07:52:35 -0800 Subject: [PATCH] feat(big): add SBigDiv --- storm/Big.cpp | 9 ++++++ storm/Big.hpp | 2 ++ storm/big/Ops.cpp | 2 +- test/Big.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/storm/Big.cpp b/storm/Big.cpp index 12b3234..c04e91a 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -44,6 +44,15 @@ void SBigDel(BigData* num) { delete num; } +void SBigDiv(BigData* a, BigData* b, BigData* c) { + uint32_t allocCount = 0; + BigBuffer &buf = a->Stack().Alloc(&allocCount); + + Div(a->Primary(), buf, b->Primary(), c->Primary(), a->Stack()); + + a->Stack().Free(allocCount); +} + void SBigFromBinary(BigData* num, const void* data, uint32_t bytes) { FromBinary(num->Primary(), data, bytes); } diff --git a/storm/Big.hpp b/storm/Big.hpp index eb9281c..bd7dac7 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -18,6 +18,8 @@ void SBigDec(BigData* a, BigData* b); void SBigDel(BigData* num); +void SBigDiv(BigData* a, BigData* b, BigData* c); + void SBigFromBinary(BigData* num, const void* data, uint32_t bytes); void SBigFromStr(BigData* num, const char* str); diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index 61a783c..4b2cdea 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -409,7 +409,7 @@ void Sub(BigBuffer& a, const BigBuffer& b, uint32_t c) { uint32_t i = 0; for (; b.IsUsed(i); i++) { - borrow += b[i] - static_cast(c);; + borrow += b[i] - c; a[i] = ExtractLowPartSx(borrow); } diff --git a/test/Big.cpp b/test/Big.cpp index fdd0776..d6f93bf 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -1,5 +1,6 @@ #include "storm/Big.hpp" #include "test/Test.hpp" +#include TEST_CASE("SBigAdd", "[big]") { SECTION("adds 0 and 1") { @@ -214,8 +215,7 @@ TEST_CASE("SBigCompare", "[big]") { } TEST_CASE("SBigCopy", "[big]") { - BigDataTest a; - BigDataTest b; + BigDataTest a, b; SECTION("copies data") { uint8_t num[] = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; @@ -232,8 +232,7 @@ TEST_CASE("SBigCopy", "[big]") { } TEST_CASE("SBigDec", "[big]") { - BigDataTest a; - BigDataTest b; + BigDataTest a, b; SECTION("decrements value by 1") { SBigFromUnsigned(b, 5); @@ -254,6 +253,71 @@ TEST_CASE("SBigDec", "[big]") { } } +TEST_CASE("SBigDiv", "[big]") { + BigDataTest a, b, c; + + SECTION("divides 2 by 1") { + SBigFromUnsigned(b, 2); + SBigFromUnsigned(c, 1); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 2); + } + + SECTION("divides 5 by 2") { + SBigFromUnsigned(b, 5); + SBigFromUnsigned(c, 2); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 2); + } + + SECTION("divides 7 by 4") { + SBigFromUnsigned(b, 7); + SBigFromUnsigned(c, 4); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 1); + } + + SECTION("divides 0x9999444488885555 by 0x2222") { + SBigFromStr(b, std::to_string(0x9999444488885555ULL).c_str()); + SBigFromUnsigned(c, 0x2222); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 2); + CHECK(a->Primary()[0] == 0x00040002); + CHECK(a->Primary()[1] == 0x48002); + } + + SECTION("divides 0x9999444488885555 by 0xFFFFFFFF") { + SBigFromStr(b, std::to_string(0x9999444488885555ULL).c_str()); + SBigFromUnsigned(c, 0xFFFFFFFF); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 0x99994445); + } + + SECTION("divides 0x9999444488885555 by 0x1111222233334444 (buffer divisor)") { + SBigFromStr(b, std::to_string(0x9999444488885555ULL).c_str()); + SBigFromStr(c, std::to_string(0x1111222233334444ULL).c_str()); + + SBigDiv(a, b, c); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 8); + } +} + TEST_CASE("SBigFromBinary", "[big]") { SECTION("creates bigdata from 0") { BigData* num;