我正在使用RGBA32缓冲区(每个组件8位),并且我需要将每个组件乘以常量,然后将乘法的每个结果添加到其他组件中: / p>
结果= r * x + g * y + b * z + a * w(两个向量rgba和xyzw之间的点积)
我试图使用英特尔SSE内在函数来加速这个过程,但我不知道如何在不改变输入的情况下做这样的事情。
有没有办法做到这一点?就像建立一个包含{x,y,z,w,x,y,z,w,x,y,z,w,x,y,z,w}的寄存器,并执行8位乘法和饱和度?
最终目标是将RGBA矢量乘以相应的颜色转换矩阵:
[ 66 129 25 0] [R]
[-38 -74 112 0] * [G]
[112 -94 -18 0] [B]
[0 0 0 0] [A]
由于
编辑1:这里是最终功能,使用浮点计算获得更多色彩精度,使用SSE将rgba图像转换为YUV444。功能需要1.9到3.5毫秒才能在intel i5 3570k上转换全高清图像,只使用一个线程(它非常容易线程化这个功能,它可以产生显着的性能提升):
void SSE_rgba2YUV444_FP(char* a, char* y, char* u, char* v)
{
__m128i mask = _mm_setr_epi8(0x00,0x04,0x08,0x0c, 0x01,0x05,0x09,0x0d, 0x02,0x06,0x0a,0x0e, 0x03,0x07,0x0b,0x0f); // Masque de mélange, chaque uint8 donne la position à donner (en offset en octet) du uint8 correspondant
float m[9] = {0.299, 0.587, 0.114, -0.1687, -0.3313, 0.5, 0.5, -0.4187, -0.0813}; // Dans le __m128i que l'on mélange
__m128i row[4];
for(int i=0; i<4; i++) {
row[i] = _mm_loadu_si128((__m128i*)&a[16*i]);
row[i] = _mm_shuffle_epi8(row[i],mask);
}
// row[i] = {rrrrggggbbbbaaaa} tous en uint8t
__m128i t0 = _mm_unpacklo_epi32(row[0], row[1]); //to = {rrrrrrrrgggggggg}
__m128i t1 = _mm_unpacklo_epi32(row[2], row[3]); //t1 = {rrrrrrrrgggggggg}
__m128i t2 = _mm_unpackhi_epi32(row[0], row[1]); //t2 = {bbbbbbbbaaaaaaaa}
__m128i t3 = _mm_unpackhi_epi32(row[2], row[3]); //t3 = {bbbbbbbbaaaaaaaa}
row[0] = _mm_unpacklo_epi64(t0, t1); // row[0] = {rrrrrrrrrrrrrrrr}
row[1] = _mm_unpackhi_epi64(t0, t1); // etc
row[2] = _mm_unpacklo_epi64(t2, t3);
__m128i v_lo[3], v_hi[3];
for(int i=0; i<3; i++) {
v_lo[i] = _mm_unpacklo_epi8(row[i],_mm_setzero_si128()); // On entrelace chaque row avec des 0, ce qui fait passer les valeurs
v_hi[i] = _mm_unpackhi_epi8(row[i],_mm_setzero_si128()); // de 8bits à 16bits pour pouvoir travailler dessus
}
__m128 v32_lo1[3], v32_hi1[3], v32_lo2[3], v32_hi2[3];
for(int i=0; i<3; i++) {
v32_lo1[i] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v_lo[i],_mm_setzero_si128()));
v32_lo2[i] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_lo[i],_mm_setzero_si128()));
v32_hi1[i] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v_hi[i],_mm_setzero_si128()));
v32_hi2[i] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_hi[i],_mm_setzero_si128()));
} // On a nos rgb sur 32 bits
__m128i yuv[3]; // {Y, U, V}
__m128 ylo1 = _mm_add_ps(_mm_mul_ps(v32_lo1[0], _mm_set1_ps(m[0])), _mm_add_ps(_mm_mul_ps(v32_lo1[1], _mm_set1_ps(m[1])), _mm_mul_ps(v32_lo1[2], _mm_set1_ps(m[2]))));
__m128 ylo2 = _mm_add_ps(_mm_mul_ps(v32_lo2[0], _mm_set1_ps(m[0])), _mm_add_ps(_mm_mul_ps(v32_lo2[1], _mm_set1_ps(m[1])), _mm_mul_ps(v32_lo2[2], _mm_set1_ps(m[2]))));
__m128 yhi1 = _mm_add_ps(_mm_mul_ps(v32_hi1[0], _mm_set1_ps(m[0])), _mm_add_ps(_mm_mul_ps(v32_hi1[1], _mm_set1_ps(m[1])), _mm_mul_ps(v32_hi1[2], _mm_set1_ps(m[2]))));
__m128 yhi2 = _mm_add_ps(_mm_mul_ps(v32_hi2[0], _mm_set1_ps(m[0])), _mm_add_ps(_mm_mul_ps(v32_hi2[1], _mm_set1_ps(m[1])), _mm_mul_ps(v32_hi2[2], _mm_set1_ps(m[2]))));
__m128i ylo1i = _mm_cvtps_epi32(ylo1);
__m128i ylo2i = _mm_cvtps_epi32(ylo2);
__m128i yhi1i = _mm_cvtps_epi32(yhi1);
__m128i yhi2i = _mm_cvtps_epi32(yhi2);
__m128i ylo = _mm_packus_epi32(ylo1i, ylo2i);
__m128i yhi = _mm_packus_epi32(yhi1i, yhi2i);
yuv[0] = _mm_packus_epi16(ylo, yhi);
ylo1 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_lo1[0], _mm_set1_ps(m[3])), _mm_add_ps(_mm_mul_ps(v32_lo1[1], _mm_set1_ps(m[4])), _mm_mul_ps(v32_lo1[2], _mm_set1_ps(m[5])))), _mm_set1_ps(128.0f));
ylo2 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_lo2[0], _mm_set1_ps(m[3])), _mm_add_ps(_mm_mul_ps(v32_lo2[1], _mm_set1_ps(m[4])), _mm_mul_ps(v32_lo2[2], _mm_set1_ps(m[5])))), _mm_set1_ps(128.0f));
yhi1 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_hi1[0], _mm_set1_ps(m[3])), _mm_add_ps(_mm_mul_ps(v32_hi1[1], _mm_set1_ps(m[4])), _mm_mul_ps(v32_hi1[2], _mm_set1_ps(m[5])))), _mm_set1_ps(128.0f));
yhi2 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_hi2[0], _mm_set1_ps(m[3])), _mm_add_ps(_mm_mul_ps(v32_hi2[1], _mm_set1_ps(m[4])), _mm_mul_ps(v32_hi2[2], _mm_set1_ps(m[5])))), _mm_set1_ps(128.0f));
ylo1i = _mm_cvtps_epi32(ylo1);
ylo2i = _mm_cvtps_epi32(ylo2);
yhi1i = _mm_cvtps_epi32(yhi1);
yhi2i = _mm_cvtps_epi32(yhi2);
ylo = _mm_packus_epi32(ylo1i, ylo2i);
yhi = _mm_packus_epi32(yhi1i, yhi2i);
yuv[1] = _mm_packus_epi16(ylo, yhi);
ylo1 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_lo1[0], _mm_set1_ps(m[6])), _mm_add_ps(_mm_mul_ps(v32_lo1[1], _mm_set1_ps(m[7])), _mm_mul_ps(v32_lo1[2], _mm_set1_ps(m[8])))), _mm_set1_ps(128.0f));
ylo2 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_lo2[0], _mm_set1_ps(m[6])), _mm_add_ps(_mm_mul_ps(v32_lo2[1], _mm_set1_ps(m[7])), _mm_mul_ps(v32_lo2[2], _mm_set1_ps(m[8])))), _mm_set1_ps(128.0f));
yhi1 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_hi1[0], _mm_set1_ps(m[6])), _mm_add_ps(_mm_mul_ps(v32_hi1[1], _mm_set1_ps(m[7])), _mm_mul_ps(v32_hi1[2], _mm_set1_ps(m[8])))), _mm_set1_ps(128.0f));
yhi2 = _mm_add_ps(_mm_add_ps(_mm_mul_ps(v32_hi2[0], _mm_set1_ps(m[6])), _mm_add_ps(_mm_mul_ps(v32_hi2[1], _mm_set1_ps(m[7])), _mm_mul_ps(v32_hi2[2], _mm_set1_ps(m[8])))), _mm_set1_ps(128.0f));
ylo1i = _mm_cvtps_epi32(ylo1);
ylo2i = _mm_cvtps_epi32(ylo2);
yhi1i = _mm_cvtps_epi32(yhi1);
yhi2i = _mm_cvtps_epi32(yhi2);
ylo = _mm_packus_epi32(ylo1i, ylo2i);
yhi = _mm_packus_epi32(yhi1i, yhi2i);
yuv[2] = _mm_packus_epi16(ylo, yhi);
_mm_storeu_si128((__m128i*)y,yuv[0]);
_mm_storeu_si128((__m128i*)u,yuv[1]);
_mm_storeu_si128((__m128i*)v,yuv[2]);
}
答案 0 :(得分:2)
以下是基于OP和Paul R的评论的解决方案。内在_mm_maddubs_epi16
要求签名的第二个参数是129
g
因素的问题。但是,我们可以通过这样做来解决这个问题
y = ((66-64)*r + (129-64)*g + (25-64)*b + -64*a) + (64*r + 64*g + 64*b + 64*a)
= (2*r + 65*g + -39*b -64*a) + 64(r + g + a)
使用这个我们只需要16位整数,我们可以一次计算16 y
个字节:
请注意,我最初使用的是128但是这导致了255*((25-128)-128)<-32768
以来的溢出。
__m128i yk = _mm_set1_epi32(0xc0d94102); -64,-39,64,2
__m128i y4[4];
for(int i=0; i<4; i++) {
__m128i a4 = _mm_loadu_si128((__m128i*)&a[16*i]);
__m128i t1 = _mm_maddubs_epi16(a4, yk);
__m128i t2 = _mm_maddubs_epi16(a4, _mm_set1_epi8(1));
t2 = _mm_slli_epi16(t2, 6); //multiply by 64
y4[i] = _mm_add_epi16(t1,t2);
}
short tmp[8];
_mm_storeu_si128((__m128i*)tmp, y4[0]);
__m128i y8_lo = _mm_hadd_epi16(y4[0], y4[1]);
__m128i y8_hi = _mm_hadd_epi16(y4[2], y4[3]);
y8_lo = _mm_add_epi16(y8_lo, _mm_set1_epi16(128));
y8_lo = _mm_srli_epi16(y8_lo, 8);
y8_lo = _mm_add_epi16(y8_lo, _mm_set1_epi16(16));
y8_hi = _mm_add_epi16(y8_hi, _mm_set1_epi16(128));
y8_hi = _mm_srli_epi16(y8_hi, 8);
y8_hi = _mm_add_epi16(y8_hi, _mm_set1_epi16(16));
__m128i y16 = _mm_packus_epi16(y8_lo,y8_hi);
以下是显示此作品的代码。我将结果与来自how to perform rgb yuv conversion in C/C++的公式(带有修改)进行了比较,即:
#define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)
#define RGB2Y(R, G, B) CLIP(( ( 66 * (0xff & R) + 129 * (0xff & G) + 25 * (0xff & B) + 128) >> 8) + 16)
代码:
#include <stdio.h>
#include <x86intrin.h>
#include <stdlib.h>
#define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)
#define RGB2Y(R, G, B) CLIP(( ( 66 * (0xff & R) + 129 * (0xff & G) + 25 * (0xff & B) + 128) >> 8) + 16)
void rgba2y_SSE_v1(char *a, char *b) {
__m128i yk = _mm_setr_epi16(66,129,25,0, 66,129,25,0);
__m128i out[4];
for(int i=0; i<4; i++) {
__m128i a4, lo, hi;
a4 = _mm_loadu_si128((__m128i*)&a[16*i]);
lo = _mm_unpacklo_epi8(a4,_mm_setzero_si128());
hi = _mm_unpackhi_epi8(a4,_mm_setzero_si128());
lo = _mm_madd_epi16(lo,yk);
lo = _mm_hadd_epi32(lo,lo);
hi = _mm_madd_epi16(hi,yk);
hi = _mm_hadd_epi32(hi,hi);
out[i] = _mm_unpackhi_epi64(lo,hi);
}
__m128i out_lo = _mm_packus_epi32(out[0], out[1]);
__m128i out_hi = _mm_packus_epi32(out[2], out[3]);
out_lo = _mm_add_epi16(out_lo, _mm_set1_epi16(128));
out_lo = _mm_srli_epi16(out_lo, 8);
out_lo = _mm_add_epi16(out_lo, _mm_set1_epi16(16));
out_hi = _mm_add_epi16(out_hi, _mm_set1_epi16(128));
out_hi = _mm_srli_epi16(out_hi, 8);
out_hi = _mm_add_epi16(out_hi, _mm_set1_epi16(16));
__m128i y16 = _mm_packus_epi16(out_lo,out_hi);
_mm_storeu_si128((__m128i*)b,y16);
}
void rgba2y_SSE_v2(char *a, char *b) {
__m128i yk = _mm_set1_epi32(0xc0d94102);
__m128i y4[4];
for(int i=0; i<4; i++) {
__m128i a4 = _mm_loadu_si128((__m128i*)&a[16*i]);
__m128i t1 = _mm_maddubs_epi16(a4, yk);
__m128i t2 = _mm_maddubs_epi16(a4, _mm_set1_epi8(1));
t2 = _mm_slli_epi16(t2, 6);
y4[i] = _mm_add_epi16(t1,t2);
}
short tmp[8];
_mm_storeu_si128((__m128i*)tmp, y4[0]);
__m128i y8_lo = _mm_hadd_epi16(y4[0], y4[1]);
__m128i y8_hi = _mm_hadd_epi16(y4[2], y4[3]);
y8_lo = _mm_add_epi16(y8_lo, _mm_set1_epi16(128));
y8_lo = _mm_srli_epi16(y8_lo, 8);
y8_lo = _mm_add_epi16(y8_lo, _mm_set1_epi16(16));
y8_hi = _mm_add_epi16(y8_hi, _mm_set1_epi16(128));
y8_hi = _mm_srli_epi16(y8_hi, 8);
y8_hi = _mm_add_epi16(y8_hi, _mm_set1_epi16(16));
__m128i y16 = _mm_packus_epi16(y8_lo,y8_hi);
_mm_storeu_si128((__m128i*)b,y16);
}
void rgba2yuv_SSE(char *a, char *b) {
__m128i mask = _mm_setr_epi8(0x00,0x04,0x08,0x0c, 0x01,0x05,0x09,0x0d, 0x02,0x06,0x0a,0x0e, 0x03,0x07,0x0b,0x0f);
short m[9] = {66, 129, 25, -38, -74, 112, 112, -94, -18};
__m128i row[4];
for(int i=0; i<4; i++) {
row[i] = _mm_loadu_si128((__m128i*)&a[16*i]);
row[i] = _mm_shuffle_epi8(row[i],mask);
}
__m128i t0 = _mm_unpacklo_epi32(row[0], row[1]);
__m128i t1 = _mm_unpacklo_epi32(row[2], row[3]);
__m128i t2 = _mm_unpackhi_epi32(row[0], row[1]);
__m128i t3 = _mm_unpackhi_epi32(row[2], row[3]);
row[0] = _mm_unpacklo_epi64(t0, t1);
row[1] = _mm_unpackhi_epi64(t0, t1);
row[2] = _mm_unpacklo_epi64(t2, t3);
__m128i v_lo[3], v_hi[3];
for(int i=0; i<3; i++) {
v_lo[i] = _mm_unpacklo_epi8(row[i],_mm_setzero_si128());
v_hi[i] = _mm_unpackhi_epi8(row[i],_mm_setzero_si128());
}
__m128i yuv[3];
for(int i=0; i<3; i++) {
__m128i yuv_lo, yuv_hi;
yuv_lo = _mm_add_epi16(_mm_add_epi16(
_mm_mullo_epi16(v_lo[0], _mm_set1_epi16(m[3*i+0])),
_mm_mullo_epi16(v_lo[1], _mm_set1_epi16(m[3*i+1]))),
_mm_mullo_epi16(v_lo[2], _mm_set1_epi16(m[3*i+2])));
yuv_lo = _mm_add_epi16(yuv_lo, _mm_set1_epi16(128));
yuv_lo = _mm_srli_epi16(yuv_lo, 8);
yuv_lo = _mm_add_epi16(yuv_lo, _mm_set1_epi16(16));
yuv_hi = _mm_add_epi16(_mm_add_epi16(
_mm_mullo_epi16(v_hi[0], _mm_set1_epi16(m[3*i+0])),
_mm_mullo_epi16(v_hi[1], _mm_set1_epi16(m[3*i+1]))),
_mm_mullo_epi16(v_hi[2], _mm_set1_epi16(m[3*i+2])));
yuv_hi = _mm_add_epi16(yuv_hi, _mm_set1_epi16(128));
yuv_hi = _mm_srli_epi16(yuv_hi, 8);
yuv_hi = _mm_add_epi16(yuv_hi, _mm_set1_epi16(16));
yuv[i] = _mm_packus_epi16(yuv_lo,yuv_hi);
}
_mm_storeu_si128((__m128i*)b,yuv[0]);
}
int main(void) {
char rgba[64];
char y1[16], y2[16], yuv[48];
for(int i=0; i<64; i++) rgba[i] = rand()%256;
rgba2y_SSE_v1(rgba,y1);
rgba2y_SSE_v2(rgba,y2);
rgba2yuv_SSE(rgba,yuv);
printf("RGB2Y: "); for(int i=0; i<16; i++) printf("%x ", 0xff & RGB2Y(rgba[4*i+0], rgba[4*i+1], rgba[4*i+2])); printf("\n");
printf("SSE_v1 "); for(int i=0; i<16; i++) printf("%x ", 0xff & y1[i]); printf("\n");
printf("SSE_v2 "); for(int i=0; i<16; i++) printf("%x ", 0xff & y2[i]); printf("\n");
printf("SSE_v3 "); for(int i=0; i<16; i++) printf("%x ", 0xff & yuv[i]); printf("\n");
}
输出:
RGB2Y: 99 ad 94 e3 9a a2 60 81 45 59 49 a5 aa 9b 60 4d
SSE_v1 99 ad 94 e3 9a a2 60 81 45 59 49 a5 aa 9b 60 4d
SSE_v2 99 ad 94 e3 9a a2 60 81 45 59 49 a5 aa 9b 60 4d
SSE_v3 99 ad 94 e3 9a a2 60 81 45 59 49 a5 aa 9b 60 4d
答案 1 :(得分:2)
这是一个解决方案,它同时找到Y,U和V,只使用垂直运算符
要做到这一点,我首先转换像这样的四个像素
rgbargbargbargba -> rrrrggggbbbbaaaa
使用带掩码的内在_mm_shuffle_epi8
。我这样做为16像素然后再转置它们
来自
row[0] : rrrrggggbbbbaaaa
row[1] : rrrrggggbbbbaaaa
row[2] : rrrrggggbbbbaaaa
ro2[3] : rrrrggggbbbbaaaa
到
row[0] : rrrrrrrrrrrrrrrr
row[1] : gggggggggggggggg
row[2] : bbbbbbbbbbbbbbbb
这与转换像这样的4x4整数矩阵的方式相同:
__m128i t0 = _mm_unpacklo_epi32(row[0], row[1]);
__m128i t1 = _mm_unpacklo_epi32(row[2], row[3]);
__m128i t2 = _mm_unpackhi_epi32(row[0], row[1]);
__m128i t3 = _mm_unpackhi_epi32(row[2], row[3]);
row[0] = _mm_unpacklo_epi64(t0, t1);
row[1] = _mm_unpackhi_epi64(t0, t1);
row[2] = _mm_unpacklo_epi64(t2, t3);
现在我将每一行分为高和低,并像这样扩展到16位
__m128i v_lo[3], v_hi[3];
for(int i=0; i<3; i++) {
v_lo[i] = _mm_unpacklo_epi8(row[i],_mm_setzero_si128());
v_hi[i] = _mm_unpackhi_epi8(row[i],_mm_setzero_si128());
}
最后,我像这样计算Y,U和V:
short m[9] = {66, 129, 25, -38, -74, 112, 112, -94, -18};
__m128i yuv[3];
for(int i=0; i<3; i++) {
__m128i yuv_lo, yuv_hi;
yuv_lo = _mm_add_epi16(_mm_add_epi16(
_mm_mullo_epi16(v_lo[0], _mm_set1_epi16(m[3*i+0])),
_mm_mullo_epi16(v_lo[1], _mm_set1_epi16(m[3*i+1]))),
_mm_mullo_epi16(v_lo[2], _mm_set1_epi16(m[3*i+2])));
yuv_lo = _mm_add_epi16(yuv_lo, _mm_set1_epi16(128));
yuv_lo = _mm_srli_epi16(yuv_lo, 8);
yuv_lo = _mm_add_epi16(yuv_lo, _mm_set1_epi16(16));
yuv_hi = _mm_add_epi16(_mm_add_epi16(
_mm_mullo_epi16(v_hi[0], _mm_set1_epi16(m[3*i+0])),
_mm_mullo_epi16(v_hi[1], _mm_set1_epi16(m[3*i+1]))),
_mm_mullo_epi16(v_hi[2], _mm_set1_epi16(m[3*i+2])));
yuv_hi = _mm_add_epi16(yuv_hi, _mm_set1_epi16(128));
yuv_hi = _mm_srli_epi16(yuv_hi, 8);
yuv_hi = _mm_add_epi16(yuv_hi, _mm_set1_epi16(16));
yuv[i] = _mm_packus_epi16(yuv_lo,yuv_hi);
}
有关此代码的工作示例,请参阅我的第一个答案和函数rgba2yuv_SSE
。