[libc-commits] [libc] [libc] Prevent constant propagation for atanf(+-Inf) in gcc. (PR #85733)

via libc-commits libc-commits at lists.llvm.org
Tue Mar 19 09:21:15 PDT 2024


lntue wrote:

> https://lemire.me/blog/2022/11/16/a-fast-function-to-check-your-floating-point-rounding-mode/ also recommends the use of `volatile`. So does https://www.ibm.com/docs/en/xl-c-and-cpp-linux/11.1.0?topic=rounding-matching-compile-time-runtime-modes.
Yes, we have similar utilities implemented for free-standing rounding mode detection: https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/rounding_mode.h
 
> Looks like Hans Boehm wrote a paper on this issue (C++26): https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2746r0.pdf
>From my understanding, Hans proposed to have a set of directed-rounding operations, and migrating people to use that instead of manipulating the rounding environment.  Then `fegetround` and `fesetround` can be deprecated, and everything else can assume rounding-to-nearest by default.  Until `fegetround` and `fesetround` are deprecated, we still have to respect the current rounding mode environment.  And in fact, in the transition period, our implementation can be used to implement directed-rounding operation by wrapping `fesetround` before and after.

> There's an old clang PR that never landed for a pragma to adjust this behavior at compile time: https://reviews.llvm.org/D125625
Yes, I saw that pragma is used in many places, but unfortunately it's not portable to clang.
 
> You might want to speak with Hans and ask him what the current state of that proposal is.
+1
 
> Another approach, would it be possible to have another array similar to `SIGNED_PI_OVER_2` but of floats with different values pre-rounded a certain way to avoid needing `volatile`? Or is it literally that calling `atanf` for the same input with different rounding modes is expected to produce different results?
To have an array of pre-rounded results, we still need to access the current rounding mode at runtime, which leads to either `fegetround` heavy-weight or the same volatile usage in https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/rounding_mode.h

Another way to do it is to use `fputil::round_result_slightly_up` and `fputil::round_result_slightly_down` as in setting other exceptional values https://github.com/llvm/llvm-project/blob/main/libc/src/math/generic/atanf.cpp#L87, which is pretty much the same: https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/except_value_utils.h#L103


https://github.com/llvm/llvm-project/pull/85733


More information about the libc-commits mailing list