1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later */ 2 3 #ifndef COMMONLIB_BSD_CLAMP_H 4 #define COMMONLIB_BSD_CLAMP_H 5 6 #include <stdint.h> 7 8 /* 9 * Clamp a value, so that it is between a lower and an upper bound. 10 */ 11 #define __MAKE_CLAMP_FUNC(type) \ 12 static inline type clamp_##type(const type min, const type val, const type max) \ 13 { \ 14 if (val > max) \ 15 return max; \ 16 if (val < min) \ 17 return min; \ 18 return val; \ 19 } \ 20 21 __MAKE_CLAMP_FUNC(s8) /* clamp_s8 */ 22 __MAKE_CLAMP_FUNC(u8) /* clamp_u8 */ 23 __MAKE_CLAMP_FUNC(s16) /* clamp_s16 */ 24 __MAKE_CLAMP_FUNC(u16) /* clamp_u16 */ 25 __MAKE_CLAMP_FUNC(s32) /* clamp_s32 */ 26 __MAKE_CLAMP_FUNC(u32) /* clamp_u32 */ 27 __MAKE_CLAMP_FUNC(s64) /* clamp_s64 */ 28 __MAKE_CLAMP_FUNC(u64) /* clamp_u64 */ 29 30 #undef __MAKE_CLAMP_FUNC 31 32 #endif /* COMMONLIB_BSD_CLAMP_H */ 33