0001-commonlib-clamp.h-Add-more-clamping-functions.patch 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. From dd58f5e9108bc596c93071705d2b53233d13ade6 Mon Sep 17 00:00:00 2001
  2. From: Angel Pons <th3fanbus@gmail.com>
  3. Date: Sat, 7 May 2022 20:36:10 +0200
  4. Subject: [PATCH 01/26] commonlib/clamp.h: Add more clamping functions
  5. Add more clamping functions that work with different types.
  6. Change-Id: I14cf335d5a54f769f8fd9184450957e876affd6b
  7. Signed-off-by: Angel Pons <th3fanbus@gmail.com>
  8. ---
  9. src/commonlib/include/commonlib/clamp.h | 26 +++++++++++++++++--------
  10. 1 file changed, 18 insertions(+), 8 deletions(-)
  11. diff --git a/src/commonlib/include/commonlib/clamp.h b/src/commonlib/include/commonlib/clamp.h
  12. index e01a107ed4..526185195c 100644
  13. --- a/src/commonlib/include/commonlib/clamp.h
  14. +++ b/src/commonlib/include/commonlib/clamp.h
  15. @@ -8,15 +8,25 @@
  16. /*
  17. * Clamp a value, so that it is between a lower and an upper bound.
  18. */
  19. -static inline u32 clamp_u32(const u32 min, const u32 val, const u32 max)
  20. -{
  21. - if (val > max)
  22. - return max;
  23. +#define __MAKE_CLAMP_FUNC(type) \
  24. + static inline type clamp_##type(const type min, const type val, const type max) \
  25. + { \
  26. + if (val > max) \
  27. + return max; \
  28. + if (val < min) \
  29. + return min; \
  30. + return val; \
  31. + } \
  32. - if (val < min)
  33. - return min;
  34. +__MAKE_CLAMP_FUNC(s8) /* clamp_s8 */
  35. +__MAKE_CLAMP_FUNC(u8) /* clamp_u8 */
  36. +__MAKE_CLAMP_FUNC(s16) /* clamp_s16 */
  37. +__MAKE_CLAMP_FUNC(u16) /* clamp_u16 */
  38. +__MAKE_CLAMP_FUNC(s32) /* clamp_s32 */
  39. +__MAKE_CLAMP_FUNC(u32) /* clamp_u32 */
  40. +__MAKE_CLAMP_FUNC(s64) /* clamp_s64 */
  41. +__MAKE_CLAMP_FUNC(u64) /* clamp_u64 */
  42. - return val;
  43. -}
  44. +#undef __MAKE_CLAMP_FUNC
  45. #endif /* COMMONLIB_CLAMP_H */
  46. --
  47. 2.39.2