是否使用XOR章,REG给优势MOV章,0? [英] Does using xor reg, reg give advantage over mov reg, 0?

查看:131
本文介绍了是否使用XOR章,REG给优势MOV章,0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是两个著名的方式为整数寄存器设置在x86零值。

There're two well-known ways to set an integer register to zero value on x86.

mov reg, 0

xor reg, reg

有一种意见认为,上述第二种是更好,因为0值并不存储在code和,节省了生产机器code的几个字节。这绝对是个好 - 较少的指令缓存使用,这有时会允许更快code执行。许多编译器产生这样code。

There's an opinion that the second variant is better since the value 0 is not stored in the code and that saves several bytes of produced machine code. This is definitely good - less instruction cache is used and this can sometimes allow for faster code execution. Many compilers produce such code.

不过有正式的XOR指令和任何先前的指令改变同一个寄存器之间的指令间的依赖性。由于有一个depedency后者指令需要等到前完成并这样可以减少处理器单元负载,影响性能。

However there's formally an inter-instruction dependency between the xor instruction and whatever earlier instruction that changes the same register. Since there's a depedency the latter instruction needs to wait until the former completes and this could reduce the processor units load and hurt performance.

add reg, 17
;do something else with reg here
xor reg, reg

很明显,异或的结果将是完全一样的,不管初始寄存器值的。但是它的处理器能够认识到这一点?

It's obvious that the result of xor will be exactly the same regardless of the initial register value. But it the processor able to recognize this?

我试着下面的测试在VC ++ 7:

I tried the following test in VC++7:

const int Count = 10 * 1000 * 1000 * 1000;
int _tmain(int argc, _TCHAR* argv[])
{
    int i;
    DWORD start = GetTickCount();
    for( i = 0; i < Count ; i++ ) {
        __asm {
            mov eax, 10
            xor eax, eax
        };
    }
    DWORD diff = GetTickCount() - start;
    start = GetTickCount();
    for( i = 0; i < Count ; i++ ) {
        __asm {
            mov eax, 10
            mov eax, 0
        };
    }
    diff = GetTickCount() - start;
    return 0;
}

使用了两个循环的优化采取完全相同的时间。这是否合理证明该处理器识别,有没有 XOR章的相关性,在早期的指令MOV EAX,0 指令?有什么能更好的测试,以检查这个?

With optimizations off both loops take exactly the same time. Does this reasonably prove that the processor recognizes that there's no dependency of xor reg, reg instruction on the earlier mov eax, 0 instruction? What could be a better test to check this?

推荐答案

为你一个实际的答案是:

an actual answer for you:

<一个href=\"http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf\"相对=nofollow>英特尔64和IA-32架构优化参考手册

第3.5.1.8是你想要的样子。

Section 3.5.1.8 is where you want to look.

在短期有时候会有这样的异或MOV可能是preferred的情况。周围的依存关系链和条件codeS的preservation问题的中心。

In short there are situations where an xor or a mov may be preferred. The issues center around dependency chains and preservation of condition codes.

这篇关于是否使用XOR章,REG给优势MOV章,0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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