[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:34:28 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:

Is this required by the standard?
It is not explicit from the [cppreference article](https://en.cppreference.com/w/cpp/numeric/math/sqrt#:~:text=If%20the%20argument%20is%20NaN%2C%20NaN%20is%20returned.).


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


More information about the libc-commits mailing list