diff --git a/storm/Big.cpp b/storm/Big.cpp index 7e59fd3..c64beaf 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -73,6 +73,10 @@ int32_t SBigIsEven(BigData* a) { return IsEven(a->Primary()); } +int32_t SBigIsOdd(BigData* a) { + return IsOdd(a->Primary()); +} + void SBigMod(BigData* a, BigData* b, BigData* c) { uint32_t allocCount = 0; auto& scratch = a->Stack().Alloc(&allocCount); diff --git a/storm/Big.hpp b/storm/Big.hpp index 1f77ad7..5d19f38 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -30,6 +30,8 @@ void SBigInc(BigData* a, BigData* b); int32_t SBigIsEven(BigData* a); +int32_t SBigIsOdd(BigData* a); + void SBigMod(BigData* a, BigData* b, BigData* c); void SBigMul(BigData* a, BigData* b, BigData* c); diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index 6d25914..7cc3a4c 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -215,6 +215,11 @@ int32_t IsEven(const BigBuffer &num) { 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) { return low + (static_cast(high) << 32); } diff --git a/storm/big/Ops.hpp b/storm/big/Ops.hpp index 2154dfc..28d3b38 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -35,6 +35,8 @@ void InsertLowPart(uint64_t& value, uint32_t low); int32_t IsEven(const BigBuffer &num); +int32_t IsOdd(const BigBuffer &num); + uint64_t MakeLarge(uint32_t low, uint32_t high); void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c); diff --git a/test/Big.cpp b/test/Big.cpp index 6eae76c..a29cbe8 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -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]") { SECTION("mods 7 by 4") { BigData* a;