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
**************************************************************************/
+2 -4
View File
@@ -97,8 +97,6 @@ void *MemBuffer::subref_impl(const char *errfmt, size_t skip, size_t take) {
return ptr + skip;
}
static forceinline constexpr size_t umax(size_t a, size_t b) { return (a >= b) ? a : b; }
/*static*/
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra) {
if (uncompressed_size == 0)
@@ -106,9 +104,9 @@ unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned e
const size_t z = uncompressed_size; // fewer keystrokes and display columns
size_t bytes = mem_size(1, z); // check size
// All literal: 1 bit overhead per literal byte; from UCL documentation
bytes = umax(bytes, z + z / 8 + 256);
bytes = upx::umax(bytes, z + z / 8 + 256);
// zstd: ZSTD_COMPRESSBOUND
bytes = umax(bytes, z + (z >> 8) + ((z < (128 << 10)) ? (((128 << 10) - z) >> 11) : 0));
bytes = upx::umax(bytes, z + (z >> 8) + ((z < (128 << 10)) ? (((128 << 10) - z) >> 11) : 0));
// add extra and 256 safety for various rounding/alignments
bytes = mem_size(1, bytes, extra, 256);
return ACC_ICONV(unsigned, bytes);
+6
View File
@@ -47,6 +47,12 @@
#endif
#endif
#if !defined(NOMINMAX)
#if defined(_WIN32) || defined(__CYGWIN__)
#define NOMINMAX 1
#endif
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
// disable silly warnings about using "deprecated" POSIX functions like fopen()
#if !defined(_CRT_NONSTDC_NO_DEPRECATE)
+8 -1
View File
@@ -44,18 +44,25 @@
// libc++ hardenining
#if defined(__cplusplus) && 0 // TODO later
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 >= 18)
#if !defined(_LIBCPP_HARDENING_MODE)
#if DEBUG
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_DEBUG
#else
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_EXTENSIVE
#endif
#endif
#endif // clang >= 18
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 < 18)
#if !defined(_LIBCPP_ENABLE_ASSERTIONS)
#if DEBUG
#define _LIBCPP_ENABLE_ASSERTIONS 1
#endif
#endif // clang >= 18
#endif
#endif // clang < 18
#endif // TODO later
/* vim:set ts=4 sw=4 et: */