在零转移在AVX2 8位移位操作 [英] 8 bit shift operation in AVX2 with shifting in zeros

查看:570
本文介绍了在零转移在AVX2 8位移位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法重建 _mm_slli_si128 指令AVX2转向一个 __ mm256i 通过X字节注册?

Is there any way to rebuild the _mm_slli_si128 instruction in AVX2 to shift an __mm256i register by x bytes?

_mm256_slli_si256 似乎只是为了执行两个 _mm_slli_si128 在[127:0]和[255:128 ]。

The _mm256_slli_si256 seems just to execute two _mm_slli_si128 on a[127:0] and a[255:128].

左移应该在工作 __ m256i 是这样的:

The left shift should work on a __m256i like this:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ..., 32] -> [2, 3, 4, 5, 6, 7, 8, 9, ..., 0]

我<一看到href=\"http://stackoverflow.com/questions/19516585/shifting-sse-avx-registers-32-bits-left-and-right-while-shifting-in-zeros\">thread有可能创建具有 _mm256_permutevar8x32_ps 为32位的移位。但我需要一个更通用的解决方案通过X字节偏移。有没有人已经为这个问题的解决方案?

I saw in thread that it is possible to create a shift with _mm256_permutevar8x32_ps for 32bit. But I need a more generic solution to shift by x bytes. Has anybody already a solution for this problem?

推荐答案

好吧,我实现了可以左移多达16个字节的功能。

okay I implemented a function that can shift left up to 16 byte.

template  <unsigned int N> __m256i _mm256_shift_left(__m256i a)
{
  __m256i mask =  _mm256_srli_si256(
          _mm256_permute2x128_si256(a, a, _MM_SHUFFLE(0,0,3,0))
          , 16-N);
  return _mm256_or_si256(_mm256_slli_si256(a,N),mask);
}

例如:

int main(int argc, char* argv[]) {
   __m256i reg =  _mm256_set_epi8(32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,
                                  14,13,12,11,10,9,8,7,6,5,4,3,2,1);

   __m256i result = _mm256_shift_left<1>(reg);
   for(int i = 0; i < 32; i++)
     printf("%2d ",((unsigned char *)&result)[i]);
   printf("\n");
}

的输出是搜索
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

编辑:新alignr指令的新版本。
谢谢你的提示@Evgney Kluev

New version with new alignr instruction. Thanks for the hint @Evgney Kluev

template  <unsigned int N> __m256i _mm256_shift_left(__m256i a)
{
  __m256i mask = _mm256_permute2x128_si256(a, a, _MM_SHUFFLE(0,0,3,0) );
  return _mm256_alignr_epi8(a,mask,16-N);
}

这篇关于在零转移在AVX2 8位移位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆