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

use apfloat for ldexp #902

Merged
merged 5 commits into from
Aug 10, 2019
Merged

use apfloat for ldexp #902

merged 5 commits into from
Aug 10, 2019

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Aug 9, 2019

No description provided.

let n = unsafe { ldexp(x, exp) };
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
let res = x.scalbn(exp.try_into().unwrap());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb the odd thing is that the C version supports i32 but your version only i16. Panicking on big values seems wrong, any advise what else to do?

Copy link
Member

@eddyb eddyb Aug 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this snippet and the fact that f64 uses 11 bits of exponent and f128 would use 15 bits, you can safely clamp it between ExpInt::min_value() and ExpInt::max_value() and it would still be outside of the valid exponent range (so the effect will be the same, presumably underflow to 0 / overflow to Inf).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this snippet

So the only use of exp is in exp as i32? Seems more reasonable then to just expose it as an i32, IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for now I went with clamping, thanks.

Copy link
Member

@eddyb eddyb Aug 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i32 is only used as an intermediary to avoid overflow (specifically for f128, it could stay entirely within i16 for f64).
Most storage and manipulation of the exponent is done in ExpInt, which is i16.

Changing ExpInt to i32 would be possible (although wasteful IMO), it would just take more effort. And I'd prefer not to change only this one function (which I only ported for completeness sake, I didn't expect it to be useful).

src/shims/foreign_items.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member Author

@bors r+

@bors
Copy link
Contributor

bors commented Aug 10, 2019

📌 Commit 04892d9 has been approved by RalfJung

@bors
Copy link
Contributor

bors commented Aug 10, 2019

⌛ Testing commit 04892d9 with merge a5eb61a...

bors added a commit that referenced this pull request Aug 10, 2019
@bors
Copy link
Contributor

bors commented Aug 10, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: RalfJung
Pushing a5eb61a to master...

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.

3 participants