将字节数组转换为向量的最有效方法 [英] Most efficient way of converting byte array to vector

查看:213
本文介绍了将字节数组转换为向量的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将16个字节的数组转换为最有效的方法是什么 uint4向量?目前,我手动将字节或整理为uint,然后进行设置 具有完整uint的向量分量.是否有OpenCL支持执行此任务?

What is the most efficient way of converting an array of 16 bytes into a uint4 vector ? currently, I manually OR the bytes into uints, then set the vector's components with the completed uints. Is there OpenCL support for performing this task?

这是针对OpenCL 1.2

This is for OpenCL 1.2

这是我的代码:

    local uchar buffer[16];
    uint v[4];
    for (int i = 0; i < 4; ++i) {
        v[i]=0;
        for (int j = 0; j < 4; ++j) {
            v[i] |= (buffer[(i<<2)+j]) << (j<<3);
        }
    }
    uint4 result = (uint4)(v[0],v[1],v[2],v[3]);

缓冲区实际上是本地缓冲区.

Edit 2: buffer is actually a local buffer.

推荐答案

您应该能够即时进行转换,而无需复制数据:

You should be able to convert it on the fly without copying the data:

local uchar buffer[16];
if(get_local_id(0) == 0)
{
    for (int x = 0; x < 4; ++x)
    {
        buffer[x] = x + 1;
        buffer[x + 4] = x + 2;
        buffer[x + 8] = x + 3;
        buffer[x + 12] = x + 4;
    }
    local uint4 *result = (local uint4*)buffer;
    printf("0x%x 0x%x 0x%x 0x%x\n", (*result).x, (*result).y, (*result).z, (*result).w);
}

结果:

0x4030201 0x5040302 0x6050403 0x7060504

如果您仍然需要复制数据,请执行以下操作:

If you need to copy the data though you do:

uint4 result = *(local uint4*)buffer;

这篇关于将字节数组转换为向量的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆