title: How conditionally negate an AVX2 int16_t vector based on another vector of 0 or 1 elements?
body: <p>I have a vector <code>int16_t beta = {1,1,0,0,0,0,0,0}</code>.</p>
  
  <p>I want to implement this equation with AVX2</p>
  
  <p><a href="https://i.stack.imgur.com/BLnyR.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BLnyR.gif" alt="enter image description here"></a></p>
  
  <pre><code>c[i] = a[i] + (-1)^beta[i] * b[i]
  </code></pre>
  
  <p>where a, b, c, and beta are all AVX2 vectors of <code>int16_t</code>.</p>
  
  <hr>
  
  <p>I have figured out that, if I can map 1 to -32768 multiplication operation can be avoided. I mean, flipping the sign of vector b can be done using OR and NEGATE functions of simd intrinsics.</p>
  
  <p>I do know that 1 can be mapped to -32768 using left shift operation, however avx2 doesn't have any bit shift operations<sup>1</sup>. Is there any way to efficiently map 1 to -32768 with simd?</p>
  
  <hr>
  
  <p>Editor's footnote 1:  <a href="https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=4067,602,2978&amp;text=shift&amp;techs=SSE,SSE2,SSE3,SSSE3,SSE4_1,SSE4_2,AVX,AVX2" rel="nofollow noreferrer"><code>_mm256_slli_epi16(x, 15)</code></a> does in fact exist.  But there are other ways to implement the whole formula so the question is interesting after all.</p>