mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(big): add Add
This commit is contained in:
parent
b2cbc02189
commit
aad7f751dd
3 changed files with 79 additions and 0 deletions
|
|
@ -1,5 +1,16 @@
|
||||||
#include "storm/big/Ops.hpp"
|
#include "storm/big/Ops.hpp"
|
||||||
|
|
||||||
|
void Add(BigBuffer& a, const BigBuffer& b, uint32_t c) {
|
||||||
|
uint64_t carry = c;
|
||||||
|
uint32_t i = 0;
|
||||||
|
for (i = 0; carry || b.IsUsed(i); i++) {
|
||||||
|
carry += b[i];
|
||||||
|
a[i] = ExtractLowPart(carry);
|
||||||
|
}
|
||||||
|
|
||||||
|
a.SetCount(i);
|
||||||
|
}
|
||||||
|
|
||||||
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c) {
|
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c) {
|
||||||
uint64_t carry = 0;
|
uint64_t carry = 0;
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include "storm/big/BigStack.hpp"
|
#include "storm/big/BigStack.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
void Add(BigBuffer& a, const BigBuffer& b, uint32_t c);
|
||||||
|
|
||||||
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c);
|
void Add(BigBuffer& a, const BigBuffer& b, const BigBuffer& c);
|
||||||
|
|
||||||
uint32_t ExtractLowPart(uint64_t& value);
|
uint32_t ExtractLowPart(uint64_t& value);
|
||||||
|
|
|
||||||
66
test/Big.cpp
66
test/Big.cpp
|
|
@ -2,6 +2,72 @@
|
||||||
#include "storm/big/Ops.hpp"
|
#include "storm/big/Ops.hpp"
|
||||||
#include "test/Test.hpp"
|
#include "test/Test.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("Add", "[big]") {
|
||||||
|
SECTION("adds 0 and 1") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 0);
|
||||||
|
|
||||||
|
uint64_t c = 1;
|
||||||
|
|
||||||
|
Add(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
a->Primary().Trim();
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 1);
|
||||||
|
CHECK(a->Primary()[0] == 1);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("adds 2 and 4") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 2);
|
||||||
|
|
||||||
|
uint64_t c = 4;
|
||||||
|
|
||||||
|
Add(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
a->Primary().Trim();
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 1);
|
||||||
|
CHECK(a->Primary()[0] == 6);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("adds 0xFFFFFFFF and 0xCCCCCCCC") {
|
||||||
|
BigData* a;
|
||||||
|
SBigNew(&a);
|
||||||
|
|
||||||
|
BigData* b;
|
||||||
|
SBigNew(&b);
|
||||||
|
SBigFromUnsigned(b, 0xFFFFFFFF);
|
||||||
|
|
||||||
|
uint64_t c = 0xCCCCCCCC;
|
||||||
|
|
||||||
|
Add(a->Primary(), b->Primary(), c);
|
||||||
|
|
||||||
|
a->Primary().Trim();
|
||||||
|
|
||||||
|
CHECK(a->Primary().Count() == 2);
|
||||||
|
CHECK(a->Primary()[0] == 0xCCCCCCCB);
|
||||||
|
CHECK(a->Primary()[1] == 0x1);
|
||||||
|
|
||||||
|
SBigDel(a);
|
||||||
|
SBigDel(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue