Skip to content

Enhance precision and safety in time calculations #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2024

Conversation

visitorckw
Copy link
Collaborator

Introduces a new inline function, mult_frac(), to handle fractional multiplication with improved precision and safety by computing x * n / d while avoiding common issues like overflow and precision loss. Additionally, the get_time_info() function has been refactored to use mult_frac() for time-related calculations, including nanosecond conversions from mach_absolute_time() and clock() in low-resolution timer mode. These updates collectively enhance the accuracy and reliability of time computations throughout the codebase.

Introduce a new inline function mult_frac() to compute "x * n / d" with
enhanced precision and safety. This function addresses the common
issues of overflow and loss of precision that can occur with
straightforward approaches to this calculation.

Directly computing "x * n / d" can lead to problems:
- Performing the division first can result in loss of precision due to
  integer truncation.
- Performing multiplication before division can risk overflow.

The mult_frac() function mitigates these issues by:
1. Calculating the quotient and remainder of 'x' divided by 'd'.
2. Using these intermediate results to perform the final calculation,
   thus avoiding intermediate overflow and preserving precision.

This approach is based on the Linux kernel's mult_frac() macro [1],
which follows a similar method to handle these calculations robustly.

Link: https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121 [1]
Refactor the get_time_info() function to use the newly added
mult_frac() function for calculations involving time conversions. This
change improves precision and mitigates the risk of overflow in the
following areas:

- Converte the computation of nanoseconds from mach_absolute_time()
  using the timebase information.
- Update the calculation of nanoseconds from clock() in low-resolution
  timer mode.

These updates make the time calculations in get_time_info() more robust
and reliable by leveraging the precise arithmetic capabilities of
mult_frac(). This change enhances the overall accuracy of time-related
computations in the code.
@jserv jserv added this to the release-2024.1 milestone Aug 31, 2024
@jserv jserv merged commit e804bff into sysprog21:master Aug 31, 2024
8 checks passed
@jserv
Copy link
Contributor

jserv commented Aug 31, 2024

Thank @visitorckw for contributing!

@visitorckw visitorckw deleted the introduce-mult-frac branch August 31, 2024 05:30
vestata pushed a commit to vestata/rv32emu that referenced this pull request Jan 24, 2025
Enhance precision and safety in time calculations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants