lundi 26 octobre 2009

GCC intrinsics for SIMD

I made some test with gcc intrinsics, for generating SIMD code (aka MMX, SSE, SSE2, SSE3, Neon). first impression documentation is really sparse don't count on official gcc docs.

I'll post an example of my experiments : applying a level modification to eight channels of audio samples. Typically a good usage of SIMD code. Once you understand the few doc and examples there is present on Internet, it's quite easy to write vectorized code. Much easier than writing it in assembly. But... there is a huge but performances aren't there ! Intrinsic code is confusing gcc. Basicly : gcc SIMD code run slower than gcc non-SIMD code. And the SIMD code is slower with full optimization turned on... perhaps the bad result explain why the documentation is not completed ;) Look like gcc optimizator is confusing himself.. To be continued when I find some time to make some assembly post-mortem.



typedef float v4sf __attribute__ ((vector_size (16))); // vector of four single float

// this structure is here for help you to access the different vector values
// the gcc v4sf type isn't able to provide simple acessors..

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

// declaration of a float vector of four 32bit float
union f4vector values;

// load the values using the convenient union
values.f[0]=1.0;
values.f[1]=2.0;
values.f[2]=3.0;
values.f[3]=4.0;

// coeficient
coef.f[0]=0.3;
coef.f[1]=0.4;
coef.f[2]=0.5;
coef.f[3]=0.6;

// now multiply the vectors (using the .v accessor):
values.v = values.v * coef.v;


If you use the good flags for gcc (-mcpu=pentium3 -mmmx -msse),
simd code will be generated.