- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 C++(或类 C)函数,我正在尝试对其进行向量化。该函数是图像合成的众多变体之一,它采用色度 444 子采样的 Y、U 或 V 图像平面,并将 src 图像合成/覆盖到 dst 图像上(其中 src 图像还包含 alpha 透明度)。
#include <cstdint>
void composite(uint8_t *__restrict__ pSrc, // Source plane
uint8_t *__restrict__ pSrcA, // Source alpha plane
uint8_t *__restrict__ pDst, // Destination plane
const std::size_t nCount) // Number of component pixels to process.
{
for (std::size_t k = 0; k < nCount; ++k)
{
uint16_t w = (pSrc[k] * pSrcA[k]);
uint16_t x = (255 - pSrcA[k]) * pDst[k];
uint16_t y = w+x;
uint16_t z = y / uint16_t{255};
pDst[k] = static_cast<uint8_t>(z);
}
}
在 AVX2 矢量化等效项中,我很难理解如何有效地读取 8 位、转换为 16 位,以及(在处理/合成之后)最终将 16 位样本转换回 8 位以存储回来内存。在读取方面,我使用中间 xmm 寄存器 - 这似乎不是最好的方法;我猜测混合寄存器系列时会出现性能损失。
我想出了(不完整):
#include <cstdint>
#include <immintrin.h>
#include <emmintrin.h>
///////////////////////////////////////////////////////////////////////////
// Credit: https://stackoverflow.com/questions/35285324/how-to-divide-16-bit-integer-by-255-with-using-sse
#define AVX2_DIV255_U16(x) _mm256_srli_epi16(_mm256_mulhi_epu16(x, _mm256_set1_epi16((short)0x8081)), 7)
///////////////////////////////////////////////////////////////////////////
/// Blends/composites/overlays two planes of Y, U, or V plane with 4:4:4 chroma subsampling over the other.
/// \param d The destination Y, U , or V component
/// \param s The source Y, U, or V component
/// \param sa The source alpha component
/// \param pixels The number of pixels that require processing.
/// \return The number of pixels processed.
int blend_plane_pixels_444_vectorized(uint8_t *__restrict__ d,
uint8_t *__restrict__ s,
uint8_t *__restrict__ sa,
const int pixels)
{
int n = 0; // Return number of component pixels processed.
for (int k = 0; k + 32 <= pixels; k += 32)
{
// Load first 16 (unaligned) of d, s, sa
// TODO: This efficient mixing xmm registers with ymm??
auto vecD0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)d));
auto vecS0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)s));
auto vecSa0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)sa));
// Load second 16 (unaligned) of d, s, sa
auto vd1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)d+16));
auto vs1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)s+16));
auto vsa1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i_u *)sa+16));
// Load 255 into register
auto vec255 = _mm256_set1_epi16(255);
// uint16_t w = (pSrc[k] * pSrcA[k]);
auto vecW0 = _mm256_mullo_epi16(vecS0, vecSa0);
auto vecW1 = _mm256_mullo_epi16(vs1, vsa1);
// uint16_t x = (255 - pSrcA[k]) * pDst[k];
auto vecX0 = _mm256_mullo_epi16(_mm256_subs_epu16(vec255, vecSa0), vecD0);
auto vecX1 = _mm256_mullo_epi16(_mm256_subs_epu16(vec255, vsa1), vd1);
// Load 127 into register
auto vec127 = _mm256_set1_epi16(127);
// uint16_t y = w+x;
auto vecY0 = _mm256_adds_epu16(_mm256_adds_epu16(vecW0, vecX0), vec127);
auto vecY1 = _mm256_adds_epu16(_mm256_adds_epu16(vecW1, vecX1), vec127);
// uint16_t z = y / uint16_t{255};
auto vecZ0 = AVX2_DIV255_U16(vecY0);
auto vecZ1 = AVX2_DIV255_U16(vecY1);
// TODO: How to get this back into 8-bit samples so that it can be stored
// back into array.
auto vecResult = _mm256_blendv_epi8(vecZ0, vecZ1, _mm256_set1_epi16(127));
// Write data back to memory (unaligned)
_mm256_storeu_si256((__m256i*)d, vecResult);
d += 32;
s += 32;
sa += 32;
n += 32;
}
return n;
}
SIMD 不是我的强项,这是我需要提高的地方 - 请保持温柔。我想我可能可以对当前的矢量化代码进行许多调整(欢迎提出建议!)
开发环境:
最佳答案
通常,如果您需要将结果重新打包为 8 位整数,最好使用 punpcklbw
/punpckhbw
解包为零,然后重新打包使用 packuswb
得到的结果。或者有时您可以将奇数和偶数字节屏蔽到单独的寄存器中,一起进行计算和位或结果。
_mm256_cvtepu8_epi16
/vpmovzxbw
的“问题”是它是车道交叉的(即,它仅从较低的 128 位一半(或内存)获取输入,但结果在上半部分和下半部分),并且没有(简单)解决方案将来自不同 channel 的 16 位值连接回 1(直到 AVX512 channel 交叉单寄存器包指令具有饱和或截断)。
在您的情况下,您实际上可以将 d
和 s
值打包在一个寄存器中,并将 a
和 255-a 打包在一起
值,并使用 vpmaddubsw
进行乘法和加法。您需要从 d
和 s
值中减去 128,然后再将它们打包在一起,因为一个参数需要是有符号的 int8
。结果将偏离 128*255
,但可以进行补偿,特别是如果您添加 127
进行舍入的话。 (如果不这样做,可以在除法(有符号除法向下舍入)和重新打包后为每个字节添加 128。
未经测试的代码,使用与您的尝试相同的签名:
// https://stackoverflow.com/questions/35285324/how-to-divide-16-bit-integer-by-255-with-using-sse
inline __m256i div255_epu16(__m256i x) {
__m256i mulhi = _mm256_mulhi_epu16(x, _mm256_set1_epi16(0x8081));
return _mm256_srli_epi16(mulhi, 7);
}
int blend_plane_pixels_444_vectorized(uint8_t *__restrict__ d,
uint8_t *__restrict__ s,
uint8_t *__restrict__ sa,
const int pixels)
{
int n = 0; // Return number of component pixels processed.
for (int k = 0; k + 32 <= pixels; k += 32)
{
// Load 32 (unaligned) of d, s, sa
__m256i vecD = _mm256_loadu_si256((__m256i_u *)d);
__m256i vecS = _mm256_loadu_si256((__m256i_u *)s );
__m256i vecA = _mm256_loadu_si256((__m256i_u *)sa);
// subtract 128 from D and S to have them in the signed domain
// subtracting 128 is equivalent ot xor with 128
vecD = _mm256_xor_si256(vecD, _mm256_set1_epi8(0x80));
vecS = _mm256_xor_si256(vecS, _mm256_set1_epi8(0x80));
// calculate 255-a (equivalent to 255 ^ a):
__m256i vecA_ = _mm256_xor_si256(vecA, _mm256_set1_epi8(0xFF));
__m256i vecAA_lo = _mm256_unpacklo_epi8(vecA, vecA_);
__m256i vecSD_lo = _mm256_unpacklo_epi8(vecS, vecD);
__m256i vecAA_hi = _mm256_unpackhi_epi8(vecA, vecA_);
__m256i vecSD_hi = _mm256_unpackhi_epi8(vecS, vecD);
// R = a * (s-128) + (255-a)*(d-128) = a*s + (255-a)*d - 128*255
__m256i vecR_lo = _mm256_maddubs_epi16(vecAA_lo,vecSD_lo);
__m256i vecR_hi = _mm256_maddubs_epi16(vecAA_hi,vecSD_hi);
// shift back to unsigned domain and add 127 for rounding
vecR_lo = _mm256_add_epi16(vecR_lo, _mm256_set1_epi16(127+128*255));
vecR_hi = _mm256_add_epi16(vecR_hi, _mm256_set1_epi16(127+128*255));
// divide (rounding down)
vecR_lo = div255_epu16(vecR_lo);
vecR_hi = div255_epu16(vecR_hi);
// re-join lower and upper half:
__m256i vecResult = _mm256_packus_epi16(vecR_lo, vecR_hi);
// Write data back to memory (unaligned)
_mm256_storeu_si256((__m256i*)d, vecResult);
d += 32;
s += 32;
sa += 32;
n += 32;
}
return n;
}
Godbolt 链接:https://godbolt.org/z/EYzLw2请注意,-march=haswell
或您想要支持的任何体系结构都至关重要,因为否则 gcc 将不会使用未对齐的数据作为内存源操作数。当然,适用一般矢量化规则,即,如果您可以控制对齐方式,则更喜欢分配对齐的数据。如果没有,您可以剥离第一个未对齐的字节(例如,来自 d
),以至少进行一次加载和存储对齐。
Clang 将展开循环(到两次内部迭代),这将稍微提高足够大输入的性能。
关于c++ - AVX2 SIMD 内联 16 位到 8 位,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58459575/
我正在尝试优化一些矩阵计算,我想知道是否可以在编译时检测 SSE/SSE2/AVX/AVX2/AVX-512/AVX-128-FMA/KCVI[ 1] 是否由编译器启用?非常适合 GCC 和 Clan
我想仅使用avx而不是avx2来实现64位转置操作。它应该这样做: // in = Hh Hl Lh Ll // | X | // out = Hh Lh Hl Ll 这就是使
如果我有一个 AVX 寄存器,里面有 4 个 double 值,我想将它的反向存储在另一个寄存器中,是否可以用一个内部命令来实现? 例如:如果我在 SSE 寄存器中有 4 个 float ,我可以使用
最初我试图重现 Agner Fog 的微体系结构指南部分“YMM 和 ZMM 向量指令的预热期”中描述的效果,它说: The processor turns off the upper parts o
我的 C++ 代码使用 SSE,现在我想改进它以支持 AVX(当它可用时)。因此,我检测 AVX 何时可用并调用使用 AVX 命令的函数。我使用 Win7 SP1 + VS2010 SP1 和带有 A
我有一大块内存,比如说 256 KiB 或更长。我想计算整个 block 中 1 位的数量,或者换句话说:将所有字节的“总体计数”值相加。 我知道 AVX-512 有一个 VPOPCNTDQ inst
有多快 tensorflow-gpu与没有 AVX 和 AVX2 相比,有 AVX 和 AVX2 吗? 我试图使用谷歌找到答案,但没有成功。很难重新编译tensorflow-gpu对于 Windows
为什么avx sqrt(非压缩)指令有三个操作数? vsqrtsd xmm1, xmm2, xmm3 这是否意味着类似于 xmm1=xmm2=sqrt(xmm3)? 编辑:下面的详细答案但总之流水线的
我正在研究Intel intrinsics guide的展开和压缩操作。我对这两个概念感到困惑: 对于__m128d _mm_mask_expand_pd (__m128d src, __mmask8
我在 Intel Intrinsic Guide v2.7 中找不到它们。您知道 AVX 或 AVX2 指令集是否支持它们吗? 最佳答案 原始 AVX 指令集中没有分散或收集指令。 AVX2 添加了收
我正在尝试将函数转换为 AVX 版本。函数本身基本上只是比较浮点数并返回真/假取决于计算。 这是原始函数: bool testSingle(float* thisFloat, float* other
我遇到了 AVX 内部指令 _mm256_testc_pd() 的一个非常奇怪的行为。在这里你可以看到这个功能的描述 https://software.intel.com/sites/landingp
我有一个 256 位 AVX 寄存器,其中包含 4 个单精度复数,存储为实数、虚数、实数、虚数等。我目前正在将整个 256 位寄存器写回内存并在那里求和,但这似乎效率低下. 如何使用 AVX(或 AV
#include "stdio.h" #include "math.h" #include "stdlib.h" #include "x86intrin.h" void dd_m(double *cl
有没有办法对 AVX 寄存器进行水平异或——特别是对 256 位寄存器的四个 64 位组件进行异或? 目标是获得 AVX 寄存器的所有 4 个 64 位组件的异或。它本质上与水平添加( _mm256_
当我尝试使用 AVX 获取数据时,出现运行时错误 - 段错误: int i = 0; const int sz = 9; size_t *src1 = (size_t *)_mm_malloc(sz*
当我尝试使用 AVX 展开最简单的循环时,出现运行时错误 - 段错误: const int sz = 9; float *src = (float *)_mm_malloc(sz*
我想将两个 256 位 vector (__m256d) 合并为一个 256位 vector ,通过省略每个 64 位 double 的上半部分。 所以,如果在下面,a_i, b_i, ... 是 3
我测试了以下简单的功能 void mul(double *a, double *b) { for (int i = 0; i #include #include #include #defi
_mm_i32gather_epi32() 的当前英特尔内在函数指南将每个子词的计算地址描述为: addr := base_addr + SignExtend64(vindex[m+31:m]) *
我是一名优秀的程序员,十分优秀!