From cc3a51562d337d9692521b8d702277cbeee09f40 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 1 Mar 2026 23:23:34 -1000 Subject: [PATCH] [core] Document safe range for fast_div1000_32 intermediate --- esphome/core/helpers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 484e410634..2713b67849 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -552,6 +552,7 @@ inline ESPHOME_ALWAYS_INLINE uint32_t fast_div1000_32(uint64_t us) { uint32_t lo = static_cast(us); uint32_t hi = static_cast(us >> 32); // Combine remainder term: hi * (2^32 % 1000) + lo + // Note: hi * R fits in uint32_t for all practical uptimes (hi < 14.5M, i.e. < 584,942 years) 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);