views:

409

answers:

4

Hello

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.

Thanks

+10  A: 

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 and delete but ensures that the returned memory meets your alignment requirement. (E.g., if you need n bytes with 16-byte alignment, you use new to allocate n + 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.

Adrian McCarthy
+1  A: 

You need a custom allocator that returns aligned storage. That ought to solve your problem.

Marcus Lindblom
+3  A: 

You need a custom allocator passed. You can build one over the std::allocator quite easily:

template <typename T, size_t TALIGN=16, size_t TBLOCK=8>
class aligned_allocator : public std::allocator<T>
{
public:
     aligned_allocator() {}
     aligned_allocator& operator=(const aligned_allocator &rhs){
         std::allocator<T>::operator=(rhs);
         return *this;
     }

     pointer allocate(size_type n, const void *hint){
         pointer p = NULL;
         size_t count = sizeof(T) * n;
         size_t count_left = count % TBLOCK;
         if( count_left != 0 )
         {
             count += TBLOCK - count_left;
         }
         if ( !hint )
         {
             p = reinterpret_cast<pointer>(aligned_malloc(count,TALIGN));
         }else{
             p = reinterpret_cast<pointer>(aligned_realloc((void*)hint,count,TALIGN));
         }
         return p;
     }

     void deallocate(pointer p, size_type n){
         aligned_free(p);
     }

     void construct(pointer p, const T &val){
         new(p) T(val);
     }

     void destroy(pointer p){
         p->~T();
     }
};

The only thing lacking here is the aligned_malloc, aligned_realloc and aligned_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).

Kornel Kisielewicz
A: 

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.

Jerry Coffin