[libc-commits] [libc] [libc][mathvec] Check every lane in exhaustive tests (PR #241104)

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


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: sriramshastry

<details>
<summary>Changes</summary>

Compare each broadcast result lane with the scalar reference and report the first mismatch. Add exact, tolerant, and signed-zero checker regressions.

Assisted-by: GitHub Copilot

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


3 Files Affected:

- (modified) libc/test/src/mathvec/exhaustive/CMakeLists.txt (+14) 
- (added) libc/test/src/mathvec/exhaustive/checker_test.cpp (+56) 
- (modified) libc/test/src/mathvec/exhaustive/exhaustive_test.h (+18-6) 


``````````diff
diff --git a/libc/test/src/mathvec/exhaustive/CMakeLists.txt b/libc/test/src/mathvec/exhaustive/CMakeLists.txt
index 099678f5121f65..707ae58df31b81 100644
--- a/libc/test/src/mathvec/exhaustive/CMakeLists.txt
+++ b/libc/test/src/mathvec/exhaustive/CMakeLists.txt
@@ -9,6 +9,20 @@ add_header_library(
     libc.src.__support.CPP.limits
 )
 
+add_fp_unittest(
+  checker_test
+  SUITE
+    libc-mathvec-tests
+  SRCS
+    checker_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.CPP.simd
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   acosf_test
   SUITE
diff --git a/libc/test/src/mathvec/exhaustive/checker_test.cpp b/libc/test/src/mathvec/exhaustive/checker_test.cpp
new file mode 100644
index 00000000000000..9c51540d31841c
--- /dev/null
+++ b/libc/test/src/mathvec/exhaustive/checker_test.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "exhaustive_test.h"
+#include "test/UnitTest/Test.h"
+
+namespace {
+
+using Vector = LIBC_NAMESPACE::cpp::simd<float>;
+constexpr size_t LANES =
+    LIBC_NAMESPACE::cpp::internal::native_vector_size<float>;
+float identity(float x) { return x; }
+LIBC_VECTOR_PCS Vector vector_identity(Vector x) { return x; }
+using ExactChecker = UnaryOpChecker<float, float, identity, vector_identity>;
+using TolerantChecker =
+    UnaryOpChecker<float, float, identity, vector_identity, 1>;
+using LlvmLibcMathvecCheckerTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+} // namespace
+
+TEST_F(LlvmLibcMathvecCheckerTest, MatchingLanes) {
+  EXPECT_EQ(ExactChecker::first_mismatching_lane(1.0f, Vector(1.0f)), LANES);
+  EXPECT_EQ(ExactChecker::first_mismatching_lane(aNaN, Vector(aNaN)), LANES);
+}
+
+TEST_F(LlvmLibcMathvecCheckerTest, EveryLane) {
+  for (size_t lane = 0; lane < LANES; ++lane) {
+    Vector actual(1.0f);
+    actual[lane] = 2.0f;
+    EXPECT_EQ(ExactChecker::first_mismatching_lane(1.0f, actual), lane);
+  }
+}
+
+TEST_F(LlvmLibcMathvecCheckerTest, TolerantEveryLane) {
+  Vector actual(FPBits(0x3f800001U).get_val());
+  EXPECT_EQ(TolerantChecker::first_mismatching_lane(1.0f, actual), LANES);
+  for (size_t lane = 0; lane < LANES; ++lane) {
+    actual[lane] = FPBits(0x3f800002U).get_val();
+    EXPECT_EQ(TolerantChecker::first_mismatching_lane(1.0f, actual), lane);
+    actual[lane] = FPBits(0x3f800001U).get_val();
+  }
+}
+
+TEST_F(LlvmLibcMathvecCheckerTest, SignedZero) {
+  Vector actual(0.0f);
+  for (size_t lane = 0; lane < LANES; ++lane) {
+    actual[lane] = -0.0f;
+    EXPECT_EQ(ExactChecker::first_mismatching_lane(0.0f, actual), lane);
+    actual[lane] = 0.0f;
+  }
+}
diff --git a/libc/test/src/mathvec/exhaustive/exhaustive_test.h b/libc/test/src/mathvec/exhaustive/exhaustive_test.h
index 4a1249af7b7622..b1ee9cec1dab9f 100644
--- a/libc/test/src/mathvec/exhaustive/exhaustive_test.h
+++ b/libc/test/src/mathvec/exhaustive/exhaustive_test.h
@@ -50,6 +50,21 @@ struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType>;
   using StorageType = typename FPBits::StorageType;
 
+  static size_t
+  first_mismatching_lane(OutType expected,
+                         LIBC_NAMESPACE::cpp::simd<OutType> actual) {
+    constexpr size_t LANES =
+        LIBC_NAMESPACE::cpp::internal::native_vector_size<OutType>;
+    for (size_t lane = 0; lane < LANES; ++lane) {
+      bool correct = TOL == 0 ? TEST_FP_EQ(expected, actual[lane])
+                              : LIBC_NAMESPACE::testing::within_ulp_tolerance(
+                                    expected, actual[lane], TOL);
+      if (!correct)
+        return lane;
+    }
+    return LANES;
+  }
+
   // Check in a range, return the number of failures.
   uint64_t check(StorageType start, StorageType stop,
                  LIBC_NAMESPACE::fputil::testing::RoundingMode rounding) {
@@ -65,13 +80,10 @@ struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
 
       LIBC_NAMESPACE::cpp::simd<FloatType> vec_x(x);
       LIBC_NAMESPACE::cpp::simd<OutType> vec_result = VectorFunc(vec_x);
-      OutType vec_res = vec_result[0];
       OutType scalar_result = ScalarFunc(x);
-      bool correct = TOL == 0 ? TEST_FP_EQ(scalar_result, vec_res)
-                              : LIBC_NAMESPACE::testing::within_ulp_tolerance(
-                                    scalar_result, vec_res, TOL);
-
-      if (!correct) {
+      size_t lane = first_mismatching_lane(scalar_result, vec_result);
+      if (lane < LIBC_NAMESPACE::cpp::internal::native_vector_size<OutType>) {
+        OutType vec_res = vec_result[lane];
         if constexpr (TOL == 0)
           EXPECT_FP_EQ(scalar_result, vec_res);
         else

``````````

</details>


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


More information about the libc-commits mailing list