Skip to content

Commit

Permalink
[clang][Interp][NFC] Simplify Integral using constexpr if
Browse files Browse the repository at this point in the history
Just keep one version of the function and differentiate between
std::is_signed() and unsigned using a constexpr if, instead of having
two different versions for the signed and unsigned cases.
  • Loading branch information
tbaederr committed Oct 14, 2022
1 parent 699449d commit 81c5b5d
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions clang/lib/AST/Interp/Integral.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,55 +213,38 @@ template <unsigned Bits, bool Signed> class Integral final {
}

private:
template <typename T>
static std::enable_if_t<std::is_signed<T>::value, bool> CheckAddUB(T A, T B,
T &R) {
return llvm::AddOverflow<T>(A, B, R);
template <typename T> static bool CheckAddUB(T A, T B, T &R) {
if constexpr (std::is_signed_v<T>) {
return llvm::AddOverflow<T>(A, B, R);
} else {
R = A + B;
return false;
}
}

template <typename T>
static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckAddUB(T A, T B,
T &R) {
R = A + B;
return false;
}

template <typename T>
static std::enable_if_t<std::is_signed<T>::value, bool> CheckSubUB(T A, T B,
T &R) {
return llvm::SubOverflow<T>(A, B, R);
}

template <typename T>
static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckSubUB(T A, T B,
T &R) {
R = A - B;
return false;
template <typename T> static bool CheckSubUB(T A, T B, T &R) {
if constexpr (std::is_signed_v<T>) {
return llvm::SubOverflow<T>(A, B, R);
} else {
R = A - B;
return false;
}
}

template <typename T>
static std::enable_if_t<std::is_signed<T>::value, bool> CheckMulUB(T A, T B,
T &R) {
return llvm::MulOverflow<T>(A, B, R);
template <typename T> static bool CheckMulUB(T A, T B, T &R) {
if constexpr (std::is_signed_v<T>) {
return llvm::MulOverflow<T>(A, B, R);
} else {
R = A * B;
return false;
}
}

template <typename T>
static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckMulUB(T A, T B,
T &R) {
R = A * B;
return false;
}

template <typename T, T Min, T Max>
static std::enable_if_t<std::is_signed<T>::value, bool>
CheckRange(int64_t V) {
return Min <= V && V <= Max;
}

template <typename T, T Min, T Max>
static std::enable_if_t<std::is_unsigned<T>::value, bool>
CheckRange(int64_t V) {
return V >= 0 && static_cast<uint64_t>(V) <= Max;
template <typename T, T Min, T Max> static bool CheckRange(int64_t V) {
if constexpr (std::is_signed_v<T>) {
return Min <= V && V <= Max;
} else {
return V >= 0 && static_cast<uint64_t>(V) <= Max;
}
}
};

Expand Down

0 comments on commit 81c5b5d

Please sign in to comment.