From 546178c70066ea5900399f4eb076c3a002586b0c Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Fri, 15 May 2026 21:31:21 +0200 Subject: [PATCH] moved isqrt to filter.h --- fw/filter.h | 21 +++++++++++++++++++++ fw/main.c | 20 -------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/fw/filter.h b/fw/filter.h index e7055fa..bbad041 100644 --- a/fw/filter.h +++ b/fw/filter.h @@ -2,6 +2,7 @@ #define _FILTER_H #include +#include #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -23,4 +24,24 @@ static_assert(-4 >> 1 == -2, ">> doesn't do sign extension"); #define I16_FP_EMA_K8(x, s) (int16_t)((((int32_t)(x)<<8) - (x) + (s)) >> 8) #define I16_FP_EMA_K16(x, s) (int16_t)((((int32_t)(x)<<16) - (x) + (s)) >> 16) +// Integer square root (binary search) +// https://en.wikipedia.org/wiki/Integer_square_root +static inline uint16_t isqrt(uint32_t x) +{ + uint16_t l = 0; // lower bound of the square root + uint16_t r = x + 1; // upper bound of the square root + + while (l != r - 1) { + uint32_t m = (l + r) / 2; // midpoint to test + if (m * m <= x) { + l = m; + } else { + r = m; + } + } + + return l; +} + + #endif // _FILTER_H diff --git a/fw/main.c b/fw/main.c index 7a33d74..a2897c9 100644 --- a/fw/main.c +++ b/fw/main.c @@ -345,26 +345,6 @@ static inline void pwm_set(uint16_t pulse_width) } -// Integer square root (binary search) -// https://en.wikipedia.org/wiki/Integer_square_root -static inline uint16_t isqrt(uint32_t x) -{ - uint16_t l = 0; // lower bound of the square root - uint16_t r = x + 1; // upper bound of the square root - - while (l != r - 1) { - uint32_t m = (l + r) / 2; // midpoint to test - if (m * m <= x) { - l = m; - } else { - r = m; - } - } - - return l; -} - - // Input: temperature difference // Output: duty-cycle 0-max_duty uint16_t pid(int16_t delta, int16_t max_duty)