Commit e76d9fc4 authored by Lionel Debieve's avatar Lionel Debieve
Browse files

lib: utils_def: add CLAMP macro



Add the standard CLAMP macro.  It ensures that
x is between the limits set by low and high.
If low is greater than high the result is undefined.
Signed-off-by: default avatarLionel Debieve <lionel.debieve@st.com>
Change-Id: Ia173bb9ca51bc8d9a8ec573bbc15636a94f881f4
parent 162fc183
...@@ -77,6 +77,15 @@ ...@@ -77,6 +77,15 @@
_x > _y ? _x : _y; \ _x > _y ? _x : _y; \
}) })
#define CLAMP(x, min, max) __extension__ ({ \
__typeof__(x) _x = (x); \
__typeof__(min) _min = (min); \
__typeof__(max) _max = (max); \
(void)(&_x == &_min); \
(void)(&_x == &_max); \
(_x > _max ? _max : (_x < _min ? _min : _x)); \
})
/* /*
* The round_up() macro rounds up a value to the given boundary in a * The round_up() macro rounds up a value to the given boundary in a
* type-agnostic yet type-safe manner. The boundary must be a power of two. * type-agnostic yet type-safe manner. The boundary must be a power of two.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment