[英]Optimize extraction of 64 bit value from AVX2 register
I try to extract 64 bit from an __m256i register. 我尝试从__m256i寄存器中提取64位。 Example of my current extraction function:
我当前的提取函数示例:
byte 31 16 15 0
byte_result_vec 000D 000C 000B 000A 000H 000G 000F 000E
_mm256_packs_epi32 -> 0D0C 0B0A 0D0C 0B0A 0H0G 0F0E 0H0G 0F0E
_mm256_packus_epi16 -> DCBA DCBA DCBA DCBA HGFE HGFE HGFE HGFE
^^^^ ^^^^
_mm256_castsi256_si128 -> HGFE HGFE HGFE HGFE
_mm256_extracti128_si256 -> DCBA DCBA DCBA DCBA
_mm_cvtsi128_si32(byte_result_vec1) -> ABCD
_mm_cvtsi128_si32(byte_result_vec2) -> EFGH
The following code is shifting 4x8 bites to the register position 0-3 and is than extracting 32 bit. 以下代码将4x8位移位到寄存器位置0-3,然后提取32位。
byte_result_vec = _mm256_packs_epi32(byte_result_vec, byte_result_vec);
byte_result_vec = _mm256_packus_epi16(byte_result_vec, byte_result_vec);
__m128i byte_result_vec1 = _mm256_castsi256_si128(byte_result_vec);
__m128i byte_result_vec2 = _mm256_extracti128_si256(byte_result_vec,1);
const int res1 = _mm_cvtsi128_si32(byte_result_vec1);
const int res2 = _mm_cvtsi128_si32(byte_result_vec2);
result_array[j] = res1;
result_array[j+1] = res2;
The code is working correct but it is slow. 该代码工作正常,但速度很慢。 It looks like that copying of res1 and res2 to the result_array takes the most time.
看起来将res1和res2复制到result_array花费的时间最多。 Is there a way to optimize it?
有没有优化的方法?
Probably this variant will be faster 可能这个变体会更快
/* byte_result_vec 000H 000G 000F 000E 000D 000C 000B 000A */
const __m256i shuffle_mask = _mm256_setr_epi8(0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1);
/* abcdefgh 0000 0000 HGFE 0000 0000 0000 0000 DCBA */
const __m256i abcdefgh = _mm256_shuffle_epi8(byte_result_vec, shuffle_mask);
/* abcd 0000 0000 0000 DCBA */
const __m128i abcd = _mm256_castsi256_si128(abcdefgh);
/* efgh 0000 0000 HGFE 0000 */
const __m128i efgh = _mm256_extracti128_si256(abcdefgh, 1);
_mm_storel_epi64((__m128i*)&result_array[j], _mm_or_si128(abcd, efgh));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.