[PATCH] D121281: [ADT] Make BitmaskEnum operations constant expressions
Pavel Labath via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 11 02:12:58 PST 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG161bddf3af09: [ADT] Make BitmaskEnum operations constant expressions (authored by labath).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D121281/new/
https://reviews.llvm.org/D121281
Files:
llvm/include/llvm/ADT/BitmaskEnum.h
llvm/include/llvm/Support/MathExtras.h
llvm/unittests/ADT/BitmaskEnumTest.cpp
Index: llvm/unittests/ADT/BitmaskEnumTest.cpp
===================================================================
--- llvm/unittests/ADT/BitmaskEnumTest.cpp
+++ llvm/unittests/ADT/BitmaskEnumTest.cpp
@@ -76,6 +76,17 @@
EXPECT_EQ(F3, f);
}
+TEST(BitmaskEnumTest, ConstantExpression) {
+ constexpr Flags f1 = ~F1;
+ constexpr Flags f2 = F1 | F2;
+ constexpr Flags f3 = F1 & F2;
+ constexpr Flags f4 = F1 ^ F2;
+ EXPECT_EQ(f1, ~F1);
+ EXPECT_EQ(f2, F1 | F2);
+ EXPECT_EQ(f3, F1 & F2);
+ EXPECT_EQ(f4, F1 ^ F2);
+}
+
TEST(BitmaskEnumTest, BitwiseNot) {
Flags f = ~F1;
EXPECT_EQ(14, f); // Largest value for f is 15.
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -707,7 +707,7 @@
/// Returns the next power of two (in 64-bits) that is strictly greater than A.
/// Returns zero on overflow.
-inline uint64_t NextPowerOf2(uint64_t A) {
+constexpr inline uint64_t NextPowerOf2(uint64_t A) {
A |= (A >> 1);
A |= (A >> 2);
A |= (A >> 4);
Index: llvm/include/llvm/ADT/BitmaskEnum.h
===================================================================
--- llvm/include/llvm/ADT/BitmaskEnum.h
+++ llvm/include/llvm/ADT/BitmaskEnum.h
@@ -77,7 +77,7 @@
/// Get a bitmask with 1s in all places up to the high-order bit of E's largest
/// value.
-template <typename E> std::underlying_type_t<E> Mask() {
+template <typename E> constexpr std::underlying_type_t<E> Mask() {
// On overflow, NextPowerOf2 returns zero with the type uint64_t, so
// subtracting 1 gives us the mask with all bits set, like we want.
return NextPowerOf2(static_cast<std::underlying_type_t<E>>(
@@ -87,7 +87,7 @@
/// Check that Val is in range for E, and return Val cast to E's underlying
/// type.
-template <typename E> std::underlying_type_t<E> Underlying(E Val) {
+template <typename E> constexpr std::underlying_type_t<E> Underlying(E Val) {
auto U = static_cast<std::underlying_type_t<E>>(Val);
assert(U >= 0 && "Negative enum values are not allowed.");
assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
@@ -99,22 +99,22 @@
}
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator~(E Val) {
+constexpr E operator~(E Val) {
return static_cast<E>(~Underlying(Val) & Mask<E>());
}
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator|(E LHS, E RHS) {
+constexpr E operator|(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) | Underlying(RHS));
}
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator&(E LHS, E RHS) {
+constexpr E operator&(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) & Underlying(RHS));
}
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator^(E LHS, E RHS) {
+constexpr E operator^(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121281.414621.patch
Type: text/x-patch
Size: 3069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220311/0b48ca4a/attachment.bin>
More information about the llvm-commits
mailing list