[libc-commits] [libc] [llvm] [libc][math] Fix near-overflow and NaN handling in expm1 and expm1f. (PR #246103)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 25 14:46:38 PST 2026


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/246103

>From fab93da192e4b0b08c36849b65cbbda1c14a5e12 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 25 Nov 2026 22:46:25 +0000
Subject: [PATCH] [libc][math] Fix near-overflow and NaN handling in expm1 and
 expm1f.

- Clamp the `-2^(-hi)` exponent when `hi >= 1023` in `expm1` so `hi == 1024` near overflow does not construct `-Inf` and raise a spurious `FE_INVALID` in `exact_add`.
- Explicitly quiet `+/-sNaN` and raise `FE_INVALID` in `expm1` and `expm1f`, and use `fputil::round_result_slightly_up(-1.0f)` for large negative finite inputs in `expm1f` so `FE_INEXACT` is raised.
- Add smoke tests for `+/-sNaN`, `+/-qNaN`, and near-overflow `expm1` inputs, and sync CMake and Bazel dependencies.

Assisted-by: Gemini
---
 libc/src/__support/math/CMakeLists.txt        |  10 ++
 libc/src/__support/math/expm1.h               | 109 +++++++++++-------
 libc/src/__support/math/expm1f.h              |  68 ++++++-----
 libc/test/src/math/smoke/CMakeLists.txt       |   2 +
 libc/test/src/math/smoke/expm1_test.cpp       |  34 +++++-
 libc/test/src/math/smoke/expm1f_test.cpp      |  22 +++-
 .../libc/src/__support/math/BUILD.bazel       |   5 +
 7 files changed, 170 insertions(+), 80 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9a312dd0fded88..6af2fe79d51a8b 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4839,15 +4839,20 @@ add_header_library(
     expm1.h
   DEPENDS
     .exp_constants
+    libc.hdr.fenv_macros
+    libc.src.__support.common
     libc.src.__support.CPP.bit
     libc.src.__support.FPUtil.double_double
     libc.src.__support.FPUtil.dyadic_float
     libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.integer_literals
+    libc.src.__support.macros.config
     libc.src.__support.macros.optimization
+    libc.src.__support.sign
 )
 
 add_header_library(
@@ -4856,13 +4861,18 @@ add_header_library(
     expm1f.h
   DEPENDS
     .common_constants
+    libc.hdr.fenv_macros
+    libc.src.__support.common
     libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.nearest_integer
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.config
     libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.cpu_features
 )
 
 add_header_library(
diff --git a/libc/src/__support/math/expm1.h b/libc/src/__support/math/expm1.h
index ad1a6962e28c63..62c5e0110ad227 100644
--- a/libc/src/__support/math/expm1.h
+++ b/libc/src/__support/math/expm1.h
@@ -1,15 +1,22 @@
-//===-- Implementation header for expm1 -------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Double-precision expm1 function.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1_H
 
+#include "hdr/fenv_macros.h"
 #include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/double_double.h"
 #include "src/__support/FPUtil/dyadic_float.h"
@@ -21,6 +28,7 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
 #include "src/__support/math/exp_constants.h"
+#include "src/__support/sign.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -76,42 +84,44 @@ LIBC_INLINE double poly_approx_d(double dx) {
   return p;
 }
 
+// Taylor polynomial coefficients in double-double precision.
+LIBC_INLINE_VAR constexpr DoubleDouble COEFFS_DD[] = {
+    {0, 0x1p0},                                      // 1
+    {0, 0x1p-1},                                     // 1/2
+    {0x1.5555555555555p-57, 0x1.5555555555555p-3},   // 1/6
+    {0x1.5555555555555p-59, 0x1.5555555555555p-5},   // 1/24
+    {0x1.1111111111111p-63, 0x1.1111111111111p-7},   // 1/120
+    {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, // 1/720
+    {0x1.a01a01a01a01ap-73, 0x1.a01a01a01a01ap-13},  // 1/5040
+};
+
 // Polynomial approximation with double-double precision:
 // Return expm1(dx) / dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
 // For |dx| < 2^-13 + 2^-30:
 //   | output - expm1(dx) | < 2^-101
 LIBC_INLINE DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
-  // Taylor polynomial.
-  constexpr DoubleDouble COEFFS[] = {
-      {0, 0x1p0},                                      // 1
-      {0, 0x1p-1},                                     // 1/2
-      {0x1.5555555555555p-57, 0x1.5555555555555p-3},   // 1/6
-      {0x1.5555555555555p-59, 0x1.5555555555555p-5},   // 1/24
-      {0x1.1111111111111p-63, 0x1.1111111111111p-7},   // 1/120
-      {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, // 1/720
-      {0x1.a01a01a01a01ap-73, 0x1.a01a01a01a01ap-13},  // 1/5040
-  };
-
-  DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2],
-                                    COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]);
+  DoubleDouble p =
+      fputil::polyeval(dx, COEFFS_DD[0], COEFFS_DD[1], COEFFS_DD[2],
+                       COEFFS_DD[3], COEFFS_DD[4], COEFFS_DD[5], COEFFS_DD[6]);
   return p;
 }
 
+// Taylor polynomial coefficients in 128-bit precision.
+[[maybe_unused]] LIBC_INLINE_VAR constexpr DFloat128 COEFFS_128[]{
+    {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
+    {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5
+    {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6
+    {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24
+    {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120
+    {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720
+    {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/5040
+};
+
 // Polynomial approximation with 128-bit precision:
 // Return (exp(dx) - 1)/dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
 // For |dx| < 2^-13 + 2^-30:
 //   | output - exp(dx) | < 2^-126.
 [[maybe_unused]] LIBC_INLINE DFloat128 poly_approx_f128(const DFloat128 &dx) {
-  constexpr DFloat128 COEFFS_128[]{
-      {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
-      {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5
-      {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6
-      {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24
-      {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120
-      {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720
-      {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/5040
-  };
-
   DFloat128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1],
                                  COEFFS_128[2], COEFFS_128[3], COEFFS_128[4],
                                  COEFFS_128[5], COEFFS_128[6]);
@@ -227,40 +237,44 @@ LIBC_INLINE double set_exceptional(double x) {
   // |x| <= 2^-53.
   if (x_abs <= 0x3ca0'0000'0000'0000ULL) {
     // expm1(x) ~ x.
-
     if (LIBC_UNLIKELY(x_abs <= 0x0370'0000'0000'0000ULL)) {
       if (LIBC_UNLIKELY(x_abs == 0))
         return x;
-      // |x| <= 2^-968, need to scale up a bit before rounding, then scale it
-      // back down.
-      return 0x1.0p-200 * fputil::multiply_add(x, 0x1.0p+200, 0x1.0p-1022);
+      // For 0 < |x| <= 2^-968, scale up by 2^200 before rounding, then scale
+      // back down by 2^-200.
+      double scaled = fputil::multiply_add(x, 0x1.0p+200, 0x1.0p-1022);
+      return 0x1.0p-200 * scaled;
     }
 
     // 2^-968 < |x| <= 2^-53.
     return fputil::round_result_slightly_up(x);
   }
 
-  // x < log(2^-54) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan.
+  // x < log(2^-54) || x >= 0x1.62e42fefa39fp+9 or inf/nan.
 
   // x < log(2^-54) or -inf/nan
   if (x_u >= 0xc042'b708'8723'20e2ULL) {
-    // expm1(-Inf) = -1
-    if (xbits.is_inf())
-      return -1.0;
-
-    // exp(nan) = nan
-    if (xbits.is_nan())
+    if (xbits.is_inf_or_nan()) {
+      if (xbits.is_inf())
+        return -1.0;
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       return x;
+    }
 
     return fputil::round_result_slightly_up(-1.0);
   }
 
   // x >= round(log(MAX_NORMAL), D, RU) = 0x1.62e42fefa39fp+9 or +inf/nan
-  // x is finite
   if (x_u < 0x7ff0'0000'0000'0000ULL)
     return fputil::set_overflow<double>();
-  // x is +inf or nan
-  return x + FPBits::inf().get_val();
+  if (xbits.is_signaling_nan()) {
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
+  }
+  return x;
 }
 
 } // namespace expm1_internal
@@ -385,9 +399,12 @@ LIBC_INLINE double expm1(double x) {
 
   DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);
 
-  // -2^(-hi)
+  // Clamp -2^(-hi) to -2^-1022 when hi >= 1023 so the biased exponent stays >=
+  // 1; the difference from -2^(-hi) is <= 2^-1022 << ERR_DD.
+  int one_exp = (hi < FPBits::EXP_BIAS) ? (FPBits::EXP_BIAS - hi) : 1;
   double one_scaled =
-      FPBits::create_value(Sign::NEG, FPBits::EXP_BIAS - hi, 0).get_val();
+      FPBits::create_value(Sign::NEG, static_cast<uint16_t>(one_exp), 0)
+          .get_val();
 
   // 2^(mid1 + mid2) - 2^(-hi)
   DoubleDouble hi_part = x_is_neg ? fputil::exact_add(one_scaled, exp_mid.hi)
@@ -438,8 +455,10 @@ LIBC_INLINE double expm1(double x) {
 
   double err_d = cpp::bit_cast<double>(ERR_D + err);
 
-  double upper = hi_part.hi + (lo + err_d);
-  double lower = hi_part.hi + (lo - err_d);
+  double lo_u = lo + err_d;
+  double lo_l = lo - err_d;
+  double upper = hi_part.hi + lo_u;
+  double lower = hi_part.hi + lo_l;
 
 #ifdef DEBUGDEBUG
   std::cout << "=== FAST PASS ===\n"
@@ -480,8 +499,10 @@ LIBC_INLINE double expm1(double x) {
 #else
   double err_dd = cpp::bit_cast<double>(ERR_DD + err);
 
-  double upper_dd = r_dd.hi + (r_dd.lo + err_dd);
-  double lower_dd = r_dd.hi + (r_dd.lo - err_dd);
+  double r_lo_u = r_dd.lo + err_dd;
+  double r_lo_l = r_dd.lo - err_dd;
+  double upper_dd = r_dd.hi + r_lo_u;
+  double lower_dd = r_dd.hi + r_lo_l;
 
   if (LIBC_LIKELY(upper_dd == lower_dd)) {
     int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
diff --git a/libc/src/__support/math/expm1f.h b/libc/src/__support/math/expm1f.h
index cf10e256136153..603a46aaf232c5 100644
--- a/libc/src/__support/math/expm1f.h
+++ b/libc/src/__support/math/expm1f.h
@@ -1,14 +1,21 @@
-//===-- Implementation header for expm1f ------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Single-precision expm1f function.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F_H
 
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/except_value_utils.h"
 #include "src/__support/FPUtil/multiply_add.h"
@@ -25,8 +32,24 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
+namespace expm1f_internal {
+
+// Degree-8 minimax polynomial for (expm1(x) - x) / x^2 on [-2^-4, 2^-4].
+LIBC_INLINE_VAR constexpr double COEFFS[] = {
+    0x1p-1,
+    0x1.55555555557ddp-3,
+    0x1.55555555552fap-5,
+    0x1.111110fcd58b7p-7,
+    0x1.6c16c1717660bp-10,
+    0x1.a0241f0006d62p-13,
+    0x1.a01e3f8d3c06p-16,
+};
+
+} // namespace expm1f_internal
+
 LIBC_INLINE float expm1f(float x) {
   using namespace common_constants_internal;
+  using namespace expm1f_internal;
   using FPBits = typename fputil::FPBits<float>;
   FPBits xbits(x);
 
@@ -61,26 +84,28 @@ LIBC_INLINE float expm1f(float x) {
 
   // When |x| > 25*log(2), or nan
   if (LIBC_UNLIKELY(x_abs >= 0x418a'a123U)) {
-    // x < log(2^-25)
+    // x < log(2^-25) or -inf/nan
     if (xbits.is_neg()) {
-      // exp(-Inf) = 0
-      if (xbits.is_inf())
-        return -1.0f;
-      // exp(nan) = nan
-      if (xbits.is_nan())
+      if (xbits.is_inf_or_nan()) {
+        if (xbits.is_inf())
+          return -1.0f;
+        if (xbits.is_signaling_nan()) {
+          fputil::raise_except_if_required(FE_INVALID);
+          return FPBits::quiet_nan().get_val();
+        }
         return x;
-#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
-      int round_mode = fputil::quick_get_round();
-      if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO)
-        return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
-#endif
-      return -1.0f;
+      }
+      return fputil::round_result_slightly_up(-1.0f);
     } else {
-      // x >= 89 or nan
-      if (xbits.uintval() >= 0x42b2'0000) {
+      // x >= 89 or +inf/nan
+      if (xbits.uintval() >= 0x42b2'0000U) {
         if (xbits.uintval() < 0x7f80'0000U)
           return fputil::set_overflow<float>();
-        return x + FPBits::inf().get_val();
+        if (xbits.is_signaling_nan()) {
+          fputil::raise_except_if_required(FE_INVALID);
+          return FPBits::quiet_nan().get_val();
+        }
+        return x;
       }
     }
   }
@@ -118,20 +143,9 @@ LIBC_INLINE float expm1f(float x) {
 #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
     }
 
-    constexpr double COEFFS[] = {0x1p-1,
-                                 0x1.55555555557ddp-3,
-                                 0x1.55555555552fap-5,
-                                 0x1.111110fcd58b7p-7,
-                                 0x1.6c16c1717660bp-10,
-                                 0x1.a0241f0006d62p-13,
-                                 0x1.a01e3f8d3c06p-16};
-
     // 2^-25 <= |x| < 2^-4
     double xd = static_cast<double>(x);
     double xsq = xd * xd;
-    // Degree-8 minimax polynomial generated by Sollya with:
-    // > display = hexadecimal;
-    // > P = fpminimax((expm1(x) - x)/x^2, 6, [|D...|], [-2^-4, 2^-4]);
 
     double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]);
     double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]);
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 754430326df0b8..63cc59385a78d6 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4696,6 +4696,7 @@ add_fp_unittest(
     expm1_test.cpp
   DEPENDS
     libc.hdr.errno_macros
+    libc.hdr.fenv_macros
     libc.src.math.expm1
     libc.src.__support.FPUtil.fp_bits
 )
@@ -4708,6 +4709,7 @@ add_fp_unittest(
     expm1f_test.cpp
   DEPENDS
     libc.hdr.errno_macros
+    libc.hdr.fenv_macros
     libc.src.math.expm1f
     libc.src.__support.FPUtil.fp_bits
 )
diff --git a/libc/test/src/math/smoke/expm1_test.cpp b/libc/test/src/math/smoke/expm1_test.cpp
index 7d605c0d5b3d9c..6f1bf806e50d6f 100644
--- a/libc/test/src/math/smoke/expm1_test.cpp
+++ b/libc/test/src/math/smoke/expm1_test.cpp
@@ -1,14 +1,18 @@
-//===-- Unittests for e^x - 1 ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Smoke tests for expm1.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
-#include "hdr/math_macros.h"
-#include "hdr/stdint_proxy.h"
+#include "hdr/fenv_macros.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/math/expm1.h"
 #include "test/UnitTest/FPMatcher.h"
@@ -17,10 +21,20 @@
 using LlvmLibcExpm1Test = LIBC_NAMESPACE::testing::FPTest<double>;
 
 TEST_F(LlvmLibcExpm1Test, SpecialNumbers) {
+  const double neg_sNaN = LIBC_NAMESPACE::testing::opaque(
+      FPBits::signaling_nan(Sign::NEG).get_val());
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);
+  EXPECT_TRUE(FPBits(LIBC_NAMESPACE::expm1(sNaN)).is_quiet_nan());
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1(neg_sNaN),
+                              FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+  EXPECT_TRUE(FPBits(LIBC_NAMESPACE::expm1(neg_sNaN)).is_quiet_nan());
 
-  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::expm1(aNaN));
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1(aNaN), 0);
+  EXPECT_MATH_ERRNO(0);
+  EXPECT_FP_EQ_WITH_EXCEPTION(neg_aNaN, LIBC_NAMESPACE::expm1(neg_aNaN), 0);
   EXPECT_MATH_ERRNO(0);
   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::expm1(inf));
   EXPECT_MATH_ERRNO(0);
@@ -31,9 +45,19 @@ TEST_F(LlvmLibcExpm1Test, SpecialNumbers) {
                               FE_OVERFLOW);
   EXPECT_MATH_ERRNO(ERANGE);
 
+  // Near-overflow input with hi == 1024 must not raise spurious FE_INVALID.
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      0x1.fffcd1fb6a8dap+1023, LIBC_NAMESPACE::expm1(0x1.62e42f2421d7dp+9),
+      FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+  EXPECT_FP_EQ_ALL_ROUNDING(0x1.fffcd1fb6a8dap+1023, 0x1.fffcd1fb6a8dap+1023,
+                            0x1.fffcd1fb6a8d9p+1023, 0x1.fffcd1fb6a8d9p+1023,
+                            LIBC_NAMESPACE::expm1(0x1.62e42f2421d7dp+9));
+
   EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::expm1(zero));
   EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::expm1(neg_zero));
-  // |x| < 2^-53, expm1(x) = x
+
+  // |x| < 2^-53, expm1(x) ~ x.
   EXPECT_FP_EQ(-0x1.23456789abcdep-55,
                LIBC_NAMESPACE::expm1(-0x1.23456789abcdep-55));
   EXPECT_FP_EQ(0x1.23456789abcdep-55,
diff --git a/libc/test/src/math/smoke/expm1f_test.cpp b/libc/test/src/math/smoke/expm1f_test.cpp
index e923d5b09e08c3..49c2e1c796bd77 100644
--- a/libc/test/src/math/smoke/expm1f_test.cpp
+++ b/libc/test/src/math/smoke/expm1f_test.cpp
@@ -1,14 +1,18 @@
-//===-- Unittests for expm1f-----------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Smoke tests for expm1f.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
-#include "hdr/math_macros.h"
-#include "hdr/stdint_proxy.h"
+#include "hdr/fenv_macros.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/math/expm1f.h"
 #include "test/UnitTest/FPMatcher.h"
@@ -17,10 +21,20 @@
 using LlvmLibcExpm1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
 
 TEST_F(LlvmLibcExpm1fTest, SpecialNumbers) {
+  const float neg_sNaN = LIBC_NAMESPACE::testing::opaque(
+      FPBits::signaling_nan(Sign::NEG).get_val());
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1f(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);
+  EXPECT_TRUE(FPBits(LIBC_NAMESPACE::expm1f(sNaN)).is_quiet_nan());
 
-  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::expm1f(aNaN));
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1f(neg_sNaN),
+                              FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+  EXPECT_TRUE(FPBits(LIBC_NAMESPACE::expm1f(neg_sNaN)).is_quiet_nan());
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expm1f(aNaN), 0);
+  EXPECT_MATH_ERRNO(0);
+  EXPECT_FP_EQ_WITH_EXCEPTION(neg_aNaN, LIBC_NAMESPACE::expm1f(neg_aNaN), 0);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::expm1f(inf));
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
index a28e38504b88fe..f09c793f74bea7 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
@@ -5763,12 +5763,15 @@ libc_support_library(
     hdrs = ["expm1.h"],
     deps = [
         ":exp_constants",
+        "//libc/hdr:fenv_macros",
         "//libc/src/__support:common",
         "//libc/src/__support:integer_literals",
+        "//libc/src/__support:sign",
         "//libc/src/__support/CPP:bit",
         "//libc/src/__support/FPUtil:double_double",
         "//libc/src/__support/FPUtil:dyadic_float",
         "//libc/src/__support/FPUtil:except_value_utils",
+        "//libc/src/__support/FPUtil:fenv_impl",
         "//libc/src/__support/FPUtil:fp_bits",
         "//libc/src/__support/FPUtil:multiply_add",
         "//libc/src/__support/FPUtil:polyeval",
@@ -5782,8 +5785,10 @@ libc_support_library(
     hdrs = ["expm1f.h"],
     deps = [
         ":common_constants",
+        "//libc/hdr:fenv_macros",
         "//libc/src/__support:common",
         "//libc/src/__support/FPUtil:except_value_utils",
+        "//libc/src/__support/FPUtil:fenv_impl",
         "//libc/src/__support/FPUtil:fp_bits",
         "//libc/src/__support/FPUtil:multiply_add",
         "//libc/src/__support/FPUtil:nearest_integer",



More information about the libc-commits mailing list