Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2007

Tip: Looking for answers? Try searching our database.

SSE and Vector

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tao - 09 Feb 2007 16:25 GMT
hi.. Group,

I want to use __m128i with vector, but it seems does not work. The following
code generated a runtime error indicating that there is a memory corruption.
   std::vector<__m128i> list;

   __m128i x = _mm_set_epi32(0, 0, 0, 2);

   list.push_back(x);

Can somebody tell me why this happen?

thanks.
Tom Widmer [VC++ MVP] - 09 Feb 2007 16:50 GMT
> hi.. Group,
>
[quoted text clipped - 7 lines]
>
> Can somebody tell me why this happen?

__m128i requires 16-byte alignment, which vector doesn't guarantee to
provide (well, it should do, but __m128i has especially strict alignment
requirements - ones that aren't met by malloc). One way of working
around this would be to specify a special vector allocator which did
align the memory correctly. e.g. (VC2005 specific due to __alignof())

#include <vector>
#include <memory>
#include <malloc.h>
#include <emmintrin.h>

template <class T>
class aligned_allocator: public std::allocator<T>
{
public:
    template <class U>
    struct rebind
    {
        typedef aligned_allocator<U> other;
    };

    aligned_allocator(){}

    template <class U>
    aligned_allocator(aligned_allocator<U> const& other){}

    template <class U>
    aligned_allocator& operator=(aligned_allocator<U> const& other){
        return *this;
    }

    T* allocate(std::size_t N, const void* hint)
    {
        return allocate(N);
    }

    T* allocate(size_type N)
    {
        return static_cast<T*>(_aligned_malloc(N * sizeof(T), __alignof(T)));
    }

    void deallocate(T* p, std::size_t N)
    {
        _aligned_free(p);
    }
};

Then:
std::vector<__m128i, aligned_allocator<__m128i> > list;

Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.