This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Vector types and type conversions


Converting a vector of floats (via a cast) to a vector of ints of the
same size uses a VIEW_CONVERT_EXPR, so the data are simply copied, not
converted.  This is different from a cast from scalar float to int,
where a conversion is performed.

>From what I can see of the source, this is deliberate.  But is this
behaviour documented anywhere?  All I can see is 

"Vector types can also be used as function arguments.  It is possible
to cast from one vector type to another, provided they are of the same
size (in fact, you can also cast vectors to and from other datatypes
of the same size)."

Andrew.


#include <stdio.h>

typedef float	v4sf  __attribute__ ((vector_size(16),aligned(16)));
typedef int	v4i   __attribute__ ((vector_size(sizeof(int)*4)));

typedef union
{
  int ints[4];
  float floats[4];
  v4i ivec;
  v4sf sfvec;
} union_t;


int main() {
    const v4sf v = { 1.2f ,2.2f ,3.3f, 4.4f };
    const v4i vRes = (v4i) v;
    union_t u;
    
    u.sfvec = v;
    printf("v4sf v = { %f, %f, %f, %f }\n", u.floats[0],u.floats[1],u.floats[2],u.floats[3]);

    u.ivec = vRes;
    printf("v4i vRes = { %d, %d, %d, %d }\n", u.ints[0],u.ints[1],u.ints[2],u.ints[3]);
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]