10#ifndef SBL_PRIMITIVES_MATH_INTEGER_MATH_HPP_
11#define SBL_PRIMITIVES_MATH_INTEGER_MATH_HPP_
35 static constexpr uint32_t
gcd(uint32_t a, uint32_t b) {
51 static constexpr uint32_t
lcm(uint32_t a, uint32_t b) {
52 return (a /
gcd(a, b)) * b;
62 static constexpr uint32_t
divideRounded(uint32_t dividend, uint32_t divisor) {
63 return (dividend + divisor / 2) / divisor;
77 if (target_freq == 0 || target_freq > base_freq) {
84 if (prescaler > 65536) {
86 }
else if (prescaler < 1) {
106 uint64_t ticks = (
static_cast<uint64_t
>(timer_freq) * interval_us) / 1000000;
109 if (ticks > 0xFFFFFFFF) {
113 return static_cast<uint32_t
>(ticks);
126 return (value <
min) ?
min : (value >
max) ?
max : value;
137 static constexpr T
min(T a, T b) {
138 return (a < b) ? a : b;
149 static constexpr T
max(T a, T b) {
150 return (a > b) ? a : b;
160 return value && !(value & (value - 1));
170 if (value == 0)
return 1;
177 value |= value >> 16;
Integer math utilities optimized for ARM Cortex-M.
static constexpr uint32_t calculatePrescaler(uint32_t base_freq, uint32_t target_freq)
Calculate timer prescaler for desired frequency.
static constexpr uint32_t calculateTimerReload(uint32_t timer_freq, uint32_t interval_us)
Calculate timer reload value for interval.
static constexpr T max(T a, T b)
Get maximum of two values.
static constexpr T min(T a, T b)
Get minimum of two values.
static constexpr uint32_t gcd(uint32_t a, uint32_t b)
Calculate greatest common divisor.
static constexpr bool isPowerOfTwo(uint32_t value)
Check if value is power of two.
static constexpr uint32_t nextPowerOfTwo(uint32_t value)
Get next power of two.
static constexpr uint32_t lcm(uint32_t a, uint32_t b)
Calculate least common multiple.
static constexpr uint32_t divideRounded(uint32_t dividend, uint32_t divisor)
Integer division with rounding.
static constexpr T clamp(T value, T min, T max)
Clamp value to range.
Root namespace for all Sound Byte Libs functionality.