5

I see a code as below:

#include "stdio.h"

#define VECTOR_SIZE         4
typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 
 // vector of four single floats

typedef union f4vector
{
    v4sf    v;
    float   f[VECTOR_SIZE];
} f4vector;

void print_vector (f4vector *v)
{
    printf("%f,%f,%f,%f\n", v->f[0], v->f[1], v->f[2], v->f[3]);
}

int main()
{
    union f4vector a, b, c;

    a.v = (v4sf){1.2, 2.3, 3.4, 4.5};
    b.v = (v4sf){5., 6., 7., 8.};
    c.v = a.v + b.v;

    print_vector(&a);
    print_vector(&b);
    print_vector(&c);
}

This code builds fine and works expectedly using gcc (it's inbuild SSE / MMX extensions and vector data types. this code is doing a SIMD vector addition using 4 single floats.

I want to understand in detail what does each keyword/function call on this typedef line means and does:

typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 

What is the vector_size() function return;

What is the __attribute__ keyword for

Here is the float data type being type defined to vfsf type?

I understand the rest part.

thanks,

-AD

Paul R
  • 208,748
  • 37
  • 389
  • 560
goldenmean
  • 18,376
  • 54
  • 154
  • 211

1 Answers1

9

__attribute__ is GCCs way of exposing functionality from the compiler that isn't in the C or C++ standards. __attribute__((vector_size(x))) instructs GCC to treat the type as a vector of size x. For SSE this is 16 bytes.

However, I would suggest using the __m128, __m128i or __m128d types found in the various <*mmintrin.h> headers. They are more portable across compilers.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Maister
  • 4,978
  • 1
  • 31
  • 34
  • The intrinsics are only more portable to MSVC. The vector extensions work for GCC, Clang, and ICC. They also don't depend on hardware. This means you can use them for e.g ARM as well. You can see the power of the vector extensions of them [here](https://stackoverflow.com/a/48283672/2542702) which I sued for GCC and Clang on both x86 and ARM. It would be easy to extend them for ICC as well but would have made my answer longer. – Z boson Jan 18 '18 at 11:23