Multiplying vector by constant using SSE

后端 未结 3 1140
说谎
说谎 2021-01-03 13:32

I have some code that operates on 4D vectors and I\'m currently trying to convert it to use SSE. I\'m using both clang and gcc on 64b linux.
Operating only on vectors is

3条回答
  •  误落风尘
    2021-01-03 14:14

    Just use intrinsics and let the compiler take care of it, e.g.

    __m128 vb = _mm_set_ps(1.0f, 2.0f, 3.0f, 4.0f); // vb = { 1.0, 2.0, 3.0, 4.0 }
    __m128 va = _mm_set1_ps(25.0f / 216.0f); // va = { 25.0f / 216.0f, 25.0f / 216.0f, 25.0f / 216.0f, 25.0f / 216.0f }
    __m128 vc = _mm_mul_ps(va, vb); // vc = va * vb
    

    If you look at the generated code it should be quite efficient - the 25.0f / 16.0f value will be calculated at compile time and _mm_set1_ps generates usually generates reasonably efficient code for splatting a vector.

    Note also that you normally only initialise a constant vector such as va just once, prior to entering a loop where you will be doing most of the actual work, so it tends not to be performance-critical.

提交回复
热议问题