all: minor cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-05-11 22:07:08 +02:00
parent cba44c45fc
commit ed3d7b0c45
5 changed files with 45 additions and 20 deletions
+36 -11
View File
@@ -232,6 +232,7 @@ struct CheckIntegral {
checkU<typename std::add_const<T>::type>();
}
};
template <class T>
struct CheckAlignment {
static void check(void) noexcept {
@@ -256,16 +257,17 @@ struct CheckAlignment {
UNUSED(t2);
}
};
template <class T>
struct TestBELE {
static noinline bool test(void) noexcept {
CheckIntegral<T>::check();
CheckAlignment<T>::check();
// arithmetic checks
T allbits = {};
assert_noexcept(allbits == 0);
allbits += 1;
allbits -= 2;
T all_bits = {};
assert_noexcept(all_bits == 0);
all_bits += 1;
all_bits -= 2;
T v1;
v1 = 1;
v1 *= 4;
@@ -279,7 +281,7 @@ struct TestBELE {
assert_noexcept((v1 >= v2));
assert_noexcept(!(v1 < v2));
assert_noexcept(!(v1 > v2));
v2 ^= allbits;
v2 ^= all_bits;
assert_noexcept(!(v1 == v2));
assert_noexcept((v1 != v2));
assert_noexcept((v1 <= v2));
@@ -305,6 +307,25 @@ struct TestBELE {
}
};
template <class T, bool T_is_signed>
struct CheckSignedness {
template <class U, bool U_is_signed>
static inline void checkU(void) noexcept {
COMPILE_TIME_ASSERT(sizeof(U) == sizeof(T));
COMPILE_TIME_ASSERT(alignof(U) == alignof(T));
COMPILE_TIME_ASSERT(U_is_signed ? ((U) 0 - 1 < 0) : ((U) 0 - 1 > 0));
constexpr U all_bits = (U) (U(0) - U(1));
COMPILE_TIME_ASSERT(U_is_signed ? (all_bits < 0) : (all_bits > 0));
}
static void check(void) noexcept {
checkU<T, T_is_signed>();
using signed_type = std::make_signed_t<T>;
checkU<signed_type, true>();
using unsigned_type = std::make_unsigned_t<T>;
checkU<unsigned_type, false>();
}
};
template <class A, class B>
struct TestNoAliasingStruct {
static noinline bool test(A *a, B *b) noexcept {
@@ -501,12 +522,16 @@ void upx_compiler_sanity_check(void) noexcept {
CheckIntegral<upx_ptraddr_t>::check();
CheckIntegral<upx_uintptr_t>::check();
COMPILE_TIME_ASSERT(ptrdiff_t(0) - 1 < 0);
COMPILE_TIME_ASSERT(intptr_t(0) - 1 < 0);
COMPILE_TIME_ASSERT(size_t(0) - 1 > 0);
COMPILE_TIME_ASSERT(uintptr_t(0) - 1 > 0);
COMPILE_TIME_ASSERT(upx_ptraddr_t(0) - 1 > 0);
COMPILE_TIME_ASSERT(upx_uintptr_t(0) - 1 > 0);
CheckSignedness<long long, true>::check();
CheckSignedness<ptrdiff_t, true>::check();
CheckSignedness<intptr_t, true>::check();
CheckSignedness<unsigned long long, false>::check();
CheckSignedness<size_t, false>::check();
CheckSignedness<uintptr_t, false>::check();
CheckSignedness<upx_off_t, true>::check();
CheckSignedness<upx_ptraddr_t, false>::check();
CheckSignedness<upx_sptraddr_t, true>::check();
CheckSignedness<upx_uintptr_t, false>::check();
COMPILE_TIME_ASSERT(sizeof(upx_charptr_unit_type) == 1)
COMPILE_TIME_ASSERT_ALIGNED1(upx_charptr_unit_type)
+2 -1
View File
@@ -145,7 +145,8 @@ typedef acc_int64_t upx_int64_t;
typedef acc_uint64_t upx_uint64_t;
typedef acc_uintptr_t upx_uintptr_t;
// see CHERI ptraddr_t / vaddr_t
typedef upx_uintptr_t upx_ptraddr_t;
typedef acc_uintptr_t upx_ptraddr_t;
typedef acc_intptr_t upx_sptraddr_t;
// UPX convention: use "byte" when dealing with data; use "char/uchar" when dealing
// with strings; use "upx_uint8_t" when dealing with small integers
+1 -2
View File
@@ -78,8 +78,7 @@ void xspan_check_range(const void *ptr, const void *base, ptrdiff_t size_in_byte
xspan_fail_range_nullbase();
#if defined(__SANITIZE_ADDRESS__) || 1
// info: pointers are out of range deliberately during internal doctest checks; see dt_xspan.cpp
ACC_COMPILE_TIME_ASSERT(sizeof(intptr_t) == sizeof(upx_ptraddr_t))
const intptr_t off = ptr_get_address(ptr) - ptr_get_address(base);
const upx_sptraddr_t off = ptr_get_address(ptr) - ptr_get_address(base);
#else
const ptrdiff_t off = (const charptr) ptr - (const charptr) base;
#endif