gpt4 book ai didi

c - 是否有标准宏来检测需要对齐内存访问的架构?

转载 作者:太空狗 更新时间:2023-10-29 17:10:14 24 4
gpt4 key购买 nike

假设是这样的:

void mask_bytes(unsigned char* dest, unsigned char* src, unsigned char* mask, unsigned int len)
{
unsigned int i;
for(i=0; i<len; i++)
{
dest[i] = src[i] & mask[i];
}
}

我可以通过编写如下内容在非对齐访问机器(例如 x86)上运行得更快:

void mask_bytes(unsigned char* dest, unsigned char* src, unsigned char* mask, unsigned int len)
{
unsigned int i;
unsigned int wordlen = len >> 2;
for(i=0; i<wordlen; i++)
{
((uint32_t*)dest)[i] = ((uint32_t*)src)[i] & ((uint32_t*)mask)[i]; // this raises SIGBUS on SPARC and other archs that require aligned access.
}
for(i=wordlen<<2; i<len; i++){
dest[i] = src[i] & mask[i];
}
}

但是它需要建立在多个架构上,所以我想做类似的事情:

void mask_bytes(unsigned char* dest, unsigned char* src, unsigned char* mask, unsigned int len)
{
unsigned int i;
unsigned int wordlen = len >> 2;

#if defined(__ALIGNED2__) || defined(__ALIGNED4__) || defined(__ALIGNED8__)
// go slow
for(i=0; i<len; i++)
{
dest[i] = src[i] & mask[i];
}
#else
// go fast
for(i=0; i<wordlen; i++)
{
// the following line will raise SIGBUS on SPARC and other archs that require aligned access.
((uint32_t*)dest)[i] = ((uint32_t*)src)[i] & ((uint32_t*)mask)[i];
}
for(i=wordlen<<2; i<len; i++){
dest[i] = src[i] & mask[i];
}
#endif
}

但我找不到任何关于编译器定义的宏(如我上面假设的 __ALIGNED4__)的任何有用信息,这些宏指定对齐或使用预处理器确定目标体系结构对齐的任何巧妙方法。我可以测试 defined (__SVR4) && defined (__sun) ,但我更喜欢 Just WorkTM 在需要对齐内存访问的其他架构上的东西。

最佳答案

虽然 x86 默默地修复了未对齐的访问,但这对于性能来说并不是最佳的。通常最好假定一定的对齐方式并自己执行修复:

unsigned int const alignment = 8;   /* or 16, or sizeof(long) */

void memcpy(char *dst, char const *src, unsigned int size) {
if((((intptr_t)dst) % alignment) != (((intptr_t)src) % alignment)) {
/* no common alignment, copy as bytes or shift around */
} else {
if(((intptr_t)dst) % alignment) {
/* copy bytes at the beginning */
}
/* copy words in the middle */
if(((intptr_t)dst + size) % alignment) {
/* copy bytes at the end */
}
}
}

此外,请查看 SIMD 指令。

关于c - 是否有标准宏来检测需要对齐内存访问的架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8418133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com