src: introduce upx::max and friends; updates for clang-19 git snapshot

This commit is contained in:
Markus F.X.J. Oberhumer
2024-05-15 14:06:05 +02:00
parent 9e0f16a629
commit 40b7e24fcc
21 changed files with 254 additions and 159 deletions
+58 -1
View File
@@ -142,7 +142,7 @@ private:
#endif
/*************************************************************************
// type_traits
// <type_traits>
**************************************************************************/
// is_bounded_array: identical to C++20 std::is_bounded_array
@@ -163,6 +163,63 @@ struct is_same_any : public std::disjunction<std::is_same<T, Ts>...> {};
template <class T, class... Ts>
inline constexpr bool is_same_any_v = is_same_any<T, Ts...>::value;
/*************************************************************************
// <bit> C++20
**************************************************************************/
template <class T>
forceinline constexpr bool has_single_bit(T x) noexcept {
return x != 0 && (x & (x - 1)) == 0;
}
/*************************************************************************
// <algorithm>
**************************************************************************/
template <class T>
inline T align_down(const T &x, const T &alignment) noexcept {
assert_noexcept(has_single_bit(alignment));
T r;
r = (x / alignment) * alignment;
return r;
}
template <class T>
inline T align_up(const T &x, const T &alignment) noexcept {
assert_noexcept(has_single_bit(alignment));
T r;
r = ((x + (alignment - 1)) / alignment) * alignment;
return r;
}
template <class T>
inline T align_gap(const T &x, const T &alignment) noexcept {
assert_noexcept(has_single_bit(alignment));
T r;
r = align_up(x, alignment) - x;
return r;
}
template <class T>
forceinline constexpr T min(const T &a, const T &b) noexcept {
return b < a ? b : a;
}
template <class T>
forceinline constexpr T max(const T &a, const T &b) noexcept {
return a < b ? b : a;
}
template <class T>
inline constexpr bool is_uminmax_type =
is_same_any_v<T, upx_uint16_t, upx_uint32_t, upx_uint64_t, unsigned long, size_t>;
template <class T, class = std::enable_if_t<is_uminmax_type<T>, T> >
forceinline constexpr T umin(const T &a, const T &b) noexcept {
return b < a ? b : a;
}
template <class T, class = std::enable_if_t<is_uminmax_type<T>, T> >
forceinline constexpr T umax(const T &a, const T &b) noexcept {
return a < b ? b : a;
}
/*************************************************************************
// util
**************************************************************************/