Converting from __m128 to __m128i results in wrong

2019-09-05 15:27发布

问题:

I need to convert a float vector (__m128) to an integer vector (__m128i), and I am using _mm_cvtps_epi32, but I am not getting the expected value. Here is a very simple example:

__m128 test = _mm_set1_ps(4.5f);
__m128i test_i = _mm_cvtps_epi32(test);

The debugger output I get:

(lldb) po test
 ([0] = 4.5, [1] = 4.5, [2] = 4.5, [3] = 4.5)
(lldb) po test_i
 ([0] = 17179869188, [1] = 17179869188)
(lldb) 

As you can see, the resulting integer is.. 17179869188? From 4.5? And why are there only two values? _mm_cvtps_epi32 should convert 4 packed 32-bit floating-point values to 4 packed 32-bit integers.

回答1:

Debugger, in this example, interprets the __m128i value as two 64-bit integers, as opposed to four 32-bit ones expected by you. The actual conversion is correct.

In your code you need to explicitly specify how to interpret the SIMD value, for example: test_i.m128i_i32[0]