summaryrefslogtreecommitdiff
path: root/Source/Core/Common/BitUtils.h
diff options
context:
space:
mode:
authorTilka <tilkax@gmail.com>2017-06-22 20:47:36 +0100
committerGitHub <noreply@github.com>2017-06-22 20:47:36 +0100
commit4e39a42e6bcbdb97820aabde88124436fb6b5979 (patch)
treef3cb1cae081cff81327b0bc20655505f6c17bff7 /Source/Core/Common/BitUtils.h
parente30484e8a7895a2a26f7e4e609c44c5b0d1c6331 (diff)
parent56158ca176eee120f29375a286182b5dd3d7fa60 (diff)
Merge pull request #5662 from blubberdiblub/improve_mmu_mask_checks
Improve MMU mask checks
Diffstat (limited to 'Source/Core/Common/BitUtils.h')
-rw-r--r--Source/Core/Common/BitUtils.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/Source/Core/Common/BitUtils.h b/Source/Core/Common/BitUtils.h
index eacf6bf099..8d5398fb39 100644
--- a/Source/Core/Common/BitUtils.h
+++ b/Source/Core/Common/BitUtils.h
@@ -99,4 +99,26 @@ constexpr Result ExtractBits(const T src) noexcept
return ExtractBits<T, Result>(src, begin, end);
}
+
+///
+/// Verifies whether the supplied value is a valid bit mask of the form 0b00...0011...11.
+/// Both edge cases of all zeros and all ones are considered valid masks, too.
+///
+/// @param mask The mask value to test for validity.
+///
+/// @tparam T The type of the value.
+///
+/// @return A bool indicating whether the mask is valid.
+///
+template <typename T>
+constexpr bool IsValidLowMask(const T mask) noexcept
+{
+ static_assert(std::is_integral<T>::value, "Mask must be an integral type.");
+ static_assert(std::is_unsigned<T>::value, "Signed masks can introduce hard to find bugs.");
+
+ // Can be efficiently determined without looping or bit counting. It's the counterpart
+ // to https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
+ // and doesn't require special casing either edge case.
+ return (mask & (mask + 1)) == 0;
+}
} // namespace Common