[libc-commits] [libc] b629414 - [libc][NFC] Simplify logic in `sqrt` (#80426)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 2 08:21:33 PST 2024
Author: Guillaume Chatelet
Date: 2024-02-02T17:21:30+01:00
New Revision: b629414ae13d6dcd641e92d0389c5d4b7638a644
URL: https://github.com/llvm/llvm-project/commit/b629414ae13d6dcd641e92d0389c5d4b7638a644
DIFF: https://github.com/llvm/llvm-project/commit/b629414ae13d6dcd641e92d0389c5d4b7638a644.diff
LOG: [libc][NFC] Simplify logic in `sqrt` (#80426)
Added:
Modified:
libc/src/__support/FPUtil/generic/sqrt.h
libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
Removed:
################################################################################
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<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() || 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();
More information about the libc-commits
mailing list