Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions libc/src/math/generic/powf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,18 +657,19 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
uint32_t y_abs = ybits.abs().uintval();

///////// BEGIN - Check exceptional cases ////////////////////////////////////
// if x or y is signaling NaN
if (xbits.is_signaling_nan() || ybits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FloatBits::quiet_nan().get_val();
}

// The single precision number that is closest to 1 is (1 - 2^-24), which has
// log2(1 - 2^-24) ~ -1.715...p-24.
// So if |y| > 151 * 2^24, and x is finite:
// |y * log2(x)| = 0 or > 151.
// Hence x^y will either overflow or underflow if x is not zero.
if (LIBC_UNLIKELY((y_abs & 0x0007'ffff) == 0) || (y_abs > 0x4f170000)) {
// y is signaling NaN
if (xbits.is_signaling_nan() || ybits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FloatBits::quiet_nan().get_val();
}

// Exceptional exponents.
if (y == 0.0f)
return 1.0f;
Expand Down Expand Up @@ -741,8 +742,8 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
}
}
if (y_abs > 0x4f17'0000) {
// y is NaN
if (y_abs > 0x7f80'0000) {
// y is NaN
if (x_u == 0x3f80'0000) { // x = 1.0f
// pow(1, NaN) = 1
return 1.0f;
Expand All @@ -764,6 +765,12 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
// y is finite and non-zero.
if (LIBC_UNLIKELY(((x_u & 0x801f'ffffU) == 0) || x_u >= 0x7f80'0000U ||
x_u < 0x0080'0000U)) {
// if x is signaling NaN
if (xbits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FloatBits::quiet_nan().get_val();
}

switch (x_u) {
case 0x3f80'0000: // x = 1.0f
return 1.0f;
Expand Down Expand Up @@ -804,7 +811,6 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
}

if (x_abs > 0x7f80'0000) {
// x is NaN.
// pow (aNaN, 0) is already taken care above.
return x;
}
Expand Down
Loading