diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h index 702de3f04a9bf..9e8ed305eeca5 100644 --- a/libc/src/__support/FPUtil/generic/sqrt.h +++ b/libc/src/__support/FPUtil/generic/sqrt.h @@ -78,21 +78,16 @@ LIBC_INLINE cpp::enable_if_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() || bits.is_nan()) { + // sqrt(+Inf) = +Inf // sqrt(+0) = +0 // sqrt(-0) = -0 + // sqrt(NaN) = NaN + // sqrt(-NaN) = -NaN return x; } else if (bits.is_neg()) { - // sqrt( negative numbers ) = NaN + // sqrt(-Inf) = NaN + // sqrt(-x) = NaN return FLT_NAN; } else { int x_exp = bits.get_exponent(); diff --git a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h index 74a536c04817c..4b2af10256176 100644 --- a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h +++ b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h @@ -43,21 +43,16 @@ LIBC_INLINE long double sqrt(long double x) { LDBits bits(x); - if (bits.is_inf_or_nan()) { - if (bits.is_neg() && (bits.get_mantissa() == 0)) { - // sqrt(-Inf) = NaN - return LDNAN; - } else { - // sqrt(NaN) = NaN - // sqrt(+Inf) = +Inf - return x; - } - } else if (bits.is_zero()) { + if (bits == LDBits::inf(Sign::POS) || bits.is_zero() || bits.is_nan()) { + // sqrt(+Inf) = +Inf // sqrt(+0) = +0 // sqrt(-0) = -0 + // sqrt(NaN) = NaN + // sqrt(-NaN) = -NaN return x; } else if (bits.is_neg()) { - // sqrt( negative numbers ) = NaN + // sqrt(-Inf) = NaN + // sqrt(-x) = NaN return LDNAN; } else { int x_exp = bits.get_explicit_exponent();