[libc-commits] [libc] [libc][NFC] Simplify logic in `sqrt` (PR #80426)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 08:15:47 PST 2024


================
@@ -78,20 +78,14 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
 
     FPBits_t bits(x);
 
-    if (bits.is_inf_or_nan()) {
-      if (bits.is_neg() && (bits.get_mantissa() == 0)) {
-        // sqrt(-Inf) = NaN
-        return FLT_NAN;
-      } else {
-        // sqrt(NaN) = NaN
-        // sqrt(+Inf) = +Inf
-        return x;
-      }
-    } else if (bits.is_zero()) {
+    if (bits == FPBits_t::inf(Sign::POS) || bits.is_zero()) {
+      // sqrt(+Inf) = +Inf
       // sqrt(+0) = +0
       // sqrt(-0) = -0
       return x;
-    } else if (bits.is_neg()) {
+    } else if (bits.is_inf_or_nan() || bits.is_neg()) {
----------------
lntue wrote:

Yes, returning NaN as-is helps with the signaling / quiet NaN semantic, and also it follows the IEEE 754's recommendation:
```
To facilitate propagation of diagnostic information contained in NaNs,
as much of that information as possible should be preserved in NaN 
results of operations.
```

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


More information about the libc-commits mailing list