feat(big): add SBigIsOdd

This commit is contained in:
Adam Heinermann 2024-11-15 08:31:27 -08:00 committed by superp00t
parent 6759cb4d7c
commit 9074869097
5 changed files with 35 additions and 0 deletions

View file

@ -73,6 +73,10 @@ int32_t SBigIsEven(BigData* a) {
return IsEven(a->Primary()); return IsEven(a->Primary());
} }
int32_t SBigIsOdd(BigData* a) {
return IsOdd(a->Primary());
}
void SBigMod(BigData* a, BigData* b, BigData* c) { void SBigMod(BigData* a, BigData* b, BigData* c) {
uint32_t allocCount = 0; uint32_t allocCount = 0;
auto& scratch = a->Stack().Alloc(&allocCount); auto& scratch = a->Stack().Alloc(&allocCount);

View file

@ -30,6 +30,8 @@ void SBigInc(BigData* a, BigData* b);
int32_t SBigIsEven(BigData* a); int32_t SBigIsEven(BigData* a);
int32_t SBigIsOdd(BigData* a);
void SBigMod(BigData* a, BigData* b, BigData* c); void SBigMod(BigData* a, BigData* b, BigData* c);
void SBigMul(BigData* a, BigData* b, BigData* c); void SBigMul(BigData* a, BigData* b, BigData* c);

View file

@ -215,6 +215,11 @@ int32_t IsEven(const BigBuffer &num) {
return num.Count() == 0 || (num[0] & 1) == 0; return num.Count() == 0 || (num[0] & 1) == 0;
} }
int32_t IsOdd(const BigBuffer &num) {
num.Trim();
return num.Count() != 0 && (num[0] & 1) != 0;
}
uint64_t MakeLarge(uint32_t low, uint32_t high) { 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);
} }

View file

@ -35,6 +35,8 @@ void InsertLowPart(uint64_t& value, uint32_t low);
int32_t IsEven(const BigBuffer &num); int32_t IsEven(const BigBuffer &num);
int32_t IsOdd(const BigBuffer &num);
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 Mul(BigBuffer& a, const BigBuffer& b, uint64_t c);

View file

@ -448,6 +448,28 @@ TEST_CASE("SBigIsEven", "[big]") {
} }
} }
TEST_CASE("SBigIsOdd", "[big]") {
BigDataTest a;
SECTION("unset zero is not odd") {
CHECK_FALSE(SBigIsOdd(a));
}
SECTION("numbers are not odd") {
auto v = GENERATE(0ULL, 2ULL, 10ULL, 10000ULL, 0xFFFFFFFEULL, 0x9999888877776666ULL);
SBigFromStr(a, std::to_string(v).c_str());
CHECK_FALSE(SBigIsOdd(a));
}
SECTION("numbers are odd") {
auto v = GENERATE(1ULL, 3ULL, 37ULL, 999999999ULL, 0xFFFFFFFFFULL, 0x9999888877776667ULL);
SBigFromStr(a, std::to_string(v).c_str());
CHECK(SBigIsOdd(a));
}
}
TEST_CASE("SBigMod", "[big]") { TEST_CASE("SBigMod", "[big]") {
SECTION("mods 7 by 4") { SECTION("mods 7 by 4") {
BigData* a; BigData* a;