移位指令比IMUL指令快吗? [英] Is a shift instruction faster than an IMUL instruction?
问题描述
哪个更快?-
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的ADD
和RSB
不同的优化机会).>
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屋!