Edited macro for ms_to_ticks#19
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the MS_TO_TICKS macro to prevent truncation issues when converting milliseconds to timer ticks. The change adds 999 to the numerator before division to implement ceiling division, ensuring that small millisecond values don't get truncated to zero ticks.
- Updated the
MS_TO_TICKSmacro to use ceiling division instead of floor division - Removed the TODO comment about potential truncation issues
|
|
||
| /* Time and tick conversions */ | ||
| #define MS_TO_TICKS(ms) ((ms) * TX_TIMER_TICKS_PER_SECOND / 1000) // u_TODO - i think this truncates if ms < 10. dunno if that will be an issue | ||
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) // |
There was a problem hiding this comment.
The ceiling division implementation is incorrect. Adding 999 assumes the divisor is always 1000, but the actual divisor is 1000. For proper ceiling division, use (((ms) * TX_TIMER_TICKS_PER_SECOND + 1000 - 1) / 1000) or the more general form (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) only if you're certain the divisor is exactly 1000.
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) // | |
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + TX_TIMER_TICKS_PER_SECOND - 1) / TX_TIMER_TICKS_PER_SECOND) // |
|
|
||
| /* Time and tick conversions */ | ||
| #define MS_TO_TICKS(ms) ((ms) * TX_TIMER_TICKS_PER_SECOND / 1000) // u_TODO - i think this truncates if ms < 10. dunno if that will be an issue | ||
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) // |
There was a problem hiding this comment.
The trailing comment is empty and should either be removed or contain meaningful documentation explaining the ceiling division behavior.
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) // | |
| #define MS_TO_TICKS(ms) (((ms) * TX_TIMER_TICKS_PER_SECOND + 999) / 1000) /* Converts milliseconds to ticks, rounding up to ensure partial ticks are counted (ceiling division). */ |
No description provided.