[PATCH v2 5/5] math: Run the x86 vector sincos tests through the lane layouts

Adhemerval Zanella adhemerval.zanella@linaro.org
Fri Nov 13 12:32:09 GMT 2026


The x86 sincos wrappers still copied the input to every lane and used
TEST_VEC_LOOP.  The variants that take the result pointers in two or
four vectors also passed the same pointers for every part, so the upper
lanes overwrote the lower ones.

Add mathvec_run_fFF and a vector RUN_TEST_fFF_11, give each lane its own
result element, and remove TEST_VEC_LOOP, INIT_VEC_LOOP and the unused
generic VECTOR_WRAPPER_fFF.

Checked on x86_64-linux-gnu.
---
 math/libm-test-driver.c                       |  62 ++++++--
 math/test-math-vector.h                       |  85 +++++------
 sysdeps/x86/fpu/test-math-vector-sincos.h     | 136 ++++++++++--------
 .../fpu/test-float-vlen8-avx2-wrappers.c      |   3 -
 4 files changed, 160 insertions(+), 126 deletions(-)

diff --git a/math/libm-test-driver.c b/math/libm-test-driver.c
index 2ff72b7b033..fdda1653fe3 100644
--- a/math/libm-test-driver.c
+++ b/math/libm-test-driver.c
@@ -30,7 +30,7 @@ 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];
+struct mathvec_changed mathvec_changed[2 * MATHVEC_MAX_LANES];
 int mathvec_num_changed;
 #endif
 
@@ -512,16 +512,18 @@ 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.  */
+/* Check the control lanes of result OUTPUT 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)
+mathvec_check_changed (const char *test_name, int output)
 {
   for (int i = 0; i < mathvec_num_changed; i++)
     {
       char *name;
+      if (mathvec_changed[i].output != output)
+	continue;
       if (asprintf (&name, "%s [control lane %d]", test_name,
 		    mathvec_changed[i].lane) == -1)
 	abort ();
@@ -545,7 +547,7 @@ mathvec_check_changed (const char *test_name)
 			mathvec_layout_desc) == -1)			\
 	    abort ();							\
 	  check_float (test_name, mathvec_ret, EXPECTED, EXCEPTIONS);	\
-	  mathvec_check_changed (test_name);				\
+	  mathvec_check_changed (test_name, 0);				\
 	  COMMON_TEST_CLEANUP;						\
 	}								\
   while (0)
@@ -1103,10 +1105,47 @@ mathvec_check_changed (const char *test_name)
 		    (ARRAY)[i].RM_##ROUNDING_MODE.expected,		\
 		    (ARRAY)[i].RM_##ROUNDING_MODE.exceptions);		\
   ROUND_RESTORE_ ## ROUNDING_MODE
-#define RUN_TEST_fFF_11(ARG_STR, FUNC_NAME, ARG, EXCEPTIONS,		\
-			EXTRA1_VAR, EXTRA1_TEST,			\
-			EXTRA1_EXPECTED, EXTRA2_VAR,			\
-			EXTRA2_TEST, EXTRA2_EXPECTED)			\
+#if TEST_MATHVEC
+/* Likewise for a vector wrapper with two results.  */
+# define RUN_TEST_fFF_11(ARG_STR, FUNC_NAME, ARG, EXCEPTIONS,		\
+			 EXTRA1_VAR, EXTRA1_TEST,			\
+			 EXTRA1_EXPECTED, EXTRA2_VAR,			\
+			 EXTRA2_TEST, EXTRA2_EXPECTED)			\
+  do									\
+    if (enable_test (EXCEPTIONS))					\
+      for (mathvec_layout = 0; mathvec_layout < mathvec_num_layouts;	\
+	   mathvec_layout++)						\
+	{								\
+	  FUNC_TEST (FUNC_NAME) (ARG, &(EXTRA1_VAR), &(EXTRA2_VAR));	\
+	  char *extra1_name, *extra2_name;				\
+	  if (asprintf (&extra1_name, "%s (%s)%s extra output 1",	\
+			this_func, (ARG_STR),				\
+			mathvec_layout_desc) == -1			\
+	      || asprintf (&extra2_name, "%s (%s)%s extra output 2",	\
+			   this_func, (ARG_STR),			\
+			   mathvec_layout_desc) == -1)			\
+	    abort ();							\
+	  if (EXTRA1_TEST)						\
+	    {								\
+	      check_float (extra1_name, EXTRA1_VAR, EXTRA1_EXPECTED,	\
+			   EXCEPTIONS);					\
+	      mathvec_check_changed (extra1_name, 1);			\
+	    }								\
+	  if (EXTRA2_TEST)						\
+	    {								\
+	      check_float (extra2_name, EXTRA2_VAR,			\
+			   EXTRA2_EXPECTED, 0);				\
+	      mathvec_check_changed (extra2_name, 2);			\
+	    }								\
+	  EXTRA_OUTPUT_TEST_CLEANUP (1);				\
+	  EXTRA_OUTPUT_TEST_CLEANUP (2);				\
+	}								\
+  while (0)
+#else
+# define RUN_TEST_fFF_11(ARG_STR, FUNC_NAME, ARG, EXCEPTIONS,		\
+			 EXTRA1_VAR, EXTRA1_TEST,			\
+			 EXTRA1_EXPECTED, EXTRA2_VAR,			\
+			 EXTRA2_TEST, EXTRA2_EXPECTED)			\
   do									\
     if (enable_test (EXCEPTIONS))					\
       {									\
@@ -1124,6 +1163,7 @@ mathvec_check_changed (const char *test_name)
 	COMMON_TEST_CLEANUP;						\
       }									\
   while (0)
+#endif
 #define RUN_TEST_LOOP_fFF_11(FUNC_NAME, ARRAY, ROUNDING_MODE,		\
 			     EXTRA1_VAR, EXTRA2_VAR)			\
   IF_ROUND_INIT_ ## ROUNDING_MODE					\
diff --git a/math/test-math-vector.h b/math/test-math-vector.h
index 0f639771830..ebc2d1879d2 100644
--- a/math/test-math-vector.h
+++ b/math/test-math-vector.h
@@ -47,9 +47,9 @@
      give the same results in both calls, so the input lane must not
      corrupt them either.
 
-   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,
+   The wrappers call mathvec_run_f, mathvec_run_ff or mathvec_run_fFF,
+   which fill the vectors for the current layout, call the routine and
+   return the results 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.  */
@@ -63,14 +63,16 @@ extern char mathvec_layout_desc[MATHVEC_LAYOUT_DESC_LEN];
 
 /* 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.  */
+   EXPECTED.  OUTPUT is 1 or 2 for the results of a routine with two
+   results, and 0 otherwise.  */
 struct mathvec_changed
 {
   int lane;
+  int output;
   FLOAT result;
   FLOAT expected;
 };
-extern struct mathvec_changed mathvec_changed[MATHVEC_MAX_LANES];
+extern struct mathvec_changed mathvec_changed[2 * MATHVEC_MAX_LANES];
 extern int mathvec_num_changed;
 
 struct mathvec_control
@@ -153,17 +155,19 @@ mathvec_fill_control (FLOAT *vec, int len)
 }
 
 /* Record in mathvec_changed the lanes other than LANE where RESULT and
-   EXPECTED, which have LEN lanes, differ.  Any two NaNs are equal.  */
+   EXPECTED, which have LEN lanes, differ, for result OUTPUT of the
+   routine.  Any two NaNs are equal.  */
 static inline void
 mathvec_compare_control (const FLOAT *result, const FLOAT *expected,
-			 int lane, int len)
+			 int lane, int len, int output)
 {
-  for (int i = 0; i < len && mathvec_num_changed < MATHVEC_MAX_LANES; i++)
+  for (int i = 0;
+       i < len && mathvec_num_changed < array_length (mathvec_changed); 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] };
+	= (struct mathvec_changed) { i, output, result[i], expected[i] };
 }
 
 /* Run a routine with one argument for the current layout of a vector
@@ -180,7 +184,7 @@ mathvec_run_f (void (*call) (const FLOAT *a, FLOAT *r), FLOAT x, int len)
     {
       FLOAT rc[len];
       call (a, rc);
-      mathvec_compare_control (r, rc, lane, len);
+      mathvec_compare_control (r, rc, lane, len, 0);
     }
   return r[lane];
 }
@@ -200,35 +204,32 @@ mathvec_run_ff (void (*call) (const FLOAT *a, const FLOAT *b, FLOAT *r),
       FLOAT rc[len];
       mathvec_fill_control (b, len);
       call (a, b, rc);
-      mathvec_compare_control (r, rc, lane, len);
+      mathvec_compare_control (r, rc, lane, len, 0);
     }
   return r[lane];
 }
 
-/* This macro is used in VECTOR_WRAPPER macros for vector tests.  */
-#define TEST_VEC_LOOP(vec, len) 				\
-  do								\
-    {								\
-      for (i = 1; i < len; i++)					\
-        {							\
-          if ((FLOAT) vec[0] != (FLOAT) vec[i])			\
-            {							\
-              vec[0] = (FLOAT) vec[0] + 0.1;			\
-	      break;						\
-            }							\
-        }							\
-    }								\
-  while (0)
+/* Likewise for a routine with one argument and two results, which CALL
+   stores in R1 and R2.  Return the results in the checked lane in *R1
+   and *R2.  */
+static inline void
+mathvec_run_fFF (void (*call) (const FLOAT *a, FLOAT *r1, FLOAT *r2),
+		 FLOAT x, FLOAT *r1, FLOAT *r2, int len)
+{
+  FLOAT a[len], ra[len], rb[len];
+  int lane = mathvec_fill (a, x, len);
 
-#define INIT_VEC_LOOP(vec, val, len)				\
-  do								\
-    {								\
-      for (i = 0; i < len; i++)					\
-        {							\
-          vec[i] = val;						\
-        }							\
-    }								\
-  while (0)
+  call (a, ra, rb);
+  if (mathvec_fill_control (a, len))
+    {
+      FLOAT rca[len], rcb[len];
+      call (a, rca, rcb);
+      mathvec_compare_control (ra, rca, lane, len, 1);
+      mathvec_compare_control (rb, rcb, lane, len, 2);
+    }
+  *r1 = ra[lane];
+  *r2 = rb[lane];
+}
 
 #define WRAPPER_DECL_f(function) extern FLOAT function (FLOAT);
 #define WRAPPER_DECL_ff(function) extern FLOAT function (FLOAT, FLOAT);
@@ -266,19 +267,3 @@ 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.  */
-#define VECTOR_WRAPPER_fFF(scalar_func, vector_func) 	\
-extern void vector_func (VEC_TYPE, VEC_TYPE *, VEC_TYPE *);	\
-void scalar_func (FLOAT x, FLOAT * r, FLOAT * r1)		\
-{						\
-  int i;					\
-  VEC_TYPE mx, mr, mr1;				\
-  INIT_VEC_LOOP (mx, x, VEC_LEN);		\
-  vector_func (mx, &mr, &mr1);			\
-  TEST_VEC_LOOP (mr, VEC_LEN);			\
-  TEST_VEC_LOOP (mr1, VEC_LEN);			\
-  *r = (FLOAT) mr[0];				\
-  *r1 = (FLOAT) mr1[0];				\
-  return;					\
-}
diff --git a/sysdeps/x86/fpu/test-math-vector-sincos.h b/sysdeps/x86/fpu/test-math-vector-sincos.h
index 8ca29e153cc..7f0d6970392 100644
--- a/sysdeps/x86/fpu/test-math-vector-sincos.h
+++ b/sysdeps/x86/fpu/test-math-vector-sincos.h
@@ -17,82 +17,94 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#define INIT_VEC_PTRS_LOOP(vec, val, len)				\
+/* Set VEC, a vector of LEN pointers, to the addresses of the LEN elements
+   of ARRAY.  */
+#define INIT_VEC_PTRS_LOOP(vec, array, len)				\
   do									\
     {									\
-      union { VEC_INT_TYPE v; __typeof__ ((val)[0]) *a[(len)]; } u;	\
-      for (i = 0; i < len; i++)						\
-	u.a[i] = &(val)[i];						\
+      union { VEC_INT_TYPE v; __typeof__ ((array)[0]) *a[(len)]; } u;	\
+      for (int i = 0; i < (len); i++)					\
+	u.a[i] = &(array)[i];						\
       (vec) = u.v;							\
     }									\
   while (0)
 
+/* Set the PARTS vectors of VECS to the addresses of the VEC_LEN elements
+   of ARRAY, so that each lane of the routine stores its result in its own
+   element.  */
+#define INIT_VEC_PTRS_PARTS(vecs, array, parts)				\
+  do									\
+    {									\
+      for (int p = 0; p < (parts); p++)					\
+	INIT_VEC_PTRS_LOOP ((vecs)[p],					\
+			    (array) + p * (VEC_LEN / (parts)),		\
+			    VEC_LEN / (parts));				\
+    }									\
+  while (0)
+
 /* Wrapper for vector sincos/sincosf compatible with x86_64 and x32 variants
    of _ZGVbN2vvv_sincos, _ZGVdN4vvv_sincos, _ZGVeN8vvv_sincos;
    x32 variants of _ZGVbN4vvv_sincosf, _ZGVcN4vvv_sincos, _ZGVdN8vvv_sincosf,
    _ZGVeN16vvv_sincosf.  */
-#define VECTOR_WRAPPER_fFF_2(scalar_func, vector_func)		\
-extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE);	\
-void scalar_func (FLOAT x, FLOAT * r, FLOAT * r1)		\
-{								\
-  int i;							\
-  FLOAT r_loc[VEC_LEN], r1_loc[VEC_LEN];			\
-  VEC_TYPE mx;							\
-  VEC_INT_TYPE mr, mr1;						\
-  INIT_VEC_LOOP (mx, x, VEC_LEN);				\
-  INIT_VEC_PTRS_LOOP (mr, r_loc, VEC_LEN);			\
-  INIT_VEC_PTRS_LOOP (mr1, r1_loc, VEC_LEN);			\
-  vector_func (mx, mr, mr1);					\
-  TEST_VEC_LOOP (r_loc, VEC_LEN);				\
-  TEST_VEC_LOOP (r1_loc, VEC_LEN);				\
-  *r = r_loc[0];						\
-  *r1 = r1_loc[0];						\
-  return;							\
+#define VECTOR_WRAPPER_fFF_2(scalar_func, vector_func)			\
+extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE);		\
+static void								\
+CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r, FLOAT *r1)	\
+{									\
+  VEC_TYPE mx;								\
+  VEC_INT_TYPE mr[1], mr1[1];						\
+  memcpy (&mx, a, sizeof (mx));						\
+  INIT_VEC_PTRS_PARTS (mr, r, 1);					\
+  INIT_VEC_PTRS_PARTS (mr1, r1, 1);					\
+  vector_func (mx, mr[0], mr1[0]);					\
+}									\
+void scalar_func (FLOAT x, FLOAT *r, FLOAT *r1)				\
+{									\
+  mathvec_run_fFF (CONCAT (scalar_func, _call), x, r, r1, VEC_LEN);	\
 }
 
 /* Wrapper for vector sincos/sincosf compatible with x86_64 variants of
-   _ZGVcN4vvv_sincos, _ZGVeN16vvv_sincosf, _ZGVbN4vvv_sincosf,
-   _ZGVdN8vvv_sincosf, _ZGVcN8vvv_sincosf.  */
-#define VECTOR_WRAPPER_fFF_3(scalar_func, vector_func)		\
-extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE,  \
-			 VEC_INT_TYPE, VEC_INT_TYPE);		\
-void scalar_func (FLOAT x, FLOAT * r, FLOAT * r1)		\
-{								\
-  int i;							\
-  FLOAT r_loc[VEC_LEN/2], r1_loc[VEC_LEN/2];			\
-  VEC_TYPE mx;							\
-  VEC_INT_TYPE mr, mr1;						\
-  INIT_VEC_LOOP (mx, x, VEC_LEN);				\
-  INIT_VEC_PTRS_LOOP (mr, r_loc, VEC_LEN/2);			\
-  INIT_VEC_PTRS_LOOP (mr1, r1_loc, VEC_LEN/2);			\
-  vector_func (mx, mr, mr, mr1, mr1);				\
-  TEST_VEC_LOOP (r_loc, VEC_LEN/2);				\
-  TEST_VEC_LOOP (r1_loc, VEC_LEN/2);				\
-  *r = r_loc[0];						\
-  *r1 = r1_loc[0];						\
-  return;							\
+   _ZGVcN4vvv_sincos, _ZGVeN16vvv_sincosf, _ZGVbN4vvv_sincosf and
+   _ZGVdN8vvv_sincosf, and the x32 variant of _ZGVcN8vvv_sincosf, which
+   take the pointers for each result in two vectors.  */
+#define VECTOR_WRAPPER_fFF_3(scalar_func, vector_func)			\
+extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE,		\
+			 VEC_INT_TYPE, VEC_INT_TYPE);			\
+static void								\
+CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r, FLOAT *r1)	\
+{									\
+  VEC_TYPE mx;								\
+  VEC_INT_TYPE mr[2], mr1[2];						\
+  memcpy (&mx, a, sizeof (mx));						\
+  INIT_VEC_PTRS_PARTS (mr, r, 2);					\
+  INIT_VEC_PTRS_PARTS (mr1, r1, 2);					\
+  vector_func (mx, mr[0], mr[1], mr1[0], mr1[1]);			\
+}									\
+void scalar_func (FLOAT x, FLOAT *r, FLOAT *r1)				\
+{									\
+  mathvec_run_fFF (CONCAT (scalar_func, _call), x, r, r1, VEC_LEN);	\
 }
 
 /* Wrapper for vector sincosf compatible with x86_64 variant of
-   _ZGVcN8vvv_sincosf.  */
-#define VECTOR_WRAPPER_fFF_4(scalar_func, vector_func) 		\
-extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE,	\
-			 VEC_INT_TYPE, VEC_INT_TYPE,		\
-			 VEC_INT_TYPE, VEC_INT_TYPE,		\
-			 VEC_INT_TYPE, VEC_INT_TYPE);		\
-void scalar_func (FLOAT x, FLOAT * r, FLOAT * r1)		\
-{								\
-  int i;							\
-  FLOAT r_loc[VEC_LEN/4], r1_loc[VEC_LEN/4];			\
-  VEC_TYPE mx;							\
-  VEC_INT_TYPE mr, mr1;						\
-  INIT_VEC_LOOP (mx, x, VEC_LEN);				\
-  INIT_VEC_PTRS_LOOP (mr, r_loc, VEC_LEN/4);			\
-  INIT_VEC_PTRS_LOOP (mr1, r1_loc, VEC_LEN/4);			\
-  vector_func (mx, mr, mr, mr, mr, mr1, mr1, mr1, mr1);		\
-  TEST_VEC_LOOP (r_loc, VEC_LEN/4);				\
-  TEST_VEC_LOOP (r1_loc, VEC_LEN/4);				\
-  *r = r_loc[0];						\
-  *r1 = r1_loc[0];						\
-  return;							\
+   _ZGVcN8vvv_sincosf, which takes the pointers for each result in four
+   vectors.  */
+#define VECTOR_WRAPPER_fFF_4(scalar_func, vector_func)			\
+extern void vector_func (VEC_TYPE, VEC_INT_TYPE, VEC_INT_TYPE,		\
+			 VEC_INT_TYPE, VEC_INT_TYPE,			\
+			 VEC_INT_TYPE, VEC_INT_TYPE,			\
+			 VEC_INT_TYPE, VEC_INT_TYPE);			\
+static void								\
+CONCAT (scalar_func, _call) (const FLOAT *a, FLOAT *r, FLOAT *r1)	\
+{									\
+  VEC_TYPE mx;								\
+  VEC_INT_TYPE mr[4], mr1[4];						\
+  memcpy (&mx, a, sizeof (mx));						\
+  INIT_VEC_PTRS_PARTS (mr, r, 4);					\
+  INIT_VEC_PTRS_PARTS (mr1, r1, 4);					\
+  vector_func (mx, mr[0], mr[1], mr[2], mr[3],				\
+	       mr1[0], mr1[1], mr1[2], mr1[3]);				\
+}									\
+void scalar_func (FLOAT x, FLOAT *r, FLOAT *r1)				\
+{									\
+  mathvec_run_fFF (CONCAT (scalar_func, _call), x, r, r1, VEC_LEN);	\
 }
diff --git a/sysdeps/x86_64/fpu/test-float-vlen8-avx2-wrappers.c b/sysdeps/x86_64/fpu/test-float-vlen8-avx2-wrappers.c
index ad93653920a..61d22129476 100644
--- a/sysdeps/x86_64/fpu/test-float-vlen8-avx2-wrappers.c
+++ b/sysdeps/x86_64/fpu/test-float-vlen8-avx2-wrappers.c
@@ -52,9 +52,6 @@ VECTOR_WRAPPER (WRAPPER_NAME (asinhf), _ZGVdN8v_asinhf)
 VECTOR_WRAPPER (WRAPPER_NAME (erfcf), _ZGVdN8v_erfcf)
 VECTOR_WRAPPER (WRAPPER_NAME (tanf), _ZGVdN8v_tanf)
 
-/* Redefinition of wrapper to be compatible with _ZGVdN8vvv_sincosf.  */
-#undef VECTOR_WRAPPER_fFF
-
 #define VEC_INT_TYPE __m256i
 
 #ifndef __ILP32__
-- 
2.53.0



More information about the Libc-alpha mailing list