让我以此作为开场白。我在 ASM 方面的经验非常有限,在 SIMD 方面的经验更是少之又少。
但碰巧我有以下 MMX/SSE 优化代码,我想移植到 AltiVec 指令以在 PPC/Cell 处理器上使用。
这可能是一个很大的问题。尽管它只有几行代码,但我在试图弄清楚这里发生的事情时遇到了无穷无尽的麻烦。
原函数:
static inline int convolve(const short *a, const short *b, int n)
{
int out = 0;
union {
__m64 m64;
int i32[2];
} tmp;
tmp.i32[0] = 0;
tmp.i32[1] = 0;
while (n >= 4) {
tmp.m64 = _mm_add_pi32(tmp.m64,
_mm_madd_pi16(*((__m64 *)a),
*((__m64 *)b)));
a += 4;
b += 4;
n -= 4;
}
out = tmp.i32[0] + tmp.i32[1];
_mm_empty();
while (n --)
out += (*(a++)) * (*(b++));
return out;
}
关于如何重写它以使用 AltiVec 指令的任何提示?
我的第一次尝试(一次非常错误的尝试)看起来像这样。但它并不完全(甚至是远程)正确。
static inline int convolve_altivec(const short *a, const short *b, int n)
{
int out = 0;
union {
vector unsigned int m128;
int i64[2];
} tmp;
vector unsigned int zero = {0, 0, 0, 0};
tmp.i64[0] = 0;
tmp.i64[1] = 0;
while (n >= 8) {
tmp.m128 = vec_add(tmp.m128,
vec_msum(*((vector unsigned short *)a),
*((vector unsigned short *)b), zero));
a += 8;
b += 8;
n -= 8;
}
out = tmp.i64[0] + tmp.i64[1];
#endif
while (n --)
out += (*(a++)) * (*(b++));
return out;
}