diff --git a/core/lib/include/assert.h b/core/lib/include/assert.h index 728789d30f..1307ba3ce8 100644 --- a/core/lib/include/assert.h +++ b/core/lib/include/assert.h @@ -22,6 +22,8 @@ #ifndef ASSERT_H #define ASSERT_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -42,6 +44,9 @@ extern "C" { * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ #define DEBUG_ASSERT_VERBOSE +#else +/* we should not include custom headers in standard headers */ +#define _likely(x) __builtin_expect((uintptr_t)(x), 1) #endif /** @@ -109,10 +114,10 @@ __NORETURN void _assert_failure(const char *file, unsigned line); * * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/assert.html */ -#define assert(cond) ((cond) ? (void)0 : _assert_failure(__FILE__, __LINE__)) +#define assert(cond) (_likely(cond) ? (void)0 : _assert_failure(__FILE__, __LINE__)) #else /* DEBUG_ASSERT_VERBOSE */ __NORETURN void _assert_panic(void); -#define assert(cond) ((cond) ? (void)0 : _assert_panic()) +#define assert(cond) (_likely(cond) ? (void)0 : _assert_panic()) #endif /* DEBUG_ASSERT_VERBOSE */ #if !defined __cplusplus diff --git a/core/lib/include/compiler_hints.h b/core/lib/include/compiler_hints.h index cc0543268c..aa600b7675 100644 --- a/core/lib/include/compiler_hints.h +++ b/core/lib/include/compiler_hints.h @@ -21,6 +21,8 @@ #ifndef COMPILER_HINTS_H #define COMPILER_HINTS_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -130,6 +132,24 @@ extern "C" { #define IS_CT_CONSTANT(expr) 0 #endif +/** + * @brief Hint to the compiler that the condition @p x is likely taken + * + * @param[in] x Condition that is likely taken + * + * @return result of @p x + */ +#define likely(x) __builtin_expect((uintptr_t)(x), 1) + +/** + * @brief Hint to the compiler that the condition @p x is likely not taken + * + * @param[in] x Condition that is unlikely + * + * @return result of @p x + */ +#define unlikely(x) __builtin_expect((uintptr_t)(x), 0) + #ifdef __cplusplus } #endif diff --git a/sys/include/test_utils/expect.h b/sys/include/test_utils/expect.h index 6417cd75b9..da3afdea9d 100644 --- a/sys/include/test_utils/expect.h +++ b/sys/include/test_utils/expect.h @@ -26,6 +26,7 @@ #define TEST_UTILS_EXPECT_H #include +#include "compiler_hints.h" #include "panic.h" #ifdef __cplusplus @@ -76,7 +77,7 @@ NORETURN static inline void _expect_failure(const char *file, unsigned line) * the condition failed in. * */ -#define expect(cond) ((cond) ? (void)0 : _expect_failure(__FILE__, __LINE__)) +#define expect(cond) (likely(cond) ? (void)0 : _expect_failure(__FILE__, __LINE__)) #ifdef __cplusplus } diff --git a/tests/periph_spi/main.c b/tests/periph_spi/main.c index 557fef2334..b390239ea3 100644 --- a/tests/periph_spi/main.c +++ b/tests/periph_spi/main.c @@ -217,9 +217,11 @@ int cmd_init(int argc, char **argv) else { printf("Trying to initialize SPI_DEV(%i): mode: %i, clk: %i, cs_port: %i, cs_pin: %i\n", dev, mode, clk, port, pin); - puts("Note: Failed assertion (crash) means configuration not supported"); + puts("(if below the program crashes with a failed assertion, then it means the" + " configuration is not supported)"); spi_acquire(spiconf.dev, spiconf.cs, spiconf.mode, spiconf.clk); spi_release(spiconf.dev); + puts("Success."); } return 0;