[core] Document exact safe input range for fast_div1000_32

This commit is contained in:
J. Nick Koston
2026-03-01 23:27:38 -10:00
parent 263d6c1085
commit bf8990dcb0
+1 -1
View File
@@ -552,7 +552,7 @@ inline ESPHOME_ALWAYS_INLINE uint32_t fast_div1000_32(uint64_t us) {
uint32_t lo = static_cast<uint32_t>(us);
uint32_t hi = static_cast<uint32_t>(us >> 32);
// Combine remainder term: hi * (2^32 % 1000) + lo
// Note: hi * R fits in uint32_t when hi < UINT32_MAX / R (~14.5M), i.e. us < ~62 quadrillion
// Safe for us < (14510024 << 32), i.e. ~6.2e16 (~1,975 years of microseconds)
uint32_t adj = hi * R + lo;
// If adj overflowed, the true value is 2^32 + adj; apply the identity again
return hi * Q + (adj < lo ? (adj + R) / D + Q : adj / D);