1

Why there is no pmulluw, pslad and pslaw commands in MMX? And why there is no movb and movw commands?

just_user
  • 91
  • 1
  • 2

2 Answers2

3

There totally is a pmulluw, but it's called pmullw. Since it only keeps the low half, there is no difference between signed and unsigned.

For a related reason, pslad and pslaw are pslld and psllw respectively. A left shift is a left shift, signedness doesn't even enter the picture, you will always shift the (assuming a shift by 1) second-to-highest bit into the highest, nothing else really makes sense (the cases in which that signed-overflows are precisely the cases in which the "full result" cannot be represented anyway, so trying to somehow preserve the sign is useless). Right shift has signed and unsigned versions.

harold
  • 61,398
  • 6
  • 86
  • 164
  • @just_user I don't know – harold Apr 13 '15 at 16:43
  • *"Since it only keeps the low half, there is no difference between signed and unsigned."* It's not immediately obvious why keeping the lower half makes no difference between signed and unsigned multiplication. Can you show me a proof? I'm trying to work it out and but not succeeded so far. – Calmarius Oct 13 '15 at 12:21
3

In addition to the other answers...

I also wondered why there is no PMULLUW. It's not immediately obvious why its equivalent with PMULLW.

If you multiply 2 16 bit numbers the result will be a 32 bit number. But since we keep the lower half we form the remainder of division by 2^16.

If you take 2 numbers X and Y and if they are positive and their multiplication is XY the result is XY mod 2^16 (Let's call 2^16 as B).

If one of them is negative then due to the 2's complement representation the actual calculation is:

(X - B)Y = XY - BY

(there X and Y the unsigned meaning of the numbers.)

Since the result is negative we need to form another 2's complement to get back the result, which is B^2 - BY + XY. After forming the modulo with B. The 2 first terms fall out because they are divisible with B so the result is XY mod B.

Same happens when both of them are negative:

(X - B)(Y - B) = XY - B(X+Y) + B^2.

In this case the result is positive, you don't need another 2's complement, but after forming the modulo The 2 terms containing the B falls out again and you have the XY mod B. That's why there is no need to have PMULLUW.


Regarding the MOVB and MOVW you have MOVD which sets the low 32 bits and zero fills the upper part. You can use this instruction with a small number to set the lower word or by and zero fill the rest. The MOVQ instruction is actually the REX.W widened version of MOVD that lets you fill the mmx registers from the 64 bit general purpose registers.

Calmarius
  • 18,570
  • 18
  • 110
  • 157
  • I was writing down a proof but now you have one yourself - a very different one though. Also I have an old proof (well, proof skeleton) in [this answer](http://stackoverflow.com/a/25242228/555045) – harold Oct 13 '15 at 13:48