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

Open
ecstatic-morse opened this issue May 22, 2020 · 7 comments
Open

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

ecstatic-morse opened this issue May 22, 2020 · 7 comments
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: A tracking issue for an RFC or an unstable feature. 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: A tracking issue for an RFC or an unstable feature. 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
Contributor

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.

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: A tracking issue for an RFC or an unstable feature. 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

No branches or pull requests

7 participants