如何确定内存对齐? (*测试*为对齐,不调整) [英] How to determine if memory is aligned? ( *testing* for alignment, not aligning )

查看:245
本文介绍了如何确定内存对齐? (*测试*为对齐,不调整)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来使用SSE / SSE2指令集优化code和到现在为止我还没有很远得到。据我所知,一个共同的SSE优化的功能是这样的:

I am new to optimizing code with SSE/SSE2 instructions and until now I have not gotten very far. To my knowledge a common SSE-optimized function would look like this:

void sse_func(const float* const ptr, int len){
    if( ptr is aligned )
    {
        for( ... ){
            // unroll loop by 4 or 2 elements
        }
        for( ....){
            // handle the rest
            // (non-optimized code)
        }
    } else {
        for( ....){
            // regular C code to handle non-aligned memory
        }
    }
}

不过,我怎么正确判断,如果内存 PTR 指向是通过例如对齐16个字节?我想我必须包括不结盟内存常规C code路径,我不能确保传递给这个函数每一个记忆会保持一致。并使用内部函数从未对齐的内存数据加载到SSE寄存器似乎是可怕的慢(甚至比常规的C code慢)。

However, how do I correctly determine if the memory ptr points to is aligned by e.g. 16 Bytes? I think I have to include the regular C code path for non-aligned memory as I cannot make sure that every memory passed to this function will be aligned. And using the intrinsics to load data from unaligned memory into the SSE registers seems to be horrible slow (Even slower than regular C code).

感谢您提前...

推荐答案

编辑:铸造是要保护自己不受INT的最有可能的可能性,一种廉价的方式和指针现在是不同的尺寸。

casting to long is a cheap way to protect oneself against the most likely possibility of int and pointers being different sizes nowadays.

由于在下面的评论中指出,有更好的解决方案,如果你愿意,包括一个头...

As pointed out in the comments below, there are better solutions if you are willing to include a header...

一个指针 P 是一个16字节边界上对齐IFF ((无符号长)P和15)== 0

A pointer p is aligned on a 16-byte boundary iff ((unsigned long)p & 15) == 0.

这篇关于如何确定内存对齐? (*测试*为对齐,不调整)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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