我正在做一些SSE代码(将4个浮点数乘以4个浮点数)与传统C代码做同样的事情。我认为我的基准测试代码在某种程度上必须是不正确的,因为它似乎认为非SSE代码比SSE快2-3倍。对标SSE指令
有人能告诉我下面的基准代码有什么问题吗?也许会提出另一种准确显示SSE和非SSE代码速度的方法。
#include <time.h>
#include <string.h>
#include <stdio.h>
#define ITERATIONS 100000
#define MULT_FLOAT4(X, Y) ({ \
asm volatile (\
"movaps (%0), %%xmm0\n\t" \
"mulps (%1), %%xmm0\n\t" \
"movaps %%xmm0, (%1)" \
:: "r" (X), "r" (Y)); })
int main(void)
{
int i, j;
float a[4] __attribute__((aligned(16))) = { 10, 20, 30, 40 };
time_t timer, sse_time, std_time;
timer = time(NULL);
for(j = 0; j < 5000; ++j)
for(i = 0; i < ITERATIONS; ++i) {
float b[4] __attribute__((aligned(16))) = { 0.1, 0.1, 0.1, 0.1 };
MULT_FLOAT4(a, b);
}
sse_time = time(NULL) - timer;
timer = time(NULL);
for(j = 0; j < 5000; ++j)
for(i = 0; i < ITERATIONS; ++i) {
float b[4] __attribute__((aligned(16))) = { 0.1, 0.1, 0.1, 0.1 };
b[0] *= a[0];
b[1] *= a[1];
b[2] *= a[2];
b[3] *= a[3];
}
std_time = time(NULL) - timer;
printf("sse_time %d\nstd_time %d\n", sse_time, std_time);
return 0;
}
感谢您的回答。 :)但是,我真的需要一个使用SSE进行图像处理的非常快速的4浮点乘法器。如果我使用的方法存在缺陷,您能否提出另一种方法来利用SSE的力量,但不会导致这种减速。我读过SSE实际上是为我想到的那种图像处理而设计的,所以肯定必须成为我想要的方式吗? (我需要像alpha混合等操作的快速4float倍数) – horseyguy 2009-11-14 16:22:15
我已更新帖子以包含答案。 – ypsu 2009-11-14 17:08:40
谢谢,不好看起来'商店转发摊位',并试图包裹我的头在这里发生了什么 – horseyguy 2009-11-14 17:32:02