Martin Perry
Martin Perry

Reputation: 9527

C++ - align all allocated double arrays to 32bytes

I have a quite a large code and I want to optimize some operations by AVX instructions. Based on my tests, improvement should be up to 4x.

However, the code uses plain old arrays (no std::vector) with new operator.

double * tmp = new double[size];

The problem is, for AVX, I need all double arrays aligned to 32-bytes boundary. The solution is to replace all new with aligned_alloc (or _aligned_malloc in MSVC) and also replace release functions with adequate one. The problem with this is I have to go through all the code and find all new, hoping not to forget one and in future everyone must not forget to use aligned alocator.

Is there any other way? I have thought of overload new operator only for double but I am not sure if this is the correct way and if so, how to do this correctly / safely.

I cannot switch to std::vector with own aligned alocator because of some external C-only libs that take array as an input params.

Upvotes: 2

Views: 212

Answers (1)

MSalters
MSalters

Reputation: 179917

Is there any other way? Yes, _mm_loadu_* and _mm_storeu_*, where the u stands for unaligned.

Upvotes: 4

Related Questions