-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
Undefined Behavior: Changing MXCSR
to _MM_ROUND_TOWARD_ZERO
on x86 and x86_64.
#776
Comments
Thanks for the issue ; I admit I'm not sure how to research "using inline assembly instead". |
The key element of using inline assembly is that you can't ever leave an inline assembly block without restoring MXCSR to the default state, or you hit the same UB problem. You'd have to do the entire round-toward-zero operation using only inline assembly. |
A bit more context: this operation occurs when using rapier/src/pipeline/physics_pipeline.rs Line 270 in cf77b5b
This "flush to zero" leads to important performance improvements. Brainstorming further: Could the entire round-toward-zero operation can be summarized as a function call, so the assembly would end up quite small (function declaration, arguments prep, function call) ? |
You cannot leave flush to zero active while executing rust code, it's simply not allowed. So you can't have functions to toggle the effect on/off, and you also can't have a function that takes a fn pointer to run with flushing set which changes the state and calls back into rust while the state is changed. This applies equally to all floating point environment settings, though flush to zero is usually the only one people try to change. |
Can you elaborate on these? From @Lokathor's comments, it sounds like these just aren't available in sound Rust today, and it might be best to do without. |
In Rust, modifying the rounding mode in
x86
andx86_64
by modifying theMXCSR
register (through_mm_setcsr
or inline assembly) is considered immediate undefined behavior.See here for more info.
The text was updated successfully, but these errors were encountered: