From: Nathael Pajani Date: Fri, 12 Jun 2020 14:37:30 +0000 (+0200) Subject: Update of lib/utils : make min and max inline, and add abs and constrain X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=ca4d7092e6370a900d63c03058372e8fbf683cc0;p=soft%2Flpc122x%2Fcore Update of lib/utils : make min and max inline, and add abs and constrain --- diff --git a/include/lib/utils.h b/include/lib/utils.h index 93d2630..bb2f761 100644 --- a/include/lib/utils.h +++ b/include/lib/utils.h @@ -65,9 +65,26 @@ static inline uint16_t ntohs(uint16_t val) __attribute__ ((alias ("byte_swap_16" static inline uint16_t htons(uint16_t val) __attribute__ ((alias ("byte_swap_16"))); -/* MIN and MAX */ -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +/* Min, Max and Abs */ +static inline uint32_t abs(int32_t val) +{ + return val < 0 ? -val : val; +} + +static inline uint32_t min(uint32_t a, uint32_t b) +{ + return a < b ? a : b; +} + +static inline uint32_t max(uint32_t a, uint32_t b) +{ + return a > b ? a : b; +} + +static inline uint32_t constrain(int32_t val, int32_t min, int32_t max) +{ + return val < min ? min : val > max ? max : val; +} #endif /* LIB_UTILS_H */ diff --git a/lib/utils.c b/lib/utils.c index c9bff81..96ad730 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -103,3 +103,4 @@ uint8_t bits_set(uint32_t x) return r; } +