Uh oh!
There was an error while loading. Please reload this page.
Expose float from_bits and to_bits in libcore. - #46931
Conversation
rust-highfive
commented
Dec 22, 2017
r? @bluss (rust_highfive has picked a reviewer for you, use r? to override) |
f7fbd0f to
4a1a4a3Compareest31
commented
Dec 22, 2017
Not sure why I didn't do it back in #39271. You should probably copy over the tests from std as well as the docs. |
hanna-kruppe
commented
Dec 22, 2017
It makes sense to have these operations in core, but note that we currently don't have a good way to expose those to end users. The This PR shouldn't/doesn't have to address that, I'm just saying this will not really be usable by anyone except other code in the standard library. There are indeed places in core that could use these methods (float parsing, and probably formatting too), so I'm in favor of this change. |
est31
commented
Dec 22, 2017
@rkruppe thanks for explaining, probably that's why I didnt do it in my PR :). As this is for internal use only, the single line of docs that we have now is probably enough. |
clarfonthey
commented
Dec 22, 2017
Isn't the Long term I assume that the goal is to have multiple inherent impl blocks, so that Regardless, I'm going to proceed with this and replace the existing unions that are used with |
hanna-kruppe
commented
Dec 22, 2017
Nope: https://play.rust-lang.org/?gist=6b061936c1e1e0d872210e120f6d0ae3&version=stable |
0cbe181 to
80d5bbfCompare80d5bbf to
a2cdeb5Compareclarfonthey
commented
Dec 23, 2017
@rkruppe I noticed! :( Regardless, it appears that these methods would be useful in |
| Zero | Subnormal | Normal => { | ||
| let bits: u64 = x.transmute(); | ||
| T::from_bits(bits + 1) | ||
| T::from_bits(x.to_bits() + T::Bits::from(1u8)) |
There was a problem hiding this comment.
This technically changes the overflow behaviour on release, but I don't think it should be a problem, as this function isn't expected to ever trigger the assert that was originally in from_bits here.
There was a problem hiding this comment.
Yeah this can't overflow. If x was the all-ones bit pattern, it'd be a NaN and we wouldn't enter this branch.
| pub trait Float: Sized { | ||
| /// Type used by `to_bits` and `from_bits`. | ||
| #[stable(feature = "core_float_bits", since = "1.24.0")] | ||
| type Bits: ops::Add<Output = Self::Bits> + From<u8> + TryFrom<u64>; |
There was a problem hiding this comment.
These bounds are pretty arbitrary. I guess it's okay for an internal trait, but alternatively you could move them to RawFloat (assuming that's the only place where they are needed).
There was a problem hiding this comment.
I made this work by duplicating the associated type in the RawFloat trait.
bluss
commented
Dec 31, 2017
To be clear -- they are not really offered as anything but doc(hidden) and unstable in libcore even with this PR, right? It seems we have no stable float methods with just libcore. |
ollie27
commented
Dec 31, 2017
Marking the methods as stable in the #![no_std]#![crate_type="lib"]pubfnfoo() -> bool{::core::num::Float::is_nan(0f32)} |
Oh, right, that damn stability hole =/ Well, since this already applies to some existing methods, this PR doesn't really make it worse. |
bluss
commented
Dec 31, 2017
ugh. It's very unfortunate that in no_std, |
bluss
commented
Dec 31, 2017
I hope that we can reach the best kind of solution by providing inherent methods for f32, f64 in libcore (composable with the additional inherent methods in std) |
shepmaster
commented
Jan 6, 2018
Happy new year from triage, @bluss! Will you be able to check this out sometime? |
shepmaster
commented
Jan 13, 2018
Ping from triage, @bluss! It's been over 6 days since we've heard from you, maybe @rust-lang/libs could assign a new reviewer? |
| assert!(significand < T::MIN_SIG, "encode_subnormal: not actually subnormal"); | ||
| // Encoded exponent is 0, the sign bit is 0, so we just have to reinterpret the bits. | ||
| T::from_bits(significand) | ||
| T::from_bits(significand.try_into().unwrap_or_else(|_| unreachable!())) |
There was a problem hiding this comment.
This seems to be a step back, isn't there a way to keep this using just from_bits? Do you know if it has any performance impact?
The assertion assert!(bits < u32::MAX as u64, "f32::from_bits: too many bits"); from the old code has been lost, but I suppose it is unreachable here.
There was a problem hiding this comment.
I wouldn't worry about performance impact, this is only called near the end of the slowest of slow paths. Edit: The other instance of this pattern, in encode_normal above, is also called in the slightly-less-slow-path, Algorithm R, but that path is still really slow in absolute terms. So I stand by my assesment.
It's still ugly as hell but that ugliness will have to be somewhere unless all the dec2flt code is restructured to work with the Bits associated type instead of u64 (which would probably not be pretty either). Maybe it could move into RawFloat::from_bits? (But if so, it should gain a nicer panic message than unreachable)
There was a problem hiding this comment.
I'll try seeing what needs to be done to use Bits instead of u64 here.
There was a problem hiding this comment.
That's probably far too big a refactor to be worthwhile for this PR.
There was a problem hiding this comment.
Moving the try_into to from_bits seems more fruitful.
bluss
commented
Jan 13, 2018
@shepmaster sure, that seems like a good idea |
kennytm
commented
Jan 17, 2018
Review ping for @rust-lang/libs! Randomly reassigning to another team member due to #46931 (comment). r? @BurntSushi Also @clarcharr could you address @bluss's comment in #46931 (review)? |
BurntSushi
commented
Jan 17, 2018
@rfcbot fcp merge |
Team member @BurntSushi has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
carols10cents
commented
Jan 22, 2018
rfcbot
commented
Jan 23, 2018
🔔 This is now entering its final comment period, as per the review above. 🔔 |
1 similar comment
rfcbot
commented
Jan 23, 2018
🔔 This is now entering its final comment period, as per the review above. 🔔 |
rfcbot
commented
Jan 23, 2018
🔔 This is now entering its final comment period, as per the review above. 🔔 |
alexcrichton
commented
Jan 23, 2018
@bors: r+ |
bors
commented
Jan 23, 2018
📌 Commit 556fb02 has been approved by |
bors
commented
Jan 24, 2018
Expose float from_bits and to_bits in libcore. These methods have no dependencies on libm and thus should be offered in libcore.
bors
commented
Jan 24, 2018
☀️ Test successful - status-appveyor, status-travis |
dhardy
commented
Jul 10, 2018
Did this really land in 1.25.0? Because I still get a compiler error with that version: Strangely I don't get warnings about conflicting keywords, which I got with older compilers (at least 1.22 and 1.23). |
These methods have no dependencies on libm and thus should be offered in libcore.