@@ -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
0 commit comments