[libc-commits] [libc] [libc][math] Qualify fdim funtions to constexpr (PR #194137)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Sat Apr 25 15:30:17 PDT 2026


================
@@ -117,10 +117,17 @@ add_or_sub(InType x, InType y) {
         // volatile prevents Clang from converting tmp to OutType and then
         // immediately back to InType before negating it, resulting in double
         // rounding.
-        volatile InType tmp = y;
-        if constexpr (IsSub)
-          tmp = -tmp;
-        return cast<OutType>(tmp);
+        if (cpp::is_constant_evaluated()) {
+          InType tmp = y;
+          if constexpr (IsSub)
+            tmp = -tmp;
+          return cast<OutType>(tmp);
+        } else {
+          volatile InType tmp = y;
+          if constexpr (IsSub)
+            tmp = -tmp;
+          return cast<OutType>(tmp);
+        }
----------------
bassiounix wrote:

I think this way is much cleaner
```suggestion
#ifdef LIBC_HAS_CONSTANT_EVALUATION
        InType tmp = y;
#else
        // volatile prevents Clang from converting tmp to OutType and then
        // immediately back to InType before negating it, resulting in double
        // rounding.
        volatile InType tmp = y;
#endif // LIBC_HAS_CONSTANT_EVALUATION
        if constexpr (IsSub)
          tmp = -tmp;
        return cast<OutType>(tmp);
```

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


More information about the libc-commits mailing list