Skip to content

[libc][math] Fix signaling NaN handling for math functions. #133347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 8, 2025

Conversation

wldfngrs
Copy link
Contributor

@wldfngrs wldfngrs commented Mar 28, 2025

Add tests for signaling NaNs, and fix function behavior for handling signaling NaN input.

Fixes #124812

@llvmbot llvmbot added the libc label Mar 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-libc

Author: wldfngrs (wldfngrs)

Changes

Add tests for signaling NaNs, and fix function behavior for handling signaling NaN input
#124812


Patch is 59.93 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/133347.diff

80 Files Affected:

  • (modified) libc/src/math/generic/CMakeLists.txt (+3)
  • (modified) libc/src/math/generic/acosf.cpp (+7)
  • (modified) libc/src/math/generic/asinf.cpp (+6)
  • (modified) libc/src/math/generic/asinhf.cpp (+7-1)
  • (modified) libc/src/math/generic/atan2.cpp (+5-1)
  • (modified) libc/src/math/generic/atan2f.cpp (+6-1)
  • (modified) libc/src/math/generic/atanhf.cpp (+4)
  • (modified) libc/src/math/generic/cos.cpp (+5-1)
  • (modified) libc/src/math/generic/cosf.cpp (+5)
  • (modified) libc/src/math/generic/cosf16.cpp (+5)
  • (modified) libc/src/math/generic/cospif.cpp (+5)
  • (modified) libc/src/math/generic/cospif16.cpp (+4)
  • (modified) libc/src/math/generic/erff.cpp (+4)
  • (modified) libc/src/math/generic/log1p.cpp (+6-1)
  • (modified) libc/src/math/generic/logf.cpp (+5)
  • (modified) libc/src/math/generic/pow.cpp (+5)
  • (modified) libc/src/math/generic/powf.cpp (+5)
  • (modified) libc/src/math/generic/sin.cpp (+5)
  • (modified) libc/src/math/generic/sincos.cpp (+6)
  • (modified) libc/src/math/generic/sincosf.cpp (+6)
  • (modified) libc/src/math/generic/sinf.cpp (+5)
  • (modified) libc/src/math/generic/sinf16.cpp (+5)
  • (modified) libc/src/math/generic/sinpif.cpp (+5)
  • (modified) libc/src/math/generic/sinpif16.cpp (+4)
  • (modified) libc/src/math/generic/tan.cpp (+4)
  • (modified) libc/src/math/generic/tanf.cpp (+5)
  • (modified) libc/src/math/generic/tanf16.cpp (+4)
  • (modified) libc/src/math/generic/tanpif16.cpp (+5)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+1)
  • (modified) libc/test/src/math/smoke/acosf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/acoshf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/asinf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/asinhf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/atan2_test.cpp (+9)
  • (modified) libc/test/src/math/smoke/atan2f_test.cpp (+9)
  • (modified) libc/test/src/math/smoke/atan_test.cpp (+3-1)
  • (modified) libc/test/src/math/smoke/atanf_test.cpp (+2)
  • (modified) libc/test/src/math/smoke/atanhf_test.cpp (+2-1)
  • (modified) libc/test/src/math/smoke/cbrt_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cbrtf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cos_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cosf16_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cosf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/coshf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cospif16_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/cospif_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/erff_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp10_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp10f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp10m1f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp2_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp2f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp2m1f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/exp_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/expf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/expm1_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/expm1f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log10_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log10f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log1p_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log1pf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log2_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log2f_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/log_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/logf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/pow_test.cpp (+40-1)
  • (modified) libc/test/src/math/smoke/powf_test.cpp (+43-1)
  • (modified) libc/test/src/math/smoke/sin_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/sincos_test.cpp (+5)
  • (modified) libc/test/src/math/smoke/sincosf_test.cpp (+5)
  • (modified) libc/test/src/math/smoke/sinf16_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/sinf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/sinhf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/sinpif16_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/sinpif_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/tan_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/tanf16_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/tanf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/tanhf_test.cpp (+3)
  • (modified) libc/test/src/math/smoke/tanpif16_test.cpp (+3)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 77c6244c5e5da..056f3e61b7e62 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4129,7 +4129,9 @@ add_entrypoint_object(
     atan2f_float.h
   DEPENDS
     .inv_trigf_utils
+    libc.hdr.fenv_macros
     libc.src.__support.FPUtil.double_double
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.nearest_integer
@@ -4147,6 +4149,7 @@ add_entrypoint_object(
   DEPENDS
     .atan_utils
     libc.src.__support.FPUtil.double_double
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.nearest_integer
diff --git a/libc/src/math/generic/acosf.cpp b/libc/src/math/generic/acosf.cpp
index 509a5ebc4973e..8dd6de2ce7474 100644
--- a/libc/src/math/generic/acosf.cpp
+++ b/libc/src/math/generic/acosf.cpp
@@ -84,10 +84,17 @@ LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
                           0x1.921fb6p+1f)
                     : /* x == 1.0f */ 0.0f;
 
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
+    // |x| <= +/-inf
     if (x_abs <= 0x7f80'0000U) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
     }
+
     return x + FPBits::quiet_nan().get_val();
   }
 
diff --git a/libc/src/math/generic/asinf.cpp b/libc/src/math/generic/asinf.cpp
index da854417e85fe..12383bf6dacae 100644
--- a/libc/src/math/generic/asinf.cpp
+++ b/libc/src/math/generic/asinf.cpp
@@ -108,10 +108,16 @@ LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
 
   // |x| > 1, return NaNs.
   if (LIBC_UNLIKELY(x_abs > 0x3f80'0000U)) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
     if (x_abs <= 0x7f80'0000U) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
     }
+
     return FPBits::quiet_nan().get_val();
   }
 
diff --git a/libc/src/math/generic/asinhf.cpp b/libc/src/math/generic/asinhf.cpp
index 37b87a821222a..0bb7065eb1cfe 100644
--- a/libc/src/math/generic/asinhf.cpp
+++ b/libc/src/math/generic/asinhf.cpp
@@ -61,8 +61,14 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
   };
 
   if (LIBC_UNLIKELY(x_abs >= 0x4bdd'65a5U)) {
-    if (LIBC_UNLIKELY(xbits.is_inf_or_nan()))
+    if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits_t::quiet_nan().get_val();
+      }
+
       return x;
+    }
 
     // Exceptional cases when x > 2^24.
     switch (x_abs) {
diff --git a/libc/src/math/generic/atan2.cpp b/libc/src/math/generic/atan2.cpp
index 8adfe3321a9ee..c757b744a91fc 100644
--- a/libc/src/math/generic/atan2.cpp
+++ b/libc/src/math/generic/atan2.cpp
@@ -8,6 +8,7 @@
 
 #include "src/math/atan2.h"
 #include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/double_double.h"
 #include "src/__support/FPUtil/multiply_add.h"
@@ -111,8 +112,11 @@ LLVM_LIBC_FUNCTION(double, atan2, (double y, double x)) {
   // Check for exceptional cases, whether inputs are 0, inf, nan, or close to
   // overflow, or close to underflow.
   if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) {
-    if (x_bits.is_nan() || y_bits.is_nan())
+    if (x_bits.is_nan() || y_bits.is_nan()) {
+      if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
+	      fputil::raise_except_if_required(FE_INVALID);
       return FPBits::quiet_nan().get_val();
+    }
     unsigned x_except = x == 0.0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
     unsigned y_except = y == 0.0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);
 
diff --git a/libc/src/math/generic/atan2f.cpp b/libc/src/math/generic/atan2f.cpp
index 726cae9c8462b..9b3397f2446a5 100644
--- a/libc/src/math/generic/atan2f.cpp
+++ b/libc/src/math/generic/atan2f.cpp
@@ -7,7 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/atan2f.h"
+#include "hdr/fenv_macros.h"
 #include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/PolyEval.h"
 #include "src/__support/FPUtil/double_double.h"
@@ -264,8 +266,11 @@ LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) {
   double den_d = static_cast<double>(den_f);
 
   if (LIBC_UNLIKELY(max_abs >= 0x7f80'0000U || num_d == 0.0)) {
-    if (x_bits.is_nan() || y_bits.is_nan())
+    if (x_bits.is_nan() || y_bits.is_nan()) {
+      if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
+	      fputil::raise_except_if_required(FE_INVALID);
       return FPBits::quiet_nan().get_val();
+    }
     double x_d = static_cast<double>(x);
     double y_d = static_cast<double>(y);
     size_t x_except = (x_d == 0.0) ? 0 : (x_abs == 0x7f80'0000 ? 2 : 1);
diff --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp
index a2051bd3e3e67..2149314d2f676 100644
--- a/libc/src/math/generic/atanhf.cpp
+++ b/libc/src/math/generic/atanhf.cpp
@@ -24,6 +24,10 @@ LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
   // |x| >= 1.0
   if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {
     if (xbits.is_nan()) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       return x;
     }
     // |x| == 1.0
diff --git a/libc/src/math/generic/cos.cpp b/libc/src/math/generic/cos.cpp
index b60082bf9c308..5da0f86812a89 100644
--- a/libc/src/math/generic/cos.cpp
+++ b/libc/src/math/generic/cos.cpp
@@ -65,7 +65,11 @@ LLVM_LIBC_FUNCTION(double, cos, (double x)) {
   } else {
     // Inf or NaN
     if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
-      // sin(+-Inf) = NaN
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+      // cos(+-Inf) = NaN
       if (xbits.get_mantissa() == 0) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/cosf.cpp b/libc/src/math/generic/cosf.cpp
index 6ea24f9ccd3fa..7cdae09869588 100644
--- a/libc/src/math/generic/cosf.cpp
+++ b/libc/src/math/generic/cosf.cpp
@@ -117,6 +117,11 @@ LLVM_LIBC_FUNCTION(float, cosf, (float x)) {
 
   // x is inf or nan.
   if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
     if (x_abs == 0x7f80'0000U) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/cosf16.cpp b/libc/src/math/generic/cosf16.cpp
index 4d42db981ce71..99bb03eb71426 100644
--- a/libc/src/math/generic/cosf16.cpp
+++ b/libc/src/math/generic/cosf16.cpp
@@ -67,6 +67,11 @@ LLVM_LIBC_FUNCTION(float16, cosf16, (float16 x)) {
 
   // cos(+/-inf) = NaN, and cos(NaN) = NaN
   if (xbits.is_inf_or_nan()) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
     if (xbits.is_inf()) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/cospif.cpp b/libc/src/math/generic/cospif.cpp
index 29566f4fceacf..5b6880f853b26 100644
--- a/libc/src/math/generic/cospif.cpp
+++ b/libc/src/math/generic/cospif.cpp
@@ -66,6 +66,11 @@ LLVM_LIBC_FUNCTION(float, cospif, (float x)) {
 
     // x is inf or nan.
     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
       if (x_abs == 0x7f80'0000U) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp
index ee74bdb4a3693..9dc25920d5cfe 100644
--- a/libc/src/math/generic/cospif16.cpp
+++ b/libc/src/math/generic/cospif16.cpp
@@ -54,6 +54,10 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
 
     // Check for NaN or infintiy values
     if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       // If value is equal to infinity
       if (x_abs == 0x7c00) {
         fputil::set_errno_if_required(EDOM);
diff --git a/libc/src/math/generic/erff.cpp b/libc/src/math/generic/erff.cpp
index 016afe4a68140..44607a52a2e57 100644
--- a/libc/src/math/generic/erff.cpp
+++ b/libc/src/math/generic/erff.cpp
@@ -135,6 +135,10 @@ LLVM_LIBC_FUNCTION(float, erff, (float x)) {
     int sign = xbits.is_neg() ? 1 : 0;
 
     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       return (x_abs > 0x7f80'0000) ? x : ONE[sign];
     }
 
diff --git a/libc/src/math/generic/log1p.cpp b/libc/src/math/generic/log1p.cpp
index 058409fed081d..09f465a6ba774 100644
--- a/libc/src/math/generic/log1p.cpp
+++ b/libc/src/math/generic/log1p.cpp
@@ -910,7 +910,12 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
           return FPBits_t::quiet_nan().get_val();
         }
         // x is +Inf or NaN
-        return x;
+        if (xbits.is_inf() && xbits.is_pos())
+          return x;
+
+        if (xbits.is_signaling_nan())
+          fputil::raise_except_if_required(FE_INVALID);
+        return FPBits_t::quiet_nan().get_val();
       }
       x_dd.hi = x;
     } else {
diff --git a/libc/src/math/generic/logf.cpp b/libc/src/math/generic/logf.cpp
index 032d658a941be..e8d2ba2cfe175 100644
--- a/libc/src/math/generic/logf.cpp
+++ b/libc/src/math/generic/logf.cpp
@@ -132,6 +132,11 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
         return FPBits::quiet_nan().get_val();
       }
       // x is +inf or nan
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
       return x;
     }
   }
diff --git a/libc/src/math/generic/pow.cpp b/libc/src/math/generic/pow.cpp
index 8a12934f6c4ba..43e99a7acf690 100644
--- a/libc/src/math/generic/pow.cpp
+++ b/libc/src/math/generic/pow.cpp
@@ -217,6 +217,11 @@ LLVM_LIBC_FUNCTION(double, pow, (double x, double y)) {
   uint64_t sign = 0;
 
   ///////// BEGIN - Check exceptional cases ////////////////////////////////////
+  // If x or y is signaling NaN
+  if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) {
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
+  }
 
   // The double precision number that is closest to 1 is (1 - 2^-53), which has
   //   log2(1 - 2^-53) ~ -1.715...p-53.
diff --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp
index 2d7deca3c77bb..2df1c57ed2d34 100644
--- a/libc/src/math/generic/powf.cpp
+++ b/libc/src/math/generic/powf.cpp
@@ -657,6 +657,11 @@ 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.
diff --git a/libc/src/math/generic/sin.cpp b/libc/src/math/generic/sin.cpp
index 4a58dcf4b173f..65ac82f158ba0 100644
--- a/libc/src/math/generic/sin.cpp
+++ b/libc/src/math/generic/sin.cpp
@@ -77,6 +77,11 @@ LLVM_LIBC_FUNCTION(double, sin, (double x)) {
     // Inf or NaN
     if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
       // sin(+-Inf) = NaN
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
       if (xbits.get_mantissa() == 0) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/sincos.cpp b/libc/src/math/generic/sincos.cpp
index 0ac2f7f997527..08c8a8298f029 100644
--- a/libc/src/math/generic/sincos.cpp
+++ b/libc/src/math/generic/sincos.cpp
@@ -85,6 +85,12 @@ LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
   } else {
     // Inf or NaN
     if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        *sin_x = *cos_x = FPBits::quiet_nan().get_val();
+        return;
+      }
+
       // sin(+-Inf) = NaN
       if (xbits.get_mantissa() == 0) {
         fputil::set_errno_if_required(EDOM);
diff --git a/libc/src/math/generic/sincosf.cpp b/libc/src/math/generic/sincosf.cpp
index 623ef636afb1e..9c7bf181e485e 100644
--- a/libc/src/math/generic/sincosf.cpp
+++ b/libc/src/math/generic/sincosf.cpp
@@ -145,6 +145,12 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
 
   // x is inf or nan.
   if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      *sinp = *cosp = FPBits::quiet_nan().get_val();
+      return;
+    }
+
     if (x_abs == 0x7f80'0000U) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/sinf.cpp b/libc/src/math/generic/sinf.cpp
index d27ce843a2c92..38ea56f5f28c6 100644
--- a/libc/src/math/generic/sinf.cpp
+++ b/libc/src/math/generic/sinf.cpp
@@ -136,6 +136,11 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
   if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
     if (x_abs == 0x7f80'0000U) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/sinf16.cpp b/libc/src/math/generic/sinf16.cpp
index 85e55a614588a..28debbd52a9a5 100644
--- a/libc/src/math/generic/sinf16.cpp
+++ b/libc/src/math/generic/sinf16.cpp
@@ -87,6 +87,11 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
   }
 
   if (xbits.is_inf_or_nan()) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
     if (xbits.is_inf()) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/sinpif.cpp b/libc/src/math/generic/sinpif.cpp
index f572ded06b25a..492689d594d90 100644
--- a/libc/src/math/generic/sinpif.cpp
+++ b/libc/src/math/generic/sinpif.cpp
@@ -83,6 +83,11 @@ LLVM_LIBC_FUNCTION(float, sinpif, (float x)) {
 
     // check for NaN values
     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
       if (x_abs == 0x7f80'0000U) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/sinpif16.cpp b/libc/src/math/generic/sinpif16.cpp
index 51ea595653b4d..68af484a6c5d3 100644
--- a/libc/src/math/generic/sinpif16.cpp
+++ b/libc/src/math/generic/sinpif16.cpp
@@ -50,6 +50,10 @@ LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) {
   if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
     // Check for NaN or infinity values
     if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       // If value is equal to infinity
       if (x_abs == 0x7c00) {
         fputil::set_errno_if_required(EDOM);
diff --git a/libc/src/math/generic/tan.cpp b/libc/src/math/generic/tan.cpp
index a899a2128d384..89b812cfc23a0 100644
--- a/libc/src/math/generic/tan.cpp
+++ b/libc/src/math/generic/tan.cpp
@@ -163,6 +163,10 @@ LLVM_LIBC_FUNCTION(double, tan, (double x)) {
   } else {
     // Inf or NaN
     if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
       // tan(+-Inf) = NaN
       if (xbits.get_mantissa() == 0) {
         fputil::set_errno_if_required(EDOM);
diff --git a/libc/src/math/generic/tanf.cpp b/libc/src/math/generic/tanf.cpp
index a15aa9796cbd8..ca5e35dca4c91 100644
--- a/libc/src/math/generic/tanf.cpp
+++ b/libc/src/math/generic/tanf.cpp
@@ -113,6 +113,11 @@ LLVM_LIBC_FUNCTION(float, tanf, (float x)) {
   if (LIBC_UNLIKELY(x_abs > 0x4d56'd354U)) {
     // Inf or NaN
     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
       if (x_abs == 0x7f80'0000U) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/src/math/generic/tanf16.cpp b/libc/src/math/generic/tanf16.cpp
index 97d201b65bbe6..229f4a363670b 100644
--- a/libc/src/math/generic/tanf16.cpp
+++ b/libc/src/math/generic/tanf16.cpp
@@ -84,6 +84,10 @@ LLVM_LIBC_FUNCTION(float16, tanf16, (float16 x)) {
 
   // tan(+/-inf) = NaN, and tan(NaN) = NaN
   if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
     // x = +/-inf
     if (x_abs == 0x7c00) {
       fputil::set_errno_if_required(EDOM);
diff --git a/libc/src/math/generic/tanpif16.cpp b/libc/src/math/generic/tanpif16.cpp
index 71cf25c9741a1..792d405b1bb9e 100644
--- a/libc/src/math/generic/tanpif16.cpp
+++ b/libc/src/math/generic/tanpif16.cpp
@@ -63,6 +63,11 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) {
   if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
     // Check for NaN or infinity values
     if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+      // is inf
       if (x_abs == 0x7c00) {
         fputil::set_errno_if_required(EDOM);
         fputil::raise_except_if_required(FE_INVALID);
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 660b68687d63c..54f5e40cdce11 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4228,6 +4228,7 @@ add_fp_unittest(
   SRCS
     pow_test.cpp
   DEPENDS
+    libc.src.errno.errno
     libc.hdr.fenv_macros
     libc.src.math.pow
 )
diff --git a/libc/test/src/math/smoke/acosf_test.cpp b/libc/test/src/math/smoke/acosf_test.cpp
index e5d56c70f2722..74f68e00011aa 100644
--- a/libc/test/src/math/smoke/acosf_test.cpp
+++ b/libc/test/src/math/smoke/acosf_test.cpp
@@ -20,6 +20,9 @@ using LlvmLibcAcosfTest = LIBC_NAMESPACE::testing::FPTest<float>;
 TEST_F(LlvmLibcAcosfTest, SpecialNumbers) {
   LIBC_NAMESPACE::libc_errno = 0;
 
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acosf(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(aNaN));
   EXPECT_MATH_ERRNO(0);
 
diff --git a/libc/test/src/math/smoke/acoshf_test.cpp b/libc/test/src/math/smoke/acoshf_test.cpp
index c4e88259919c3..c5ba88055ac57 100644
--- a/libc/test/src/math/smoke/acoshf_test.cpp
+++ b/libc/test/src/math/smoke/acoshf_test.cpp
@@ -20,6 +20,9 @@ using LlvmLibcAcoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
 TEST_F(LlvmLibcAcoshfTest, SpecialNumbers) {
   LIBC_NAMESP...
[truncated]

Copy link

github-actions bot commented Mar 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@wldfngrs
Copy link
Contributor Author

@lntue, @overmighty please review

@wldfngrs
Copy link
Contributor Author

@jhuber6 Regarding the botched PR, I wasn't paying close attention to what I was committing, my bad!

@lntue lntue changed the title Fix #124812 issue [libc][math] Fix signaling NaN handling for math functions. Mar 28, 2025
@lntue lntue requested review from lntue and overmighty March 28, 2025 11:56
@@ -657,6 +657,11 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this one inside the condition:

if (LIBC_UNLIKELY((y_abs & 0x0007'ffff) == 0) || (y_abs > 0x4f170000)) {

and add another one, that only check if xbits.is_signaling_nan inside the condition:

  if (LIBC_UNLIKELY(((x_u & 0x801f'ffffU) == 0) || x_u >= 0x7f80'0000U ||
                    x_u < 0x0080'0000U)) {

Copy link
Member

@overmighty overmighty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to change other math functions in this same PR? The title says functions in plural.

@wldfngrs
Copy link
Contributor Author

wldfngrs commented Apr 1, 2025

Are you planning to change other math functions in this same PR? The title says functions in plural.

I'm confused. I updated about 80 files?

@overmighty
Copy link
Member

Oops, I guess I was only viewing the changes from the last commit in the "Files changed" tab instead of the changes from all commits.

@@ -7,7 +7,9 @@
//===----------------------------------------------------------------------===//

#include "src/math/atan2f.h"
#include "hdr/fenv_macros.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other .cpp files edited by this PR that use FE_INVALID but don't include hdr/fenv_macros.h. Maybe we should leave this for another PR though. Thoughts @lntue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@overmighty overmighty merged commit fdf2094 into llvm:main Apr 8, 2025
16 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 8, 2025

LLVM Buildbot has detected a new failure on builder libc-aarch64-ubuntu-dbg running on libc-aarch64-ubuntu while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/104/builds/19829

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[561/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.log10f_test.__unit__.__build__
[562/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.atanf_test.__unit__.__build__
[563/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.log1p_test.__unit__.__build__
[564/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT.__build__
[565/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cos_test.__unit__.__build__
[566/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp10_test.__unit__.__build__.dir/exp10_test.cpp.o
[567/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[568/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.coshf_test.__unit__.__build__.dir/coshf_test.cpp.o
[569/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp2f_test.__unit__.__build__.dir/exp2f_test.cpp.o
[570/1289] Running unit test libc.test.src.math.smoke.sinpif16_test.__unit__
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif16_test.__unit__ 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/build/libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSinpif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/test/src/math/smoke/sinpif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((1 | 2 | 4 | 8 | 16))) & ((1) ? (1) : static_cast<int>((1 | 2 | 4 | 8 | 16)))
      Which is: 0
To be equal to: (1)
      Which is: 1
[  FAILED  ] LlvmLibcSinpif16Test.SpecialNumbers
[ RUN      ] LlvmLibcSinpif16Test.Integers
[       OK ] LlvmLibcSinpif16Test.Integers (9 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[571/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinf_test.__unit__.__NO_ROUND_OPT.__build__.dir/sinf_test.cpp.o
[572/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp10_test.__unit__.__build__
[573/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif_test.__unit__.__build__.dir/sinpif_test.cpp.o
[574/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cospif_test.__unit__.__build__.dir/cospif_test.cpp.o
[575/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp2f_test.__unit__.__build__
[576/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.coshf_test.__unit__.__build__
[577/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.tanf_test.__unit__.__build__.dir/tanf_test.cpp.o
[578/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.atanhf_test.__unit__.__NO_ROUND_OPT.__build__.dir/atanhf_test.cpp.o
[579/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sincosf_test.__unit__.__build__.dir/sincosf_test.cpp.o
[580/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.coshf_test.__unit__.__NO_ROUND_OPT.__build__.dir/coshf_test.cpp.o
[581/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.tan_test.__unit__.__NO_ROUND_OPT.__build__.dir/tan_test.cpp.o
[582/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cos_test.__unit__.__NO_ROUND_OPT.__build__.dir/cos_test.cpp.o
[583/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sincosf_test.__unit__.__NO_ROUND_OPT.__build__.dir/sincosf_test.cpp.o
[584/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinf_test.__unit__.__build__.dir/sinf_test.cpp.o
[585/1289] Running unit test libc.test.src.math.lrintl_test.__unit__
[==========] Running 6 tests from 1 test suite.
[ RUN      ] LlvmLibcRoundToIntegerTest.InfinityAndNaN
[       OK ] LlvmLibcRoundToIntegerTest.InfinityAndNaN (7 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.RoundNumbers
[       OK ] LlvmLibcRoundToIntegerTest.RoundNumbers (121 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.Fractions
[       OK ] LlvmLibcRoundToIntegerTest.Fractions (26 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.IntegerOverflow
[       OK ] LlvmLibcRoundToIntegerTest.IntegerOverflow (25 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.SubnormalRange
[       OK ] LlvmLibcRoundToIntegerTest.SubnormalRange (1080 ms)
Step 7 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[561/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.log10f_test.__unit__.__build__
[562/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.atanf_test.__unit__.__build__
[563/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.log1p_test.__unit__.__build__
[564/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT.__build__
[565/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cos_test.__unit__.__build__
[566/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp10_test.__unit__.__build__.dir/exp10_test.cpp.o
[567/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[568/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.coshf_test.__unit__.__build__.dir/coshf_test.cpp.o
[569/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp2f_test.__unit__.__build__.dir/exp2f_test.cpp.o
[570/1289] Running unit test libc.test.src.math.smoke.sinpif16_test.__unit__
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif16_test.__unit__ 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/build/libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSinpif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/test/src/math/smoke/sinpif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((1 | 2 | 4 | 8 | 16))) & ((1) ? (1) : static_cast<int>((1 | 2 | 4 | 8 | 16)))
      Which is: 0
To be equal to: (1)
      Which is: 1
[  FAILED  ] LlvmLibcSinpif16Test.SpecialNumbers
[ RUN      ] LlvmLibcSinpif16Test.Integers
[       OK ] LlvmLibcSinpif16Test.Integers (9 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[571/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinf_test.__unit__.__NO_ROUND_OPT.__build__.dir/sinf_test.cpp.o
[572/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp10_test.__unit__.__build__
[573/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif_test.__unit__.__build__.dir/sinpif_test.cpp.o
[574/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cospif_test.__unit__.__build__.dir/cospif_test.cpp.o
[575/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp2f_test.__unit__.__build__
[576/1289] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.coshf_test.__unit__.__build__
[577/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.tanf_test.__unit__.__build__.dir/tanf_test.cpp.o
[578/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.atanhf_test.__unit__.__NO_ROUND_OPT.__build__.dir/atanhf_test.cpp.o
[579/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sincosf_test.__unit__.__build__.dir/sincosf_test.cpp.o
[580/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.coshf_test.__unit__.__NO_ROUND_OPT.__build__.dir/coshf_test.cpp.o
[581/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.tan_test.__unit__.__NO_ROUND_OPT.__build__.dir/tan_test.cpp.o
[582/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cos_test.__unit__.__NO_ROUND_OPT.__build__.dir/cos_test.cpp.o
[583/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sincosf_test.__unit__.__NO_ROUND_OPT.__build__.dir/sincosf_test.cpp.o
[584/1289] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinf_test.__unit__.__build__.dir/sinf_test.cpp.o
[585/1289] Running unit test libc.test.src.math.lrintl_test.__unit__
[==========] Running 6 tests from 1 test suite.
[ RUN      ] LlvmLibcRoundToIntegerTest.InfinityAndNaN
[       OK ] LlvmLibcRoundToIntegerTest.InfinityAndNaN (7 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.RoundNumbers
[       OK ] LlvmLibcRoundToIntegerTest.RoundNumbers (121 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.Fractions
[       OK ] LlvmLibcRoundToIntegerTest.Fractions (26 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.IntegerOverflow
[       OK ] LlvmLibcRoundToIntegerTest.IntegerOverflow (25 us)
[ RUN      ] LlvmLibcRoundToIntegerTest.SubnormalRange
[       OK ] LlvmLibcRoundToIntegerTest.SubnormalRange (1080 ms)

@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 8, 2025

LLVM Buildbot has detected a new failure on builder libc-aarch64-ubuntu-fullbuild-dbg running on libc-aarch64-ubuntu while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/71/builds/19841

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[451/1190] Running unit test libc.test.src.math.smoke.log1pf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcLog1pfTest.SpecialNumbers
[       OK ] LlvmLibcLog1pfTest.SpecialNumbers (8 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[452/1190] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif_test.__unit__.__build__.dir/sinpif_test.cpp.o
[453/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[454/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp10_test.__unit__.__build__
[455/1190] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp10_test.__unit__.__NO_ROUND_OPT.__build__.dir/exp10_test.cpp.o
[456/1190] Running unit test libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcCospif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/test/src/math/smoke/cospif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10))) & ((0x4) ? (0x4) : static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10)))
      Which is: 0
To be equal to: (0x4)
      Which is: 4
[  FAILED  ] LlvmLibcCospif16Test.SpecialNumbers
[ RUN      ] LlvmLibcCospif16Test.Integers
[       OK ] LlvmLibcCospif16Test.Integers (15 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[457/1190] Running unit test libc.test.src.math.smoke.sinpif16_test.__unit__
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif16_test.__unit__ 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSinpif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/test/src/math/smoke/sinpif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10))) & ((0x4) ? (0x4) : static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10)))
      Which is: 0
To be equal to: (0x4)
      Which is: 4
[  FAILED  ] LlvmLibcSinpif16Test.SpecialNumbers
[ RUN      ] LlvmLibcSinpif16Test.Integers
[       OK ] LlvmLibcSinpif16Test.Integers (8 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[458/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.tanhf_test.__unit__.__NO_ROUND_OPT.__build__
[459/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cosf_test.__unit__.__build__
[460/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sincosf_test.__unit__.__build__
[461/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinhf_test.__unit__.__build__
[462/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif_test.__unit__.__build__
[463/1190] Running unit test libc.test.src.math.smoke.logbf128_test.__unit__
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcLogbTest.SpecialNumbers
[       OK ] LlvmLibcLogbTest.SpecialNumbers (5 us)
[ RUN      ] LlvmLibcLogbTest.PowersOfTwo
[       OK ] LlvmLibcLogbTest.PowersOfTwo (7 us)
[ RUN      ] LlvmLibcLogbTest.SomeIntegers
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[451/1190] Running unit test libc.test.src.math.smoke.log1pf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcLog1pfTest.SpecialNumbers
[       OK ] LlvmLibcLog1pfTest.SpecialNumbers (8 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[452/1190] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif_test.__unit__.__build__.dir/sinpif_test.cpp.o
[453/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[454/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.exp10_test.__unit__.__build__
[455/1190] Building CXX object libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.exp10_test.__unit__.__NO_ROUND_OPT.__build__.dir/exp10_test.cpp.o
[456/1190] Running unit test libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke/libc.test.src.math.smoke.cospif16_test.__unit__.__NO_ROUND_OPT.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcCospif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/test/src/math/smoke/cospif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10))) & ((0x4) ? (0x4) : static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10)))
      Which is: 0
To be equal to: (0x4)
      Which is: 4
[  FAILED  ] LlvmLibcCospif16Test.SpecialNumbers
[ RUN      ] LlvmLibcCospif16Test.Integers
[       OK ] LlvmLibcCospif16Test.Integers (15 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[457/1190] Running unit test libc.test.src.math.smoke.sinpif16_test.__unit__
FAILED: libc/test/src/math/smoke/CMakeFiles/libc.test.src.math.smoke.sinpif16_test.__unit__ 
cd /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke && /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/build/libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif16_test.__unit__.__build__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcSinpif16Test.SpecialNumbers
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/test/src/math/smoke/sinpif16_test.cpp:20: FAILURE
      Expected: __llvm_libc_20_0_0_git::fputil::test_except( static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10))) & ((0x4) ? (0x4) : static_cast<int>((0x1 | 0x2 | 0x4 | 0x8 | 0x10)))
      Which is: 0
To be equal to: (0x4)
      Which is: 4
[  FAILED  ] LlvmLibcSinpif16Test.SpecialNumbers
[ RUN      ] LlvmLibcSinpif16Test.Integers
[       OK ] LlvmLibcSinpif16Test.Integers (8 us)
Ran 2 tests.  PASS: 1  FAIL: 1
[458/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.tanhf_test.__unit__.__NO_ROUND_OPT.__build__
[459/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.cosf_test.__unit__.__build__
[460/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sincosf_test.__unit__.__build__
[461/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinhf_test.__unit__.__build__
[462/1190] Linking CXX executable libc/test/src/math/smoke/libc.test.src.math.smoke.sinpif_test.__unit__.__build__
[463/1190] Running unit test libc.test.src.math.smoke.logbf128_test.__unit__
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcLogbTest.SpecialNumbers
[       OK ] LlvmLibcLogbTest.SpecialNumbers (5 us)
[ RUN      ] LlvmLibcLogbTest.PowersOfTwo
[       OK ] LlvmLibcLogbTest.PowersOfTwo (7 us)
[ RUN      ] LlvmLibcLogbTest.SomeIntegers

@wldfngrs wldfngrs deleted the fix_sNaN branch May 24, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc][math] Fix signaling nans and floating point exceptions for some math functions.
5 participants