32 ms·
In x86 assembly almost all multiplication operations return a result double the width of the inputs: http://www.aldeid.com/wiki/X86-assembly/Instructions/mul ht
by sounds 12y ago
In x86 assembly almost all multiplication operations return a result double the width of the inputs: http://www.aldeid.com/wiki/X86-assembly/Instructions/mul http://www.aldeid.com/wiki/X86-assembly/Instructions/mul
Checking for overflow is just a matter of testing if the high-order bits are zero.
- stormbrew 12y agoYep, in assembly it's usually no question possible. That's why I specified portable C. C has some interesting gaps for something that's long been described as a macro for assembly. A rotate operator would also be nice.
- pbsd 12y agoThe correct thing to do is also to check the carry and overflow flags after (I)MUL, which does the right thing even when only the lower bits of the multiplication are saved. Here's an example where there is overflow, but your suggestion would not catch it: mov eax, 0x7fffffff mov ecx, 2 imul ecx ; edx:eax = eax * ecx ; edx = 0 ; eax = 0xfffffffe ; overflow flag = 1 There is signed overflow here, but testing edx (the higher word of the result) would not catch it.
- sounds 12y agoYes. I decided not to mention IMUL to keep the post simple. There are also issues with detecting overflow of SSE multiplies (signed and unsigned).