Search code examples
visual-c++g++sseicc

accessing __m128 fields across compilers


I've noticed that accessing __m128 fields by index is possible in gcc, without using the union trick.

__m128 t;

float r(t[0] + t[1] + t[2] + t[3]);

I can also load a __m128 just like an array:

__m128 t{1.f, 2.f, 3.f, 4.f};

This is all in line with gcc's vector extensions. These, however, may not be available elsewhere. Are the loading and accessing features supported by the intel compiler and msvc?


Solution

  • To load a __m128, you can write _mm_setr_ps(1.f, 2.f, 3.f, 4.f), which is supported by GCC, ICC, MSVC and clang.

    So far as I know, clang and recent versions of GCC support accessing __m128 fields by index. I don't know how to do this in ICC or MSVC. I guess _mm_extract_ps works for all 4 compilers but its return type is insane making it painful to use.