Skip to content

Commit 781253b

Browse files
authored
Rollup merge of rust-lang#114813 - RalfJung:fpu-control, r=Amanieu
explain why we can mutate the FPU control word This is usually not allowed (see rust-lang/stdarch#1454), but here we have a special case.
2 parents cbab5ad + b4714a8 commit 781253b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

library/core/src/num/dec2flt/fpu.rs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ pub use fpu_precision::set_precision;
88
// round to 80 bits causing double rounding to happen when values are eventually represented as
99
// 32/64 bit float values. To overcome this, the FPU control word can be set so that the
1010
// computations are performed in the desired precision.
11+
//
12+
// Note that normally, it is Undefined Behavior to alter the FPU control word while Rust code runs.
13+
// The compiler assumes that the control word is always in its default state. However, in this
14+
// particular case the semantics with the altered control word are actually *more faithful*
15+
// to Rust semantics than the default -- arguably it is all the code that runs *outside* of the scope
16+
// of a `set_precision` guard that is wrong.
17+
// In other words, we are only using this to work around <https://github.com/rust-lang/rust/issues/114479>.
18+
// Sometimes killing UB with UB actually works...
19+
// (If this is used to set 32bit precision, there is still a risk that the compiler moves some 64bit
20+
// operation into the scope of the `set_precision` guard. So it's not like this is totally sound.
21+
// But it's not really any less sound than the default state of 80bit precision...)
1122
#[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
1223
mod fpu_precision {
1324
use core::arch::asm;

library/core/src/num/dec2flt/number.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl Number {
5151
/// There is an exception: disguised fast-path cases, where we can shift
5252
/// powers-of-10 from the exponent to the significant digits.
5353
pub fn try_fast_path<F: RawFloat>(&self) -> Option<F> {
54+
// Here we need to work around <https://github.com/rust-lang/rust/issues/114479>.
5455
// The fast path crucially depends on arithmetic being rounded to the correct number of bits
5556
// without any intermediate rounding. On x86 (without SSE or SSE2) this requires the precision
5657
// of the x87 FPU stack to be changed so that it directly rounds to 64/32 bit.

0 commit comments

Comments
 (0)