diff options
author | Angel Pons <th3fanbus@gmail.com> | 2022-05-07 20:36:10 +0200 |
---|---|---|
committer | Felix Held <felix-coreboot@felixheld.de> | 2022-12-12 13:38:11 +0000 |
commit | f7571c43f8d0e53cc1fb18757206b1024f9ac88b (patch) | |
tree | b87e0965362bc633fab5c03a904551a5e92fefe3 /src/commonlib/bsd/include | |
parent | 58d2947855da6986eaf42b08db5c693709f50eac (diff) |
commonlib/clamp.h: Add more clamping functions
Add more clamping functions that work with different types.
Change-Id: I14cf335d5a54f769f8fd9184450957e876affd6b
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/64175
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'src/commonlib/bsd/include')
-rw-r--r-- | src/commonlib/bsd/include/commonlib/bsd/clamp.h | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/commonlib/bsd/include/commonlib/bsd/clamp.h b/src/commonlib/bsd/include/commonlib/bsd/clamp.h index 66513b0337..cdee4c8542 100644 --- a/src/commonlib/bsd/include/commonlib/bsd/clamp.h +++ b/src/commonlib/bsd/include/commonlib/bsd/clamp.h @@ -8,15 +8,25 @@ /* * Clamp a value, so that it is between a lower and an upper bound. */ -static inline u32 clamp_u32(const u32 min, const u32 val, const u32 max) -{ - if (val > max) - return max; +#define __MAKE_CLAMP_FUNC(type) \ + static inline type clamp_##type(const type min, const type val, const type max) \ + { \ + if (val > max) \ + return max; \ + if (val < min) \ + return min; \ + return val; \ + } \ - if (val < min) - return min; +__MAKE_CLAMP_FUNC(s8) /* clamp_s8 */ +__MAKE_CLAMP_FUNC(u8) /* clamp_u8 */ +__MAKE_CLAMP_FUNC(s16) /* clamp_s16 */ +__MAKE_CLAMP_FUNC(u16) /* clamp_u16 */ +__MAKE_CLAMP_FUNC(s32) /* clamp_s32 */ +__MAKE_CLAMP_FUNC(u32) /* clamp_u32 */ +__MAKE_CLAMP_FUNC(s64) /* clamp_s64 */ +__MAKE_CLAMP_FUNC(u64) /* clamp_u64 */ - return val; -} +#undef __MAKE_CLAMP_FUNC #endif /* COMMONLIB_BSD_CLAMP_H */ |