[libc-commits] [libc] [libc][mathvec] Require matching infinities in ULP comparisons (PR #241107)

via libc-commits libc-commits at lists.llvm.org
Tue Nov 10 20:51:00 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: sriramshastry

<details>
<summary>Changes</summary>

Classify infinities before comparing finite encoding distances. Add float and double coverage without changing finite, NaN, or signed-zero comparison behavior.

Assisted-by: GitHub Copilot

---
Full diff: https://github.com/llvm/llvm-project/pull/241107.diff


3 Files Affected:

- (modified) libc/test/UnitTest/SIMDMatcher.h (+3) 
- (modified) libc/test/src/mathvec/CMakeLists.txt (+10) 
- (added) libc/test/src/mathvec/simd_matcher_test.cpp (+71) 


``````````diff
diff --git a/libc/test/UnitTest/SIMDMatcher.h b/libc/test/UnitTest/SIMDMatcher.h
index c70c92c19dc7d1..ad70de78f40141 100644
--- a/libc/test/UnitTest/SIMDMatcher.h
+++ b/libc/test/UnitTest/SIMDMatcher.h
@@ -28,6 +28,9 @@ inline bool within_ulp_tolerance(T expected, T actual, uint64_t tolerance) {
   if (expected_bits.is_nan() || actual_bits.is_nan())
     return expected_bits.is_nan() && actual_bits.is_nan();
 
+  if (expected_bits.is_inf() || actual_bits.is_inf())
+    return expected_bits.uintval() == actual_bits.uintval();
+
   // Find the absolute difference of the input bits
   auto expected_uint = expected_bits.uintval();
   auto actual_uint = actual_bits.uintval();
diff --git a/libc/test/src/mathvec/CMakeLists.txt b/libc/test/src/mathvec/CMakeLists.txt
index 3a41ab0da9759f..fbf65ba3871108 100644
--- a/libc/test/src/mathvec/CMakeLists.txt
+++ b/libc/test/src/mathvec/CMakeLists.txt
@@ -8,6 +8,16 @@ add_header_library(
     libc.src.__support.CPP.simd
 )
 
+add_fp_unittest(
+  simd_matcher_test
+  SUITE
+    libc-mathvec-tests
+  SRCS
+    simd_matcher_test.cpp
+  DEPENDS
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   acosf_test
   SUITE
diff --git a/libc/test/src/mathvec/simd_matcher_test.cpp b/libc/test/src/mathvec/simd_matcher_test.cpp
new file mode 100644
index 00000000000000..3012657142bd24
--- /dev/null
+++ b/libc/test/src/mathvec/simd_matcher_test.cpp
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "test/UnitTest/SIMDMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::Sign;
+using LIBC_NAMESPACE::testing::within_ulp_tolerance;
+
+template <typename T> void test_infinities() {
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
+  constexpr uint64_t TOLERANCES[] = {0, 1, UINT64_MAX};
+  constexpr Sign SIGNS[] = {Sign::POS, Sign::NEG};
+  for (uint64_t tolerance : TOLERANCES) {
+    for (Sign sign : SIGNS) {
+      T inf = FPBits::inf(sign).get_val();
+      T max = FPBits::max_normal(sign).get_val();
+      EXPECT_TRUE(within_ulp_tolerance(inf, inf, tolerance));
+      EXPECT_FALSE(within_ulp_tolerance(inf, max, tolerance));
+      EXPECT_FALSE(within_ulp_tolerance(max, inf, tolerance));
+    }
+    T inf = FPBits::inf().get_val();
+    T neg_inf = FPBits::inf(Sign::NEG).get_val();
+    EXPECT_FALSE(within_ulp_tolerance(inf, neg_inf, tolerance));
+    EXPECT_FALSE(within_ulp_tolerance(neg_inf, inf, tolerance));
+  }
+}
+
+template <typename T> void test_finite_values() {
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
+  T x = T(1.0);
+  T next = FPBits(FPBits(x).uintval() + 1).get_val();
+  T next2 = FPBits(FPBits(x).uintval() + 2).get_val();
+  EXPECT_TRUE(within_ulp_tolerance(x, x, 0));
+  EXPECT_FALSE(within_ulp_tolerance(x, next, 0));
+  EXPECT_TRUE(within_ulp_tolerance(x, next, 1));
+  EXPECT_TRUE(within_ulp_tolerance(next, x, 1));
+  EXPECT_FALSE(within_ulp_tolerance(x, next2, 1));
+  EXPECT_TRUE(within_ulp_tolerance(x, next2, 2));
+  EXPECT_TRUE(within_ulp_tolerance(-x, -next, 1));
+  EXPECT_FALSE(within_ulp_tolerance(-x, -next2, 1));
+  EXPECT_FALSE(within_ulp_tolerance(T(0.0), T(-0.0), 0));
+  EXPECT_TRUE(within_ulp_tolerance(T(-0.0), T(-0.0), 0));
+}
+
+template <typename T> void test_nans() {
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
+  T nan = FPBits::quiet_nan().get_val();
+  T other_nan = FPBits::quiet_nan(Sign::NEG, 1).get_val();
+  EXPECT_TRUE(within_ulp_tolerance(nan, other_nan, 0));
+  EXPECT_FALSE(within_ulp_tolerance(nan, T(1.0), UINT64_MAX));
+  EXPECT_FALSE(within_ulp_tolerance(T(1.0), nan, UINT64_MAX));
+  EXPECT_FALSE(within_ulp_tolerance(nan, FPBits::inf().get_val(), UINT64_MAX));
+}
+
+TEST(LlvmLibcSIMDMatcherTest, FloatInfinities) { test_infinities<float>(); }
+TEST(LlvmLibcSIMDMatcherTest, DoubleInfinities) { test_infinities<double>(); }
+TEST(LlvmLibcSIMDMatcherTest, FloatFiniteValues) {
+  test_finite_values<float>();
+}
+TEST(LlvmLibcSIMDMatcherTest, DoubleFiniteValues) {
+  test_finite_values<double>();
+}
+TEST(LlvmLibcSIMDMatcherTest, FloatNaNs) { test_nans<float>(); }
+TEST(LlvmLibcSIMDMatcherTest, DoubleNaNs) { test_nans<double>(); }

``````````

</details>


https://github.com/llvm/llvm-project/pull/241107


More information about the libc-commits mailing list