diff --git a/core/lib/include/macros/utils.h b/core/lib/include/macros/utils.h index 2b6b6f7282..1e25245ff5 100644 --- a/core/lib/include/macros/utils.h +++ b/core/lib/include/macros/utils.h @@ -63,6 +63,22 @@ extern "C" { #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +/** + * @brief Limit a value to an inclusive range + * + * If @p val is > @p high, @p high is returned. If @p val is < @p low, @p low + * is returned. Otherwise, @p val is returned. + * + * @note This macro evaluate its arguments more than once. + * + * @param[in] val value to limit + * @param[in] low minimum limit + * @param[in] high maximum limit + * + * @return range limited value + */ +#define LIMIT(val, low, high) ((val < low) ? low : (val > high) ? high : val) + #ifdef __cplusplus } #endif