Repository navigation
Make mxcsr::write and mxcsr::update unsafe - #627
Merged
Merged
Conversation
Rust assumes the default floating-point environment. MxCsr values with unmasked exceptions, a non-default rounding mode, DAZ or FTZ can be built in safe code, and loading one makes optimized code disagree with code evaluated at run time (or trap on an unmasked exception). core documents changing these bits as immediate undefined behavior (_mm_setcsr), so the functions that load them cannot be safe.
Freax13
approved these changes
Oct 29, 2026
Freax13
left a comment
Member
There was a problem hiding this comment.
Thank you for your contribution!
LGTM, thanks!
Some more reading on this:
Freax13
enabled auto-merge
October 29, 2026 18:50
rustdoc is pointing out that write could refer to a function and a macro (`core::write!`). Clarify that we mean the function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
registers::mxcsr::writeandregisters::mxcsr::updateare safe functions, and anyMxCsrvalue can be built in safe code, for exampleMxCsr::default() | MxCsr::ROUNDING_CONTROL_POSITIVE. Loading a value with a non-default rounding mode, unmasked exceptions, DAZ or FTZ breaks an assumption the compiler relies on. Safe code can then observe optimized and unoptimized evaluation of the same expression disagree, or take an unexpected#XMon an unmasked exception. This PR makes both functionsunsafeand documents the requirement.Why this cannot be a safe function
Rust assumes that floating-point code runs in the default floating-point environment.
corestates this explicitly for the equivalent intrinsic,core::arch::x86_64::_mm_setcsr:The primitive docs for
f64likewise define arithmetic as "rounded according to the roundTiesToEven direction defined in IEEE 754-2008". The compiler uses this when it evaluates floating-point operations at compile time.The inline-assembly rules point the same way.
asm.rules.preserved-registerstreats only the MXCSR exception flags (PE, UE, OE, ZE, DE, IE) as status flags. The control bits are not something anasm!block may leave changed.Repro
write's body, taking the raw bits (0x1F80 | (0b10 << 13)isMxCsr::default() | MxCsr::ROUNDING_CONTROL_POSITIVE):rustc 1.99.0 (b940084d7 2026-09-28),-C opt-level=3, x86_64 Linux: