From 935807ad1a4214be21c65acc80a715e010dc7a92 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 26 Aug 2018 20:58:15 +0200 Subject: Explicitly factor out some const expressions --- chilbert/detail/MultiBitVec.hpp | 10 ++++++---- chilbert/detail/operations.hpp | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'chilbert/detail') 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& value) { typename MultiBitVec::Rack s = 0; + constexpr size_t left_shift = MultiBitVec::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::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 std::enable_if_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); } } -- cgit v1.2.1