移位指令比IMUL指令快吗? [英] Is a shift instruction faster than an IMUL instruction?

查看:120
本文介绍了移位指令比IMUL指令快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更快?-

val = val*10;

val = (val<<3) + (val<<2);

与移位指令相比,imul需要多少个时钟周期?

解决方案

在这种情况下,尽管您的手动优化"需要更多寄存器(这可能会使周围的代码变慢),但它们可能花费相同的周期: /p>

val = val * 10;
lea    (%eax,%eax,4),%eax
add    %eax,%eax

vs

val = (val<<3) + (val<<1);
lea    (%eax,%eax,1),%edx
lea    (%edx,%eax,8),%eax

编译器知道如何进行强度降低,并且可能比您更好.另外,当您将代码移植到其他平台(例如ARM)时,编译器也知道如何在该平台上进行强度降低(x86的LEA提供了与ARM的ADDRSB不同的优化机会).

Which one is faster -

val = val*10;

or

val = (val<<3) + (val<<2);

How many clock cycles does imul take when compared to shift instruction?

解决方案

In this case they probably take the same amount of cycles, though your manual "optimization" needs one more register (which can slow down the surrounding code):

val = val * 10;
lea    (%eax,%eax,4),%eax
add    %eax,%eax

vs

val = (val<<3) + (val<<1);
lea    (%eax,%eax,1),%edx
lea    (%edx,%eax,8),%eax

The compiler knows how to do strength reduction, and probably much better than you. Also, when you port your code to other platform (say, ARM), the compiler knows how to do strenght reduction on that platform too (x86's LEA provides different optimization opportunities than ARM's ADD and RSB).

这篇关于移位指令比IMUL指令快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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