10

On Linux and Mac, one can do

__m128 x;
__m128i n = (__m128i)x;

This operation copies the bit representation of x to n, and is useful for implementing various branch-free conditional operations operating on SSE floating point registers. On MSVC 11, it gives

eikonal-generated.h(1228) : error C2440: 'type cast' : cannot convert from '__m128' to '__m128i'; No constructor could take the source type, or constructor overload resolution was ambiguous

What is the equivalent in Microsoft Visual Studio?

Note that I am not asking for the standard float-to-int conversion function _mm_cvtepi32_ps, which does numerically meaningful conversion.

1 Answer 1

20

With MSVC you need to use:

_mm_castsi128_ps for bitwise cast from __m128i to __m128

and

_mm_castps_si128 for bitwise cast from __m128 to __m128i

For other compilers (gcc, ICC, et al) you can just use normal casts.

7
  • You can actually get away without any casts at all if you use -flax-vector-conversions (gcc, icc) Nov 30, 2012 at 0:24
  • Now the question would be if one can or must use normal casts on other compilers, i.e. do these cast intrisics exist in gcc, icc and all the others, too, and thus provide a platform-independent solution (which is one of the major points of intrinsics at all)? I for myself just always went the ugly way of a reinterpret_cast (or its C pointer cast version), which the compiler of course also made into a noop, yet it doesn't look too good and an intrinsic would be a clearer option. Aug 14, 2013 at 8:47
  • @Christian: unfortunately there are various minor discrepancies between MS and the civilized world when it comes to SSE intrinsics etc - I have a common header that I use for portable SSE code where I implement wrappers and various macros which get conditionally compiled for the different platforms.
    – Paul R
    Aug 14, 2013 at 9:11
  • @PaulR So those casts are not supported on gcc, icc, ...? Aug 14, 2013 at 9:44
  • 1
    @ChristianRau: I'm late to the party, but these are supported since GCC 4 (according to the bug tracker; I have verified it in 4.9). Oct 20, 2015 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.