# Equivalent of \`#pragma STDC FENV\_ACCESS ON\`

**URL:** <https://internals.rust-lang.org/t/equivalent-of-pragma-stdc-fenv-access-on/14934>\
**Category:** language design\
**Created:** [June 28, 2021, 9:09pm UTC](https://internals.rust-lang.org/t/equivalent-of-pragma-stdc-fenv-access-on/14934 "2021-06-28T21:09:16Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [June 29, 2021, 5:15pm UTC](https://internals.rust-lang.org/t/equivalent-of-pragma-stdc-fenv-access-on/14934/4 "2021-06-29T17:15:54Z")

</div>

> [@josh](#):
>
> In theory, we could do constant evaluation in a non-default floating-point mode, assuming we can accurately reflect the desired behavior.

Yeah, this would work with an "I know the floating point environment is some specific non-standard one" setting, though not with an "I don't know the floating point environment" setting.

In C, the standard `FENV_ACCESS` pragma is "I don't know the environment", but it seems there are some nonstandard command-line arguments for setting a specific environment: for example, GCC has `-mfp-rounding-mode` for rounding (but Clang doesn't), and Clang has `-fdenormal-fp-math` for denormal handling (but GCC doesn't).

In any case, it looks like LLVM IR supports both setting a specific rounding mode and exception behavior (using arguments to the constrained FP math intrinsics), and setting specific denormal behavior (using a function attribute). Or even if this was lacking in some way, we could always have _rustc_ assume a specific environment, but tell LLVM to be agnostic. This would be suboptimal but safe.

Setting a specific floating point environment does have major advantages: in addition to avoiding issues with `const` as you mentioned, it also avoids nerfing the optimizer. I'm not sure if LLVM supports all the same optimizations with constrained floating point intrinsics as it does with regular floating point instructions, but at least in theory it could, as long as the rounding mode and denormal handling is known, and exceptions are off. (Having exceptions on, or potentially on, inherently hinders optimizations since it makes floating point operations side-effecting.)

On the other hand, being environment agnostic has its own advantages:

- Avoids forcing the compiler to model every floating point environment someone might want to run code in. Right now, [we don't even perfectly simulate the _default_ FPU behavior across supported architectures](https://github.com/rust-lang/rust/issues/77745), though this can be fixed. When it comes to obscure nonstandard environment settings (there are things more obscure than disabling denormals 🙂), it may be easier to punt.

- Easier to write code that dynamically changes the environment. For example, in my actual use case (that I ended up writing in C), I'm just a shared library living in the same process as arbitrary other code and I want to be a good citizen. So my function changes the floating point environment, performs a batch of calculations (large enough that the penalty of changing isn't a big deal), then changes back before returning.

---

_[View the full topic](https://internals.rust-lang.org/t/equivalent-of-pragma-stdc-fenv-access-on/14934)._
