I found Why `1f64.sqrt()` is not constant? - #2 by jessa0, but the answer links to a merged tracking issue.
This thread on Zulip looks to be more recent:
#general > why isn’t sqrt const for f32? (and can I fix that?)
Skimming to that thread (and looking at the related PR[1]) sounds like the answer is mostly just nobody has finished putting in all the work needed to make it const.
like the answer is mostly just nobody has finished putting in all the work needed to make it const.
How difficult would it be do with no prior experience working on the compiler? or is someone else already working on it?
I think the fact that there is no unified representation of floating numbers blocks the stabilization. The current sqrt function does not guarantee consistent results across architectures, so it would be required to implement a new sqrt function capable of running at compile-time.
There is a unified representation of floating numbers - IEEE754 - and this defines a bit-exact result for the current sqrt function on all IEEE754 compliant platforms (which nowadays is everything apart from 32-bit x86).
But compilers tend to use soft-float for constant functions anyway, because the legacy of pre-IEEE754 floating point (which is anything released before 1985, including Intel x87[1]) has made a lot of compiler authors very wary of trusting the hardware. And as x87 survived for so long (it's the only guaranteed to work FPU on all 32-bit x86 platforms - you need SSE2 for IEEE754 FP on x86, which only became required in 64-bit x86), it's been a relevant problem up until very recently for compiler authors.
The x87 lacks the ability to set the register rounding length - it can only round to extended precision in registers, not to
binary32orbinary64(which it can only round to on stores). As a result, the only way to get IEEE754-compliantf32andf64results from an x87 FPU is to store every intermediate result to memory, and reload before using it in the next operation, ruining your performance story. ↩︎
I see. Thank you for the detailed explanation!
Sort of, the details of NaNs varies between architectures, e.g. what is a signaling vs a quiet NaN. And beyond that what each bit represent (if anything).
However, I believe that is explicitly documented as non-deterministic (or something to that effect) for const eval.
You're probably thinking of this.
This RFC proposes that we accept, for the first time, that a
const fncan behave non-deterministically at run-time, and produce target/version/flag-specific results at compile-time. It can also produce different results when called at compile-time and at run-time.
That's interesting, I always assumed const fns where roughly equivalent to mathematically pure functions, except that they could panic.
I believe the rule is "deterministic within a single compilation invocation[1] during compile time", which is not "pure" in the sense functional language people typically think of. Whether it's "mathematically pure" is probably a debate about vocabulary.
const fn plus(a: &mut i32) -> i32 {
*a = a.wrapping_add(1);
*a
}
static EXAMPLE: i32 = const {
let mut a = 0;
// I passed in the same value in some sense (reference ala an address),
// and got different results. Mathematically pure or no?
plus(&mut a);
plus(&mut a)
};
(And they need not be deterministic at run time since that RFC.)
probably can be refined to some set of compilation configuration ↩︎
I think you meant to assign: *a = a.wrapping_add(1);
This is complicated by the IEEE754 definition of NaN (as pointed out by @Vorpal); IEEE754 does not define which of the many NaN bit patterns a given operation outputs when its input is a number and the output is a NaN, and from an IEEE754 perspective, all qNaNs are the same, all sNaNs are the same, and once the signalling behaviour of an sNaN is done, it's the same as a qNaN.
The reason for supplying so many NaN bit patterns is that a programmer can put information into the NaN bit pattern, and IEEE754 says that the implementation must choose one input NaN (and if there's more than one, the implementation can choose any of them - this choice does not have to be deterministic), then output the matching qNaN for that input NaN (same if it's a qNaN, with the signalling bit cleared if it's an sNaN), assuming that you're not changing FP format (and possibly with the sign bit changed). Thus, if you have an algorithm that only conditionally uses some of its inputs, you can set a unique NaN bit pattern for each NaN in the input, and if a NaN is used by the algorithm, you get to know one of the NaNs used by looking at the output of the algorithm.
This does, however, as Vorpal pointed out, make it possible to identify the IEEE754 implementation in use in some cases, by performing an operation that creates a NaN (e.g. f64::sqrt(-1.0)) and seeing what NaN the implementation (hardware or software) selects. For const evaluation, though, that can be worked to an extent around by adding an is_nan check to the output of whatever implementation you choose, and forcing the NaN to a fixed bit pattern - which means that, at best, you can distinguish runtime from compile time evaluation by looking at the bit pattern.
While it might seem possible to determine which NaN the target would produce, this isn't usually worth the effort; because the bit pattern is unspecified (and they're all "NaN" in IEEE754 land), programmers don't usually care what the NaN bit pattern is from an operation, and also different ISA-compatible hardware is allowed to produce different NaNs for the same operation. There's also things like AArch32's "default NaN", which changes the behaviour from IEEE754 at runtime to always produce a specified bit pattern for all NaNs, including those propagated from input NaNs, which compile-time evaluation can't check.
It's probably too late to do this, but I wonder wouldn't it have been better to treat computations resulting in NaN as a panic during const eval? In other words, f64::sqrt(-1.0) in const context would result in a compilation error.
We could've done the same for overflows as well. Today we have an inconsistency where const C: u8 = 255 + 255; results in a compilation error, while doing the same with add(255, 255) will pass with disabled overflow checks.
Not sure it's feasible. If some operations are not deterministic, then a build may randomly panic.
Ex:. sqrt(sin(Pi))
I am not sure I understand your point. Operations used in const fns must be deterministic. Complex functions like sin may produce a different result at runtime (e.g. because a different mathlib was used or because of hardware peculiarities), but they must always produce the same value when executed in const contexts.
Operations are only non-deterministic in IEEE754 because the implementation has decided to make them non-deterministic; const evaluation could use a soft float library that's fully deterministic.
IEEE754 strongly recommends that all operations work as-if they were computed to infinite precision and then rounded in accordance with the rounding mode in effect[1]. As long as all the operations you use are computed this way, you have results that are fully deterministic modulo NaN folding[2].
If you do that, then sin(Pi) is deterministic, as is sqrt(), and thus sqrt(sin(Pi)) is also deterministic and will not panic.
IEEE754 requires that any implementation either doesn't provide some of the basic operations (add, subtract, multiply, divide, square root, fused multiple-add), or provides a way for you to tell the implementation that they must be computed this way. Rust says that the default is that the basic operations are computed deterministically, but you can opt-out with things like
algebraic_add. ↩︎You can make the bitwise result fully determined by the inputs by finishing a sequence of IEEE754 operations with the equivalent of
if result.is_nan() { result = fXX::NAN }- basically changing all the NaNs that could have been output into a single qNaN. ↩︎
Well that's the problem with floats.
Are const float operations be deterministic on different processors? I thought the answer was "no".
The answer is "yes" - runtime may not be for the recommended operations like trigonometry (since you might use a fast path that's not equivalent to computing to infinite precision and rounding), but the basic operations must be.
And given that there is a "correct" answer according to IEEE754 floating point, you can always implement a fully deterministic set of IEEE754 operations, possibly at the expense of performance. For const evaluation, that would be a good path to go down.
It would be very bad if x.sin() produced a different value at compile time and at runtime.
So if sin has to be non-portable between platforms, I would never make it const.
Instead, use something like softfloat::F32::sin for your const needs.
Alternatively, somebody could implement exactfloat with perfect rounding so you could use exactfloat::F32::sin.