Search code examples
cx86ssesimdavx

How to combine two __m128 values to __m256?


I would like to combine two __m128 values to one __m256.

Something like this:

__m128 a = _mm_set_ps(1, 2, 3, 4);
__m128 b = _mm_set_ps(5, 6, 7, 8);

to something like:

__m256 c = { 1, 2, 3, 4, 5, 6, 7, 8 };

are there any intrinsics that I can use to do this?


Solution

  • This should do what you want:

    __m128 a = _mm_set_ps(1,2,3,4);
    __m128 b = _mm_set_ps(5,6,7,8);
    
    __m256 c = _mm256_castps128_ps256(a);
    c = _mm256_insertf128_ps(c,b,1);
    

    If the order is reversed from what you want, then just switch a and b.


    The intrinsic of interest is _mm256_insertf128_ps which will let you insert a 128-bit register into either lower or upper half of a 256-bit AVX register:

    http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011/compiler_c/intref_cls/common/intref_avx_insertf128_ps.htm

    The complete family of them is here: