-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there, I think what you're probably missing is that a signed 16-bit integer has one more possible value: -32768 (0x8000). Apparently you want to clamp the result to +/-32767 (not including -32768), so you'll need to check for this result and correct accordingly. For adding positive integers you've already got it. The smallest possible overflow is something like 16384+16384=32768=0x8000=-32768 and so the sign bit flips. The largest possible overflow is something like 32767+32767=65534=0xFFFE=-2 and so the sign bit also flips. For adding a negative and a nonnegative integer you're also right (and be aware that I'm saying *nonnegative* and not *positive* for a reason - zero has MSB=0 here, but it's not strictly positive). The general case could potentially end with -32768 (if you added 0 + -32768), but you say neither of your input numbers can ever be -32768 so this isn't a problem here. For adding two negative numbers, the largest addition that doesn't overflow is something like -16384-16383=-32767=0x8001. You then have the special overflow case, -16384-16384=-32768=0x8000, which should be checked for and translated to -32767. You then have the general overflow cases, which range from -16384-16385=-32769=0x7FFF=32767 and so on up to - -32767-32767=-65534=2. So you can see here that the overflow cases all lead to either positive numbers *or* the number -32768. Note that it's fairly easy to test for both the special and general overflow cases in the negative-negative addition, because if you consider the result as though it were unsigned, you just need to check "is result <= 0x8000". There may be fancier ways to combine these cases using the STATUS register bits. Chris Matthew Rhys-Roberts wrote: [snip] | If input and output words are both negative... well this is where I go | snow-blind. | | I first thought that if the sign bit went from 1 to 0, then a rollover | had occurred. But this didn't account for all cases. [snip] -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: GnuPT 2.7.2 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkhlGGQACgkQiD2svb/jCb4ebwCgq8DIzY/7ZXnkvHE+YaQ5v1zI TmYAn3mUR/WoJ29on9OTcRzJGc799BDA =xk15 -----END PGP SIGNATURE----- -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist