CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Testing for overflow in 16 and 32 bit maths

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

Testing for overflow in 16 and 32 bit maths
PostPosted: Tue Feb 15, 2011 4:51 pm     Reply with quote

I have the code:
Code:

   int16 a, b, c;
   c=a+b;

or
Code:

   int16 a, b, c;
   c=a-b;

how can I tell if my int16 c has overflowed?

Testing the Status bits doesn't seem to work (possible because it's a 16 bit sum rather than 8 bit).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 15, 2011 5:32 pm     Reply with quote

The C FAQ has a section on integer math overflow:
http://c-faq.com/misc/intovf.html
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 2:51 am     Reply with quote

Why do you think that that status register bits (C and OV) aren't valid for multi byte arithmetic. The status value after an arithmetic operation holds the result of MSB processing, which in fact does inform about signed overflow or unsigned carry.
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 9:21 am     Reply with quote

On further investigating I have found that you are right FvM, the status register bits are valid for multi byte arithmetic. The problem was I was using the SFR tab in the debug window which doesn't seem to give a true value of the STATUS register, even if you set a breakpoint and use the refresh button.

Thanks for your reply.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 12:52 pm     Reply with quote

Are you proposing that in the C language, we can depend upon machine
level flags being persistent and available at the C language source level ?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 1:59 pm     Reply with quote

It may work, but I would not consider it good C programming form to depend on the overflow flag. It would be better to design the data path so overflow will never happen.
_________________
The search for better is endless. Instead simply find very good and get the job done.
levdev



Joined: 19 May 2010
Posts: 36
Location: UK

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 2:39 pm     Reply with quote

One of the calculations I am doing is a simple Pythagoras calculation:
Code:

int16 a,b,c;
int32 a2, b2, c2;
a=get_val_from_sensor_a();
b=get_val_from_sensor_b();
a2=_mul(a2,a2);
b2=_mul(b2,b2);
c2=a2+b2;
c=sqrt32(c2);

Now if a2+b2>2^32-1 then c2 will overflow. In my case if it overflows I don't need to know the value as it is out of range, so I could set it to the maximum value 2^32-1. But how do I know if it overflows? At the moment I am testing the STATUS register which seems to be working OK. I can't pre-test the a and b values because the expression combines to the two sqaures so only by calculating this expression can you get the result. I don't want to get into using 64 bit maths on a PIC18F. I suppose I could divide the a and b values by two before squaring and then double c at the end, but then I will lose some precision.

Any other suggestions would be appreciated.
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Wed Feb 16, 2011 3:10 pm     Reply with quote

levdev wrote:
One of the calculations I am doing is a simple Pythagoras calculation:
Code:

int16 a,b,c;
int32 a2, b2, c2;
a=get_val_from_sensor_a();
b=get_val_from_sensor_b();
a2=_mul(a2,a2);
b2=_mul(b2,b2);
c2=a2+b2;
c=sqrt32(c2);

Now if a2+b2>2^32-1 then c2 will overflow. In my case if it overflows I don't need to know the value as it is out of range, so I could set it to the maximum value 2^32-1. But how do I know if it overflows? At the moment I am testing the STATUS register which seems to be working OK. I can't pre-test the a and b values because the expression combines to the two sqaures so only by calculating this expression can you get the result. I don't want to get into using 64 bit maths on a PIC18F. I suppose I could divide the a and b values by two before squaring and then double c at the end, but then I will lose some precision.

Any other suggestions would be appreciated.


Easy. Check to see if the result (a^2 + b^2) is less than either a^2 or b^2. If it is, there was an overflow. It's not a valid test for all combinations of a & b, but it's probably sufficient most of the time.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group