diff --git a/os/common/portability/CW/ccportab.h b/os/common/portability/CW/ccportab.h index f6dc3cd1b..574a571b1 100644 --- a/os/common/portability/CW/ccportab.h +++ b/os/common/portability/CW/ccportab.h @@ -120,6 +120,20 @@ * compiler. */ #define CC_ROMCONST const + +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#define CC_LIKELY(x) x + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#define CC_UNLIKELY(x) x /** @} */ /*===========================================================================*/ diff --git a/os/common/portability/GCC/ccportab.h b/os/common/portability/GCC/ccportab.h index e0c8a1013..f4612ef55 100644 --- a/os/common/portability/GCC/ccportab.h +++ b/os/common/portability/GCC/ccportab.h @@ -120,6 +120,20 @@ * compiler. */ #define CC_ROMCONST const + +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#define CC_LIKELY(x) __builtin_expect(!!(x), 1) + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#define CC_UNLIKELY(x) __builtin_expect(!!(x), 0) /** @} */ /*===========================================================================*/ diff --git a/os/common/portability/GHS/ccportab.h b/os/common/portability/GHS/ccportab.h index d3aadf2ed..affde08f8 100644 --- a/os/common/portability/GHS/ccportab.h +++ b/os/common/portability/GHS/ccportab.h @@ -120,6 +120,20 @@ * compiler. */ #define CC_ROMCONST const + +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#define CC_LIKELY(x) x + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#define CC_UNLIKELY(x) x /** @} */ /*===========================================================================*/ diff --git a/os/common/portability/IAR/ccportab.h b/os/common/portability/IAR/ccportab.h index efccb7324..a1440e1c3 100644 --- a/os/common/portability/IAR/ccportab.h +++ b/os/common/portability/IAR/ccportab.h @@ -120,6 +120,20 @@ * compiler. */ #define CC_ROMCONST const + +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#define CC_LIKELY(x) x + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#define CC_UNLIKELY(x) x /** @} */ /*===========================================================================*/ diff --git a/os/common/portability/Keil/ccportab.h b/os/common/portability/Keil/ccportab.h index 5bc0faa75..13c41ab51 100644 --- a/os/common/portability/Keil/ccportab.h +++ b/os/common/portability/Keil/ccportab.h @@ -120,6 +120,20 @@ * compiler. */ #define CC_ROMCONST const + +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#define CC_LIKELY(x) x + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#define CC_UNLIKELY(x) x /** @} */ /*===========================================================================*/ diff --git a/os/common/ports/ARM-common/chtypes.h b/os/common/ports/ARM-common/chtypes.h index fc3f420ec..734819aca 100644 --- a/os/common/ports/ARM-common/chtypes.h +++ b/os/common/ports/ARM-common/chtypes.h @@ -101,6 +101,28 @@ typedef uint64_t port_stkalign_t; */ #define SIZEOF_PTR PORT_ARCH_SIZEOF_DATA_PTR +/** + * @brief Marks a boolean expression as likely true. + * + * @param[in] x a valid expression + */ +#if defined(CC_LIKELY) || defined(__DOXYGEN__) +#define PORT_LIKELY(x) CC_LIKELY(x) +#else +#define PORT_LIKELY(x) x +#endif + +/** + * @brief Marks a boolean expression as likely false. + * + * @param[in] x a valid expression + */ +#if defined(CC_UNLIKELY) || defined(__DOXYGEN__) +#define PORT_UNLIKELY(x) CC_UNLIKELY(x) +#else +#define PORT_UNLIKELY(x) x +#endif + #endif /* CHTYPES_H */ /** @} */ diff --git a/os/rt/include/chearly.h b/os/rt/include/chearly.h index 6846bf6c9..e5eab0a5e 100644 --- a/os/rt/include/chearly.h +++ b/os/rt/include/chearly.h @@ -146,7 +146,7 @@ typedef struct ch_os_instance os_instance_t; * * @param[in] a literal to be string-ified */ -#define __CH_STRINGIFY(a) #a +#define __CH_STRINGIFY(a) #a /** * @brief Structure field offset utility. @@ -161,6 +161,39 @@ typedef struct ch_os_instance os_instance_t; ((size_t)((char *)&((st *)0)->m - (char *)0)) \ /*lint -restore*/ +/** + * @brief Marks an expression result as used. + * + * @param[in] x a valid expression + */ +#define __CH_USED(x) (void)(x) + +/** + * @brief Marks a boolean expression as likely true. + * @note No namespace prefix for this macro because it is commonly defined + * by operating systems. + * + * @param[in] x a valid expression + */ +#if defined(PORT_LIKELY) || defined(__DOXYGEN__) +#define likely(x) PORT_LIKELY(x) +#else +#define likely(x) x +#endif + +/** + * @brief Marks a boolean expression as likely false. + * @note No namespace prefix for this macro because it is commonly defined + * by operating systems. + * + * @param[in] x a valid expression + */ +#if defined(PORT_UNLIKELY) || defined(__DOXYGEN__) +#define unlikely(x) PORT_UNLIKELY(x) +#else +#define unlikely(x) x +#endif + /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/