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

Tracking issue for #![feature(const_float_bits_conv)] #72447

Closed
ecstatic-morse opened this issue May 22, 2020 · 8 comments · Fixed by #129555
Closed

Tracking issue for #![feature(const_float_bits_conv)] #72447

ecstatic-morse opened this issue May 22, 2020 · 8 comments · Fixed by #129555
Assignees
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-floating-point Area: Floating point numbers and arithmetic C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented May 22, 2020

Tracking issue for the #![feature(const_float_bits_conv)] feature gate, which makes the following methods on f32 and f64 const fn:

  • formerly #![feature(float_bits_conv)]
    • to_bits
    • from_bits
  • formerly #![feature(float_to_from_bytes)]
    • to_bytes_be
    • to_bytes_le
    • to_bytes_ne
    • from_bytes_be
    • from_bytes_le
    • from_bytes_ne

These require transmute within a const fn to stabilize, and–unlike the recently stabilized #![feature(const_int_conversion)]–do not have a const-stable alternate implementation. Nevertheless, these may be useful for nightly users.

Stabilization is blocked on #57241.

@ecstatic-morse ecstatic-morse added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label May 22, 2020
@ecstatic-morse ecstatic-morse self-assigned this May 22, 2020
@ecstatic-morse ecstatic-morse added A-const-eval Area: Constant evaluation (MIR interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. labels May 22, 2020
@jonas-schievink jonas-schievink added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 22, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue Jun 12, 2020
… r=RalfJung,oli-obk

Const floating point bitcasts and classification

Makes the `f32` and `f64` methods described in rust-lang#72447 and rust-lang#72505 unstably const.

r? @RalfJung
@KodrAus KodrAus added A-floating-point Area: Floating point numbers and arithmetic Libs-Tracked Libs issues that are tracked on the team's project board. labels Jul 29, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 23, 2020
…=RalfJung

Const floating point bitcasts and classification

Makes the `f32` and `f64` methods described in rust-lang#72447 and rust-lang#72505 unstably const.

r? @RalfJung
@lebensterben
Copy link
Contributor

Is there anyway to hard-code this without nightly?
I've a real world use case, I need to define
pub const NA_REAL: f64 = f64::from_le_bytes([0xa2, 0x07, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f]);
which is the NA (not NaN) value used in R.

@ecstatic-morse
Copy link
Contributor Author

transmute is stable in constants and statics (but not in const fn) as of 1.46.

@lebensterben
Copy link
Contributor

pub const NA_REAL: f64 = unsafe {
    std::mem::transmute(u64::from_le_bytes([
        0xa2, 0x07, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f,
    ]))
};

This works for me now.

@bstrie
Copy link
Contributor

bstrie commented Feb 14, 2021

Stabilization is blocked on #57241.

Does that issue need to be a blocker for this? The functions in question aren't doing arithmetic on floats, they're just casting bit representations. AIUI while float arithmetic might be nonportable, bit representations are well-specified, yes?

@bstrie
Copy link
Contributor

bstrie commented Feb 14, 2021

Actually, I take that back after looking at #73288 . from_bits(foo).to_bits() isn't even guaranteed to round-trip properly...

@thomcc
Copy link
Member

thomcc commented May 6, 2021

Is there any reason from_bits couldn't be stabilized on its own (without to_bits)? I think to_bits is the problematic one (or at least, it's the one with open questions).

It might not seem useful to provide just from_bits (and not to_bits) to const fn, but here are two use cases:

  1. Parsing floats at compile time, either from env!/option_env! or user provided string literals (e.g. you could imagine a version of that hex float crate that use const fn instead of proc macros).
  2. A software floating point implementation that works in const fn (admittedly, as a way to hack around the current lack of support).

The 2nd is what I was poking around with, when I realized that I can't actually turn the result into a user-friendly f32/f64 (instead I have to give them a u32/u64, which needs to be from_bitsed outside of const).

@workingjubilee
Copy link
Member

per @burrbull in #57241 (comment)
"const fn transmute is already stabilized"

This allows routing around these using unsafe const code. I think that the question now is whether we want to stabilize the safe methods or to instead fix the bugs that can cause unconst behavior (and thus, continue to make people unsafely assert that they believe the result will be const or, in effect, that it will not cause problems) and then stabilize them.

@RalfJung
Copy link
Member

RalfJung commented Aug 3, 2024

With rust-lang/rfcs#3514 having been accepted, I think this is unblocked. I am working on stabilizing const_fn_floating_point_arithmetic; anyone reading this should feel free to pick up stabilizing const_float_bits_conv. :) It'd be nice if they ended up both being part of the same release, though it's also not a big deal if they end up stretched out over multiple releases.

@bors bors closed this as completed in 3b2139b Sep 8, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 8, 2024
Rollup merge of rust-lang#129555 - RalfJung:const_float_bits_conv, r=dtolnay

stabilize const_float_bits_conv

This stabilizes `const_float_bits_conv`, and thus fixes rust-lang#72447. With rust-lang#128596 having landed, this is entirely a libs-only question now.

```rust
impl f32 {
    pub const fn to_bits(self) -> u32;
    pub const fn from_bits(v: u32) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 4];
    pub const fn to_le_bytes(self) -> [u8; 4]
    pub const fn to_ne_bytes(self) -> [u8; 4];
    pub const fn from_be_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self;
}

impl f64 {
    pub const fn to_bits(self) -> u64;
    pub const fn from_bits(v: u64) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 8];
    pub const fn to_le_bytes(self) -> [u8; 8]
    pub const fn to_ne_bytes(self) -> [u8; 8];
    pub const fn from_be_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self;
}
````

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
RalfJung pushed a commit to RalfJung/miri that referenced this issue Sep 10, 2024
stabilize const_float_bits_conv

This stabilizes `const_float_bits_conv`, and thus fixes rust-lang/rust#72447. With rust-lang/rust#128596 having landed, this is entirely a libs-only question now.

```rust
impl f32 {
    pub const fn to_bits(self) -> u32;
    pub const fn from_bits(v: u32) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 4];
    pub const fn to_le_bytes(self) -> [u8; 4]
    pub const fn to_ne_bytes(self) -> [u8; 4];
    pub const fn from_be_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self;
}

impl f64 {
    pub const fn to_bits(self) -> u64;
    pub const fn from_bits(v: u64) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 8];
    pub const fn to_le_bytes(self) -> [u8; 8]
    pub const fn to_ne_bytes(self) -> [u8; 8];
    pub const fn from_be_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self;
}
````

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Sep 22, 2024
stabilize const_float_bits_conv

This stabilizes `const_float_bits_conv`, and thus fixes rust-lang/rust#72447. With rust-lang/rust#128596 having landed, this is entirely a libs-only question now.

```rust
impl f32 {
    pub const fn to_bits(self) -> u32;
    pub const fn from_bits(v: u32) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 4];
    pub const fn to_le_bytes(self) -> [u8; 4]
    pub const fn to_ne_bytes(self) -> [u8; 4];
    pub const fn from_be_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self;
}

impl f64 {
    pub const fn to_bits(self) -> u64;
    pub const fn from_bits(v: u64) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 8];
    pub const fn to_le_bytes(self) -> [u8; 8]
    pub const fn to_ne_bytes(self) -> [u8; 8];
    pub const fn from_be_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self;
}
````

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Sep 25, 2024
stabilize const_float_bits_conv

This stabilizes `const_float_bits_conv`, and thus fixes rust-lang/rust#72447. With rust-lang/rust#128596 having landed, this is entirely a libs-only question now.

```rust
impl f32 {
    pub const fn to_bits(self) -> u32;
    pub const fn from_bits(v: u32) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 4];
    pub const fn to_le_bytes(self) -> [u8; 4]
    pub const fn to_ne_bytes(self) -> [u8; 4];
    pub const fn from_be_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 4]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self;
}

impl f64 {
    pub const fn to_bits(self) -> u64;
    pub const fn from_bits(v: u64) -> Self;
    pub const fn to_be_bytes(self) -> [u8; 8];
    pub const fn to_le_bytes(self) -> [u8; 8]
    pub const fn to_ne_bytes(self) -> [u8; 8];
    pub const fn from_be_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_le_bytes(bytes: [u8; 8]) -> Self;
    pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self;
}
````

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-floating-point Area: Floating point numbers and arithmetic C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants