diff --git a/bdk/utils/util.c b/bdk/utils/util.c index b351038..d249f9d 100644 --- a/bdk/utils/util.c +++ b/bdk/utils/util.c @@ -75,6 +75,34 @@ char *strcpy_ns(char *dst, char *src) return dst; } +// Approximate square root finder for a 64-bit number. +u64 sqrt64(u64 num) +{ + u64 base = 0; + u64 limit = num; + u64 square_root = 0; + + while (base <= limit) + { + u64 tmp_sqrt = (base + limit) / 2; + + if (tmp_sqrt * tmp_sqrt == num) { + square_root = tmp_sqrt; + break; + } + + if (tmp_sqrt * tmp_sqrt < num) + { + square_root = base; + base = tmp_sqrt + 1; + } + else + limit = tmp_sqrt - 1; + } + + return square_root; +} + u32 get_tmr_s() { return RTC(APBDEV_RTC_SECONDS); diff --git a/bdk/utils/util.h b/bdk/utils/util.h index c22b78b..d942f2d 100644 --- a/bdk/utils/util.h +++ b/bdk/utils/util.h @@ -84,6 +84,7 @@ typedef struct _nyx_storage_t u8 bit_count(u32 val); u32 bit_count_mask(u8 bits); char *strcpy_ns(char *dst, char *src); +u64 sqrt64(u64 num); void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops); u32 crc32_calc(u32 crc, const u8 *buf, u32 len);