[libc-commits] [libc] [llvm] [libc][math] Add floating-point comparison macros. (PR #243957)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 19:47:09 PST 2026


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

>From c4b1e0dc34e78541f45e28df550bdfc9a8d61182 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 18 Nov 2026 18:11:11 +0000
Subject: [PATCH 1/4] [libc][math] Define C99 floating-point comparison macros
 in math.h.

Add `isgreater`, `isgreaterequal`, `isless`, `islessequal`,
`islessgreater`, and `isunordered` to `math-function-macros.h` using
the corresponding `__builtin_is*` intrinsics, along with C and C++
include unit tests.
---
 libc/docs/dev/builtin_compatibility.md        |   6 +
 .../llvm-libc-macros/math-function-macros.h   |  13 +-
 libc/test/include/CMakeLists.txt              |  27 ++++
 .../include/math_comparison_macros_test.c     |  46 ++++++
 .../include/math_comparison_macros_test.cpp   | 133 ++++++++++++++++++
 libc/utils/docgen/docgen_test.py              |   5 +-
 .../libc/test/include/BUILD.bazel             |  22 +++
 7 files changed, 250 insertions(+), 2 deletions(-)
 create mode 100644 libc/test/include/math_comparison_macros_test.c
 create mode 100644 libc/test/include/math_comparison_macros_test.cpp

diff --git a/libc/docs/dev/builtin_compatibility.md b/libc/docs/dev/builtin_compatibility.md
index 1f3f52c7f90716..30df0a539f2752 100644
--- a/libc/docs/dev/builtin_compatibility.md
+++ b/libc/docs/dev/builtin_compatibility.md
@@ -108,9 +108,15 @@ basepoint that ships each one.
 | `__builtin_is_aligned`               |             |             |             |             |             |             |             |             |             |             |             |             |             |
 | `__builtin_is_constant_evaluated`    |             |             |             |             |             | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_isfinite`                 | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_isgreater`                | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_isgreaterequal`           | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_isinf`                    | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_isless`                   | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_islessequal`              | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_islessgreater`            | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_isnan`                    | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_isnormal`                 | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
+| `__builtin_isunordered`              | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_launder`                  |             |             |             | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_ldexp`                    | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
 | `__builtin_ldexpf`                   |             |             |             |             |             |             |             | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} | {{ check }} |
diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 1fbcd603d2045c..629295e5ded4ec 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -1,10 +1,15 @@
-//===-- Definition of function macros from math.h -------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Definitions of function-like macros from math.h.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
 #define LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
@@ -57,5 +62,11 @@
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
 #define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
+#define isgreater(x, y) __builtin_isgreater(x, y)
+#define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
+#define isless(x, y) __builtin_isless(x, y)
+#define islessequal(x, y) __builtin_islessequal(x, y)
+#define islessgreater(x, y) __builtin_islessgreater(x, y)
+#define isunordered(x, y) __builtin_isunordered(x, y)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 7f6706e2621e66..d44c0250aca6af 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -421,6 +421,33 @@ add_libc_test(
     libc.include.llvm-libc-macros.math_function_macros
 )
 
+add_fp_unittest(
+  math_comparison_macros_test
+  SUITE
+    libc-include-tests
+  SRCS
+    math_comparison_macros_test.cpp
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.include.llvm-libc-macros.math_function_macros
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.macros.properties.types
+)
+
+add_libc_test(
+  math_comparison_macros_c_test
+  C_TEST
+  SUITE
+    libc-include-tests
+  SRCS
+    math_comparison_macros_test.c
+  COMPILE_OPTIONS
+    -Wall
+    -Werror
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 # TODO(https://github.com/llvm/llvm-project/issues/114618): fix linkage failures
 # add_libc_test(
 #   issignaling_c_test
diff --git a/libc/test/include/math_comparison_macros_test.c b/libc/test/include/math_comparison_macros_test.c
new file mode 100644
index 00000000000000..bfc522ed6758e1
--- /dev/null
+++ b/libc/test/include/math_comparison_macros_test.c
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for math comparison macros in C.
+///
+//===----------------------------------------------------------------------===//
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+#include "test/UnitTest/LibcCTest.h"
+
+#if !defined(isgreater) || !defined(isgreaterequal) || !defined(isless) ||     \
+    !defined(islessequal) || !defined(islessgreater) || !defined(isunordered)
+#error "One or more math comparison macros are not defined"
+#else
+TEST(math_comparison_macros) {
+  EXPECT_TRUE(isgreater(2.0f, 1.0f) == 1);
+  EXPECT_TRUE(isgreater(1.0, 2.0) == 0);
+  EXPECT_TRUE(isgreater(__builtin_nan(""), 1.0) == 0);
+
+  EXPECT_TRUE(isgreaterequal(1.0, 1.0) == 1);
+  EXPECT_TRUE(isgreaterequal(0.0, -0.0) == 1);
+  EXPECT_TRUE(isgreaterequal(__builtin_nanf(""), 0.0f) == 0);
+
+  EXPECT_TRUE(isless(-1.0L, 0.0L) == 1);
+  EXPECT_TRUE(isless(0.0f, -0.0f) == 0);
+  EXPECT_TRUE(isless(1.0, __builtin_nan("")) == 0);
+
+  EXPECT_TRUE(islessequal(-0.0, 0.0) == 1);
+  EXPECT_TRUE(islessequal(2.0f, 1.0) == 0);
+  EXPECT_TRUE(islessequal(__builtin_nanl(""), 1.0L) == 0);
+
+  EXPECT_TRUE(islessgreater(1.0f, -1.0f) == 1);
+  EXPECT_TRUE(islessgreater(0.0, -0.0) == 0);
+  EXPECT_TRUE(islessgreater(__builtin_nan(""), 0.0) == 0);
+
+  EXPECT_TRUE(isunordered(__builtin_nanf(""), 1.0f) == 1);
+  EXPECT_TRUE(isunordered(1.0, __builtin_nan("")) == 1);
+  EXPECT_TRUE(isunordered(1.0L, -1.0L) == 0);
+}
+#endif
diff --git a/libc/test/include/math_comparison_macros_test.cpp b/libc/test/include/math_comparison_macros_test.cpp
new file mode 100644
index 00000000000000..3aa6d45b92e3ed
--- /dev/null
+++ b/libc/test/include/math_comparison_macros_test.cpp
@@ -0,0 +1,133 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for math comparison macros.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/macros/properties/types.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+template <typename T>
+class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+  void testComparisons() {
+    const T neg_two = T(-2);
+    const T neg_one = T(-1);
+    const T pos_one = T(1);
+    const T pos_two = T(2);
+
+    // Strictly ordered values: neg_inf < neg_two < neg_one < -0.0 == +0.0 <
+    // min_denormal < min_normal < pos_one < pos_two < max_normal < inf.
+    const T ordered[] = {neg_inf,  neg_max_normal, neg_two,    neg_one,
+                         neg_zero, min_denormal,   min_normal, pos_one,
+                         pos_two,  max_normal,     inf};
+    constexpr size_t N = sizeof(ordered) / sizeof(ordered[0]);
+
+    for (size_t i = 0; i < N; ++i) {
+      for (size_t j = 0; j < N; ++j) {
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        EXPECT_EQ(isgreater(ordered[i], ordered[j]), i > j ? 1 : 0);
+        EXPECT_EQ(isgreaterequal(ordered[i], ordered[j]), i >= j ? 1 : 0);
+        EXPECT_EQ(isless(ordered[i], ordered[j]), i < j ? 1 : 0);
+        EXPECT_EQ(islessequal(ordered[i], ordered[j]), i <= j ? 1 : 0);
+        EXPECT_EQ(islessgreater(ordered[i], ordered[j]), i != j ? 1 : 0);
+        EXPECT_EQ(isunordered(ordered[i], ordered[j]), 0);
+        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+      }
+    }
+
+    // +0.0 and -0.0 compare equal and ordered.
+    LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+    EXPECT_EQ(isgreater(zero, neg_zero), 0);
+    EXPECT_EQ(isgreater(neg_zero, zero), 0);
+    EXPECT_EQ(isgreaterequal(zero, neg_zero), 1);
+    EXPECT_EQ(isgreaterequal(neg_zero, zero), 1);
+    EXPECT_EQ(isless(zero, neg_zero), 0);
+    EXPECT_EQ(isless(neg_zero, zero), 0);
+    EXPECT_EQ(islessequal(zero, neg_zero), 1);
+    EXPECT_EQ(islessequal(neg_zero, zero), 1);
+    EXPECT_EQ(islessgreater(zero, neg_zero), 0);
+    EXPECT_EQ(islessgreater(neg_zero, zero), 0);
+    EXPECT_EQ(isunordered(zero, neg_zero), 0);
+    EXPECT_EQ(isunordered(neg_zero, zero), 0);
+    EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+
+    // Quiet NaN comparisons must return 0 (except isunordered -> 1) without
+    // raising FE_INVALID.
+    const T nans[] = {aNaN, neg_aNaN};
+    const T vals[] = {neg_inf, neg_one, neg_zero, zero, pos_one, inf, aNaN};
+    for (T qnan : nans) {
+      for (T v : vals) {
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        EXPECT_EQ(isgreater(qnan, v), 0);
+        EXPECT_EQ(isgreater(v, qnan), 0);
+        EXPECT_EQ(isgreaterequal(qnan, v), 0);
+        EXPECT_EQ(isgreaterequal(v, qnan), 0);
+        EXPECT_EQ(isless(qnan, v), 0);
+        EXPECT_EQ(isless(v, qnan), 0);
+        EXPECT_EQ(islessequal(qnan, v), 0);
+        EXPECT_EQ(islessequal(v, qnan), 0);
+        EXPECT_EQ(islessgreater(qnan, v), 0);
+        EXPECT_EQ(islessgreater(v, qnan), 0);
+        EXPECT_EQ(isunordered(qnan, v), 1);
+        EXPECT_EQ(isunordered(v, qnan), 1);
+        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+      }
+    }
+
+    // Macro arguments with side effects must be evaluated once.
+    T lhs = pos_one;
+    T rhs = pos_two;
+    EXPECT_EQ(isless(lhs++, rhs++), 1);
+    EXPECT_FP_EQ(lhs, pos_two);
+    EXPECT_FP_EQ(rhs, T(3));
+  }
+};
+
+using LlvmLibcMathComparisonMacrosFloatTest = MathComparisonMacrosTest<float>;
+TEST_F(LlvmLibcMathComparisonMacrosFloatTest, All) { testComparisons(); }
+
+using LlvmLibcMathComparisonMacrosDoubleTest = MathComparisonMacrosTest<double>;
+TEST_F(LlvmLibcMathComparisonMacrosDoubleTest, All) { testComparisons(); }
+
+using LlvmLibcMathComparisonMacrosLongDoubleTest =
+    MathComparisonMacrosTest<long double>;
+TEST_F(LlvmLibcMathComparisonMacrosLongDoubleTest, All) { testComparisons(); }
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+using LlvmLibcMathComparisonMacrosFloat16Test =
+    MathComparisonMacrosTest<float16>;
+TEST_F(LlvmLibcMathComparisonMacrosFloat16Test, All) { testComparisons(); }
+#endif
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+using LlvmLibcMathComparisonMacrosFloat128Test =
+    MathComparisonMacrosTest<float128>;
+TEST_F(LlvmLibcMathComparisonMacrosFloat128Test, All) { testComparisons(); }
+#endif
+
+TEST(LlvmLibcMathComparisonMacrosMixedTest, MixedTypes) {
+  LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_EQ(isgreater(2.0f, 1.0), 1);
+  EXPECT_EQ(isgreaterequal(1.0f, 1.0L), 1);
+  EXPECT_EQ(isless(1.0f, 2.0), 1);
+  EXPECT_EQ(islessequal(2.0, 2.0L), 1);
+  EXPECT_EQ(islessgreater(1.0f, 2.0L), 1);
+  EXPECT_EQ(isunordered(__builtin_nanf(""), 1.0), 1);
+  EXPECT_EQ(isunordered(1.0f, __builtin_nanl("")), 1);
+  EXPECT_EQ(LIBC_NAMESPACE::fputil::get_except(), 0);
+}
diff --git a/libc/utils/docgen/docgen_test.py b/libc/utils/docgen/docgen_test.py
index b8c086221c4f85..1029ab4460f1c3 100644
--- a/libc/utils/docgen/docgen_test.py
+++ b/libc/utils/docgen/docgen_test.py
@@ -89,7 +89,10 @@ def test_implements_fn(self):
     def test_implements_macro(self):
         self.assertTrue(Header("assert.h").implements_macro("assert"))
         self.assertTrue(Header("math.h").implements_macro("isnan"))
-        self.assertFalse(Header("math.h").implements_macro("isgreater"))
+        self.assertTrue(Header("math.h").implements_macro("isgreater"))
+        self.assertFalse(
+            Header("math.h").implements_macro("__STDC_VERSION_MATH_H__")
+        )
         self.assertFalse(Header("sys/syscall.h").macro_file_exists())
         self.assertTrue(Header("sys/syscall.h").implements_macro("SYS_read"))
         self.assertTrue(Header("errno.h").implements_macro("errno"))
diff --git a/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
index 7abc875314c26d..e11be5f2af266e 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
@@ -268,6 +268,28 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "math_comparison_macros_test",
+    srcs = ["math_comparison_macros_test.cpp"],
+    full_build_only = True,
+    deps = [
+        "//libc/hdr:fenv_macros",
+        "//libc/include:llvm_libc_macros_math_function_macros",
+        "//libc/src/__support/FPUtil:fenv_impl",
+        "//libc/src/__support/macros/properties:types",
+        "//libc/test/UnitTest:fp_test_helpers",
+    ],
+)
+
+libc_test(
+    name = "math_comparison_macros_c_test",
+    srcs = ["math_comparison_macros_test.c"],
+    c_test = True,
+    deps = [
+        "//libc/include:llvm_libc_macros_math_function_macros",
+    ],
+)
+
 libc_test(
     name = "issubnormal_c_test",
     srcs = ["issubnormal_test.c"],

>From 51b283f3ce5e68e06e626d2fe369cc45c3edf6b4 Mon Sep 17 00:00:00 2001
From: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
Date: Thu, 19 Nov 2026 00:00:49 +0200
Subject: [PATCH 2/4] [libc][math] Collapse the assertFalse call to one line

darker flagged the multi-line split: it fits within 88 columns, so black
collapses it. Fixes the code_formatter check on this PR.

Assisted-by: pi (swift-iq3_xxs via llama-server)
---
 libc/utils/docgen/docgen_test.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/utils/docgen/docgen_test.py b/libc/utils/docgen/docgen_test.py
index 1029ab4460f1c3..13b92b05944733 100644
--- a/libc/utils/docgen/docgen_test.py
+++ b/libc/utils/docgen/docgen_test.py
@@ -90,9 +90,7 @@ def test_implements_macro(self):
         self.assertTrue(Header("assert.h").implements_macro("assert"))
         self.assertTrue(Header("math.h").implements_macro("isnan"))
         self.assertTrue(Header("math.h").implements_macro("isgreater"))
-        self.assertFalse(
-            Header("math.h").implements_macro("__STDC_VERSION_MATH_H__")
-        )
+        self.assertFalse(Header("math.h").implements_macro("__STDC_VERSION_MATH_H__"))
         self.assertFalse(Header("sys/syscall.h").macro_file_exists())
         self.assertTrue(Header("sys/syscall.h").implements_macro("SYS_read"))
         self.assertTrue(Header("errno.h").implements_macro("errno"))

>From 01a5977d81fceb3d97a8b6d52aa674d907caf7af Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 19 Nov 2026 03:17:24 +0000
Subject: [PATCH 3/4] Update tests due to compiler runtime issues.

---
 .../include/math_comparison_macros_test.c     |  2 +-
 .../include/math_comparison_macros_test.cpp   | 48 ++++++++++++++-----
 2 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/libc/test/include/math_comparison_macros_test.c b/libc/test/include/math_comparison_macros_test.c
index bfc522ed6758e1..89a33fb3c4240e 100644
--- a/libc/test/include/math_comparison_macros_test.c
+++ b/libc/test/include/math_comparison_macros_test.c
@@ -43,4 +43,4 @@ TEST(math_comparison_macros) {
   EXPECT_TRUE(isunordered(1.0, __builtin_nan("")) == 1);
   EXPECT_TRUE(isunordered(1.0L, -1.0L) == 0);
 }
-#endif
+#endif // defined(isgreater) && ... && defined(isunordered)
diff --git a/libc/test/include/math_comparison_macros_test.cpp b/libc/test/include/math_comparison_macros_test.cpp
index 3aa6d45b92e3ed..1504fc0bd20478 100644
--- a/libc/test/include/math_comparison_macros_test.cpp
+++ b/libc/test/include/math_comparison_macros_test.cpp
@@ -24,7 +24,7 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
   DECLARE_SPECIAL_CONSTANTS(T)
 
 public:
-  void testComparisons() {
+  void testComparisons(bool check_ordered_qnan_fe_invalid = true) {
     const T neg_two = T(-2);
     const T neg_one = T(-1);
     const T pos_one = T(1);
@@ -73,6 +73,12 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
     for (T qnan : nans) {
       for (T v : vals) {
         LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        EXPECT_EQ(islessgreater(qnan, v), 0);
+        EXPECT_EQ(islessgreater(v, qnan), 0);
+        EXPECT_EQ(isunordered(qnan, v), 1);
+        EXPECT_EQ(isunordered(v, qnan), 1);
+        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+
         EXPECT_EQ(isgreater(qnan, v), 0);
         EXPECT_EQ(isgreater(v, qnan), 0);
         EXPECT_EQ(isgreaterequal(qnan, v), 0);
@@ -81,11 +87,10 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
         EXPECT_EQ(isless(v, qnan), 0);
         EXPECT_EQ(islessequal(qnan, v), 0);
         EXPECT_EQ(islessequal(v, qnan), 0);
-        EXPECT_EQ(islessgreater(qnan, v), 0);
-        EXPECT_EQ(islessgreater(v, qnan), 0);
-        EXPECT_EQ(isunordered(qnan, v), 1);
-        EXPECT_EQ(isunordered(v, qnan), 1);
-        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+        if (check_ordered_qnan_fe_invalid)
+          EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
+        else
+          LIBC_NAMESPACE::fputil::clear_except(FE_INVALID);
       }
     }
 
@@ -98,27 +103,48 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
   }
 };
 
+#if defined(LIBC_COMPILER_IS_CLANG) && defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
+// LLVM's SelectionDAG softens ordered fp128 predicates (fcmp ogt/oge/olt/ole)
+// directly to __gttf2/__getf2/__lttf2/__letf2, which raise FE_INVALID on quiet
+// NaNs when linked against libgcc.
+constexpr bool CLANG_F128_SOFTFP_SIGNALS_QNAN = true;
+#else
+constexpr bool CLANG_F128_SOFTFP_SIGNALS_QNAN = false;
+#endif // defined(LIBC_COMPILER_IS_CLANG) &&
+       // defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
+
 using LlvmLibcMathComparisonMacrosFloatTest = MathComparisonMacrosTest<float>;
 TEST_F(LlvmLibcMathComparisonMacrosFloatTest, All) { testComparisons(); }
 
 using LlvmLibcMathComparisonMacrosDoubleTest = MathComparisonMacrosTest<double>;
 TEST_F(LlvmLibcMathComparisonMacrosDoubleTest, All) { testComparisons(); }
 
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
+constexpr bool LONG_DOUBLE_SIGNALS_QNAN = CLANG_F128_SOFTFP_SIGNALS_QNAN;
+#else
+constexpr bool LONG_DOUBLE_SIGNALS_QNAN = false;
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
+
 using LlvmLibcMathComparisonMacrosLongDoubleTest =
     MathComparisonMacrosTest<long double>;
-TEST_F(LlvmLibcMathComparisonMacrosLongDoubleTest, All) { testComparisons(); }
+TEST_F(LlvmLibcMathComparisonMacrosLongDoubleTest, All) {
+  testComparisons(/*check_ordered_qnan_fe_invalid=*/!LONG_DOUBLE_SIGNALS_QNAN);
+}
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
 using LlvmLibcMathComparisonMacrosFloat16Test =
     MathComparisonMacrosTest<float16>;
 TEST_F(LlvmLibcMathComparisonMacrosFloat16Test, All) { testComparisons(); }
-#endif
+#endif // LIBC_TYPES_HAS_FLOAT16
 
-#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 using LlvmLibcMathComparisonMacrosFloat128Test =
     MathComparisonMacrosTest<float128>;
-TEST_F(LlvmLibcMathComparisonMacrosFloat128Test, All) { testComparisons(); }
-#endif
+TEST_F(LlvmLibcMathComparisonMacrosFloat128Test, All) {
+  testComparisons(
+      /*check_ordered_qnan_fe_invalid=*/!CLANG_F128_SOFTFP_SIGNALS_QNAN);
+}
+#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 
 TEST(LlvmLibcMathComparisonMacrosMixedTest, MixedTypes) {
   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);

>From 917eed3c6dcd91cde36bafee7f01b3508f7e1e52 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 19 Nov 2026 03:46:42 +0000
Subject: [PATCH 4/4] Remove exception checks.

---
 libc/test/include/CMakeLists.txt              |  2 -
 .../include/math_comparison_macros_test.cpp   | 73 +++++--------------
 .../libc/test/include/BUILD.bazel             |  2 -
 3 files changed, 18 insertions(+), 59 deletions(-)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index d44c0250aca6af..0ea0ae28702a06 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -428,9 +428,7 @@ add_fp_unittest(
   SRCS
     math_comparison_macros_test.cpp
   DEPENDS
-    libc.hdr.fenv_macros
     libc.include.llvm-libc-macros.math_function_macros
-    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.macros.properties.types
 )
 
diff --git a/libc/test/include/math_comparison_macros_test.cpp b/libc/test/include/math_comparison_macros_test.cpp
index 1504fc0bd20478..aee1913ad45249 100644
--- a/libc/test/include/math_comparison_macros_test.cpp
+++ b/libc/test/include/math_comparison_macros_test.cpp
@@ -11,8 +11,6 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "hdr/fenv_macros.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
@@ -24,7 +22,7 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
   DECLARE_SPECIAL_CONSTANTS(T)
 
 public:
-  void testComparisons(bool check_ordered_qnan_fe_invalid = true) {
+  void testComparisons() {
     const T neg_two = T(-2);
     const T neg_one = T(-1);
     const T pos_one = T(1);
@@ -39,19 +37,16 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
 
     for (size_t i = 0; i < N; ++i) {
       for (size_t j = 0; j < N; ++j) {
-        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
         EXPECT_EQ(isgreater(ordered[i], ordered[j]), i > j ? 1 : 0);
         EXPECT_EQ(isgreaterequal(ordered[i], ordered[j]), i >= j ? 1 : 0);
         EXPECT_EQ(isless(ordered[i], ordered[j]), i < j ? 1 : 0);
         EXPECT_EQ(islessequal(ordered[i], ordered[j]), i <= j ? 1 : 0);
         EXPECT_EQ(islessgreater(ordered[i], ordered[j]), i != j ? 1 : 0);
         EXPECT_EQ(isunordered(ordered[i], ordered[j]), 0);
-        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
       }
     }
 
     // +0.0 and -0.0 compare equal and ordered.
-    LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
     EXPECT_EQ(isgreater(zero, neg_zero), 0);
     EXPECT_EQ(isgreater(neg_zero, zero), 0);
     EXPECT_EQ(isgreaterequal(zero, neg_zero), 1);
@@ -64,33 +59,24 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
     EXPECT_EQ(islessgreater(neg_zero, zero), 0);
     EXPECT_EQ(isunordered(zero, neg_zero), 0);
     EXPECT_EQ(isunordered(neg_zero, zero), 0);
-    EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
 
-    // Quiet NaN comparisons must return 0 (except isunordered -> 1) without
-    // raising FE_INVALID.
-    const T nans[] = {aNaN, neg_aNaN};
+    // NaN comparisons return 0, except isunordered which returns 1.
+    const T nans[] = {aNaN, neg_aNaN, sNaN, neg_sNaN};
     const T vals[] = {neg_inf, neg_one, neg_zero, zero, pos_one, inf, aNaN};
-    for (T qnan : nans) {
+    for (T nan_val : nans) {
       for (T v : vals) {
-        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
-        EXPECT_EQ(islessgreater(qnan, v), 0);
-        EXPECT_EQ(islessgreater(v, qnan), 0);
-        EXPECT_EQ(isunordered(qnan, v), 1);
-        EXPECT_EQ(isunordered(v, qnan), 1);
-        EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
-
-        EXPECT_EQ(isgreater(qnan, v), 0);
-        EXPECT_EQ(isgreater(v, qnan), 0);
-        EXPECT_EQ(isgreaterequal(qnan, v), 0);
-        EXPECT_EQ(isgreaterequal(v, qnan), 0);
-        EXPECT_EQ(isless(qnan, v), 0);
-        EXPECT_EQ(isless(v, qnan), 0);
-        EXPECT_EQ(islessequal(qnan, v), 0);
-        EXPECT_EQ(islessequal(v, qnan), 0);
-        if (check_ordered_qnan_fe_invalid)
-          EXPECT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_INVALID), 0);
-        else
-          LIBC_NAMESPACE::fputil::clear_except(FE_INVALID);
+        EXPECT_EQ(isgreater(nan_val, v), 0);
+        EXPECT_EQ(isgreater(v, nan_val), 0);
+        EXPECT_EQ(isgreaterequal(nan_val, v), 0);
+        EXPECT_EQ(isgreaterequal(v, nan_val), 0);
+        EXPECT_EQ(isless(nan_val, v), 0);
+        EXPECT_EQ(isless(v, nan_val), 0);
+        EXPECT_EQ(islessequal(nan_val, v), 0);
+        EXPECT_EQ(islessequal(v, nan_val), 0);
+        EXPECT_EQ(islessgreater(nan_val, v), 0);
+        EXPECT_EQ(islessgreater(v, nan_val), 0);
+        EXPECT_EQ(isunordered(nan_val, v), 1);
+        EXPECT_EQ(isunordered(v, nan_val), 1);
       }
     }
 
@@ -103,33 +89,15 @@ class MathComparisonMacrosTest : public LIBC_NAMESPACE::testing::Test {
   }
 };
 
-#if defined(LIBC_COMPILER_IS_CLANG) && defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
-// LLVM's SelectionDAG softens ordered fp128 predicates (fcmp ogt/oge/olt/ole)
-// directly to __gttf2/__getf2/__lttf2/__letf2, which raise FE_INVALID on quiet
-// NaNs when linked against libgcc.
-constexpr bool CLANG_F128_SOFTFP_SIGNALS_QNAN = true;
-#else
-constexpr bool CLANG_F128_SOFTFP_SIGNALS_QNAN = false;
-#endif // defined(LIBC_COMPILER_IS_CLANG) &&
-       // defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
-
 using LlvmLibcMathComparisonMacrosFloatTest = MathComparisonMacrosTest<float>;
 TEST_F(LlvmLibcMathComparisonMacrosFloatTest, All) { testComparisons(); }
 
 using LlvmLibcMathComparisonMacrosDoubleTest = MathComparisonMacrosTest<double>;
 TEST_F(LlvmLibcMathComparisonMacrosDoubleTest, All) { testComparisons(); }
 
-#ifdef LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
-constexpr bool LONG_DOUBLE_SIGNALS_QNAN = CLANG_F128_SOFTFP_SIGNALS_QNAN;
-#else
-constexpr bool LONG_DOUBLE_SIGNALS_QNAN = false;
-#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
-
 using LlvmLibcMathComparisonMacrosLongDoubleTest =
     MathComparisonMacrosTest<long double>;
-TEST_F(LlvmLibcMathComparisonMacrosLongDoubleTest, All) {
-  testComparisons(/*check_ordered_qnan_fe_invalid=*/!LONG_DOUBLE_SIGNALS_QNAN);
-}
+TEST_F(LlvmLibcMathComparisonMacrosLongDoubleTest, All) { testComparisons(); }
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
 using LlvmLibcMathComparisonMacrosFloat16Test =
@@ -140,14 +108,10 @@ TEST_F(LlvmLibcMathComparisonMacrosFloat16Test, All) { testComparisons(); }
 #ifdef LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 using LlvmLibcMathComparisonMacrosFloat128Test =
     MathComparisonMacrosTest<float128>;
-TEST_F(LlvmLibcMathComparisonMacrosFloat128Test, All) {
-  testComparisons(
-      /*check_ordered_qnan_fe_invalid=*/!CLANG_F128_SOFTFP_SIGNALS_QNAN);
-}
+TEST_F(LlvmLibcMathComparisonMacrosFloat128Test, All) { testComparisons(); }
 #endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 
 TEST(LlvmLibcMathComparisonMacrosMixedTest, MixedTypes) {
-  LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
   EXPECT_EQ(isgreater(2.0f, 1.0), 1);
   EXPECT_EQ(isgreaterequal(1.0f, 1.0L), 1);
   EXPECT_EQ(isless(1.0f, 2.0), 1);
@@ -155,5 +119,4 @@ TEST(LlvmLibcMathComparisonMacrosMixedTest, MixedTypes) {
   EXPECT_EQ(islessgreater(1.0f, 2.0L), 1);
   EXPECT_EQ(isunordered(__builtin_nanf(""), 1.0), 1);
   EXPECT_EQ(isunordered(1.0f, __builtin_nanl("")), 1);
-  EXPECT_EQ(LIBC_NAMESPACE::fputil::get_except(), 0);
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
index e11be5f2af266e..c5d113f9bb2ddc 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
@@ -273,9 +273,7 @@ libc_test(
     srcs = ["math_comparison_macros_test.cpp"],
     full_build_only = True,
     deps = [
-        "//libc/hdr:fenv_macros",
         "//libc/include:llvm_libc_macros_math_function_macros",
-        "//libc/src/__support/FPUtil:fenv_impl",
         "//libc/src/__support/macros/properties:types",
         "//libc/test/UnitTest:fp_test_helpers",
     ],



More information about the libc-commits mailing list