C++ standard requires allocation functions (malloc()
and operator new()
) to allocate memory suitably aligned for any standard type. As these functions don't receive the alignment requirement as an argument, on practice it means that the alignment for all allocations is the same and is the alignment of a standard type with the largest alignment requirement, which often is long double
and/or long long
(see boost max_align union).
Vector instructions, such as SSE and AVX, have stronger alignment requirements (16-byte aligned for 128-bit access and 32-byte aligned for 256-bit access) than that provided by the standard C++ allocation functions. posix_memalign()
or memalign()
can be used to satisfy such allocations with stronger alignment requirements.
In C++17 the allocation functions accept an additional argument of type std::align_val_t
.
You can make use of it like:
#include <immintrin.h>
#include <memory>
#include <new>
int main() {
std::unique_ptr<__m256i[]> arr{new(std::align_val_t{alignof(__m256i)}) __m256i[32]};
}
Moreover, in C++17 the standard allocators have been updated to respect type's alignment, so you can simply do:
#include <immintrin.h>
#include <vector>
int main() {
std::vector<__m256i> arr2(32);
}
Or (no heap allocation involved and supported in C++11):
#include <immintrin.h>
#include <array>
int main() {
std::array<__m256i, 32> arr3;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…