[libc-commits] [libc] [libc][math] Prevent spurious floating-point exceptions in fdim (PR #228749)
Chen Miao via libc-commits
libc-commits at lists.llvm.org
Sun Oct 18 03:12:23 PDT 2026
================
----------------
ChenMiaoi wrote:
I compared the ternary expression and the `if` statement using 80-bit `long double` on x86. Both examples retain the early-return checks for `isnan` and are compiled with Clang 23.1.0 and trunk at `-O2`, without `FENV_ACCESS` enabled.
1. Ternary expression (https://godbolt.org/z/3KTa4G5GY)
Both Clang 23.1.0 and trunk perform the subtraction unconditionally, then use a conditional move to select the return value. Therefore, even when `x <= y` and the final result is `+0`, `x - y` is still evaluated.
2. If statement (https://godbolt.org/z/9ThKTTG3G)
Clang 23.1.0 preserves the conditional branch and skips the subtraction when `x <= y`. However, trunk still generates an unconditional subtraction followed by a conditional move to select the result, so it does not avoid the unnecessary floating-point operation.
For the following four inputs, all four configurations return `+0`, but the floating-point exception flags differ:
| Input `(x, y)` | `if` / Clang 23.1.0 | `if` / trunk | Ternary / Clang 23.1.0 | Ternary / trunk |
|---|---|---|---|---|
| `(+inf, +inf)` | None | `FE_INVALID` | `FE_INVALID` | `FE_INVALID` |
| `(-inf, -inf)` | None | `FE_INVALID` | `FE_INVALID` | `FE_INVALID` |
| `(-max, -min_subnormal)` | None | `FE_INEXACT` | `FE_INEXACT` | `FE_INEXACT` |
| `(-max, +max)` | None | `FE_OVERFLOW + FE_INEXACT` | `FE_OVERFLOW + FE_INEXACT` | `FE_OVERFLOW + FE_INEXACT` |
https://github.com/llvm/llvm-project/pull/228749
More information about the libc-commits
mailing list