From 3775471e26dcf941681ab0af1fc9e2272fd57b57 Mon Sep 17 00:00:00 2001 From: Phi Bang Nguyen Date: Tue, 4 Nov 2025 17:25:55 +0100 Subject: [PATCH 1/3] sys: util: Add SIGN macro Add a macro to determine the sign of a value. Signed-off-by: Phi Bang Nguyen Signed-off-by: Trung Hieu Le --- include/zephyr/sys/util.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/zephyr/sys/util.h b/include/zephyr/sys/util.h index d65aab7ece216..3c4bd792583f8 100644 --- a/include/zephyr/sys/util.h +++ b/include/zephyr/sys/util.h @@ -1027,6 +1027,17 @@ static inline size_t sys_count_bits(const void *value, size_t len) return cnt; } +/** + * @brief Returns the sign of a number. + * + * @param x The input value to determine the sign + * + * @retval 1 if x is positive + * @retval -1 if x is negative + * @retval 0 if x is zero + */ +#define SIGN(x) (((x) > 0) - ((x) < 0)) + #ifdef __cplusplus } #endif From 2103e6bb90acc234175f9f3b13e01b09be1232ac Mon Sep 17 00:00:00 2001 From: Phi Bang Nguyen Date: Tue, 4 Nov 2025 17:31:03 +0100 Subject: [PATCH 2/3] sys: util: Add gcd and lcm utilities Add helpers to compute Greates Common Divisor (GCD) and Least Common Multiple (LCM). Signed-off-by: Phi Bang Nguyen Signed-off-by: Trung Hieu Le --- include/zephyr/sys/util.h | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/zephyr/sys/util.h b/include/zephyr/sys/util.h index 3c4bd792583f8..e64fad8ca757b 100644 --- a/include/zephyr/sys/util.h +++ b/include/zephyr/sys/util.h @@ -1038,6 +1038,84 @@ static inline size_t sys_count_bits(const void *value, size_t len) */ #define SIGN(x) (((x) > 0) - ((x) < 0)) +/** + * @brief Compute the Greatest Common Divisor (GCD) of two integers + * using the Euclidean algorithm. + * + * @param a First integer + * @param b Second integer + * + * @return The greatest common divisor of a and b, always returns a positive value. + * If one of the parameters is 0, returns the absolute value of the other parameter. + */ +#define gcd(a, b) \ + _Generic((a), \ + int8_t : gcd_s, \ + int16_t : gcd_s, \ + int32_t : gcd_s, \ + uint8_t : gcd_u, \ + uint16_t : gcd_u, \ + uint32_t : gcd_u)(a, b) + +static ALWAYS_INLINE uint32_t gcd_u(uint32_t a, uint32_t b) +{ + uint32_t c; + + if (a == 0) { + return b; + } + + if (b == 0) { + return a; + } + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return b; +} + +static ALWAYS_INLINE int32_t gcd_s(int32_t a, int32_t b) +{ + return gcd_u(a < 0 ? -a : a, b < 0 ? -b : b); +} + +/** + * @brief Compute the Least Common Multiple (LCM) of two integers. + * + * @param a First integer + * @param b Second integer + * + * @retval The least common multiple of a and b. + * @retval 0 if either input is 0. + */ +#define lcm(a, b) \ + _Generic((a), \ + int8_t : lcm_s, \ + int16_t : lcm_s, \ + int32_t : lcm_s, \ + uint8_t : lcm_u, \ + uint16_t : lcm_u, \ + uint32_t : lcm_u)(a, b) + +static ALWAYS_INLINE uint64_t lcm_u(uint32_t a, uint32_t b) +{ + if (a == 0 || b == 0) { + return 0; + } + + return (uint64_t)(a / gcd_u(a, b)) * (uint64_t)b; +} + +static ALWAYS_INLINE int64_t lcm_s(int32_t a, int32_t b) +{ + return lcm_u(a < 0 ? -a : a, b < 0 ? -b : b); +} + #ifdef __cplusplus } #endif From 0d169a4ad9c43c546a832f3cd4d040f641258a08 Mon Sep 17 00:00:00 2001 From: Phi Bang Nguyen Date: Thu, 13 Nov 2025 11:05:07 +0100 Subject: [PATCH 3/3] west.yml: Update hal_stm32 revision for SIGN fix Update the revision to include SIGN macro fix Signed-off-by: Phi Bang Nguyen --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 5aa1253ab1a89..855db41d7b215 100644 --- a/west.yml +++ b/west.yml @@ -250,7 +250,7 @@ manifest: groups: - hal - name: hal_stm32 - revision: 286dd285b5bb4fddafdfff27b5405264e5a61bfe + revision: pull/327/head path: modules/hal/stm32 groups: - hal