If I want to process data in a std::vector with SSE, I need 16 byte alignment. How can I achieve that? Do I need to write my own allocator? Or does the default allocator already align to 16 byte boundaries?
If I want to process data in a std::vector with SSE, I need 16
Share
C++ standard requires allocation functions (
malloc()andoperator new()) to allocate memory suitably aligned for any standard type. As these functions don’t receive the alignment requirement as an argument, in practice it means that the alignment for all allocations is the same, and is that of a standard type with the largest alignment requirement, which often islong doubleand/orlong 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()ormemalign()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:
Moreover, in C++17 the standard allocators have been updated to respect type’s alignment, so you can simply do:
Or (no heap allocation involved and supported in C++11):