gct
gct

Reputation: 14583

Using struct as SSE vector type in gcc?

Is it possible in GCC to use a struct or class as a vector type for SSE instructions?

something like:

typedef struct vfloat __attribute__((vector_size(16))) {
   float x,y,z,w;
} vfloat;

Rather than the canonical:

typedef float v4sf __attribute__ ((vector_size(16)));

union vfloat {
    v4sf  v;
    float f[4];
};

Would be very handy, but I can't seem to make it work.

Upvotes: 3

Views: 1120

Answers (2)

Nominal Animal
Nominal Animal

Reputation: 39366

The structure you seem be looking for is

typedef float  v4sf __attribute__ ((vector_size(16)));
typedef struct vfloat  vfloat;

struct vfloat {
    union {
        v4sf       v;
        float      f[4];
        struct {
            float  x;
            float  y;
            float  z;
            float  w;
        };
    };
};

However, the aliasing rules may bite you later on in the future. I recommend using accessor macros or static inline functions instead.

Upvotes: 0

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

Could you make a union like the one you posted but with your struct instead of float f[4]; as the second member? That would give you the behavior you want.

Upvotes: 2

Related Questions