尝试将std :: aligned_storage与SSE和new一起使用

Naz*_*554 3 c++ memory-management sse memory-alignment c++11

我想尝试使用C++中的SSE instrincs来获取一些浮点数的平方根.但是当我尝试存储结果时,我得到一个例外.我可以像这样使用std :: aligned_storage吗?

#include <iostream>
#include <type_traits>
#include <xmmintrin.h>
using namespace std;

using float_storage = aligned_storage<4 * sizeof(float), 16>;

int main()
{
    int N;
    cin >> N;

    float_storage * values = new float_storage[ N / 4 ]; // 4 floats in pack

    for(int i = 0; i < N / 4; i++)
    {
        void *vptr = static_cast<void*>(&values[i]);
        float *fptr = static_cast<float*>(vptr);

        for(int i = 0; i < 4; i++)
            cin >> fptr[i];
    }

    for(int i = 0; i < N / 4; i++)
    {
        void *vptr = static_cast<void*>(&values[i]);
        float *fptr = static_cast<float*>(vptr);
        __m128 x = _mm_loadu_ps(fptr);
        x = _mm_sqrt_ps(x);
        _mm_store_ps(fptr, x); // im getting an crash here
    }

    for(int i = 0; i < N / 4; i++)
    {
        void *vptr = static_cast<void*>(&values[i]);
        float *fptr = static_cast<float*>(vptr);

        for(int i = 0; i < 4; i++)
            cout << fptr[i] << endl;
    }

    delete[] values;
}
Run Code Online (Sandbox Code Playgroud)

Pup*_*ppy 6

是的aligned_storage<size, align>::type.aligned_storage本身只是一个元编程结构.

此外,如果我没记错的话,即使您使用具有更高对齐要求的类型,new也仅被评定为.std::max_align_tnew