samedi 21 avril 2018

C++17 array alignment for AVX2 Intrinsics

This is my first time posting a question on StackOverflow (actually first time posting a CS question in general) so I apologize if it's not well formatted or confusing to follow >.<

I haven't been using C++ for long so this may seem like a silly question, but I know that C++11 added in support for dynamic alignment with alignas. However, all the examples I can find tend to use structs, e.g.

struct alignas(32) vec8 {
    float f[8];
}

So far, I have been using this struct wrapped in std::vectors to compose larger arrays for usage with AVX2 intrinsics:

std::vector<vec8*> array(size / 8)

However, this leads to very messy usage when I want to access a particular element in the array:

array[(int)(location / 8)]->f[location % 8]

Is there a way to define a custom method for retrieving a specific element with these structs?

In addition, is this the best way to prepare aligned arrays for AVX2 intrinsics? I need to pass these arrays between functions quite frequently, create new arrays in local scope to be used in wider scope, and be able to access both individual elements and groups of 8 elements. This std::vector wrapped struct method seems really tedious for freeing memory after usage, accessing elements, and generally handling large arrays in performance-efficient but convenient ways.

Thanks for reading!

Aucun commentaire:

Enregistrer un commentaire