[PATCH v2 4/5] math: Check the control lanes in the libmvec tests
Adhemerval Zanella
adhemerval.zanella@linaro.org
Fri Nov 13 12:32:08 GMT 2026
The layouts with control values only check the input lane. Also call
the routine with the control value in every lane, and check each control
lane whose result changed, using the ulps of the routine:
tan_sve (inf) [lane 0, other lanes min_subnorm] [control lane 1]
The wrappers now only load, call and store. The mathvec_run_f and
mathvec_run_ff do the rest.
Checked on x86_64-linux-gnu and aarch64-linux-gnu.
---
math/libm-test-driver.c | 24 ++-
math/test-math-vector.h | 194 ++++++++++++++----
.../aarch64/fpu/test-double-sve-wrappers.c | 27 +--
sysdeps/aarch64/fpu/test-float-sve-wrappers.c | 27 +--
.../aarch64/fpu/test-vpcs-vector-wrapper.h | 21 +-
5 files changed, 213 insertions(+), 80 deletions(-)
diff --git a/math/libm-test-driver.c b/math/libm-test-driver.c
index 0f18bd9c38b..2ff72b7b033 100644
--- a/math/libm-test-driver.c
+++ b/math/libm-test-driver.c
@@ -30,6 +30,8 @@ const int flag_test_mathvec = TEST_MATHVEC;
int mathvec_layout;
int mathvec_num_layouts = 1;
char mathvec_layout_desc[MATHVEC_LAYOUT_DESC_LEN];
+struct mathvec_changed mathvec_changed[MATHVEC_MAX_LANES];
+int mathvec_num_changed;
#endif
#if TEST_NARROW
@@ -510,8 +512,27 @@ 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. */
#if TEST_MATHVEC
+/* Check the control lanes whose results changed in the last call to a vector
+ wrapper against their results with the control value in every lane (see
+ test-math-vector.h). TEST_NAME is the name of the test of the checked
+ lane. */
+static inline void
+mathvec_check_changed (const char *test_name)
+{
+ for (int i = 0; i < mathvec_num_changed; i++)
+ {
+ char *name;
+ if (asprintf (&name, "%s [control lane %d]", test_name,
+ mathvec_changed[i].lane) == -1)
+ abort ();
+ check_float (name, mathvec_changed[i].result,
+ mathvec_changed[i].expected, 0);
+ free (name);
+ }
+}
+
/* Run CALL and check its result once for each lane layout provided by the
- vector wrapper. */
+ vector wrapper, including the control lanes that changed. */
# define RUN_TEST_MATHVEC(ARG_STR, CALL, EXPECTED, EXCEPTIONS) \
do \
if (enable_test (EXCEPTIONS)) \
@@ -524,6 +545,7 @@ struct test_Ff_b1_data
mathvec_layout_desc) == -1) \
abort (); \
check_float (test_name, mathvec_ret, EXPECTED, EXCEPTIONS); \
+ mathvec_check_changed (test_name); \
COMMON_TEST_CLEANUP; \
} \
while (0)
diff --git a/math/test-math-vector.h b/math/test-math-vector.h
index 475491bf95d..0f639771830 100644
--- a/math/test-math-vector.h
+++ b/math/test-math-vector.h
@@ -39,27 +39,52 @@
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
+ the remaining lanes to one of a few control 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.
+ special-case handling of other lanes. The routine is also called
+ with the control value in every lane, and the control lanes must
+ give the same results in both calls, so the input lane must not
+ corrupt them either.
- 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. */
+ The wrappers call mathvec_run_f or mathvec_run_ff, which fill the
+ vectors for the current layout, call the routine and return the
+ result in the checked lane. They also set mathvec_num_layouts,
+ mathvec_layout_desc, which the driver appends to the test name, and
+ mathvec_changed, the control lanes whose results differ between the
+ two calls, which the driver checks with the ulps of the routine. */
#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)
+/* The most lanes of any vector type. */
+#define MATHVEC_MAX_LANES 64
+
+/* A control lane whose result with the input in the checked lane,
+ RESULT, differs from its result with the control value in every lane,
+ EXPECTED. */
+struct mathvec_changed
{
- static const struct
- {
- FLOAT value;
- const char *name;
- } others[] =
+ int lane;
+ FLOAT result;
+ FLOAT expected;
+};
+extern struct mathvec_changed mathvec_changed[MATHVEC_MAX_LANES];
+extern int mathvec_num_changed;
+
+struct mathvec_control
+{
+ FLOAT value;
+ const char *name;
+};
+
+/* Return the control value of the current layout for a vector with LEN lanes,
+ or NULL if the layout copies the input to every lane. */
+static inline const struct mathvec_control *
+mathvec_get_control (int len)
+{
+ static const struct mathvec_control controls[] =
{
{ FUNC (__builtin_inf) (), "inf" },
{ -FUNC (__builtin_inf) (), "-inf" },
@@ -74,15 +99,27 @@ mathvec_fill (FLOAT *vec, FLOAT val, int len)
{ 0.0, "0" },
{ -0.0, "-0" }
};
- int layout = mathvec_layout;
+
+ mathvec_num_layouts = len + 2 * array_length (controls);
+ if (mathvec_layout < len)
+ return NULL;
+ return &controls[(mathvec_layout - len) / 2];
+}
+
+/* Fill VEC, which has LEN lanes, with VAL according to the current layout
+ and return the lane to check. */
+static inline int
+mathvec_fill (FLOAT *vec, FLOAT val, int len)
+{
+ const struct mathvec_control *control = mathvec_get_control (len);
int lane;
- mathvec_num_layouts = len + 2 * array_length (others);
- if (layout < len)
+ mathvec_num_changed = 0;
+ if (control == NULL)
{
for (int i = 0; i < len; i++)
vec[i] = val;
- lane = layout;
+ lane = mathvec_layout;
if (lane == 0)
mathvec_layout_desc[0] = '\0';
else
@@ -91,17 +128,83 @@ mathvec_fill (FLOAT *vec, FLOAT val, int len)
}
else
{
- int other = (layout - len) / 2;
- lane = (layout - len) % 2 == 0 ? 0 : len - 1;
+ lane = (mathvec_layout - len) % 2 == 0 ? 0 : len - 1;
for (int i = 0; i < len; i++)
- vec[i] = others[other].value;
+ vec[i] = control->value;
vec[lane] = val;
snprintf (mathvec_layout_desc, MATHVEC_LAYOUT_DESC_LEN,
- " [lane %d, other lanes %s]", lane, others[other].name);
+ " [lane %d, other lanes %s]", lane, control->name);
}
return lane;
}
+/* Fill VEC, which has LEN lanes, with the control value of the current
+ layout. Return 0 if the layout has no control value. */
+static inline int
+mathvec_fill_control (FLOAT *vec, int len)
+{
+ const struct mathvec_control *control = mathvec_get_control (len);
+
+ if (control == NULL)
+ return 0;
+ for (int i = 0; i < len; i++)
+ vec[i] = control->value;
+ return 1;
+}
+
+/* Record in mathvec_changed the lanes other than LANE where RESULT and
+ EXPECTED, which have LEN lanes, differ. Any two NaNs are equal. */
+static inline void
+mathvec_compare_control (const FLOAT *result, const FLOAT *expected,
+ int lane, int len)
+{
+ for (int i = 0; i < len && mathvec_num_changed < MATHVEC_MAX_LANES; i++)
+ if (i != lane
+ && !(__builtin_isnan (result[i]) && __builtin_isnan (expected[i]))
+ && memcmp (&result[i], &expected[i], sizeof (FLOAT)) != 0)
+ mathvec_changed[mathvec_num_changed++]
+ = (struct mathvec_changed) { i, result[i], expected[i] };
+}
+
+/* Run a routine with one argument for the current layout of a vector
+ with LEN lanes and input X. CALL loads A into a vector, calls the
+ routine and stores the result in R. */
+static inline FLOAT
+mathvec_run_f (void (*call) (const FLOAT *a, FLOAT *r), FLOAT x, int len)
+{
+ FLOAT a[len], r[len];
+ int lane = mathvec_fill (a, x, len);
+
+ call (a, r);
+ if (mathvec_fill_control (a, len))
+ {
+ FLOAT rc[len];
+ call (a, rc);
+ mathvec_compare_control (r, rc, lane, len);
+ }
+ return r[lane];
+}
+
+/* Likewise for a routine with two arguments and inputs X and Y. */
+static inline FLOAT
+mathvec_run_ff (void (*call) (const FLOAT *a, const FLOAT *b, FLOAT *r),
+ FLOAT x, FLOAT y, int len)
+{
+ FLOAT a[len], b[len], r[len];
+ int lane = mathvec_fill (a, x, len);
+
+ mathvec_fill (b, y, len);
+ call (a, b, r);
+ if (mathvec_fill_control (a, len))
+ {
+ FLOAT rc[len];
+ mathvec_fill_control (b, len);
+ call (a, b, rc);
+ mathvec_compare_control (r, rc, lane, len);
+ }
+ return r[lane];
+}
+
/* This macro is used in VECTOR_WRAPPER macros for vector tests. */
#define TEST_VEC_LOOP(vec, len) \
do \
@@ -132,33 +235,36 @@ mathvec_fill (FLOAT *vec, FLOAT val, int len)
#define WRAPPER_DECL_fFF(function) extern void function (FLOAT, FLOAT *, FLOAT *);
/* Wrapper from scalar to vector function. */
-#define VECTOR_WRAPPER(scalar_func, vector_func) \
-extern VEC_TYPE vector_func (VEC_TYPE); \
-FLOAT scalar_func (FLOAT x) \
-{ \
- 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 VECTOR_WRAPPER(scalar_func, vector_func) \
+extern VEC_TYPE vector_func (VEC_TYPE); \
+static void \
+CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r) \
+{ \
+ VEC_TYPE mx, mr; \
+ memcpy (&mx, a, sizeof (mx)); \
+ mr = vector_func (mx); \
+ memcpy (r, &mr, sizeof (mr)); \
+} \
+FLOAT scalar_func (FLOAT x) \
+{ \
+ return mathvec_run_f (CONCAT (scalar_func, _call), x, VEC_LEN); \
}
/* Wrapper from scalar 2 argument function to vector one. */
-#define VECTOR_WRAPPER_ff(scalar_func, vector_func) \
-extern VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE); \
-FLOAT scalar_func (FLOAT x, FLOAT y) \
-{ \
- 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]; \
+#define VECTOR_WRAPPER_ff(scalar_func, vector_func) \
+extern VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE); \
+static void \
+CONCAT (scalar_func, _call) (const FLOAT *a, const FLOAT *b, FLOAT *r) \
+{ \
+ 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)); \
+} \
+FLOAT scalar_func (FLOAT x, FLOAT y) \
+{ \
+ return mathvec_run_ff (CONCAT (scalar_func, _call), x, y, VEC_LEN); \
}
/* 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 b7697597237..ada4fb289fd 100644
--- a/sysdeps/aarch64/fpu/test-double-sve-wrappers.c
+++ b/sysdeps/aarch64/fpu/test-double-sve-wrappers.c
@@ -25,29 +25,30 @@
predication. */
#define SVE_VECTOR_WRAPPER(scalar_func, vector_func) \
extern VEC_TYPE vector_func (VEC_TYPE, svbool_t); \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r) \
+ { \
+ svbool_t pg = svptrue_b64 (); \
+ svst1_f64 (pg, r, vector_func (svld1_f64 (pg, a), pg)); \
+ } \
FLOAT scalar_func (FLOAT x) \
{ \
- 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]; \
+ return mathvec_run_f (CONCAT (scalar_func, _call), x, VEC_LEN); \
}
#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) \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, const FLOAT *b, FLOAT *r) \
{ \
- 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]; \
+ svst1_f64 (pg, r, vector_func (mx, my, pg)); \
+ } \
+ FLOAT scalar_func (FLOAT x, FLOAT y) \
+ { \
+ return mathvec_run_ff (CONCAT (scalar_func, _call), x, y, VEC_LEN); \
}
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 2b7315b4eca..ad0547b4ffe 100644
--- a/sysdeps/aarch64/fpu/test-float-sve-wrappers.c
+++ b/sysdeps/aarch64/fpu/test-float-sve-wrappers.c
@@ -25,29 +25,30 @@
predication. */
#define SVE_VECTOR_WRAPPER(scalar_func, vector_func) \
extern VEC_TYPE vector_func (VEC_TYPE, svbool_t); \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r) \
+ { \
+ svbool_t pg = svptrue_b32 (); \
+ svst1_f32 (pg, r, vector_func (svld1_f32 (pg, a), pg)); \
+ } \
FLOAT scalar_func (FLOAT x) \
{ \
- 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]; \
+ return mathvec_run_f (CONCAT (scalar_func, _call), x, VEC_LEN); \
}
#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) \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, const FLOAT *b, FLOAT *r) \
{ \
- 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]; \
+ svst1_f32 (pg, r, vector_func (mx, my, pg)); \
+ } \
+ FLOAT scalar_func (FLOAT x, FLOAT y) \
+ { \
+ return mathvec_run_ff (CONCAT (scalar_func, _call), x, y, VEC_LEN); \
}
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 8a3d3836212..7072a0add02 100644
--- a/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h
+++ b/sysdeps/aarch64/fpu/test-vpcs-vector-wrapper.h
@@ -20,29 +20,32 @@
#define VPCS_VECTOR_WRAPPER(scalar_func, vector_func) \
extern __attribute__ ((aarch64_vector_pcs)) \
VEC_TYPE vector_func (VEC_TYPE); \
- FLOAT scalar_func (FLOAT x) \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r) \
{ \
- 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]; \
+ } \
+ FLOAT scalar_func (FLOAT x) \
+ { \
+ return mathvec_run_f (CONCAT (scalar_func, _call), x, VEC_LEN); \
}
#define VPCS_VECTOR_WRAPPER_ff(scalar_func, vector_func) \
extern __attribute__ ((aarch64_vector_pcs)) \
VEC_TYPE vector_func (VEC_TYPE, VEC_TYPE); \
- FLOAT scalar_func (FLOAT x, FLOAT y) \
+ static void \
+ CONCAT (scalar_func, _call) (const FLOAT *a, const FLOAT *b, FLOAT *r) \
{ \
- 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]; \
+ } \
+ FLOAT scalar_func (FLOAT x, FLOAT y) \
+ { \
+ return mathvec_run_ff (CONCAT (scalar_func, _call), x, y, VEC_LEN); \
}
--
2.53.0
More information about the Libc-alpha
mailing list