[PATCH v2 1/5] math: Test libmvec functions with mixed vector lanes
Adhemerval Zanella
adhemerval.zanella@linaro.org
Fri Nov 13 12:32:05 GMT 2026
The libmvec tests copy each input to every lane, so they never run the
code that merges the fast path with the lanes that need special
handling, and bugs there went unnoticed (BZ 34922, BZ 34923, BZ 34940).
The lane check is also weak: TEST_VEC_LOOP's +0.1 does not change NaN,
infinite or large results, and the SVE wrappers only read the last lane.
Run each input with the input in every lane, checking each lane in turn,
and with the input in the first or last lane and +/-Inf, +/-qNaN,
+/-MAX, +/-TRUE_MIN or +/-0 in the others. A failure names the layout:
tan_sve (-0) [lane 0, other lanes inf]
The x86_64 sincos tests keep the old check for now.
Checked on x86_64-linux-gnu and aarch64-linux-gnu.
---
math/libm-test-driver.c | 60 ++++++++---
math/test-math-vector.h | 102 +++++++++++++++---
.../aarch64/fpu/test-double-sve-wrappers.c | 22 ++--
sysdeps/aarch64/fpu/test-float-sve-wrappers.c | 22 ++--
.../aarch64/fpu/test-vpcs-vector-wrapper.h | 29 ++---
5 files changed, 182 insertions(+), 53 deletions(-)
diff --git a/math/libm-test-driver.c b/math/libm-test-driver.c
index d4fd1465b09..0f18bd9c38b 100644
--- a/math/libm-test-driver.c
+++ b/math/libm-test-driver.c
@@ -24,6 +24,13 @@
const int flag_test_errno = TEST_ERRNO;
const int flag_test_exceptions = TEST_EXCEPTIONS;
const int flag_test_mathvec = TEST_MATHVEC;
+#if TEST_MATHVEC
+/* Lane layout state shared with the vector wrappers, see
+ test-math-vector.h. */
+int mathvec_layout;
+int mathvec_num_layouts = 1;
+char mathvec_layout_desc[MATHVEC_LAYOUT_DESC_LEN];
+#endif
#if TEST_NARROW
const int snan_tests_arg = SNAN_TESTS (ARG_FLOAT);
@@ -502,8 +509,34 @@ struct test_Ff_b1_data
/* Run an individual test, including any required setup and checking
of results, or loop over all tests in an array. */
-#define RUN_TEST_1_f(ARG_STR, FUNC_NAME, ARG, EXPECTED, \
- EXCEPTIONS) \
+#if TEST_MATHVEC
+/* Run CALL and check its result once for each lane layout provided by the
+ vector wrapper. */
+# define RUN_TEST_MATHVEC(ARG_STR, CALL, EXPECTED, EXCEPTIONS) \
+ do \
+ if (enable_test (EXCEPTIONS)) \
+ for (mathvec_layout = 0; mathvec_layout < mathvec_num_layouts; \
+ mathvec_layout++) \
+ { \
+ FLOAT mathvec_ret = (CALL); \
+ char *test_name; \
+ if (asprintf (&test_name, "%s (%s)%s", this_func, (ARG_STR), \
+ mathvec_layout_desc) == -1) \
+ abort (); \
+ check_float (test_name, mathvec_ret, EXPECTED, EXCEPTIONS); \
+ COMMON_TEST_CLEANUP; \
+ } \
+ while (0)
+# define RUN_TEST_1_f(ARG_STR, FUNC_NAME, ARG, EXPECTED, EXCEPTIONS) \
+ RUN_TEST_MATHVEC (ARG_STR, FUNC_TEST (FUNC_NAME) (ARG), EXPECTED, \
+ EXCEPTIONS)
+# define RUN_TEST_2_f(ARG_STR, FUNC_NAME, ARG1, ARG2, EXPECTED, \
+ EXCEPTIONS) \
+ RUN_TEST_MATHVEC (ARG_STR, FUNC_TEST (FUNC_NAME) (ARG1, ARG2), \
+ EXPECTED, EXCEPTIONS)
+#else
+# define RUN_TEST_1_f(ARG_STR, FUNC_NAME, ARG, EXPECTED, \
+ EXCEPTIONS) \
do \
if (enable_test (EXCEPTIONS)) \
{ \
@@ -513,6 +546,18 @@ struct test_Ff_b1_data
COMMON_TEST_CLEANUP; \
} \
while (0)
+# define RUN_TEST_2_f(ARG_STR, FUNC_NAME, ARG1, ARG2, EXPECTED, \
+ EXCEPTIONS) \
+ do \
+ if (enable_test (EXCEPTIONS)) \
+ { \
+ COMMON_TEST_SETUP (ARG_STR); \
+ check_float (test_name, FUNC_TEST (FUNC_NAME) (ARG1, ARG2), \
+ EXPECTED, EXCEPTIONS); \
+ COMMON_TEST_CLEANUP; \
+ } \
+ while (0)
+#endif
#define RUN_TEST_LOOP_1_f(FUNC_NAME, ARRAY, ROUNDING_MODE) \
IF_ROUND_INIT_ ## ROUNDING_MODE \
for (size_t i = 0; i < sizeof (ARRAY) / sizeof (ARRAY)[0]; i++) \
@@ -540,17 +585,6 @@ struct test_Ff_b1_data
(ARRAY)[i].RM_##ROUNDING_MODE.expected, \
(ARRAY)[i].RM_##ROUNDING_MODE.exceptions); \
ROUND_RESTORE_ ## ROUNDING_MODE
-#define RUN_TEST_2_f(ARG_STR, FUNC_NAME, ARG1, ARG2, EXPECTED, \
- EXCEPTIONS) \
- do \
- if (enable_test (EXCEPTIONS)) \
- { \
- COMMON_TEST_SETUP (ARG_STR); \
- check_float (test_name, FUNC_TEST (FUNC_NAME) (ARG1, ARG2), \
- EXPECTED, EXCEPTIONS); \
- COMMON_TEST_CLEANUP; \
- } \
- while (0)
#define RUN_TEST_LOOP_2_f(FUNC_NAME, ARRAY, ROUNDING_MODE) \
IF_ROUND_INIT_ ## ROUNDING_MODE \
for (size_t i = 0; i < sizeof (ARRAY) / sizeof (ARRAY)[0]; i++) \
diff --git a/math/test-math-vector.h b/math/test-math-vector.h
index 73d63e837fb..6c82279ee54 100644
--- a/math/test-math-vector.h
+++ b/math/test-math-vector.h
@@ -16,6 +16,11 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <array_length.h>
+#include <float.h>
+#include <stdio.h>
+#include <string.h>
+
#define TEST_MATHVEC 1
#define TEST_NARROW 0
#define TEST_ERRNO 0
@@ -27,6 +32,74 @@
#define WRAPPER_NAME(function) CONCAT (function, VEC_SUFF)
#define FUNC_TEST(function) WRAPPER_NAME (FUNC (function))
+/* The driver runs each test input once for each lane layout, selecting
+ the layout in mathvec_layout:
+
+ - The first layouts copy the input to every lane, and each of them
+ checks a different lane.
+
+ - The other layouts set the first or the last lane to the input and
+ the remaining lanes to one of a few special values, and check the
+ lane with the input. Most vector routines handle these values in a
+ separate path, so this catches lanes that are corrupted by the
+ special-case handling of other lanes.
+
+ Fill VEC, which has LEN lanes, with VAL according to the current layout
+ and return the lane to check. Also set mathvec_num_layouts, and
+ mathvec_layout_desc which the driver appends to the test name. */
+#define MATHVEC_LAYOUT_DESC_LEN 64
+extern int mathvec_layout;
+extern int mathvec_num_layouts;
+extern char mathvec_layout_desc[MATHVEC_LAYOUT_DESC_LEN];
+
+static inline int
+mathvec_fill (FLOAT *vec, FLOAT val, int len)
+{
+ static const struct
+ {
+ FLOAT value;
+ const char *name;
+ } others[] =
+ {
+ { FUNC (__builtin_inf) (), "inf" },
+ { -FUNC (__builtin_inf) (), "-inf" },
+ { FUNC (__builtin_nan) (""), "qNaN" },
+ { -FUNC (__builtin_nan) (""), "-qNaN" },
+ { CONCAT (PREFIX, _MAX), "max" },
+ { -CONCAT (PREFIX, _MAX), "-max" },
+ { CONCAT (PREFIX, _TRUE_MIN), "min_subnorm" },
+ { -CONCAT (PREFIX, _TRUE_MIN), "-min_subnorm" },
+ { 0.0, "0" },
+ { -0.0, "-0" }
+ };
+ int layout = mathvec_layout;
+ int lane;
+
+ mathvec_num_layouts = len + 2 * array_length (others);
+ if (layout < len)
+ {
+ for (int i = 0; i < len; i++)
+ vec[i] = val;
+ lane = layout;
+ if (lane == 0)
+ mathvec_layout_desc[0] = '\0';
+ else
+ snprintf (mathvec_layout_desc, MATHVEC_LAYOUT_DESC_LEN,
+ " [lane %d]", lane);
+ }
+ else
+ {
+ int other = (layout - len) / 2;
+ lane = (layout - len) % 2 == 0 ? 0 : len - 1;
+ for (int i = 0; i < len; i++)
+ vec[i] = others[other].value;
+ vec[lane] = val;
+ snprintf (mathvec_layout_desc, MATHVEC_LAYOUT_DESC_LEN,
+ " [lane %d, other lanes %s]", lane, others[other].name);
+ }
+ return lane;
+}
+
/* This macro is used in VECTOR_WRAPPER macros for vector tests. */
#define TEST_VEC_LOOP(vec, len) \
do \
@@ -61,12 +134,13 @@
extern VEC_TYPE vector_func (VEC_TYPE); \
FLOAT scalar_func (FLOAT x) \
{ \
- int i; \
- VEC_TYPE mx; \
- INIT_VEC_LOOP (mx, x, VEC_LEN); \
- VEC_TYPE mr = vector_func (mx); \
- TEST_VEC_LOOP (mr, VEC_LEN); \
- return ((FLOAT) mr[0]); \
+ FLOAT a[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ VEC_TYPE mx, mr; \
+ memcpy (&mx, a, sizeof (mx)); \
+ mr = vector_func (mx); \
+ memcpy (r, &mr, sizeof (mr)); \
+ return r[lane]; \
}
/* Wrapper from scalar 2 argument function to vector one. */
@@ -74,13 +148,15 @@ FLOAT scalar_func (FLOAT x) \
extern VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE); \
FLOAT scalar_func (FLOAT x, FLOAT y) \
{ \
- int i; \
- VEC_TYPE mx, my; \
- INIT_VEC_LOOP (mx, x, VEC_LEN); \
- INIT_VEC_LOOP (my, y, VEC_LEN); \
- VEC_TYPE mr = vector_func (mx, my); \
- TEST_VEC_LOOP (mr, VEC_LEN); \
- return ((FLOAT) mr[0]); \
+ FLOAT a[VEC_LEN], b[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ mathvec_fill (b, y, VEC_LEN); \
+ VEC_TYPE mx, my, mr; \
+ memcpy (&mx, a, sizeof (mx)); \
+ memcpy (&my, b, sizeof (my)); \
+ mr = vector_func (mx, my); \
+ memcpy (r, &mr, sizeof (mr)); \
+ return r[lane]; \
}
/* Wrapper from scalar 3 argument function to vector one. */
diff --git a/sysdeps/aarch64/fpu/test-double-sve-wrappers.c b/sysdeps/aarch64/fpu/test-double-sve-wrappers.c
index e6e3d652c92..b7697597237 100644
--- a/sysdeps/aarch64/fpu/test-double-sve-wrappers.c
+++ b/sysdeps/aarch64/fpu/test-double-sve-wrappers.c
@@ -27,19 +27,27 @@
extern VEC_TYPE vector_func (VEC_TYPE, svbool_t); \
FLOAT scalar_func (FLOAT x) \
{ \
- VEC_TYPE mx = svdup_n_f64 (x); \
- VEC_TYPE mr = vector_func (mx, svptrue_b64 ()); \
- return svlastb_f64 (svptrue_b64 (), mr); \
+ FLOAT a[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ svbool_t pg = svptrue_b64 (); \
+ VEC_TYPE mr = vector_func (svld1_f64 (pg, a), pg); \
+ svst1_f64 (pg, r, mr); \
+ return r[lane]; \
}
#define SVE_VECTOR_WRAPPER_ff(scalar_func, vector_func) \
extern VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE, svbool_t); \
FLOAT scalar_func (FLOAT x, FLOAT y) \
{ \
- VEC_TYPE mx = svdup_n_f64 (x); \
- VEC_TYPE my = svdup_n_f64 (y); \
- VEC_TYPE mr = vector_func (mx, my, svptrue_b64 ()); \
- return svlastb_f64 (svptrue_b64 (), mr); \
+ FLOAT a[VEC_LEN], b[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ mathvec_fill (b, y, VEC_LEN); \
+ svbool_t pg = svptrue_b64 (); \
+ VEC_TYPE mx = svld1_f64 (pg, a); \
+ VEC_TYPE my = svld1_f64 (pg, b); \
+ VEC_TYPE mr = vector_func (mx, my, pg); \
+ svst1_f64 (pg, r, mr); \
+ return r[lane]; \
}
SVE_VECTOR_WRAPPER (acos_sve, _ZGVsMxv_acos)
diff --git a/sysdeps/aarch64/fpu/test-float-sve-wrappers.c b/sysdeps/aarch64/fpu/test-float-sve-wrappers.c
index 2a01f93b5ac..2b7315b4eca 100644
--- a/sysdeps/aarch64/fpu/test-float-sve-wrappers.c
+++ b/sysdeps/aarch64/fpu/test-float-sve-wrappers.c
@@ -27,19 +27,27 @@
extern VEC_TYPE vector_func (VEC_TYPE, svbool_t); \
FLOAT scalar_func (FLOAT x) \
{ \
- VEC_TYPE mx = svdup_n_f32 (x); \
- VEC_TYPE mr = vector_func (mx, svptrue_b32 ()); \
- return svlastb_f32 (svptrue_b32 (), mr); \
+ FLOAT a[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ svbool_t pg = svptrue_b32 (); \
+ VEC_TYPE mr = vector_func (svld1_f32 (pg, a), pg); \
+ svst1_f32 (pg, r, mr); \
+ return r[lane]; \
}
#define SVE_VECTOR_WRAPPER_ff(scalar_func, vector_func) \
extern VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE, svbool_t); \
FLOAT scalar_func (FLOAT x, FLOAT y) \
{ \
- VEC_TYPE mx = svdup_n_f32 (x); \
- VEC_TYPE my = svdup_n_f32 (y); \
- VEC_TYPE mr = vector_func (mx, my, svptrue_b32 ()); \
- return svlastb_f32 (svptrue_b32 (), mr); \
+ FLOAT a[VEC_LEN], b[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ mathvec_fill (b, y, VEC_LEN); \
+ svbool_t pg = svptrue_b32 (); \
+ VEC_TYPE mx = svld1_f32 (pg, a); \
+ VEC_TYPE my = svld1_f32 (pg, b); \
+ VEC_TYPE mr = vector_func (mx, my, pg); \
+ svst1_f32 (pg, r, mr); \
+ return r[lane]; \
}
SVE_VECTOR_WRAPPER (acosf_sve, _ZGVsMxv_acosf)
diff --git a/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h b/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h
index 0e541135f67..8a3d3836212 100644
--- a/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h
+++ b/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h
@@ -22,12 +22,13 @@
VEC_TYPE vector_func (VEC_TYPE); \
FLOAT scalar_func (FLOAT x) \
{ \
- int i; \
- VEC_TYPE mx; \
- INIT_VEC_LOOP (mx, x, VEC_LEN); \
- VEC_TYPE mr = vector_func (mx); \
- TEST_VEC_LOOP (mr, VEC_LEN); \
- return ((FLOAT) mr[0]); \
+ FLOAT a[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ VEC_TYPE mx, mr; \
+ memcpy (&mx, a, sizeof (mx)); \
+ mr = vector_func (mx); \
+ memcpy (r, &mr, sizeof (mr)); \
+ return r[lane]; \
}
#define VPCS_VECTOR_WRAPPER_ff(scalar_func, vector_func) \
@@ -35,11 +36,13 @@
VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE); \
FLOAT scalar_func (FLOAT x, FLOAT y) \
{ \
- int i; \
- VEC_TYPE mx, my; \
- INIT_VEC_LOOP (mx, x, VEC_LEN); \
- INIT_VEC_LOOP (my, y, VEC_LEN); \
- VEC_TYPE mr = vector_func (mx, my); \
- TEST_VEC_LOOP (mr, VEC_LEN); \
- return ((FLOAT) mr[0]); \
+ FLOAT a[VEC_LEN], b[VEC_LEN], r[VEC_LEN]; \
+ int lane = mathvec_fill (a, x, VEC_LEN); \
+ mathvec_fill (b, y, VEC_LEN); \
+ VEC_TYPE mx, my, mr; \
+ memcpy (&mx, a, sizeof (mx)); \
+ memcpy (&my, b, sizeof (my)); \
+ mr = vector_func (mx, my); \
+ memcpy (r, &mr, sizeof (mr)); \
+ return r[lane]; \
}
--
2.53.0
More information about the Libc-alpha
mailing list