diff --git a/libweston/color-management.c b/libweston/color-management.c index f7478c699..725333d3c 100644 --- a/libweston/color-management.c +++ b/libweston/color-management.c @@ -1743,7 +1743,7 @@ weston_compositor_enable_color_management_protocol(struct weston_compositor *com weston_assert_bit_is_set(compositor, compositor->color_manager->supported_rendering_intents, - WESTON_RENDER_INTENT_PERCEPTUAL); + 1ull << WESTON_RENDER_INTENT_PERCEPTUAL); if (!wl_global_create(compositor->wl_display, &wp_color_manager_v1_interface, diff --git a/shared/helpers.h b/shared/helpers.h index 7ff60d03e..42f75e35d 100644 --- a/shared/helpers.h +++ b/shared/helpers.h @@ -25,6 +25,7 @@ #include "config.h" #include +#include #ifdef __cplusplus extern "C" { @@ -284,6 +285,18 @@ bswap32(uint32_t x) #endif } +/** + * Returns whether 64-bit value x is a power of 2. + * + * @param x a 64-bit value. + * @return true if x is a power of 2. + */ +static inline bool +is_pow2_64(uint64_t x) +{ + return (x & (x - 1)) == 0; +} + /** * Returns the highest power of two lesser than or equal to 32-bit value x. * Saturated to 0 (which isn't a power of two) if x is lesser than 2^0. diff --git a/shared/weston-assert.h b/shared/weston-assert.h index e8d39a552..5d2fa2491 100644 --- a/shared/weston-assert.h +++ b/shared/weston-assert.h @@ -125,10 +125,11 @@ do { \ ({ \ struct weston_compositor *ec = compositor; \ uint64_t v = (value); \ - uint8_t b = (bit); \ - bool cond = (v >> b) & 1; \ + uint64_t b = (bit); \ + bool cond = (v & b) == b; \ + weston_assert_true(compositor, is_pow2_64(bit)); \ if (!cond) \ - custom_assert_fail_(ec, "%s:%u: Assertion failed! Bit %s (%u) of %s (0x%" PRIx64 ") is not set.\n", \ + custom_assert_fail_(ec, "%s:%u: Assertion failed! Bit \"%s\" (%" PRIu64 ") of \"%s\" (0x%" PRIx64 ") is not set.\n", \ __FILE__, __LINE__, #bit, b, #value, v); \ cond; \ }) @@ -137,10 +138,11 @@ do { \ ({ \ struct weston_compositor *ec = compositor; \ uint64_t v = (value); \ - uint8_t b = (bit); \ - bool cond = (v >> b) & 1; \ - if (cond) \ - custom_assert_fail_(ec, "%s:%u: Assertion failed! Bit %s (%u) of %s (0x%" PRIx64 ") is set.\n", \ + uint64_t b = (bit); \ + bool cond = (v & b) == 0; \ + weston_assert_true(compositor, is_pow2_64(bit)); \ + if (!cond) \ + custom_assert_fail_(ec, "%s:%u: Assertion failed! Bit \"%s\" (%" PRIu64 ") of \"%s\" (0x%" PRIx64 ") is set.\n", \ __FILE__, __LINE__, #bit, b, #value, v); \ cond; \ }) diff --git a/tests/assert-test.c b/tests/assert-test.c index 011dacb70..42736fbd4 100644 --- a/tests/assert-test.c +++ b/tests/assert-test.c @@ -134,9 +134,9 @@ TEST(asserts) abort_if_not(ret == false); uint32_t bitfield = 0xffff; - ret = weston_assert_bit_is_set(compositor, bitfield, 2); + ret = weston_assert_bit_is_set(compositor, bitfield, 1ull << 2); abort_if_not(ret); - ret = weston_assert_bit_is_set(compositor, bitfield, 57); + ret = weston_assert_bit_is_set(compositor, bitfield, 1ull << 57); abort_if_not(ret == false); uint64_t max_uint64 = UINT64_MAX;