【问题标题】:Trying to use std::aligned_storage with SSE and new尝试将 std::aligned_storage 与 SSE 和 new 一起使用
【发布时间】:2014-06-04 17:38:35
【问题描述】:

我想尝试在 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;
}

【问题讨论】:

    标签: c++ c++11 memory-management sse memory-alignment


    【解决方案1】:

    它是aligned_storage&lt;size, align&gt;::typealigned_storage 本身只是一个元编程结构。

    另外,如果我没记错的话,new 仅被评为std::max_align_t,即使您 new 设置了具有更高对齐要求的类型。

    【讨论】:

    • 但是可以还是不能?
    猜你喜欢
    • 2013-04-17
    • 2013-10-18
    • 2011-12-25
    • 2012-11-22
    • 2023-03-10
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多