From dacd3b06f9e546136deda34578d350584786df8f Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Tue, 20 Apr 2021 00:40:21 +0200 Subject: [PATCH] Utils additions and fixes --- include/lib/utils.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/include/lib/utils.h b/include/lib/utils.h index b4406a3..2f7b0a8 100644 --- a/include/lib/utils.h +++ b/include/lib/utils.h @@ -61,8 +61,8 @@ uint8_t bits_set(uint32_t x); static inline uint32_t ntohl(uint32_t val) __attribute__ ((alias ("byte_swap_32"))); static inline uint32_t htonl(uint32_t val) __attribute__ ((alias ("byte_swap_32"))); /* Short versions */ -static inline uint32_t ntohs(uint32_t val) __attribute__ ((alias ("byte_swap_16"))); -static inline uint32_t htons(uint32_t val) __attribute__ ((alias ("byte_swap_16"))); +static inline uint32_t ntohs(uint16_t val) __attribute__ ((alias ("byte_swap_16"))); +static inline uint32_t htons(uint16_t val) __attribute__ ((alias ("byte_swap_16"))); /* MIN and MAX */ @@ -70,6 +70,24 @@ static inline uint32_t htons(uint32_t val) __attribute__ ((alias ("byte_swap_16" #define MAX(a, b) (((a) > (b)) ? (a) : (b)) +/* abs + * Return absolute value of signed integer + */ +static inline uint32_t abs(int32_t val) +{ + return val < 0 ? -val : val; +} + + +/* constrain + * Return the value if it is between min and max + * Return min if the value is below min, or max if the value is above max + */ +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 */ -- 2.43.0