From: Nathael Pajani Date: Mon, 19 Apr 2021 22:40:21 +0000 (+0200) Subject: Utils additions and fixes X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=dacd3b06f9e546136deda34578d350584786df8f;p=soft%2Flpc82x%2Fcore Utils additions and fixes --- 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 */