Is there a way to enforce STL container alignment to specific byte, using attribute((aligned))perhaps? the target compilers are not Microsoft Visual C++.
What libraries, if any, provide specialized templates of STL algorithms which have specific explicit vectorization, e.g. SSE. My compilers of interest are g++, Intel, and IBM XL.
You need a custom allocator that returns aligned storage. That ought to solve your problem.
Instead of writing your own allocator, as suggested before, you can use
boost::alignment::aligned_allocator
.With STL containers, you can provide your own allocator via an optional template parameter. I wouldn't recommend writing an entire allocator from scratch, but you could write one that's just a wrapper around
new
anddelete
but ensures that the returned memory meets your alignment requirement. (E.g., if you needn
bytes with 16-byte alignment, you usenew
to allocaten + 15
bytes and return a pointer to the first 16-byte aligned address in that block.)But it might be enough just to add the alignment attribute to the element type. That's outside the scope of the standard, so you'd have to check your compiler documentation and try it.
You need a custom allocator passed. You can build one over the
std::allocator
quite easily:The only thing lacking here is the
aligned_malloc
,aligned_realloc
andaligned_free
. You either need to implement them yourself (shouldn't be that hard), or find versions of those on the internet (I've seen at least one in OGRE engine).You've already gotten some good answers, but it seems like it's worth adding that C++ 0x includes an
std::align()
, which should making implementing things like this a bit easier.