For signed addition, overflow happens
- when adding a positive number with a positive number and the result is negative
- when adding a negative number with a negative number and the result is positive
For signed subtraction, overflow happens
- when given a positive number we subtract a negative number and the result is negative
- when given a negative number we subtract a positive number and the result is positive
A quick read of these should make mathematical sense (the sum of two positive numbers cannot be negative).
You can check for these conditions to determine overflow.
When overflow happens, then you simply compare the signs of the inputs to determine the result.
So, here's an approach:
check the signs and if they are opposite, then the positive one is larger.
- in other words, XOR the signs and if the XOR result is 1, they have differing (opposite) signs.
- the answer to
sgti
is then the sign of the immediate, which is also the sign of rd negated.
otherwise they are either both positive or both negative, and the subtraction should yield the proper result without overflowing
- so the answer is in the sign bit of the subtraction result specifically, the sign of the result negated is the answer for
sgti
.
The sign bit is the high bit, the MSB, aka bit 31.