4

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

6
  • 1
    std::vector doesn't guarantee alignment for SSE types.
    – Mysticial
    Aug 29, 2012 at 17:21
  • vector of __mm128 won't push_back()
    – Mysticial
    Aug 29, 2012 at 17:22
  • @Mystical, so how am I supposed to store them then? That thread doesn't actually show how to do it. Aug 29, 2012 at 17:25
  • You have to override the default memory allocator with one that aligns properly. It's messy, but I'm not aware of any other way.
    – Mysticial
    Aug 29, 2012 at 17:26
  • There is really no "built in" allocator to do this? It looks like a huge mess to write manually, and I don't really see any official looking "here use this for an Allocator for stl::vector and SSE" online anywhere - does one exist? Aug 29, 2012 at 17:34

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.