Skip to content
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

LibCrypto: Improve precision of BigFraction::to_double #3174

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

manuel-za
Copy link
Contributor

Fixes #2639

@manuel-za manuel-za force-pushed the 2639 branch 7 times, most recently from 918decc to 8882a9c Compare January 9, 2025 14:56
@manuel-za manuel-za marked this pull request as ready for review January 9, 2025 22:26
@manuel-za manuel-za requested a review from alimpfard as a code owner January 9, 2025 22:26
@manuel-za
Copy link
Contributor Author

manuel-za commented Jan 9, 2025

Improves coverage for Temporal::Duration when running test262. Two 262 Temporal tests from @trflynn89's list still fail:

  • precision-exact-mathematical-values-5.js
  • precision-exact-mathematical-values-6.js

Out of the failing tests, 1 in 3, resp. 1 in 4 conditions fail. Maybe a rounding issue? I looked briefly at the Temporal code. Nothing obvious sticks out. UnsignedBigInteger.to_double() may be another avenue to investigate.

I am reasonably confident in the solution:

  • calculation to 64-bit precision
  • rounding to double (53 bits) using the default rounding mode for UnsignedBigInteger.to_double() : ECMAScriptNumberValueFor (alias for RoundingMode::IEEERoundAndTiesToEvenMantissa)
  • staying precise after that (only changes are to the exponent)

...but hey, I've been wrong before 😃

@trflynn89: I could totally benefit from a second pair of eyes...

Copy link

github-actions bot commented Feb 1, 2025

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

@github-actions github-actions bot added the stale label Feb 1, 2025
- Before: UnsignedBigInteger::shift_right( n ) trigger index
  verification error for n>31. An assumption of
  num_bits<UnsignedBigInteger::BITS_IN_WORD was being made
- After: shift_right( n ) works correctly for n>31.
Before:
- a separate Word element allocation of the underlying Vector<Word> was
necessary for every new word in a multi-word shift
- two additional temporary UnsignedBigInteger buffers were allocated
and passed through, including in downstream calls (e.g. Multiplication)
- an additional allocation and word shift for the carry
- FIXME note seems to point to some of these issues

After:
- main change is in LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp
- one single allocation per call, using shift_left_by_n_words
- only the input "number" and "output" need to be allocated by the
  caller
- downstream calls are adapted not to allocate or pass temporary
  buffers
- noticeable performance improvement when running TestBigInteger:
  from 41-42s (before) to 28-29s (after) on Intel Core i9 laptop

NOTE: making this change in a separate commit than shift_right, even if
it is in the same file BitwiseOperations.cpp since:
- it is a "bonus" addition: not necessary for fixing the bug, but
  logically related to the shift_right code
- it brings a chain of downstream interface modifications (7 files),
  unrelated to shift_right
These were likely cut-and-paste artifacts from
UnsignedBigInteger::multiplied_by; not caught by the "unused-varible".

NOTE: making this change in a separate commit than shift_left, since
it is logically unrelated.
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.

LibCrypto: Improve Crypto::BigFraction::to_double
1 participant