https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100927
Bug ID: 100927
Summary: [sse2] floating point to integer conversion functions
incorrect results w/ NaN constants + optimization
Product: gcc
Version: 11.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
_mm_cvttpd_epi32, _mm_cvttpd_pi32, _mm_cvttps_epi32, and _mm_cvttsd_si32 are
supposed to return INT32_MIN for NaN inputs. However, when compiled with
optimization on GCC, if the values are known at compile time NaN inputs result
in 0 in the output.
Here is a quick test case, using _mm_cvttpd_epi32:
#include <xmmintrin.h>
#include <stdio.h>
int main(void) {
static const double values[] = {
__builtin_nan(""), -__builtin_nan("")
};
int32_t res[4];
_mm_storeu_si128((__m128i*) res, _mm_cvttpd_epi32(_mm_loadu_pd(values)));
for (int i = 0 ; i < 4 ; i++) {
printf("%d\n", res[i]);
}
return 0;
}
Compile with `gcc -O1 -o test test.c` and you get all zeros, `gcc -O0 -o test
test.c` and the first two elements of the result are INT32_MIN as they should
be. Changing the const to volatile (and adding -Wno-discarded-qualifiers)
"fixes" the issue.