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

[C++] DecimalRealConversion could multiply by 5 instead of 10 #44161

Open
bkietz opened this issue Sep 18, 2024 · 2 comments
Open

[C++] DecimalRealConversion could multiply by 5 instead of 10 #44161

bkietz opened this issue Sep 18, 2024 · 2 comments

Comments

@bkietz
Copy link
Member

bkietz commented Sep 18, 2024

Describe the enhancement requested

To construct the decimal corresponding to a floating point value, we need to perform the multiplication and division mant * 2^k * 10^scale (k < 0). When performing those operations directly would result in overflow, they are broken into smaller steps of interleaved multiplication and division.

Multiplication by 10 then division by 2 can be losslessly simplified to multiplication by 5. Taking advantage of this should increase perfomance by reducing the number of interleaved steps as well as widening the range across which we can use the faster single step approach. Rewriting the above expression as mant * 5^scale * 2^(k + scale), we can use two cases:

  • if k + scale >= 0 then we directly multiply by 5^scale then by 2^(k + scale)
  • else if we can prove that 5^scale * mant cannot overflow, multiply by 5^scale and use RoundedRightShift to divide by `1/2^(k+scale)
  • else let the greatest safe power of 5 for multiplication be 5^N; interleave multiplication by 5^N and division by 4^N since that will keep the range of active bits consistent and easy to debug

Component(s)

C++

@bkietz
Copy link
Member Author

bkietz commented Sep 18, 2024

@pitrou what do you think?

@pitrou
Copy link
Member

pitrou commented Sep 18, 2024

That's a rather interesting idea. You'll have to make sure there are enough tests to exercise all three cases thoroughly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants