[libc-commits] [libc] [libc][math] Prevent spurious floating-point exceptions in fdim (PR #228749)

via libc-commits libc-commits at lists.llvm.org
Sat Oct 17 06:35:14 PDT 2026


================

----------------
lntue wrote:

In your Godbolt example (https://godbolt.org/z/TY53Gdx46), the preceding isnan checks from fputil::fdim were omitted. When those early returns are included (as in the actual fputil::fdim implementation), changing the ternary to an if statement:    
  
```
  if (x <= y)
    return T(0);
  return x - y;
```  
keeps the branch and only executes `x - y` when `x > y`, even at -O2 and -O3 without FENV_ACCESS:
  
- x86_64: https://godbolt.org/z/Ye7rGG99j
- armhf (armv7 Clang -O3 also emits vcmp.f32 + bxls lr before vsub.f32)
  
So switching to the if statement should also fix the armhf QEMU test failure directly without needing LIBC_FENV_ACCESS_ON in `BasicOperations.h` or `libc/src/math/generic/fdim*.cpp` (which also avoids blocking inlining into callers that don't enable FENV_ACCESS).

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


More information about the libc-commits mailing list