Hi guys,
I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd?
best
kevin
Hi guys,
I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd?
best
kevin
That's just how 2-s complement integers work -- signed and unsigned
addition / subtraction are the same at the bit-level (but signed and
unsigned division are not).
-- Sanjoy
Hi Kevin,
Thank you very much!
best
keivn
So if the overflow happens for either one of the case, the return value will be implementation dependent?
best
kevin
A hypothetical ‘uadd’ would have the exact same behavior as ‘sadd’ when using two’s complement. We need both ‘udiv’ and ‘sdiv’ because dividing acts differently with negative numbers. For example, dividing by 1 by 0b11111111111111111111111111111111 gives -1 for signed division and ‘0’ for unsigned division.
If the instructions have "nuw" or "nsw" flags, the results are
undefined in the respective case.
Otherwise they're defined to follow C's unsigned rules (i.e. they
return the full precision unsigned sum/difference reduced mod 2^n).
Cheers.
Tim.
That is very clear!
best
kevin