5

I am trying to make some SIMD code that works on MSVC compile with Clang on Xcode 6. Unfortunately I get an error where the array access operators have been overloaded in a custom vector class that I am unable to fix. The vector template has specializations for arrays of length 4 and 8 which use SIMD intrinsics, but the array access operator to return a reference to an element of the vector (for updating that element) gives me an error on clang "non-const reference cannot bind to vector element".

Full source code

The overloaded operators:

#ifdef _MSC_VER
  float operator[](int idx) const { return v.m256_f32[idx]; } // m256_f32 MSVC only
  float& operator[](int idx) { return v.m256_f32[idx]; }
#else
  float operator[](int idx) const { return v[idx]; }
  float& operator[](int idx) { return v[idx]; }
#endif

The error from Clang:

non-const reference cannot bind to vector element
  float& operator[](int idx) { return v[idx]; }
                                      ^~~~~~

2 Answers 2

5

I think you'll probably need to use a union for this, e.g.:

union U {
    __m256 v;
    float a[8];
};

and the value operator would then be:

float operator[](int idx) const { U u = { v }; return u.a[idx]; }

The reference operator is trickier though, and the only way I can see to do it is via type punning, so with the usual caveats:

float& operator[](int idx) { return ((float *)&v)[idx]; }

I'm not even sure this will compile, and you may need -fno-strict-aliasing.

To avoid this nastiness I suppose you could consider changing you member variable from __m256 v; to U u;.

I just hope you're not doing this kind of thing inside any performance-critical loops.

7
  • The whole reason for using simd is performance so don't want to do anything that will drastically slow it down!
    – olilarkin
    Oct 25, 2014 at 8:39
  • @olilarkin: sure - but if you're accessing individual elements of a vector within a performance-critical loop then you're doing SIMD all wrong anyway - typically this kind of operation should only come after a loop, where it's not performance-critical.
    – Paul R
    Oct 25, 2014 at 9:15
  • sorry i misunderstood you... the elements are not accessed individually in the performance-critical part, just in the initialization
    – olilarkin
    Oct 25, 2014 at 9:40
  • 1
    Cool - be careful about strict aliasing though. Consider making your member variable a union as suggested above.
    – Paul R
    Oct 25, 2014 at 9:43
  • 2
    Related: stackoverflow.com/q/24787268/ @PaulR, you are right that technically this violates strict aliasing requirements. But in practice, every existing compiler (to my knowledge) allows SIMD vector types to alias arrays of their components. So this is actually safe in practice even without -fno-strict-aliasing
    – Nemo
    Nov 16, 2014 at 16:03
2

This only works for reading, so you wouldn't be able to return a reference to float. But this should works with runtime values:

  • >= SSE4.1+: use PEXTRD intrinsics like _mm_extract_ps
  • before SSE4.1: _mm_cvtsi128_si32 + _mm_srli_si128 to access any 32-bit item (then case with a union)

Your Answer

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

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