c++ - 如何矢量化 data_i16[0 到 15]?
问题描述
我在Intel Intrinsic 网站上,无法弄清楚我想要什么指令组合。我想做的是
result = high_table[i8>>4] & low_table[i8&15]
两个表都是 16 位(或更多)。shuffle 似乎是我想要的(_mm_shuffle_epi8),但是获得 8 位值对我不起作用。似乎没有 16 位版本,非字节版本似乎需要第二个参数作为立即值。
我应该如何实现这一点?我是否为每个表调用 _mm_shuffle_epi8 两次,将其转换为 16 位并将值移动 8?如果是这样,我想查看哪个演员表和班次指令?
解决方案
要将传入的索引拆分为两个半字节向量,您需要通常的位移位和 AND。SSE 没有 8 位移位,因此您必须使用更宽的位移和 AND 来模拟移动到字节顶部的位。(因为不幸的是,这个用例_mm_shuffle_epi8
不会忽略高位。如果设置了顶部选择器位,它将将该输出元素归零。)
您绝对不想将传入i8
向量扩展到 16 位元素;那将无法与_mm_shuffle_epi8
.
AVX2 具有vpermd
:从 8x 32 位元素的向量中选择双字。(只有 3 位索引,所以它不适合您的用例,除非您的 nibbles 只有 0..7)。AVX512BW 有更广泛的洗牌,包括vpermi2w
索引到两个向量的串联表中,或者只是vpermw
索引单词。
但是对于只有 SSSE3 的 128 位向量,是的pshufb
( _mm_shuffle_epi8
) 是要走的路。您需要两个单独的向量high_table
,一个用于高字节,一个用于每个单词条目的低字节。另外两个向量用于 low_table 的一半。
使用_mm_unpacklo_epi8
和_mm_unpackhi_epi8
交错两个向量的低 8 字节,或两个向量的高 8 字节。这将为您提供所需的 16 位 LUT 结果,每个字的上半部分来自高半向量。
即,您正在使用此交错从两个 8 位 LUT 中构建一个 16 位 LUT。你要为两个不同的 LUT 重复这个过程两次。
代码看起来像
// UNTESTED, haven't tried even compiling this.
// produces 2 output vectors, you might want to just put this in a loop instead of making a helper function for 1 vector.
// so I'll omit actually returning them.
void foo(__m128i indices)
{
// these optimize away, only used at compile time for the vector initializers
static const uint16_t high_table[16] = {...},
static const uint16_t low_table[16] = {...};
// each LUT needs a separate vector of high-byte and low-byte parts
// don't use SIMD intrinsics to load from the uint16_t tables and deinterleave at runtime, just get the same 16x 2 x 2 bytes of data into vector constants at compile time.
__m128i high_LUT_lobyte = _mm_setr_epi8(high_table[0]&0xff, high_table[1]&0xff, high_table[2]&0xff, ... );
__m128i high_LUT_hibyte = _mm_setr_epi8(high_table[0]>>8, high_table[1]>>8, high_table[2]>>8, ... );
__m128i low_LUT_lobyte = _mm_setr_epi8(low_table[0]&0xff, low_table[1]&0xff, low_table[2]&0xff, ... );
__m128i low_LUT_hibyte = _mm_setr_epi8(low_table[0]>>8, low_table[1]>>8, low_table[2]>>8, ... );
// split the input indexes: emulate byte shift with wider shift + AND
__m128i lo_idx = _mm_and_si128(indices, _mm_set1_epi8(0x0f));
__m128i hi_idx = _mm_and_si128(_mm_srli_epi32(indices, 4), _mm_set1_epi8(0x0f));
__m128i lolo = _mm_shuffle_epi8(low_LUT_lobyte, lo_idx);
__m128i lohi = _mm_shuffle_epi8(low_LUT_hibyte, lo_idx);
__m128i hilo = _mm_shuffle_epi8(high_LUT_lobyte, hi_idx);
__m128i hihi = _mm_shuffle_epi8(high_LUT_hibyte, hi_idx);
// interleave results of LUT lookups into vectors 16-bit elements
__m128i low_result_first = _mm_unpacklo_epi8(lolo, lohi);
__m128i low_result_second = _mm_unpackhi_epi8(lolo, lohi);
__m128i high_result_first = _mm_unpacklo_epi8(hilo, hihi);
__m128i high_result_second = _mm_unpackhi_epi8(hilo, hihi);
// first 8x 16-bit high_table[i8>>4] & low_table[i8&15] results
__m128i and_first = _mm_and_si128(low_result_first, high_result_first);
// second 8x 16-bit high_table[i8>>4] & low_table[i8&15] results
__m128i and_second = _mm_and_si128(low_result_second, high_result_second);
// TOOD: do something with the results.
}
你可以在交错之前,高半对高半,低对低。对于指令级并行性来说,这可能会更好一些,让 AND 的执行与 shuffle 重叠。(通过 Skylake 的 Intel Haswell 只有 1/clock 的 shuffle 吞吐量。)
选择变量名是与此类事情的斗争。有些人只是放弃并为一些中间步骤使用无意义的名称。
推荐阅读
- mysql - 测试中的命令导致测试无法完全退出
- java - jackson-dataformat-csv:没有 POJO 的映射数字值
- python - 如何在 python 上绘制 8 位图像的 16,32 和 64 bin 直方图?
- python - 如何在不等待python的情况下执行linux脚本并返回http post响应
- javascript - 在 Textarea 中按 ID 搜索元素并替换它
- linux - Nohup vs Nohup &
- .net - 我应该从 .NET Framework 4.5.1 升级吗?
- c# - 如何使用非法字符?
- dynamic-programming - 无前缀编码的动态规划
- javascript - 提升和全局变量