mirror of
https://github.com/TomHarte/CLK.git
synced 2025-04-15 09:37:32 +00:00
Apply const
liberally.
This commit is contained in:
parent
da1d52033b
commit
d7b7152315
@ -21,7 +21,8 @@ enum class Operation {
|
||||
/// • 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 <Operation operation, int bit, typename IntT> bool carried_out(IntT lhs, IntT rhs, IntT result) {
|
||||
template <Operation operation, int bit, typename IntT>
|
||||
constexpr bool carried_out(const IntT lhs, const IntT rhs, const IntT result) {
|
||||
// Additive:
|
||||
//
|
||||
// 0 and 0 => didn't.
|
||||
@ -33,10 +34,9 @@ template <Operation operation, int bit, typename IntT> bool carried_out(IntT lhs
|
||||
// 1 and 0 => didn't
|
||||
// 1 and 1 or 0 and 0 => did if 1.
|
||||
// 0 and 1 => did.
|
||||
if constexpr (operation == Operation::Subtract) {
|
||||
rhs = ~rhs;
|
||||
}
|
||||
const bool carry = IntT(1 << bit) & (lhs | rhs) & ((lhs & rhs) | ~result);
|
||||
static_assert(operation == Operation::Add || operation == Operation::Subtract);
|
||||
const auto adj_rhs = (operation == Operation::Subtract) ? ~rhs : rhs;
|
||||
const bool carry = IntT(1 << bit) & (lhs | adj_rhs) & ((lhs & adj_rhs) | ~result);
|
||||
if constexpr (operation == Operation::Subtract) {
|
||||
return !carry;
|
||||
} else {
|
||||
@ -48,21 +48,24 @@ template <Operation operation, int bit, typename IntT> bool carried_out(IntT lhs
|
||||
/// • @c lhs + @c rhs; or
|
||||
/// • @c lhs - @c rhs;
|
||||
/// producing @c result.
|
||||
template <int bit, typename IntT> bool carried_in(IntT lhs, IntT rhs, IntT result) {
|
||||
template <int bit, typename IntT>
|
||||
constexpr bool carried_in(const IntT lhs, const IntT rhs, const IntT result) {
|
||||
// 0 and 0 or 1 and 1 => did if 1.
|
||||
// 0 and 1 or 1 and 0 => did if 0.
|
||||
return IntT(1 << bit) & (lhs ^ rhs ^ result);
|
||||
}
|
||||
|
||||
/// @returns An int of type @c IntT with only the most-significant bit set.
|
||||
template <typename IntT> constexpr IntT top_bit() {
|
||||
template <typename IntT>
|
||||
constexpr IntT top_bit() {
|
||||
static_assert(!std::numeric_limits<IntT>::is_signed);
|
||||
constexpr IntT max = std::numeric_limits<IntT>::max();
|
||||
return max - (max >> 1);
|
||||
}
|
||||
|
||||
/// @returns The number of bits in @c IntT.
|
||||
template <typename IntT> constexpr int bit_size() {
|
||||
template <typename IntT>
|
||||
constexpr int bit_size() {
|
||||
return sizeof(IntT) * 8;
|
||||
}
|
||||
|
||||
@ -71,10 +74,11 @@ template <typename IntT> constexpr int bit_size() {
|
||||
/// • @c lhs - @c rhs (if @c is_add is false)
|
||||
/// and the result was @c result. All other bits will be clear.
|
||||
template <Operation operation, typename IntT>
|
||||
IntT overflow(IntT lhs, IntT rhs, IntT result) {
|
||||
constexpr IntT overflow(const IntT lhs, const IntT rhs, const IntT result) {
|
||||
const IntT output_changed = result ^ lhs;
|
||||
const IntT input_differed = lhs ^ rhs;
|
||||
|
||||
static_assert(operation == Operation::Add || operation == Operation::Subtract);
|
||||
if constexpr (operation == Operation::Add) {
|
||||
return top_bit<IntT>() & output_changed & ~input_differed;
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user