How to efficiently define a 128-bit constant?

I work with the SSE2 command set in MS Visual Studio. I use it to do some calculations with 16-bit data.

Suppose 8 values ​​are loaded into the SSE register. I want to add a constant (e.g. 42) to all of them. This is how I would like my code to look.

__m128i values; // 8 values, 16 bits each
const __m128i my_const_42 = ???; // What should i write here?
values = _mm_add_epi16(values, my_const_2); // Add 42 to the 8 values

Now, how can I define a constant? The following two methods work, but one is inefficient and the other is ugly.

  • my_const_42 = _mm_set_epi16(42, 42, 42, 42, 42, 42, 42, 42) - the compiler generates 8 commands to "create" a constant
  • my_const_42 = {42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0}- it is difficult to understand what is happening; changing 42to, for example, is -42not trivial

Is it more convenient to express a 128-bit constant?

+5
source share
2 answers

. MSDN , . :

  • , "MMX, SSE SSE2 Intrinsics",
  • , "Streaming SIMD Extensions 2",
  • - " ",
  • :
  • , .

- , _mm_set1_epi16 (short w)

+8

- SSE ( NEON). . , , . , :

 xmmTemp = _mm_cmpeq_epi16(xmmA, xmmA); // FFFF
 xmmTemp = _mm_slli_epi16 (mmxTemp, 7); // now it has 0xFF80 (-128)

 xmmTemp = _mm_cmpeq_epi16(xmmA, xmmA); // FFFF
 xmmTemp = _mm_slli_epi16 (mmxTemp, 15); // 0x8000
 xmmTemp = _mm_srli_epi16 (mmxTemp, 11); // 0x10 (positive 16)
+2

All Articles