I have the following piece of code that crashes on assert(!isnan(x))
when compiled with clang. If I compile using -DWITH_MMX=0
, it runs fine. I observe same behaviour on Compiler Explorer and locally on my macOS.
I don't understand why the assignment of 42.0
to a long double
variable produces NaN
when I the program uses MMX intrinsics.
I've tried to use Compiler Explorer to figure this out, but I don't get it. Could someone help me understand what's happening here?
// cc -O0 -DWITH_MMX=1 -o nan nan.c
#include <mmintrin.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
int main()
{
#if WITH_MMX
__m64 a = _m_from_int(4);
__m64 b = _m_from_int(8);
__m64 ab = _m_paddb(a, b);
int c = _m_to_int(ab);
assert(c == 12);
#endif
long double x = 42.0L;
assert(!isnan(x)); // 42.0 should not be NaN
printf("done\n");
}