如何从C代码获取单操作数imul [英] How to get single-operand imul from C code

查看:173
本文介绍了如何从C代码获取单操作数imul的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用C写什么才能使汇编器显示一个操作数的imul?例如:

What do I have to write in C to get the assembler to show the imul with one operand? For example:

imul  %ebp

推荐答案

如果要编写C代码,以便编译器使用一个操作数发出imul,那么唯一的方法是使用加宽有符号乘法,即将操作数强制转换为带符号类型寄存器长度的两倍.因此,在32位模式下,以下代码将产生预期的输出

If you want to write C code so that the compiler emits imul with one operand then the only way is to use widening signed multiplication, i.e. cast the operand(s) to a signed type twice the register length. Hence in 32-bit mode the following code will produce the expected output

long long multiply(int x, int y) {
    return (long long)x * y;
}

由于 2号补码中的非扩展乘法对于有符号和无符号类型都是相同的,因此编译器几乎总是使用imul具有多个操作数,因为它更快,更灵活.

Because non-widening multiplication in 2's complement is the same for both signed and unsigned types, compilers will almost always use imul with multiple operands as it's faster and more flexible.

在x86_64中,即使输入只是32位,编译器还是喜欢使用多操作数imul来产生64位结果.结果,您还必须像上面那样使用双寄存器宽度(128位)类型.不过,Clang,ICC和GCC确实通过__int128对此提供了支持.

In x86_64 compilers prefer to use multi-operand imul to produce 64-bit results, even when the inputs were only 32-bits to start with. As a result you'll also have to use a double-register-width (128-bit) type like above. Clang, ICC and GCC do support that via __int128, though.

  • What gcc versions support the __int128 intrinsic type?
  • Is there a 128 bit integer in gcc?

下面的代码段

__int128_t multiply(long long x, long long y) {
    return  (__int128_t)x * y;
}

将是如果您使用MSVC,则可以使用

If you use MSVC then you can use _umul128 in 64-bit mode

这篇关于如何从C代码获取单操作数imul的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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