all: minor cleanups
This commit is contained in:
+14
-5
@@ -180,7 +180,7 @@ inline constexpr bool is_same_any_v = is_same_any<T, Ts...>::value;
|
||||
|
||||
template <class T>
|
||||
forceinline constexpr bool has_single_bit(T x) noexcept {
|
||||
return x != 0 && (x & (x - 1)) == 0;
|
||||
return !(x == 0) && (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
@@ -191,21 +191,30 @@ template <class T>
|
||||
inline constexpr T align_down(const T &x, const T &alignment) noexcept {
|
||||
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
|
||||
T r = {};
|
||||
r = (x / alignment) * alignment;
|
||||
r = x - (x & (alignment - 1));
|
||||
return r;
|
||||
}
|
||||
template <class T>
|
||||
inline constexpr T align_down_gap(const T &x, const T &alignment) noexcept {
|
||||
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
|
||||
T r = {};
|
||||
r = x & (alignment - 1);
|
||||
return r;
|
||||
}
|
||||
template <class T>
|
||||
inline constexpr T align_up(const T &x, const T &alignment) noexcept {
|
||||
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
|
||||
T r = {};
|
||||
r = ((x + (alignment - 1)) / alignment) * alignment;
|
||||
constexpr T zero = {};
|
||||
r = x + ((zero - x) & (alignment - 1));
|
||||
return r;
|
||||
}
|
||||
template <class T>
|
||||
inline constexpr T align_gap(const T &x, const T &alignment) noexcept {
|
||||
inline constexpr T align_up_gap(const T &x, const T &alignment) noexcept {
|
||||
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
|
||||
T r = {};
|
||||
r = align_up(x, alignment) - x;
|
||||
constexpr T zero = {};
|
||||
r = (zero - x) & (alignment - 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,9 @@ static_assert(sizeof(void *) == sizeof(long));
|
||||
#pragma warning(disable : 4244) // W3: conversion from 'type1' to 'type2', possible loss of data
|
||||
#pragma warning(disable : 4267) // W3: conversion from 'size_t' to 'type', possible loss of data
|
||||
#pragma warning(disable : 4820) // W4: padding added after data member
|
||||
#if _MSC_VER >= 1800
|
||||
#pragma warning(disable : 4464) // W4: relative include path contains '..'
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#undef snprintf
|
||||
|
||||
+19
-5
@@ -292,7 +292,8 @@ void *upx_calloc(size_t n, size_t element_size) may_throw {
|
||||
}
|
||||
|
||||
// simple unoptimized memswap()
|
||||
// TODO later: CHERI clang bug/miscompilation with upx_memswap() ???
|
||||
// TODO later: CHERI clang-14 bug/miscompilation with upx_memswap(); or
|
||||
// maybe caused by tagged-memory issues ???
|
||||
void upx_memswap(void *aa, void *bb, size_t bytes) noexcept {
|
||||
if (aa != bb && bytes != 0) {
|
||||
byte *a = (byte *) aa;
|
||||
@@ -448,11 +449,24 @@ TEST_CASE("upx_memswap") {
|
||||
memset(array, 0xfb, sizeof(array));
|
||||
array[1] = &a;
|
||||
array[3] = &b;
|
||||
CHECK(*array[1] == 11);
|
||||
CHECK(*array[3] == 22);
|
||||
#if defined(__CHERI__) && defined(__CHERI_PURE_CAPABILITY__)
|
||||
// TODO later: CHERI clang-14 bug/miscompilation with upx_memswap(); or
|
||||
// maybe caused by tagged-memory issues ???
|
||||
memswap_no_overlap((byte *) array, (byte *) (array + 2), 2 * sizeof(array[0]));
|
||||
#else
|
||||
upx_memswap(array, array + 2, 2 * sizeof(array[0]));
|
||||
assert(array[1] == &b);
|
||||
assert(array[3] == &a);
|
||||
assert(*array[1] == 22);
|
||||
assert(*array[3] == 11);
|
||||
#endif
|
||||
CHECK(array[1] == &b);
|
||||
CHECK(array[3] == &a);
|
||||
CHECK(*array[1] == 22);
|
||||
CHECK(*array[3] == 11);
|
||||
memswap_no_overlap((byte *) array, (byte *) (array + 2), 2 * sizeof(array[0]));
|
||||
CHECK(array[1] == &a);
|
||||
CHECK(array[3] == &b);
|
||||
CHECK(*array[1] == 11);
|
||||
CHECK(*array[3] == 22);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -110,6 +110,10 @@ forceinline bool ptr_is_aligned(const void *p) noexcept {
|
||||
static_assert(upx::has_single_bit(Alignment));
|
||||
return (ptr_get_address(p) & (Alignment - 1)) == 0;
|
||||
}
|
||||
forceinline bool ptr_is_aligned(const void *p, size_t alignment) noexcept {
|
||||
assert_noexcept(upx::has_single_bit(alignment));
|
||||
return (ptr_get_address(p) & (alignment - 1)) == 0;
|
||||
}
|
||||
|
||||
// ptrdiff_t with nullptr checks and asserted size; will throw on failure
|
||||
// NOTE: returns size_in_bytes, not number of elements!
|
||||
|
||||
Reference in New Issue
Block a user