Skip to content

Commit 1d41543

Browse files
authored
[libc][math] Fix log1p SEGV with large inputs when FTZ/DAZ flags are set. (#115541)
1 parent 6beaa12 commit 1d41543

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

libc/src/math/generic/log1p.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,8 @@ constexpr Float128 BIG_COEFFS[4]{
822822
{Sign::NEG, -128, 0x80000000'00000000'00000000'00000000_u128},
823823
};
824824

825-
LIBC_INLINE double log1p_accurate(int e_x, int index,
826-
fputil::DoubleDouble m_x) {
825+
[[maybe_unused]] LIBC_INLINE double log1p_accurate(int e_x, int index,
826+
fputil::DoubleDouble m_x) {
827827
Float128 e_x_f128(static_cast<float>(e_x));
828828
Float128 sum = fputil::quick_mul(LOG_2, e_x_f128);
829829
sum = fputil::quick_add(sum, LOG_R1[index]);
@@ -882,7 +882,6 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
882882

883883
constexpr int EXP_BIAS = FPBits_t::EXP_BIAS;
884884
constexpr int FRACTION_LEN = FPBits_t::FRACTION_LEN;
885-
constexpr uint64_t FRACTION_MASK = FPBits_t::FRACTION_MASK;
886885
FPBits_t xbits(x);
887886
uint64_t x_u = xbits.uintval();
888887

@@ -954,12 +953,12 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
954953
// |x_dd.lo| < ulp(x_dd.hi)
955954

956955
FPBits_t xhi_bits(x_dd.hi);
956+
uint64_t xhi_frac = xhi_bits.get_mantissa();
957957
x_u = xhi_bits.uintval();
958958
// Range reduction:
959959
// Find k such that |x_hi - k * 2^-7| <= 2^-8.
960-
int idx =
961-
static_cast<int>(((x_u & FRACTION_MASK) + (1ULL << (FRACTION_LEN - 8))) >>
962-
(FRACTION_LEN - 7));
960+
int idx = static_cast<int>((xhi_frac + (1ULL << (FRACTION_LEN - 8))) >>
961+
(FRACTION_LEN - 7));
963962
int x_e = xhi_bits.get_exponent() + (idx >> 7);
964963
double e_x = static_cast<double>(x_e);
965964

@@ -974,17 +973,21 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
974973
constexpr double ERR_HI[2] = {0x1.0p-85, 0.0};
975974
double err_hi = ERR_HI[hi == 0.0];
976975

977-
// Scaling factior = 2^(-xh_bits.get_exponent())
978-
uint64_t s_u = (static_cast<uint64_t>(EXP_BIAS) << (FRACTION_LEN + 1)) -
979-
(x_u & FPBits_t::EXP_MASK);
980-
// When the exponent of x is 2^1023, its inverse, 2^(-1023), is subnormal.
981-
const double EXPONENT_CORRECTION[2] = {0.0, 0x1.0p-1023};
982-
double scaling = FPBits_t(s_u).get_val() + EXPONENT_CORRECTION[s_u == 0];
976+
// Scale x_dd by 2^(-xh_bits.get_exponent()).
977+
int64_t s_u = static_cast<int64_t>(x_u & FPBits_t::EXP_MASK) -
978+
(static_cast<int64_t>(EXP_BIAS) << FRACTION_LEN);
983979
// Normalize arguments:
984980
// 1 <= m_dd.hi < 2
985981
// |m_dd.lo| < 2^-52.
986982
// This is exact.
987-
fputil::DoubleDouble m_dd{scaling * x_dd.lo, scaling * x_dd.hi};
983+
uint64_t m_hi = FPBits_t::one().uintval() | xhi_frac;
984+
985+
uint64_t m_lo =
986+
FPBits_t(x_dd.lo).abs().get_val() > x_dd.hi * 0x1.0p-127
987+
? static_cast<uint64_t>(cpp::bit_cast<int64_t>(x_dd.lo) - s_u)
988+
: 0;
989+
990+
fputil::DoubleDouble m_dd{FPBits_t(m_lo).get_val(), FPBits_t(m_hi).get_val()};
988991

989992
// Perform range reduction:
990993
// r * m - 1 = r * (m_dd.hi + m_dd.lo) - 1

libc/test/src/math/smoke/log1p_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "test/UnitTest/FPMatcher.h"
1414
#include "test/UnitTest/Test.h"
1515

16-
#include <stdint.h>
17-
1816
using LlvmLibcLog1pTest = LIBC_NAMESPACE::testing::FPTest<double>;
1917

2018
TEST_F(LlvmLibcLog1pTest, SpecialNumbers) {
@@ -26,6 +24,9 @@ TEST_F(LlvmLibcLog1pTest, SpecialNumbers) {
2624
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::log1p(-0.0));
2725
EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1p(-1.0),
2826
FE_DIVBYZERO);
27+
28+
EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
29+
LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
2930
}
3031

3132
#ifdef LIBC_TEST_FTZ_DAZ
@@ -36,18 +37,24 @@ TEST_F(LlvmLibcLog1pTest, FTZMode) {
3637
ModifyMXCSR mxcsr(FTZ);
3738

3839
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
40+
EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
41+
LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
3942
}
4043

4144
TEST_F(LlvmLibcLog1pTest, DAZMode) {
4245
ModifyMXCSR mxcsr(DAZ);
4346

4447
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
48+
EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
49+
LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
4550
}
4651

4752
TEST_F(LlvmLibcLog1pTest, FTZDAZMode) {
4853
ModifyMXCSR mxcsr(FTZ | DAZ);
4954

5055
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
56+
EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
57+
LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
5158
}
5259

5360
#endif

0 commit comments

Comments
 (0)