Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros/utils: add LIMIT() and ABS() macros #20352

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
macros/utils: add LIMIT() macro
This patch adds a macro that can be used to limit a value to a given
range.
Enoch247 committed Feb 8, 2024
commit 78a1a18683ea1d5101824fca9a31d9d72335de61
16 changes: 16 additions & 0 deletions core/lib/include/macros/utils.h
Original file line number Diff line number Diff line change
@@ -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