1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-24 05:18:36 +00:00

Require overt acknowledgement of meaning.

This commit is contained in:
Thomas Harte
2025-01-28 17:42:26 -05:00
parent 53a3e88d16
commit 01ddc24c02
5 changed files with 38 additions and 32 deletions
+10 -5
View File
@@ -12,11 +12,16 @@
namespace Numeric {
enum class Operation {
Add,
Subtract,
};
/// @returns @c true if from @c bit there was:
/// • carry after calculating @c lhs + @c rhs if @c is_add is true; or
/// • borrow after calculating @c lhs - @c rhs if @c is_add is false;
/// producing @c result.
template <bool is_add, int bit, typename IntT> bool carried_out(IntT lhs, IntT rhs, IntT result) {
template <Operation operation, int bit, typename IntT> bool carried_out(IntT lhs, IntT rhs, IntT result) {
// Additive:
//
// 0 and 0 => didn't.
@@ -28,11 +33,11 @@ template <bool is_add, int bit, typename IntT> bool carried_out(IntT lhs, IntT r
// 1 and 0 => didn't
// 1 and 1 or 0 and 0 => did if 1.
// 0 and 1 => did.
if constexpr (!is_add) {
if constexpr (operation == Operation::Subtract) {
rhs = ~rhs;
}
const bool carry = IntT(1 << bit) & (lhs | rhs) & ((lhs & rhs) | ~result);
if constexpr (!is_add) {
if constexpr (operation == Operation::Subtract) {
return !carry;
} else {
return carry;
@@ -65,12 +70,12 @@ template <typename IntT> constexpr int bit_size() {
/// • @c lhs + @c rhs (if @c is_add is true); or
/// • @c lhs - @c rhs (if @c is_add is false)
/// and the result was @c result. All other bits will be clear.
template <bool is_add, typename IntT>
template <Operation operation, typename IntT>
IntT overflow(IntT lhs, IntT rhs, IntT result) {
const IntT output_changed = result ^ lhs;
const IntT input_differed = lhs ^ rhs;
if constexpr (is_add) {
if constexpr (operation == Operation::Add) {
return top_bit<IntT>() & output_changed & ~input_differed;
} else {
return top_bit<IntT>() & output_changed & input_differed;