I am working with SSE2 instruction set in MS Visual Studio. I am using it to do some calculations with 16-bit data.
Suppose i have 8 values loaded into a SSE register. I want to add a constant (e.g. 42) to all of them. Here 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 the constant? The following two ways work, but one is inefficient, and the other is ugly.
my_const_42 = _mm_set_epi16(42, 42, 42, 42, 42, 42, 42, 42)– compiler generates 8 commands to “build” the constantmy_const_42 = {42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0}– hard to understand what is going on; changing42to e.g.-42is not trivial
Is there any way to express the 128-bit constant more conveniently?
Ninety percent of the battle is finding the correct intrinsic. The MSDN Library is pretty well organized, start at this page. From there, drill down like this:
Set is golden, out pops
_mm_set1_epi16 (short w)