Skip to content

Commit bc087b3

Browse files
cvinayakcarlescufi
authored andcommitted
kernel: Improve precision of ticks and ms conversions
The following 2 improvements are contained in this patch: - When converting from ms to ticks, instead of using hardware cycles per tick, use hardware cycles per second. This ensures that the multiplication is done before the division, increasing precision. - When converting from ticks to ms, instead of using cycles per tick and cycles per sec, use ticks per sec. This too increases the precision. The concept is to make the dividend as large as possible compared to the divisor in order to lose as little precision as possible. Fixes #8898 Fixes #9459 Fixes #9466 Fixes #9468 Signed-off-by: Vinayak Kariappa Chettimada <[email protected]> Signed-off-by: Carles Cufi <[email protected]>
1 parent 19b7eaa commit bc087b3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

include/kernel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
13631363
/* use 64-bit math to keep precision */
13641364
return (s32_t)ceiling_fraction(
13651365
(s64_t)ms * sys_clock_hw_cycles_per_sec,
1366-
(s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_tick);
1366+
((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
1367+
sys_clock_ticks_per_sec);
13671368
#else
13681369
/* simple division keeps precision */
13691370
s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
@@ -1383,8 +1384,7 @@ static inline s64_t __ticks_to_ms(s64_t ticks)
13831384

13841385
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
13851386
/* use 64-bit math to keep precision */
1386-
return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
1387-
sys_clock_hw_cycles_per_sec;
1387+
return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
13881388
#else
13891389
/* simple multiplication keeps precision */
13901390
u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;

0 commit comments

Comments
 (0)