Skip to content

Commit 9371d95

Browse files
committed
[libc] Improve performance of generic hypot when the exponent difference is sufficiently large.
Simplify the logic when the exponent difference is at least MantissaLength + 2, while still maintaining correct rounding for all rounding modes. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D119843
1 parent 097f0fd commit 9371d95

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

libc/src/__support/FPUtil/Hypot.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,38 +143,28 @@ static inline T hypot(T x, T y) {
143143
return y;
144144
}
145145

146+
uint16_t x_exp = x_bits.get_unbiased_exponent();
147+
uint16_t y_exp = y_bits.get_unbiased_exponent();
148+
uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);
149+
150+
if ((exp_diff >= MantissaWidth<T>::VALUE + 2) || (x == 0) || (y == 0)) {
151+
return abs(x) + abs(y);
152+
}
153+
146154
uint16_t a_exp, b_exp, out_exp;
147155
UIntType a_mant, b_mant;
148156
DUIntType a_mant_sq, b_mant_sq;
149157
bool sticky_bits;
150158

151-
if ((x_bits.get_unbiased_exponent() >=
152-
y_bits.get_unbiased_exponent() + MantissaWidth<T>::VALUE + 2) ||
153-
(y == 0)) {
154-
if ((y != 0) && (get_round() == FE_UPWARD)) {
155-
UIntType out_bits = FPBits_t(abs(x)).uintval();
156-
return T(FPBits_t(++out_bits));
157-
}
158-
return abs(x);
159-
} else if ((y_bits.get_unbiased_exponent() >=
160-
x_bits.get_unbiased_exponent() + MantissaWidth<T>::VALUE + 2) ||
161-
(x == 0)) {
162-
if ((x != 0) && (get_round() == FE_UPWARD)) {
163-
UIntType out_bits = FPBits_t(abs(y)).uintval();
164-
return T(FPBits_t(++out_bits));
165-
}
166-
return abs(y);
167-
}
168-
169159
if (abs(x) >= abs(y)) {
170-
a_exp = x_bits.get_unbiased_exponent();
160+
a_exp = x_exp;
171161
a_mant = x_bits.get_mantissa();
172-
b_exp = y_bits.get_unbiased_exponent();
162+
b_exp = y_exp;
173163
b_mant = y_bits.get_mantissa();
174164
} else {
175-
a_exp = y_bits.get_unbiased_exponent();
165+
a_exp = y_exp;
176166
a_mant = y_bits.get_mantissa();
177-
b_exp = x_bits.get_unbiased_exponent();
167+
b_exp = x_exp;
178168
b_mant = x_bits.get_mantissa();
179169
}
180170

0 commit comments

Comments
 (0)