0

I want to shift right every element of a __m128i register by a different amount.I know this is possible by multiplication if we want to shift left like below:

__m128i mul_constant = _mm_set_epi32(8, 4, 2, 1);
__m128i left_vshift = _mm_mullo_epi32(R, mul_constant);

But, what is the solution if we want to shift it right?

Hadi Ranjbar
  • 1,692
  • 4
  • 21
  • 44
  • They added the variable-length shift in AVX2. But I guess you need this for older processors? – Mysticial Apr 30 '18 at 21:31
  • @Mysticial Yes. I need a SSE3-4 instruction. – Hadi Ranjbar Apr 30 '18 at 21:39
  • 1
    Possible duplicates: [Shifiting xmm integer register values using non-AVX instructions on Intel x86 architecture](https://stackoverflow.com/q/46993981/253056) and [Shifting 4 integers right by different values SIMD](https://stackoverflow.com/q/38363423/253056) and [SSE2 shift by vector](https://stackoverflow.com/q/38605451/253056). – Paul R Apr 30 '18 at 23:17
  • @HadiRanjbar: If you need a non-AVX answer, why did you tag this with `[avx]`? Do you also want an AVX2 `vpsrlvd` answer? – Peter Cordes May 01 '18 at 01:32
  • 2
    Possible duplicate of [Shifting 4 integers right by different values SIMD](https://stackoverflow.com/questions/38363423/shifting-4-integers-right-by-different-values-simd) – Peter Cordes May 01 '18 at 01:58
  • 1
    @PaulR: heh, I was amused to find that I'd answered all 3 of those duplicates you found :P And two of them were *very* similar to each other, mostly just leaving one of the 4 elements unused. – Peter Cordes May 01 '18 at 02:00
  • @PeterCordes: yes, I got a bad case of déjà vu when this question came up! – Paul R May 01 '18 at 02:03

1 Answers1

1

I finally did it like below: Shifting every byte by a different amount to left and then a 32-bit right shift by 3 gave me what I wanted.

 R = _mm_mullo_epi32(R, _mm_set_epi32(1, 2, 4, 8));
 R = _mm_srli_epi32(R, 3);
Hadi Ranjbar
  • 1,692
  • 4
  • 21
  • 44