Index: include/llvm/ADT/PointerEmbeddedInt.h =================================================================== --- include/llvm/ADT/PointerEmbeddedInt.h +++ include/llvm/ADT/PointerEmbeddedInt.h @@ -11,6 +11,7 @@ #define LLVM_ADT_POINTEREMBEDDEDINT_H #include "llvm/ADT/DenseMapInfo.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/PointerLikeTypeTraits.h" #include @@ -30,6 +31,8 @@ class PointerEmbeddedInt { uintptr_t Value; + // Note: This '<' is correct; using '<=' would result in some shifts + // overflowing their storage types. static_assert(Bits < sizeof(uintptr_t) * CHAR_BIT, "Cannot embed more bits than we have in a pointer!"); @@ -42,26 +45,34 @@ Mask = static_cast(-1) << Bits }; + static constexpr const struct RawValueTag {} RawValue = {}; + friend class PointerLikeTypeTraits; - explicit PointerEmbeddedInt(uintptr_t Value) : Value(Value) {} + explicit PointerEmbeddedInt(uintptr_t Value, RawValueTag) : Value(Value) {} public: PointerEmbeddedInt() : Value(0) {} - PointerEmbeddedInt(IntT I) : Value(static_cast(I) << Shift) { - assert((I & Mask) == 0 && "Integer has bits outside those preserved!"); + PointerEmbeddedInt(IntT I) { + *this = I; } PointerEmbeddedInt &operator=(IntT I) { - assert((I & Mask) == 0 && "Integer has bits outside those preserved!"); + assert((std::is_signed::value ? llvm::isInt(I) + : llvm::isUInt(I)) && + "Integer has bits outside those preserved!"); Value = static_cast(I) << Shift; return *this; } // Note that this implicit conversion additionally allows all of the basic // comparison operators to work transparently, etc. - operator IntT() const { return static_cast(Value >> Shift); } + operator IntT() const { + if (std::is_signed::value) + return static_cast(static_cast(Value) >> Shift); + return static_cast(Value >> Shift); + } }; // Provide pointer like traits to support use with pointer unions and sum @@ -75,10 +86,10 @@ return reinterpret_cast(P.Value); } static inline T getFromVoidPointer(void *P) { - return T(reinterpret_cast(P)); + return T(reinterpret_cast(P), T::RawValue); } static inline T getFromVoidPointer(const void *P) { - return T(reinterpret_cast(P)); + return T(reinterpret_cast(P), T::RawValue); } enum { NumLowBitsAvailable = T::Shift }; Index: unittests/ADT/PointerEmbeddedIntTest.cpp =================================================================== --- unittests/ADT/PointerEmbeddedIntTest.cpp +++ unittests/ADT/PointerEmbeddedIntTest.cpp @@ -43,4 +43,38 @@ EXPECT_FALSE(42 >= J); } +TEST(PointerEmbeddedIntTest, intptr_t) { + PointerEmbeddedInt IPos = 42, INeg = -42; + EXPECT_EQ(42, IPos); + EXPECT_EQ(-42, INeg); + + PointerEmbeddedInt U = 42, USaturated = 255; + EXPECT_EQ(42U, U); + EXPECT_EQ(255U, USaturated); + + PointerEmbeddedInt::digits> + IMax = std::numeric_limits::max() >> 1, + IMin = std::numeric_limits::min() >> 1; + EXPECT_EQ(std::numeric_limits::max() >> 1, IMax); + EXPECT_EQ(std::numeric_limits::min() >> 1, IMin); + + PointerEmbeddedInt::digits - 1> + UMax = std::numeric_limits::max() >> 1, + UMin = std::numeric_limits::min() >> 1; + EXPECT_EQ(std::numeric_limits::max() >> 1, UMax); + EXPECT_EQ(std::numeric_limits::min() >> 1, UMin); +} + +TEST(PointerEmbeddedIntTest, PointerLikeTypeTraits) { + PointerEmbeddedInt I = 42; + using ITraits = PointerLikeTypeTraits; + EXPECT_EQ(42, ITraits::getFromVoidPointer(ITraits::getAsVoidPointer(I))); + + PointerEmbeddedInt::digits - 1> + Max = std::numeric_limits::max() >> 1; + using MaxTraits = PointerLikeTypeTraits; + EXPECT_EQ(std::numeric_limits::max() >> 1, + MaxTraits::getFromVoidPointer(MaxTraits::getAsVoidPointer(Max))); +} + } // end anonymous namespace