Flush to Zero when a computation results in a denormal number in linux

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 03:11:48

问题


A computation in my C code is producing a gradual underflow, and when it happens the program is terminating with SIGFPE. How can I flush the result to zero when a gradual underflow (Denormal) results from a computation, and not terminate the execution? (I am working on a redhat linux machine). Thanks.


回答1:


You haven't specified the architecture - I'm going to take a guess that it's a relatively recent x86[-64], in which case you can manipulate the SSE control register using _mm_getcsr, _mm_setcsr, specified in the <xmmintrin.h> (or <immintrin.h>) header.

The 'flush-to-zero' bit is set with 0x8000, and 'denormals-are-zero' (for inputs / src) is set with 0x0040.

_mm_setcsr(_mm_getcsr() | 0x8040); or with <pmmintrin.h> (SSE3) :

_mm_setcsr(_mm_getcsr() | (_MM_FLUSH_ZERO_ON | _MM_DENORMALS_ZERO_ON));

This might make it easier to determine the source of the underflow, but it shouldn't be considered a solution, since the FP environment is no longer IEEE-754 compliant.



来源:https://stackoverflow.com/questions/17981908/flush-to-zero-when-a-computation-results-in-a-denormal-number-in-linux

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