pic11
pic11

Reputation: 14973

Strict aliasing and __m128i type

When using SSE2 intrinsic functions to do bit-wise operations, one has to cast pointers from int* to __m128i*. Does this code break strict aliasing rule?

void bit_twiddling_func(int size, int const* input, int* output) {
    const __m128* x = (const __m128*)input;
    const __m128* y = (const __m128*)output;

    for (int i=0; i < size/4; ++i, ++x, ++y) {
        __m128i x4 = _mm_load_si128(x); // load 4 integers

        // do some bit twiddling

        _mm_store_si128(y, x4); // store 4 integers
    }
}

Thank you!

Upvotes: 2

Views: 981

Answers (1)

brandx
brandx

Reputation: 1053

Yes; it breaks strict aliasing rules. Two different types can't point to the same location in memory. Here you have x pointing to input and y pointing to output, but they're of differing types.

You could change the signature of your function to take __m128* parameters, but it's probably easiest to leave it be. It'll likely work just fine if you're careful that the input/output arguments point to memory with the proper alignment and size constraints (i.e. they should each point to something where your loop doesn't index off the end or load uninitialized data.)

Upvotes: 2

Related Questions