diff --git a/core/lib/include/macros/utils.h b/core/lib/include/macros/utils.h index 2b6b6f728209b..225b0eddcefb8 100644 --- a/core/lib/include/macros/utils.h +++ b/core/lib/include/macros/utils.h @@ -63,6 +63,23 @@ extern "C" { #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +/** + * @brief Limit a value to an inclusive range + * + * If @p value is > @p max, @p max is returned. If @p value is < @p min, @p min + * is returned. Otherwise, @p value is returned. + * + * @note This relies on the `MAX() and `MIN()` macros, which may evaluate the + * arguments more than once. + * + * @param[in] min minimum limit + * @param[in] value value to limit + * @param[in] max maximum limit + * + * @return range limited value + */ +#define LIMIT(min, value, max) MAX(min, MIN(value, max)) + #ifdef __cplusplus } #endif