C++ STL data structure alignment, algorithm vector

2019-01-24 04:53发布

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.

5条回答
Lonely孤独者°
2楼-- · 2019-01-24 05:35

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-24 05:40

Instead of writing your own allocator, as suggested before, you can use boost::alignment::aligned_allocator.

查看更多
干净又极端
4楼-- · 2019-01-24 05:43

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.

查看更多
闹够了就滚
5楼-- · 2019-01-24 05:43

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).

查看更多
爷、活的狠高调
6楼-- · 2019-01-24 05:45

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.

查看更多
登录 后发表回答