I don't always want to store a number of values
[inside my vector] that's a multiple of 4.
There, that should make things more clear. My statement is related to the hack where each element of the vector stores 4 floating point values instead of just one so that QVector gives the proper data alignment.
If it's still not clear I'll quote this as well:
...I need the QVector().data() pointer to be 16-byte memory aligned
QVector allocates a contiguous memory region for its values and the pointer resulted from that memory allocation needs to be 16-byte memory aligned. The memory alignment has nothing to do with the number of elements inside the vector.
I simply want QVector<float> to return a 16-byte aligned data pointer but it won't because sizeof(float) == 4 (in my case) and not 16. Actually things get more complicated because Qt seems to use the Q_ALIGNOF macro to determine the alignment. So, for example, regular 32-bit code:
struct test0 { float f;};
struct test1 { float f; double d;};
struct test2 { float f[4]; };
union test3 { float f[4]; __m128 spfp; };
qDebug() << sizeof(test0) << Q_ALIGNOF(test0);
qDebug() << sizeof(test1) << Q_ALIGNOF(test1);
qDebug() << sizeof(test2) << Q_ALIGNOF(test2);
qDebug() << sizeof(test3) << Q_ALIGNOF(test3);
struct test0 { float f;};
struct test1 { float f; double d;};
struct test2 { float f[4]; };
union test3 { float f[4]; __m128 spfp; };
qDebug() << sizeof(test0) << Q_ALIGNOF(test0);
qDebug() << sizeof(test1) << Q_ALIGNOF(test1);
qDebug() << sizeof(test2) << Q_ALIGNOF(test2);
qDebug() << sizeof(test3) << Q_ALIGNOF(test3);
To copy to clipboard, switch view to plain text mode
For Visual Studio Q_ALIGNOF defaults to the __alignof operator, I assume that the same behavior is used for all compilers. The first qDebug value outputs the size of the structure/union and the second value gives you the alignment Qt decides to use. Output goes as follows:
4 4 // on my machine sizeof(float) == 4 and sizeof(double) == 8
16 8 // sizeof is 16 (8 + 4 + (extra 4 padding, bonus from Visual C++))
// Q_ALIGNOF returns the largest data type (double)
16 4 // sizeof is 16 (4 floats)
// Q_ALIGNOF returns only the base data type's sizeof, which is 4
16 16 // sizeof is 16 and Q_ALIGNOF sees the __m128 and returns 16; unfortunately now I'm forced to store 4 values at once (even if I only need 1, 2 or 3; so a QVector using this type would always store a multiple of 4 floating point values)
4 4 // on my machine sizeof(float) == 4 and sizeof(double) == 8
16 8 // sizeof is 16 (8 + 4 + (extra 4 padding, bonus from Visual C++))
// Q_ALIGNOF returns the largest data type (double)
16 4 // sizeof is 16 (4 floats)
// Q_ALIGNOF returns only the base data type's sizeof, which is 4
16 16 // sizeof is 16 and Q_ALIGNOF sees the __m128 and returns 16; unfortunately now I'm forced to store 4 values at once (even if I only need 1, 2 or 3; so a QVector using this type would always store a multiple of 4 floating point values)
To copy to clipboard, switch view to plain text mode
Bookmarks