aboutsummaryrefslogtreecommitdiffstats
path: root/chilbert/detail
diff options
context:
space:
mode:
Diffstat (limited to 'chilbert/detail')
-rw-r--r--chilbert/detail/MultiBitVec.hpp10
-rw-r--r--chilbert/detail/operations.hpp3
2 files changed, 8 insertions, 5 deletions
diff --git a/chilbert/detail/MultiBitVec.hpp b/chilbert/detail/MultiBitVec.hpp
index 4bf5085..283601f 100644
--- a/chilbert/detail/MultiBitVec.hpp
+++ b/chilbert/detail/MultiBitVec.hpp
@@ -351,11 +351,13 @@ gray_code(MultiBitVec<Derived>& value)
{
typename MultiBitVec<Derived>::Rack s = 0;
+ constexpr size_t left_shift = MultiBitVec<Derived>::bits_per_rack - 1;
for (size_t ri = 0; ri < value.num_racks(); ++ri) {
- const size_t i = value.num_racks() - ri - 1;
- const auto t = value.rack(i) & 1;
- gray_code(value.rack(i));
- value.rack(i) ^= (s << (MultiBitVec<Derived>::bits_per_rack - 1));
+ const size_t i = value.num_racks() - ri - 1;
+ auto& rack = value.rack(i);
+ const auto t = rack & 1;
+ gray_code(rack);
+ rack ^= (s << left_shift);
s = t;
}
}
diff --git a/chilbert/detail/operations.hpp b/chilbert/detail/operations.hpp
index 94635b4..1c6b8bf 100644
--- a/chilbert/detail/operations.hpp
+++ b/chilbert/detail/operations.hpp
@@ -157,7 +157,8 @@ template <typename T>
std::enable_if_t<std::is_integral<T>::value>
gray_code_inv(T& value)
{
- for (T shift = 1; shift < sizeof(T) * CHAR_BIT; shift <<= 1) {
+ constexpr T shift_end = sizeof(T) * CHAR_BIT;
+ for (T shift = 1; shift < shift_end; shift <<= 1) {
value ^= (value >> shift);
}
}