shared: Let bit asserts take a bit instead of a position

It's much more common to have bits defined as enums than having a
position, so let's just directly take a bit as argument to the bit
asserts. For the few cases where only the position is available, it's
easy to get the right bit using a shift.

The is_pow2_64() helper functions have been added in order to validate
the bit passed as argument and prevent programming errors.

Signed-off-by: Loïc Molinari <loic.molinari@collabora.com>
This commit is contained in:
Loïc Molinari 2025-01-26 17:19:32 +01:00
parent 57c3cee7ed
commit 12819c4151
4 changed files with 25 additions and 10 deletions

View file

@ -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,

View file

@ -25,6 +25,7 @@
#include "config.h"
#include <stdint.h>
#include <stdbool.h>
#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.

View file

@ -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; \
})

View file

@ -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;