我尝试了 3 个编译器:MS Visual Studio 2012、gcc481 和 Intel icl 13.1。正如您指出的那样,他们都发出警告。我发现 gcc 和 MS 都自动为 tmp1 生成初始化代码,即使它们警告缺少初始化。MS 编译器生成不需要的内存访问:movaps xmm0,xmmword ptr [rsp]
. Gcc 生成更高效的xorps xmm0,xmm0
. 因此,在 gcc 的情况下,添加tmp1=_mm_setzero_ps()
会消除警告并生成与不添加完全相同的代码。在 MS 的情况下,添加tmp1=_mm_setzero_ps()
使代码更短并且可能更快。只有英特尔编译器足够聪明,可以避免不必要的初始化。这是 MS 和 gcc 编译器的一种可能的解决方法:
__m128 tmp1 = _mm_loadh_pi(_mm_load_ps (src), (__m64*)(src + 4));
代码生成是:
movaps xmm0,xmmword ptr [rcx]
movhps xmm0,qword ptr [rcx+10h]
它看起来更短,但应该进行基准测试以确保它更快。
09/12/2013:不同警告抑制思路的测试代码:
#include <xmmintrin.h>
#include <stdint.h>
#include <stdio.h>
//---------------------------------------------------------------------------
// original code from http://download.intel.com/design/PentiumIII/sml/24504301.pdf
__m128 func1 (float *src)
{
__m128 tmp1;
tmp1 = _mm_loadh_pi(_mm_loadl_pi(tmp1, (__m64*)(src)), (__m64*)(src+ 4));
return tmp1;
}
//---------------------------------------------------------------------------
// original code plus tmp1 initialization
__m128 func2 (float *src)
{
__m128 tmp1 = _mm_loadh_pi(_mm_loadl_pi (_mm_setzero_ps (), (__m64*)(src)), (__m64*)(src + 4));
return tmp1;
}
//---------------------------------------------------------------------------
// use redundant load to eliminate warning
__m128 func3 (float *src)
{
__m128 tmp1 = _mm_loadh_pi(_mm_load_ps (src), (__m64*)(src + 4));
return tmp1;
}
//---------------------------------------------------------------------------
static void dump (void *data)
{
float *f16 = data;
int index;
for (index = 0; index < 4; index++)
printf ("%g ", f16 [index]);
printf ("\n");
}
//---------------------------------------------------------------------------
int main (void)
{
float f [8] = {1, 2, 3, 4, 5, 6, 7, 8};
__m128 tmp;
tmp = func1 (f);
dump (&tmp);
tmp = func2 (f);
dump (&tmp);
tmp = func3 (f);
dump (&tmp);
return 0;
}
构建命令:
gcc -O3 -Wall -Wfatal-errors sample.c -osample.exe
objdump -Mintel --disassemble sample.exe > disasm.txt
cl -Ox -Zi -W4 sample.c
dumpbin -disasm -symbols sample.exe > disasm.txt
icl -Ox -Zi sample.c
dumpbin -disasm -symbols sample.exe > disasm.txt