mirror of
https://gitlab.freedesktop.org/wayland/weston.git
synced 2025-12-24 21:50:20 +01:00
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:
parent
57c3cee7ed
commit
12819c4151
4 changed files with 25 additions and 10 deletions
|
|
@ -1743,7 +1743,7 @@ weston_compositor_enable_color_management_protocol(struct weston_compositor *com
|
||||||
|
|
||||||
weston_assert_bit_is_set(compositor,
|
weston_assert_bit_is_set(compositor,
|
||||||
compositor->color_manager->supported_rendering_intents,
|
compositor->color_manager->supported_rendering_intents,
|
||||||
WESTON_RENDER_INTENT_PERCEPTUAL);
|
1ull << WESTON_RENDER_INTENT_PERCEPTUAL);
|
||||||
|
|
||||||
if (!wl_global_create(compositor->wl_display,
|
if (!wl_global_create(compositor->wl_display,
|
||||||
&wp_color_manager_v1_interface,
|
&wp_color_manager_v1_interface,
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -284,6 +285,18 @@ bswap32(uint32_t x)
|
||||||
#endif
|
#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.
|
* 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.
|
* Saturated to 0 (which isn't a power of two) if x is lesser than 2^0.
|
||||||
|
|
|
||||||
|
|
@ -125,10 +125,11 @@ do { \
|
||||||
({ \
|
({ \
|
||||||
struct weston_compositor *ec = compositor; \
|
struct weston_compositor *ec = compositor; \
|
||||||
uint64_t v = (value); \
|
uint64_t v = (value); \
|
||||||
uint8_t b = (bit); \
|
uint64_t b = (bit); \
|
||||||
bool cond = (v >> b) & 1; \
|
bool cond = (v & b) == b; \
|
||||||
|
weston_assert_true(compositor, is_pow2_64(bit)); \
|
||||||
if (!cond) \
|
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); \
|
__FILE__, __LINE__, #bit, b, #value, v); \
|
||||||
cond; \
|
cond; \
|
||||||
})
|
})
|
||||||
|
|
@ -137,10 +138,11 @@ do { \
|
||||||
({ \
|
({ \
|
||||||
struct weston_compositor *ec = compositor; \
|
struct weston_compositor *ec = compositor; \
|
||||||
uint64_t v = (value); \
|
uint64_t v = (value); \
|
||||||
uint8_t b = (bit); \
|
uint64_t b = (bit); \
|
||||||
bool cond = (v >> b) & 1; \
|
bool cond = (v & b) == 0; \
|
||||||
if (cond) \
|
weston_assert_true(compositor, is_pow2_64(bit)); \
|
||||||
custom_assert_fail_(ec, "%s:%u: Assertion failed! Bit %s (%u) of %s (0x%" PRIx64 ") is set.\n", \
|
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); \
|
__FILE__, __LINE__, #bit, b, #value, v); \
|
||||||
cond; \
|
cond; \
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -134,9 +134,9 @@ TEST(asserts)
|
||||||
abort_if_not(ret == false);
|
abort_if_not(ret == false);
|
||||||
|
|
||||||
uint32_t bitfield = 0xffff;
|
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);
|
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);
|
abort_if_not(ret == false);
|
||||||
|
|
||||||
uint64_t max_uint64 = UINT64_MAX;
|
uint64_t max_uint64 = UINT64_MAX;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue