Skip to content

[clang][Interp] Fix value truncation when casting int128 to smaller size #67961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions clang/lib/AST/Interp/IntegralAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ template <unsigned Bits, bool Signed> class Integral;
class Boolean;

template <bool Signed> class IntegralAP final {
private:
template <typename T> static T truncateCast(const APSInt &V) {
return std::is_signed_v<T> ? V.trunc(sizeof(T) * 8).getSExtValue()
: V.trunc(sizeof(T) * 8).getZExtValue();
}

public:
APSInt V;

Expand All @@ -55,14 +61,14 @@ template <bool Signed> class IntegralAP final {
bool operator<=(IntegralAP RHS) const { return V <= RHS.V; }

explicit operator bool() const { return !V.isZero(); }
explicit operator int8_t() const { return V.getSExtValue(); }
explicit operator uint8_t() const { return V.getZExtValue(); }
explicit operator int16_t() const { return V.getSExtValue(); }
explicit operator uint16_t() const { return V.getZExtValue(); }
explicit operator int32_t() const { return V.getSExtValue(); }
explicit operator uint32_t() const { return V.getZExtValue(); }
explicit operator int64_t() const { return V.getSExtValue(); }
explicit operator uint64_t() const { return V.getZExtValue(); }
explicit operator int8_t() const { return truncateCast<int8_t>(V); }
explicit operator uint8_t() const { return truncateCast<uint8_t>(V); }
explicit operator int16_t() const { return truncateCast<int16_t>(V); }
explicit operator uint16_t() const { return truncateCast<uint16_t>(V); }
explicit operator int32_t() const { return truncateCast<int32_t>(V); }
explicit operator uint32_t() const { return truncateCast<uint32_t>(V); }
explicit operator int64_t() const { return truncateCast<int64_t>(V); }
explicit operator uint64_t() const { return truncateCast<uint64_t>(V); }

template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
assert(NumBits > 0);
Expand Down
10 changes: 9 additions & 1 deletion clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace i128 {
static_assert(Two == 2, "");

constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
static_assert(AllOnes == static_cast<uint128_t>(-1), "");
static_assert(AllOnes == UINT128_MAX, "");

#if __cplusplus >= 201402L
template <typename T>
Expand All @@ -70,6 +70,14 @@ namespace i128 {
static_assert(CastFrom<double>(12) == 12, "");
static_assert(CastFrom<long double>(12) == 12, "");

static_assert(CastFrom<char>(AllOnes) == -1, "");
static_assert(CastFrom<unsigned char>(AllOnes) == 0xFF, "");
static_assert(CastFrom<long>(AllOnes) == -1, "");
static_assert(CastFrom<unsigned short>(AllOnes) == 0xFFFF, "");
static_assert(CastFrom<int>(AllOnes) == -1, "");
static_assert(CastFrom<int128_t>(AllOnes) == -1, "");
static_assert(CastFrom<uint128_t>(AllOnes) == AllOnes, "");

template <typename T>
constexpr __int128 CastTo(T A) {
int128_t B = (int128_t)A;
Expand Down