On Tue, Sep 13, 2016 at 3:38 PM, Doug Ingraham <dpi at dustyoldcomputers.com>
wrote:
Thanks for an interesting bit of optimization!
Need some more optimization fun? :) Vince and I were working on some code
to add two signed 12 bit numbers and detect overflow, returning MAX_INT or
MIN_INT in AC in the case of overflow, or the sum in AC otherwise. Here's
what Vince came up with so far:
CHKOVF, 0
TAD OVFA /GET A
TAD OVFB /ADD B
DCA OVFSUM /SAVE IT
TAD OVFA /A XOR B
AND OVFB
CMA IAC
TAD OVFA
TAD OVFB
SPA CLA /IF SIGNS DIFFER...
JMP NOPROB /WE'RE DONE
TAD OVFA /MIGHT BE OVERFLOW, A XOR SUM
AND OVFSUM
CMA IAC
TAD OVFA
TAD OVFSUM
SMA /DID WE OVERFLOW (DIFFERENT SIGNS)?
JMP NOPROB /NO, NO PROBLEM
CLA CLL CMA RAR /YES, AC=3777 (MAX INT)
DCA OVFSUM /SAVE IT
TAD OVFA /GET THE SIGN OF CORRECT RESULT
SPA CLA /SHOULD IT BE NEGATIVE?
ISZ OVFSUM /YES, 3777 -> 4000 (MIN INT)
NOPROB, CLA /GET CORRECTED SUM
TAD OVFSUM
JMP I CHKOVF /OUTTA HERE
OVFA, 0
OVFB, 0
OVFSUM, 0
This tests the signs of both numbers; if they differ, there's no chance of
overflow. If they're the same, it checks the signs of the augend with the
sum; if they differ, an overflow occurred, and MAX_INT or MIN_INT will be
returned depending on the sign of the augend.
We tried clever tricks previously, using SNL/SZL and SMA/SPA after shifting
the sign of the augend into the link and keeping the addend in the AC, but
found these to be longer.
Kyle