From 272b12b00168efc0cf08ad0f69f52b45067a40b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hm?= <134922046+LordofGhost@users.noreply.github.com> Date: Sun, 23 Mar 2025 19:55:49 +0100 Subject: [PATCH 1/3] debug mod functionality --- src/main.cpp | 13 +++++++------ src/vec/operations.h | 9 ++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f7106d8..9291e80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,13 +57,14 @@ int main(int argc, char* argv[]) { //std::vector num1 = operations::convertToVector(55555555); //std::vector num2 = operations::convertToVector(45678); - // 00010000 00000000 00000000 00000000 - std::vector num1 = {0b00000000, 0b00000000, 0b00000000, 0b00010000}; - // 00000000 00000100 00000000 00000000 - std::vector num2 = {0b00000000, 0b00000000, 0b00000100}; + std::vector num1 = {11}; + std::vector num2 = {3}; - std::vector result = operations::mul(num1, num2); - std::cout << operations::isBigger(num1, num2) << std::endl; + std::vector remainder; + + std::vector result = operations::div(num1, num2, remainder); + + std::cout << "Remainder: " << remainder[0] << std::endl; // This prints out the result as a hex number for (auto it = result.rbegin(); it != result.rend(); ++it) { diff --git a/src/vec/operations.h b/src/vec/operations.h index fc68774..934477b 100644 --- a/src/vec/operations.h +++ b/src/vec/operations.h @@ -267,7 +267,9 @@ namespace operations { [[nodiscard]] std::vector div( const std::vector dividend, - const std::vector &divisor) noexcept { + const std::vector &divisor, + std::vector &remaining) noexcept +{ std::vector quotient; std::uint8_t quotientBuffer = 0; std::uint16_t quotientBitIndex = 0; @@ -301,6 +303,11 @@ namespace operations { * digits are not supported */ if (dividendIndex < 0) { quotientBuffer <<= 1; + + if (true) { + remaining = dividendMask; + } + break; } From 6647b4a64b399eb19599d536c2cbb7fb01f3acbf Mon Sep 17 00:00:00 2001 From: Jochen <97750753+Jochengehtab@users.noreply.github.com> Date: Sun, 23 Mar 2025 20:12:34 +0100 Subject: [PATCH 2/3] fix mod --- src/main.cpp | 4 ++-- src/vec/operations.h | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9291e80..c188528 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,8 +62,8 @@ int main(int argc, char* argv[]) { std::vector remainder; - std::vector result = operations::div(num1, num2, remainder); - + std::vector result = operations::div(num1, num2, nullptr); + std::cout << "Remainder: " << remainder[0] << std::endl; // This prints out the result as a hex number diff --git a/src/vec/operations.h b/src/vec/operations.h index 934477b..b017302 100644 --- a/src/vec/operations.h +++ b/src/vec/operations.h @@ -268,7 +268,7 @@ namespace operations { [[nodiscard]] std::vector div( const std::vector dividend, const std::vector &divisor, - std::vector &remaining) noexcept + std::vector *remaining = nullptr) noexcept { std::vector quotient; std::uint8_t quotientBuffer = 0; @@ -291,6 +291,19 @@ namespace operations { } if (isEqual(dividendMask, divisor) || isBigger(dividendMask, divisor)) { + + /* Stop the loop if the dividend is smaller than the divisor, because fractional + * digits are not supported */ + if (dividendIndex < 0) { + quotientBuffer <<= 1; + + if (remaining != nullptr) { + *remaining = dividendMask; + } + + break; + } + // Shift the dividend and set the new bit as high quotientBuffer <<= 1; quotientBuffer++; @@ -304,8 +317,8 @@ namespace operations { if (dividendIndex < 0) { quotientBuffer <<= 1; - if (true) { - remaining = dividendMask; + if (remaining != nullptr) { + *remaining = dividendMask; } break; From fa1ca61d160231cfe87495430fbe60f4c3aca6d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hm?= <134922046+LordofGhost@users.noreply.github.com> Date: Sun, 23 Mar 2025 20:26:13 +0100 Subject: [PATCH 3/3] cleanup code --- src/main.cpp | 18 ------------------ src/vec/operations.h | 12 ++++-------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c188528..15e2ebd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,23 +54,5 @@ int main(int argc, char* argv[]) { cli::help(); } - //std::vector num1 = operations::convertToVector(55555555); - //std::vector num2 = operations::convertToVector(45678); - - std::vector num1 = {11}; - std::vector num2 = {3}; - - std::vector remainder; - - std::vector result = operations::div(num1, num2, nullptr); - - std::cout << "Remainder: " << remainder[0] << std::endl; - - // This prints out the result as a hex number - for (auto it = result.rbegin(); it != result.rend(); ++it) { - printf("%02X", *it); - } - std::cout << std::endl; - return 0; } diff --git a/src/vec/operations.h b/src/vec/operations.h index b017302..c76a995 100644 --- a/src/vec/operations.h +++ b/src/vec/operations.h @@ -297,10 +297,8 @@ namespace operations { if (dividendIndex < 0) { quotientBuffer <<= 1; - if (remaining != nullptr) { - *remaining = dividendMask; - } - + // If remaining pointer is passed, set the remaining value + if (remaining != nullptr) *remaining = dividendMask; break; } @@ -317,10 +315,8 @@ namespace operations { if (dividendIndex < 0) { quotientBuffer <<= 1; - if (remaining != nullptr) { - *remaining = dividendMask; - } - + // If remaining pointer is passed, set the remaining value + if (remaining != nullptr) *remaining = dividendMask; break; }