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

Add float conversions to and from bytes #58756

Closed
wants to merge 1 commit into from

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Feb 26, 2019

Use the same API as for integers.

Fixes #57492.

@rust-highfive
Copy link
Collaborator

r? @aidanhs

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 26, 2019
@tbu-
Copy link
Contributor Author

tbu- commented Feb 26, 2019

Will add docs if the general approach is deemed to be okay.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:1570503c:start=1551196401450103822,finish=1551196405862117709,duration=4412013887
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export IMAGE=x86_64-gnu-llvm-6.0
---
[00:09:52]    Compiling rustc_lsan v0.0.0 (/checkout/src/librustc_lsan)
[00:09:55] error[E0308]: mismatched types
[00:09:55]    --> src/libcore/num/f64.rs:516:20
[00:09:55]     |
[00:09:55] 516 |         swap_if_le(Self::from_ne_bytes(bytes))
[00:09:55]     |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected array of 8 elements, found f64
[00:09:55]     |
[00:09:55]     = note: expected type `[u8; 8]`
[00:09:55]                found type `f64`
[00:09:55] error[E0308]: mismatched types
[00:09:55]    --> src/libcore/num/f64.rs:516:9
[00:09:55]     |
[00:09:55]     |
[00:09:55] 515 |     pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
[00:09:55]     |                                                   ---- expected `f64` because of return type
[00:09:55] 516 |         swap_if_le(Self::from_ne_bytes(bytes))
[00:09:55]     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found array of 8 elements
[00:09:55]     = note: expected type `f64`
[00:09:55]     = note: expected type `f64`
[00:09:55]                found type `[u8; 8]`
[00:09:55] error[E0308]: mismatched types
[00:09:55]    --> src/libcore/num/f64.rs:523:20
[00:09:55]     |
[00:09:55]     |
[00:09:55] 523 |         swap_if_be(Self::from_ne_bytes(bytes))
[00:09:55]     |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected array of 8 elements, found f64
[00:09:55]     |
[00:09:55]     = note: expected type `[u8; 8]`
[00:09:55]                found type `f64`
[00:09:55] error[E0308]: mismatched types
[00:09:55]    --> src/libcore/num/f64.rs:523:9
[00:09:55]     |
[00:09:55]     |
[00:09:55] 522 |     pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
[00:09:55]     |                                                   ---- expected `f64` because of return type
[00:09:55] 523 |         swap_if_be(Self::from_ne_bytes(bytes))
[00:09:55]     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found array of 8 elements
[00:09:55]     = note: expected type `f64`
[00:09:55]     = note: expected type `f64`
[00:09:55]                found type `[u8; 8]`
[00:10:01] error: aborting due to 4 previous errors
[00:10:01] 
[00:10:01] For more information about this error, try `rustc --explain E0308`.
[00:10:01] error: Could not compile `core`.
---
travis_time:end:046ffba8:start=1551197019631286950,finish=1551197019636406152,duration=5119202
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:00982b0c
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:0a3e86b8
travis_time:start:0a3e86b8
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:0e7167ce
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

src/libcore/num/f32.rs Outdated Show resolved Hide resolved
@scottmcm scottmcm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 27, 2019
@Dylan-DPC-zz Dylan-DPC-zz added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 18, 2019
@Dylan-DPC-zz
Copy link

ping from triage @tbu- waiting for your updates on the PR in reply to the review above

Use the same API as for integers.

Fixes rust-lang#57492.
@tbu- tbu- force-pushed the pr_floatfromtobytes branch from ac81d82 to f39230e Compare March 18, 2019 11:53
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:140483b4:start=1552910074182578083,finish=1552910075151753730,duration=969175647
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
$ export GCP_CACHE_BUCKET=rust-lang-ci-cache
Setting environment variables from .travis.yml
---
[00:04:28]    Compiling rustc_asan v0.0.0 (/checkout/src/librustc_asan)
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f32.rs:536:5
[00:04:42]     |
[00:04:42] 536 |     pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
[00:04:42]     |
[00:04:42]     = note: `-D missing-docs` implied by `-D warnings`
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f32.rs:543:5
[00:04:42]     |
[00:04:42] 543 |     pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f32.rs:550:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 550 |     pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:494:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 494 |     pub const fn to_be_bytes(self) -> [u8; 8] {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:501:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 501 |     pub const fn to_le_bytes(self) -> [u8; 8] {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:508:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 508 |     pub const fn to_ne_bytes(self) -> [u8; 8] {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:515:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 515 |     pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:522:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 522 |     pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
[00:04:42] 
[00:04:42] error: missing documentation for a method
[00:04:42]    --> src/libcore/num/f64.rs:529:5
[00:04:42]     |
[00:04:42]     |
[00:04:42] 529 |     pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
[00:04:42] 
[00:04:43] error: aborting due to 9 previous errors
[00:04:43] 
[00:04:43] error: Could not compile `core`.
---
travis_time:end:02a6fb47:start=1552910369789953950,finish=1552910369795876935,duration=5922985
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:285fb487
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:2abc0ac8
travis_time:start:2abc0ac8
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:26f79a02
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@tbu-
Copy link
Contributor Author

tbu- commented Mar 18, 2019

I'm not really sure what to put into the examples. Does that work?

@Dylan-DPC-zz
Copy link

ping from triage @tbu- @aidanhs any updates on this?

@tbu-
Copy link
Contributor Author

tbu- commented Mar 25, 2019

I was wondering whether the examples are fine or what I should extend them with.

@Centril
Copy link
Contributor

Centril commented Mar 30, 2019

Ping from triage...

r? @scottmcm
cc @tbu-

@rust-highfive rust-highfive assigned scottmcm and unassigned aidanhs Mar 30, 2019
@scottmcm
Copy link
Member

scottmcm commented Apr 2, 2019

The code here seems fine, but since it's a new API I think it should have someone from @rust-lang/libs take a look at the APIs and comment if they're interested before going in.

(Not a full FCP, since they're just unstable, but I'm not up-to-date on what they're looking for in new stuff.)

Also, @tbu-, you have travis failures: error: missing documentation for a method

@SimonSapin
Copy link
Contributor

I personally feel that the need for these APIs is less pressing than the integer ones, because this operation can already be achieved without unsafe code. (Without transmute, in particular.)

But I’m not entirely opposed either. I could see the argument that this more discoverable, it might not be obvious that e.g. u32::from_be_bytes and f32::from_bits can be combined in this way.

Let’s see what other @rust-lang/libs members think.

@alexcrichton
Copy link
Member

I'd personally be in favor of being consistent across all the primitive types, so I'd be ok merging and eventually stabilizing this. I don't feel too strongly though and agree @SimonSapin that the pressure isn't so high as there's already methods of doing this, but I'd just fall a little bit on the other side of the fence!

@Dylan-DPC-zz Dylan-DPC-zz added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 29, 2019
@alexcrichton
Copy link
Member

@SimonSapin this was pinged back to the libs team recently, do you feel that this shouldn't land as unstable to start off with? It looks like there aren't a ton of other opinions about htese APIs.

@SimonSapin
Copy link
Contributor

SimonSapin commented May 1, 2019

Alright, let’s land this. @tbu-, could you make a few changes?

  • Change the tracking issue number to Tracking issue for floats ↔ bytes conversions #60446
  • Remove rustc_const_unstable attributes as they’re redundant with unstable attributes. When doing FCP to stabilize we’ll do so as const fn, no need for a separate FCP just for constness.
  • Add some documentation to from_* methods and f64 methods.

@SimonSapin SimonSapin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels May 1, 2019
@Centril
Copy link
Contributor

Centril commented May 1, 2019

  • When doing FCP to stabilize we’ll do so as const fn, no need for a separate FCP just for constness.

I would suggest keeping rustc_const_unstable as it is highly unlikely that you'll get to stabilize the constness concurrently with the usual stabilization. The function to_bits isn't const fn (and won't be soon) and moreover we do not generally allow floating point related logic in const fn because of differences in behavior between runtime and compile time function execution (if there provably cannot be differences in this instance then it may be workable but we need a larger discussion that should not block the initial stabilization).

@jonas-schievink
Copy link
Contributor

Ping from triage @tbu-, this still needs a few changes before it can be merged.

@jonas-schievink
Copy link
Contributor

if there provably cannot be differences in this instance then it may be workable

AFAIK, NaN payloads are generally processor-specific, and they can be observed and manipulated using these functions. Perhaps this should not be a const fn at all then?

@Dylan-DPC-zz
Copy link

ping from triage @tbu- any updates?

@scottmcm
Copy link
Member

scottmcm commented Jul 2, 2019

@tbu- I'm going to close this for now to keep the backlog clean. Please do re-open it if you get the chance to address the comments in #58756 (comment).

@jonas-schievink jonas-schievink added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2019
@tesuji
Copy link
Contributor

tesuji commented Jul 4, 2019

I reworked this in #62366 .

bors added a commit that referenced this pull request Jul 8, 2019
Add float conversions to and from bytes

Rework of #58756. Address #58756 (comment).

Fixes #57492.

r? @SimonSapin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

from_*e_bytes and to_n*_bytes equivalents for floats
10 participants