AnsweredAssumed Answered

Testing for an overflow condition

Question asked by Scott Phillips on May 17, 2007
Latest reply on May 21, 2007 by Scott Phillips
How do you infer the BVS (Branch if overflow set) instruction using C?
I have two signed 16 bit variables that I need to add together.  If, however, the addition will cause the result to overflow a 16 bit value, the code needs to do something different.  Currently my code looks something like:
INT16  temp1, temp2;
if (((INT32)(temp1 + temp2) > 32767) || ((INT32)(temp1+ temp2) < -32768))
The code compiles without warnings, but it generates excessive assembly instructions.  I want my C code to effectively compile down to an ADD instruction followed by an BVS instruction.
The reason I care, is because I am porting an Intel 196 design written in assembly to a 9S12E design written in C.  So far, I'm getting quite a bit of code bloat associated with the conversion to C.  I know that I could just inline some assembly instructions, but this defeats our goal of code portability.  My manager is also assembly code phobic and whines that it is hard to understand.  I, on the other hand, enjoy the control and efficiency that assembly affords.
Since my C programming expertise is minimal, I thought I'd ask for help.
Thanks -
A reluctant C programmer