Search code examples
qtqt5vectorizationmemory-alignmentauto-vectorization

Memory aligned QVector().data()


I'm writing a program using Qt5, and I need to allocate a QVector <float> having its data() pointer 32-byte aligned.

Is there anyway I could do this without modifying the Qt library itself?

My code looks something like this:

QVector <float> vec;
vec.resize(n);
float *wricker_ptr = wricker.data(); // this should be 32-byte aligned
for (int i=0; i<n; i++)
{
    wricker_ptr[i] = /* some computed value */;
}

I'm using Intel's C++ Compiler.


Solution

  • Two solutions come to mind:

    1. Forget it: use std::vector and a suitable allocator. QVector's data payload is allocated with alignof(T)
    2. The 32 byte alignment smells of SIMD processing, so you could use a QVector<__m256i> or similar and reinterpret_cast in and out.

    ¹ not entirely true, see http://thread.gmane.org/gmane.comp.lib.qt.devel/22326/focus=22596