broken arithmetic operations on vectors (c and c++)

Aldy Hernandez aldyh@redhat.com
Mon Jun 13 13:26:00 GMT 2005


> I don't see any checks that the vectors are compatible.  So given
> 
> 	vector_size(16) float a, b;
> 	vector_size(16) int c;
> 	a = b + c;

The above will error, but only because the assignment will trigger an
incompatability before we get a chance to build the binary-op.  However, 
"b + c" on its own will not error (and should).

In keeping with the C front end, I think we should allow arithmetic
between same type vectors with different signs.  I tried to use comptypes,
and a few other functions to no avail.  The easiest thing I could think
of was using the mode.  Is this correct?

Also, should I check TREE_TYPE or TYPE_MAIN_VARIANT (TREE_TYPE ()) ?

The patch below is untested except with the testcase below.  I just want
to see if this is the right approach.

Y'all might be interested to know that the C front end is also broken
wrt the testcase below, as well as with a few other variations.  I'll look
into them when I'm done with the C++ front end.

Aldy

	* cp/typeck.c (build_binary_op): Check compatability of vector
	types.
	* testsuite/g++.dg/conversion/simd3.C: New.

Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.634
diff -c -p -r1.634 typeck.c
*** cp/typeck.c	10 Jun 2005 17:35:35 -0000	1.634
--- cp/typeck.c	13 Jun 2005 13:13:56 -0000
*************** build_binary_op (enum tree_code code, tr
*** 3182,3188 ****
        if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
  	{
  	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)))
! 	    error ("can't convert between vector values of different size");
  	  arithmetic_types_p = 1;
  	}
      }
--- 3182,3202 ----
        if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
  	{
  	  if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)))
! 	    {
! 	      error ("can't convert between vector values of different size");
! 	      return error_mark_node;
! 	    }
! 	  /* The internal mode must be the same, so we allow arithmetic
! 	     between different signness, if the modes are the same.
! 
! 	     e.g.
! 	     V4SI arithmetic with "unsigned V4SI" will work, but
! 	     V4SI arithmetic with "V2SI" won't.  */
! 	  if (TYPE_MODE (TREE_TYPE (type0)) != TYPE_MODE (TREE_TYPE (type1)))
! 	    {
! 	      error ("can't convert between vector values of different types");
! 	      return error_mark_node;
! 	    }
  	  arithmetic_types_p = 1;
  	}
      }
Index: testsuite/g++.dg/conversion/simd3.C
===================================================================
RCS file: testsuite/g++.dg/conversion/simd3.C
diff -N testsuite/g++.dg/conversion/simd3.C
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/conversion/simd3.C	13 Jun 2005 13:13:56 -0000
***************
*** 0 ****
--- 1,15 ----
+ /* { dg-do compile } */
+ 
+ typedef int myint;
+ 
+ float __attribute__((vector_size(16))) b;
+ int __attribute__((vector_size(16))) d;
+ myint __attribute__((vector_size(16))) d2;
+ unsigned int __attribute__((vector_size(16))) e;
+ 
+ void foo()
+ {
+ 	b + d; /* { dg-error "can't convert between vector values of different types" } */
+ 	d += e;
+ 	d2 += d;
+ }



More information about the Gcc-patches mailing list