Skip to content

Commit

Permalink
macros/utils: add LIMIT() macro
Browse files Browse the repository at this point in the history
This patch adds a macro that can be used to limit a value to a given
range.
  • Loading branch information
Enoch247 committed Feb 7, 2024
1 parent 4df5306 commit 4b9ba70
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/lib/include/macros/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b9ba70

Please sign in to comment.