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

 手机用户2502883113 发布于 2023-01-11 16:48

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

#include 
#include 
#include 
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(&values[i]);
        float *fptr = static_cast(vptr);

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

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

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

    delete[] values;
}

Puppy.. 6

是的aligned_storage::type.aligned_storage本身只是一个元编程结构.

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

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

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

    2023-01-11 16:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有