Skip to content

Commit b629414

Browse files
authored
[libc][NFC] Simplify logic in sqrt (#80426)
1 parent 3050311 commit b629414

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

libc/src/__support/FPUtil/generic/sqrt.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,16 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
7878

7979
FPBits_t bits(x);
8080

81-
if (bits.is_inf_or_nan()) {
82-
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
83-
// sqrt(-Inf) = NaN
84-
return FLT_NAN;
85-
} else {
86-
// sqrt(NaN) = NaN
87-
// sqrt(+Inf) = +Inf
88-
return x;
89-
}
90-
} else if (bits.is_zero()) {
81+
if (bits == FPBits_t::inf(Sign::POS) || bits.is_zero() || bits.is_nan()) {
82+
// sqrt(+Inf) = +Inf
9183
// sqrt(+0) = +0
9284
// sqrt(-0) = -0
85+
// sqrt(NaN) = NaN
86+
// sqrt(-NaN) = -NaN
9387
return x;
9488
} else if (bits.is_neg()) {
95-
// sqrt( negative numbers ) = NaN
89+
// sqrt(-Inf) = NaN
90+
// sqrt(-x) = NaN
9691
return FLT_NAN;
9792
} else {
9893
int x_exp = bits.get_exponent();

libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,16 @@ LIBC_INLINE long double sqrt(long double x) {
4343

4444
LDBits bits(x);
4545

46-
if (bits.is_inf_or_nan()) {
47-
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
48-
// sqrt(-Inf) = NaN
49-
return LDNAN;
50-
} else {
51-
// sqrt(NaN) = NaN
52-
// sqrt(+Inf) = +Inf
53-
return x;
54-
}
55-
} else if (bits.is_zero()) {
46+
if (bits == LDBits::inf(Sign::POS) || bits.is_zero() || bits.is_nan()) {
47+
// sqrt(+Inf) = +Inf
5648
// sqrt(+0) = +0
5749
// sqrt(-0) = -0
50+
// sqrt(NaN) = NaN
51+
// sqrt(-NaN) = -NaN
5852
return x;
5953
} else if (bits.is_neg()) {
60-
// sqrt( negative numbers ) = NaN
54+
// sqrt(-Inf) = NaN
55+
// sqrt(-x) = NaN
6156
return LDNAN;
6257
} else {
6358
int x_exp = bits.get_explicit_exponent();

0 commit comments

Comments
 (0)