5

As stated in the title, why does the ARM instruction set distinguish between signed and unsigned only on division?

SDIV and UDIV are available but that's not the case with ADD, SUB and MUL.

JohnnyFromBF
  • 9,873
  • 10
  • 45
  • 59
  • possible duplicate of [How to do ADD/SUB signed or unsigned integer correctly?](http://stackoverflow.com/questions/21359298/how-to-do-add-sub-signed-or-unsigned-integer-correctly) (in the "signed arithmetic _is_ unsigned arithmetic because twos complement" sense) – Notlikethat Apr 16 '15 at 17:09
  • Possible duplicate of [Why are signed and unsigned multiplication different instructions on x86(-64)?](http://stackoverflow.com/questions/14063599/why-are-signed-and-unsigned-multiplication-different-instructions-on-x86-64) – phuclv Dec 24 '16 at 11:27
  • [Which arithmetic operations are the same on unsigned and two's complement signed numbers?](http://stackoverflow.com/q/21475286/995714), [signed and unsigned arithmetic implementation on x86](http://stackoverflow.com/q/25241477/995714), http://stackoverflow.com/a/13229313/995714 – phuclv Dec 24 '16 at 11:29

1 Answers1

12

addition and subtraction of signed and unsigned numbers of the same size produce exactly the same bit patterns in two's complement math (which ARM uses), so there is no need for separate instructions.

For example, if we take byte-sized values:

0xFC +4 
signed: -4+4 = 0
unsigned: 252 +4 = 256 = 0x100 = 0x00 (truncated to byte)

Multiplication result does change depending on whether the operands are interpreted as signed or unsigned, however the MUL instruction produces only the low 32 bits of the result, which are the same in both cases. In recent ARM processors there are instructions which produce the full 64-bit result, and those come in pairs just like SDIV and UDIV: UMULL, UMLAL, SSMULL, SMLAL:

Signed and Unsigned Long Multiply, with optional Accumulate, with 32-bit operands, and 64-bit result and accumulator.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109