Capture SIGFPE from SIMD instruction

我是研究僧i 提交于 2019-12-06 21:13:37

Two things.

First, I misunderstood the documentation. Exceptions need to be unmasked to be caught. Calling _mm_setcsr(0x00001D80); will allow SIGFPE to fire on divide by zero.

Second, gcc was optimizing out my divide instruction even with -O0.

Given source line

_mm_div_ss(s1, s2);

Compiling with gcc -S -O0 -msse2 a.c gives

76     movaps  -24(%ebp), %xmm0
77     movaps  %xmm0, -72(%ebp)
78     movaps  -40(%ebp), %xmm0
79     movaps  %xmm0, -88(%ebp)

a1     subl    $12, %esp        ; renumbered to show insertion below
a2     pushl   $.LC2
a3     call    puts
a4     addl    $16, %esp

While source line

s2 = _mm_div_ss(s1, s2); // add "s2 = "

gives

76     movaps  -24(%ebp), %xmm0
77     movaps  %xmm0, -72(%ebp)
78     movaps  -40(%ebp), %xmm0
79     movaps  %xmm0, -88(%ebp)
       movaps  -72(%ebp), %xmm0
       divss   -88(%ebp), %xmm0
       movaps  %xmm0, -40(%ebp)
a1     subl    $12, %esp
a2     pushl   $.LC2
a3     call    puts
a4     addl    $16, %esp

With those changes, the SIGFPE handler is called according to the divide-by-zero flag in MXCSR.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!