What do I have to write in C to get the assembler to show the imul
with one operand? For example:
imul %ebp
What do I have to write in C to get the assembler to show the imul
with one operand? For example:
imul %ebp
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;
}
Because non-widening multiplication in 2's complement is the same for both signed and unsigned types, compilers will almost always use multi-operand imul
as it's faster and more flexible.
In x86_64 compilers use multi-operand imul
to produce 64-bit results even when the inputs were only 32-bits because they still typically have better latency and throughput than single-operand imul
. 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.
The below snippet
__int128_t multiply(long long x, long long y) {
return (__int128_t)x * y;
}
will be compiled to
mov rax, rdi
imul rsi
ret
If you use MSVC then you can use _umul128
in 64-bit mode