1
votes

Everything I've read about using C/C++ intrinsic types for SIMD capabilities like MMX and SSE indicate that you should use those as opaque types and not reference the internals directly.

However, when I look at many examples, they work by taking (explicitly aligned) pointers to raw data and reinterpreting them as pointers to the intrinsic types prior to doing the work. But, by using pointers to intrinsic types, and especially when aliasing other data as those types, are you not violating that rule?

1
Normally the pointers to the raw data would be a unsigned char* or void* which has special rules when it comes to pointer aliasing. Once you have the actual intrinsic type, you'd use an intrinsic type pointer to reference it. - Tony The Lion

1 Answers

5
votes

What you appear to have discovered is that most examples suck.

It's actually fairly safe to reference the "internals" of a SSE type (as long as it compiles), and in practice you can just alias them in memory to a "normal" datatype in memory, and a lot of people do it because it makes the code a bit more convenient to write and to use.

But it pretty much ruins the point of using SIMD. It prevents a lot of optimizations, and it means that the compiler has to constantly store/load the SIMD datatypes, rather than just keeping them in a SIMD register.

You can do this, but you should not. And as you've discovered, many people read "should" as "feel free to do this".