现代编译器将x * 2操作优化到x < 1? [英] Do modern compilers optimize the x * 2 operation to x &lt;&lt; 1?

查看:98
本文介绍了现代编译器将x * 2操作优化到x < 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++编译器是否会优化此操作?

Does the C++ compiler optimize this operation?

我相信是的。

推荐答案

实际上VS2008优化到x + x:

Actually VS2008 optimizes this to x+x:

01391000  push        ecx  
    int x = 0;

    scanf("%d", &x);
01391001  lea         eax,[esp] 
01391004  push        eax  
01391005  push        offset string "%d" (13920F4h) 
0139100A  mov         dword ptr [esp+8],0 
01391012  call        dword ptr [__imp__scanf (13920A4h)] 

    int y = x * 2;
01391018  mov         ecx,dword ptr [esp+8] 
0139101C  lea         edx,[ecx+ecx]

在x64版本中,它更加明确,并使用:

In an x64 build it is even more explicit and uses:

    int y = x * 2;
000000013FB9101E  mov         edx,dword ptr [x] 

    printf("%d", y);
000000013FB91022  lea         rcx,[string "%d" (13FB921B0h)] 
000000013FB91029  add         edx,edx


b $ b

这是最大化速度(/ O2)

This is will the optimization settings on 'Maximize speed' (/O2)

这篇关于现代编译器将x * 2操作优化到x < 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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