不使用memcpy复制字节 [英] Copying bytes without memcpy
问题描述
我在char数组中存储了几种不同类型的变量.通常我会这样写它们:
I have several variables of different types stored in a char array. Normally I would write them to the array this way:
int a = 5;
memcpy(offset, (char*)&a, sizeof(int))
但是,memcpy在OpenCL内核中不起作用.没有此功能,最简单的方法是什么?
However, memcpy doesn't work in OpenCL kernels. What would be the easiest way to do the same without this function?
推荐答案
您可以轻松地提供mymemcpy
You can easily enough provide mymemcpy
void mymemcpy(unsigned char *dest, const unsigned char *src, size_t N)
{
size_t i;
for(i=0;i<N;i++)
dest[i] = src[i];
}
但是效率不是很高,因为大多数副本都是4或8字节倍数的对齐副本.如果可以确定对齐方式为8个字节,则以unsigned long long为单位进行复制.有时甚至值得填充一个缓冲区以使其达到8个字节的倍数.
However it's not very efficient because most copies are aligned copies of multiples of 4 or 8 bytes. If you can work out that alignment is 8 bytes, copy in units of unsigned long long. Sometimes it's even worth padding a buffer to bring it up to a multiple of 8 bytes.
这篇关于不使用memcpy复制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!