_mm_fmadd_pd程序收到信号SIGILL,非法指令

时间:2017-03-17 09:36:46

标签: c++ x86 simd intrinsics fma

我收到以下代码的奇怪错误:

#include <assert.h>
#include <stdio.h>
#include <immintrin.h>

inline static double myfma(double x,double y, double z) {
    double r; // result                                                                                                                                                     
    __m128d xx, yy, zz,rr;

    xx = _mm_set_sd(x);// xx[0]=x, xx[1]=undefined                                                                                                                          
    yy = _mm_set_sd(y);// yy[0]=y, yy[1]=undefined                                                                                                                          
    zz = _mm_set_sd(z);// zz[0]=z, zz[1]=undefined                                                                                                                          
    r = _mm_cvtsd_f64(_mm_fmadd_pd(xx,yy,zz));

    return r;
}

void testfma() {
    double x, y, z, res;
    x = 1.0;
    y = 2.0;
    z = 3.0;

    res =  myfma(x,y,z);
    printf("test: res = x*y + z \n");
    printf("    x: %g\n", x);
    printf("    y: %g\n", y);
    printf("    z: %g\n", z);
    assert(res == 5.0);
}


int main() {
    testfma();
    return 0; 
}

将代码编译为:

g++ test.cpp -o a.out -std=c++11 -mavx2 -mfma  -march=native -g

当我运行可执行文件时,我收到消息:

Illegal instruction (core dumped)

使用gdb获取更多详细信息:

gdb ./a.out
(gdb) r
(gdb) r
Starting program: ....

Program received signal SIGILL, Illegal instruction.
0x000000000040067d in _mm_fmadd_pd(double __vector(2), double __vector(2), double __vector(2)) (__C=..., __B=..., __A=...)
    at /usr/lib/gcc/x86_64-linux-gnu/5/include/fmaintrin.h:42
42                                                 (__v2df)__C);

但是当使用valgrind时如下:

valgrind ./a.out
==9825== Memcheck, a memory error detector
==9825== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9825== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright 

info
==9825== Command: ./helios.x
==9825== 
test: res = x*y + z 
    x: 1
    y: 2
    z: 3
    res: 5
==9825== 
==9825== HEAP SUMMARY:
==9825==     in use at exit: 0 bytes in 0 blocks
==9825==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==9825== 
==9825== All heap blocks were freed -- no leaks are possible
==9825== 
==9825== For counts of detected and suppressed errors, rerun with: -v
==9825== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

该计划似乎正在运作。我在这里缺少什么?如何以强大的方式使用_mm_fmadd_pd?无论是在英特尔还是AMD处理器上运行,都可以使示例正常工作吗?是否可以使用g ++或icpc进行编译?

1 个答案:

答案 0 :(得分:0)

我的猜测是你的CPU不支持FMA指令。它在valgrind下没有失败的原因是因为valgrind可以模仿某些指令。

如果您只想要SISD,可能需要考虑使用std::fmaWith gcc it generates an inline FMA instruction,但如果您为非FMA目标进行编译,那么它将fall back to a non-FMA implementation