Why is the size of StaticVector<float, 3> is 16 bytes, not 12?

Issue #189 resolved
Віталій Карпенко created an issue
#define BLAZE_USE_PADDING 0
#include "blaze/Blaze.h"

typedef float vec3_t[3];

int main() {
    blaze::StaticVector<float, 3> vec1;
    vec3_t vec2;
    printf("vec1: %d bytes, vec2: %d bytes\n", sizeof(vec1), sizeof(vec2));
}

Result: vec1: 16 bytes, vec2: 12 bytes

Comments (5)

  1. Klaus Iglberger

    Hi Віталій!

    Thanks for raising this issue. The reason that a StaticVector<float,3> requires 16 bytes instead of 12 is because vectorization is still turned on. Vectorization requires an alignment of 16 bytes for SSE or even 32 bytes for AVX (we assume SSE for the following discussion). Since float is a vectorizable data type a StaticVector<float,3> is subject to these alignment restrictions and the size is padded to the next multiple of 16.

    In case padding is turned off it is no longer possible to use a StaticVector<float,3> in vectorized operations. Hence it is reasonable to assume that the alignment restrictions are lifted and its size is reduced to 12. However, unintuitively a StaticVector<float,5> would be subject to the restrictions and would require 32 bytes since the first four float values could be uses for vector instructions.

    using T1 = StaticVector<float,3>;
    using T2 = StaticVector<float,5>;
    
    sizeof( T1 );  // evaluates to 16, could be 12
    sizeof( T2 );  // evaluates to 32 and should not be 20 as long as vectorization is turned on
    

    We will discuss the implications of introducing this special case. We apologize for the delay to resolve this issue. Thanks again for raising this issue,

    Best regards,

    Klaus!

  2. Klaus Iglberger

    Commit 5c7a55c reworks the alignment evaluation of the StaticVector class template. Commits 9c852e6, 635fdab, and 8a11e27 do the same for the HybridVector, StaticMatrix, and HybridMatrix class templates, respectively. The fix is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.4.

  3. Klaus Iglberger

    Hi Віталій!

    In Blaze 3.7 we’ve extended the interface for StaticVector and StaticMatrix (see issue #134). With this feature you’ll be able to explicitly specify whether you need alignment and/or padding. This will give you full control over the size of a StaticVector and StaticMatrix instance. Hopefully this is useful for you,

    Best regards,

    Klaus!

  4. Log in to comment