I am trying to store an SSE type in an stl container. I’ve tried this:
#include <iostream>
#include <vector>
int main()
{
typedef int v4sf __attribute__ (( vector_size(4*sizeof(float)) ));
v4sf a; // compiles
std::vector<v4sf> v1; // compiles, but nothing is actually allocated
// std::vector<v4sf> v2(10); // compiler error: can’t convert between vector values of different size
std::vector<v4sf> v(10, a); // Compiles, but segfaults
return 0;
}
but as noted, allocating without providing an object to copy produces a compiler error, while allocating with providing an object compiles but segfaults. Can anyone explain why I can’t store these SSE objects in an STL container like this (or better, provide a correct way to do it)?
To make it working, you have to implement a custom Allocator. To use it, it is the argument next to the type:
std::vector< SSEType, CustomAlloc> container;
Where CustomAlloc is the Allocator.
You have to use an alligned_malloc or memalign for getting memory inside of your Allocater, but this is the way to succeed here.
An example of such a (not so easy implementation) can be found here:
Implementing Allocator example
I did already much with SSE and I observed, it is the most easy way to use an alligned malloc and use this for my calculations.