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

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 07:59:22 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()) {
----------------
gchatelet wrote:

Anyways, I think returning the same NaN makes more sense so I've changed the code accordingly.

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


More information about the libc-commits mailing list