[libc-commits] [libc] [libc][mathvec] Rotate varied-case controls through every lane (PR #241111)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 19:39:34 PST 2026
https://github.com/sriramshastry updated https://github.com/llvm/llvm-project/pull/241111
>From ed7796429fa315b16fe7f92a34cc5d553d158b55 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Tue, 10 Nov 2026 18:53:10 +0000
Subject: [PATCH 1/2] [libc][mathvec] Rotate varied-case controls through every
lane
Exercise each mixed control input in every lane for exact and tolerant comparisons. Preserve broadcast and default-lane wrappers and add helper regressions.
Assisted-by: GitHub Copilot
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
libc/test/src/mathvec/CMakeLists.txt | 12 +++
libc/test/src/mathvec/UnitTestWrappers.h | 81 ++++++++++++-------
.../src/mathvec/unit_test_wrappers_test.cpp | 70 ++++++++++++++++
3 files changed, 136 insertions(+), 27 deletions(-)
create mode 100644 libc/test/src/mathvec/unit_test_wrappers_test.cpp
diff --git a/libc/test/src/mathvec/CMakeLists.txt b/libc/test/src/mathvec/CMakeLists.txt
index 6283304a0a3286..3e946271660a44 100644
--- a/libc/test/src/mathvec/CMakeLists.txt
+++ b/libc/test/src/mathvec/CMakeLists.txt
@@ -8,6 +8,18 @@ add_header_library(
libc.src.__support.CPP.simd
)
+add_fp_unittest(
+ unit_test_wrappers_test
+ SUITE
+ libc-mathvec-tests
+ SRCS
+ unit_test_wrappers_test.cpp
+ DEPENDS
+ .unit_test_wrappers
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.CPP.simd
+)
+
add_fp_unittest(
acosf_test
SUITE
diff --git a/libc/test/src/mathvec/UnitTestWrappers.h b/libc/test/src/mathvec/UnitTestWrappers.h
index 353a638bcf226d..0e59b060e92ffb 100644
--- a/libc/test/src/mathvec/UnitTestWrappers.h
+++ b/libc/test/src/mathvec/UnitTestWrappers.h
@@ -52,10 +52,11 @@ struct UnaryOp {
namespace internal {
template <typename Op>
-LIBC_INLINE typename Op::VectorType
-make_input(typename Op::ScalarType x, typename Op::ScalarType control) {
+LIBC_INLINE typename Op::VectorType make_input(typename Op::ScalarType x,
+ typename Op::ScalarType control,
+ size_t control_lane) {
typename Op::VectorType input(x);
- input[0] = control;
+ input[control_lane] = control;
return input;
}
@@ -63,8 +64,9 @@ make_input(typename Op::ScalarType x, typename Op::ScalarType control) {
template <typename Op>
LIBC_INLINE typename Op::VectorType wrap_ref(typename Op::ScalarType x,
- typename Op::ScalarType control) {
- return Op::eval_scalar(internal::make_input<Op>(x, control));
+ typename Op::ScalarType control,
+ size_t control_lane = 0) {
+ return Op::eval_scalar(internal::make_input<Op>(x, control, control_lane));
}
template <typename Op>
@@ -73,9 +75,10 @@ LIBC_INLINE typename Op::VectorType wrap_ref(typename Op::ScalarType x) {
}
template <typename Op>
-LIBC_INLINE typename Op::VectorType
-wrap_vector(typename Op::ScalarType x, typename Op::ScalarType control) {
- return Op::eval_vector(internal::make_input<Op>(x, control));
+LIBC_INLINE typename Op::VectorType wrap_vector(typename Op::ScalarType x,
+ typename Op::ScalarType control,
+ size_t control_lane = 0) {
+ return Op::eval_vector(internal::make_input<Op>(x, control, control_lane));
}
template <typename Op>
@@ -84,32 +87,56 @@ LIBC_INLINE typename Op::VectorType wrap_vector(typename Op::ScalarType x) {
}
// A helper macro to test a variety of cases for a given input and operation.
-// Passes in various edge cases as the control input to ensure that vectorized
-// implementations behaves correctly across different input types.
+// Rotate edge-case control inputs through every lane.
#define TEST_VARIED_CASES(x, Op) \
do { \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), -(x)), wrap_vector<Op>((x), -(x))); \
- EXPECT_SIMD_EQ(wrap_ref<Op>(-(x), (x)), wrap_vector<Op>(-(x), (x))); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), aNaN), wrap_vector<Op>((x), aNaN)); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), inf), wrap_vector<Op>((x), inf)); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), neg_inf), wrap_vector<Op>((x), neg_inf)); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), 0.0), wrap_vector<Op>((x), 0.0)); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), -0.0), wrap_vector<Op>((x), -0.0)); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), 1.0), wrap_vector<Op>((x), 1.0)); \
+ for (size_t control_lane = 0; \
+ control_lane < LIBC_NAMESPACE::cpp::internal::native_vector_size< \
+ typename Op::ScalarType>; \
+ ++control_lane) { \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), -(x), control_lane), \
+ wrap_vector<Op>((x), -(x), control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>(-(x), (x), control_lane), \
+ wrap_vector<Op>(-(x), (x), control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), aNaN, control_lane), \
+ wrap_vector<Op>((x), aNaN, control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), inf, control_lane), \
+ wrap_vector<Op>((x), inf, control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), neg_inf, control_lane), \
+ wrap_vector<Op>((x), neg_inf, control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), 0.0, control_lane), \
+ wrap_vector<Op>((x), 0.0, control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), -0.0, control_lane), \
+ wrap_vector<Op>((x), -0.0, control_lane)); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), 1.0, control_lane), \
+ wrap_vector<Op>((x), 1.0, control_lane)); \
+ } \
} while (0)
// Use the same control inputs with an allowed difference in output ULPs.
#define TEST_VARIED_CASES_TOL(x, Op, TOL) \
do { \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), -(x)), wrap_vector<Op>((x), -(x)), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>(-(x), (x)), wrap_vector<Op>(-(x), (x)), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), aNaN), wrap_vector<Op>((x), aNaN), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), inf), wrap_vector<Op>((x), inf), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), neg_inf), wrap_vector<Op>((x), neg_inf), \
- TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), 0.0), wrap_vector<Op>((x), 0.0), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), -0.0), wrap_vector<Op>((x), -0.0), TOL); \
- EXPECT_SIMD_EQ(wrap_ref<Op>((x), 1.0), wrap_vector<Op>((x), 1.0), TOL); \
+ for (size_t control_lane = 0; \
+ control_lane < LIBC_NAMESPACE::cpp::internal::native_vector_size< \
+ typename Op::ScalarType>; \
+ ++control_lane) { \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), -(x), control_lane), \
+ wrap_vector<Op>((x), -(x), control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>(-(x), (x), control_lane), \
+ wrap_vector<Op>(-(x), (x), control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), aNaN, control_lane), \
+ wrap_vector<Op>((x), aNaN, control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), inf, control_lane), \
+ wrap_vector<Op>((x), inf, control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), neg_inf, control_lane), \
+ wrap_vector<Op>((x), neg_inf, control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), 0.0, control_lane), \
+ wrap_vector<Op>((x), 0.0, control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), -0.0, control_lane), \
+ wrap_vector<Op>((x), -0.0, control_lane), TOL); \
+ EXPECT_SIMD_EQ(wrap_ref<Op>((x), 1.0, control_lane), \
+ wrap_vector<Op>((x), 1.0, control_lane), TOL); \
+ } \
} while (0)
// A helper macro to test a full range of float values, from 0 to 0x7f800000.
diff --git a/libc/test/src/mathvec/unit_test_wrappers_test.cpp b/libc/test/src/mathvec/unit_test_wrappers_test.cpp
new file mode 100644
index 00000000000000..3bac8831e5cb9e
--- /dev/null
+++ b/libc/test/src/mathvec/unit_test_wrappers_test.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/CPP/simd.h"
+#include "test/UnitTest/Test.h"
+#include "test/src/mathvec/UnitTestWrappers.h"
+
+namespace {
+
+constexpr size_t LANES =
+ LIBC_NAMESPACE::cpp::internal::native_vector_size<float>;
+bool seen_control[LANES];
+
+float identity(float x) { return x; }
+
+LIBC_VECTOR_PCS LIBC_NAMESPACE::cpp::simd<float>
+record_control(LIBC_NAMESPACE::cpp::simd<float> input) {
+ for (size_t lane = 0; lane < LANES; ++lane)
+ if (input[lane] == 1.0f)
+ seen_control[lane] = true;
+ return input;
+}
+
+using RecordingOp =
+ LIBC_NAMESPACE::testing::mathvec::UnaryOp<float, identity, record_control>;
+using LIBC_NAMESPACE::testing::mathvec::wrap_ref;
+using LIBC_NAMESPACE::testing::mathvec::wrap_vector;
+using LlvmLibcMathvecWrappersTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+} // namespace
+
+TEST_F(LlvmLibcMathvecWrappersTest, ControlLane) {
+ for (size_t control_lane = 0; control_lane < LANES; ++control_lane) {
+ auto input =
+ LIBC_NAMESPACE::testing::mathvec::internal::make_input<RecordingOp>(
+ 2.0f, -0.0f, control_lane);
+ for (size_t lane = 0; lane < LANES; ++lane)
+ EXPECT_FP_EQ(lane == control_lane ? -0.0f : 2.0f, input[lane]);
+ }
+}
+
+TEST_F(LlvmLibcMathvecWrappersTest, BroadcastAndDefaultLane) {
+ auto broadcast = wrap_vector<RecordingOp>(2.0f);
+ auto mixed = wrap_vector<RecordingOp>(2.0f, -0.0f);
+ for (size_t lane = 0; lane < LANES; ++lane) {
+ EXPECT_FP_EQ(2.0f, broadcast[lane]);
+ EXPECT_FP_EQ(lane == 0 ? -0.0f : 2.0f, mixed[lane]);
+ }
+}
+
+TEST_F(LlvmLibcMathvecWrappersTest, ExactVariedCases) {
+ for (size_t lane = 0; lane < LANES; ++lane)
+ seen_control[lane] = false;
+ TEST_VARIED_CASES(2.0f, RecordingOp);
+ for (size_t lane = 0; lane < LANES; ++lane)
+ EXPECT_TRUE(seen_control[lane]);
+}
+
+TEST_F(LlvmLibcMathvecWrappersTest, TolerantVariedCases) {
+ for (size_t lane = 0; lane < LANES; ++lane)
+ seen_control[lane] = false;
+ TEST_VARIED_CASES_TOL(2.0f, RecordingOp, 1);
+ for (size_t lane = 0; lane < LANES; ++lane)
+ EXPECT_TRUE(seen_control[lane]);
+}
>From 2c5e0aaf88a3338959530446c4e7bd552d8ed7c2 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Thu, 19 Nov 2026 03:34:46 +0000
Subject: [PATCH 2/2] [libc][mathvec] Use the newer file header comment style
Address review feedback on the unit test wrappers test.
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
libc/test/src/mathvec/unit_test_wrappers_test.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libc/test/src/mathvec/unit_test_wrappers_test.cpp b/libc/test/src/mathvec/unit_test_wrappers_test.cpp
index 3bac8831e5cb9e..17632356b64e30 100644
--- a/libc/test/src/mathvec/unit_test_wrappers_test.cpp
+++ b/libc/test/src/mathvec/unit_test_wrappers_test.cpp
@@ -5,6 +5,11 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains unit tests for the mathvec unit test wrappers.
+///
+//===----------------------------------------------------------------------===//
#include "src/__support/CPP/simd.h"
#include "test/UnitTest/Test.h"
More information about the libc-commits
mailing list