Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!sdd.hp.com!decwrl!sgi!karsh@trifolium.esd.sgi.com From: karsh@trifolium.esd.sgi.com (Bruce Karsh) Newsgroups: comp.sys.sgi Subject: Re: detecting integer overflow Message-ID: <67704@sgi.sgi.com> Date: 25 Aug 90 05:00:25 GMT References: <67688@sgi.sgi.com> <67702@sgi.sgi.com> Sender: guest@sgi.sgi.com Reply-To: karsh@trifolium.sgi.com (Bruce Karsh) Distribution: comp.sys.sgi Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 49 In article <67702@sgi.sgi.com> karsh@trifolium.sgi.com (Bruce Karsh) writes: >This should be > if( (~a^b & a^c) < 0) handle_overflow(); I can't get anything right today. Bruce Holloway pointed out the precedence problem. It should be: if( ((~a^b) & (a^c)) < 0) handle_overflow(); because the precedence of & is higher than the precedence of ^. Just to make sure this is right this time, a test case is attached below. /* Test of software overflow handling. */ main() { ofltst(0x80000000,0x80000000); ofltst(0x80000000,0x00000000); ofltst(0x80000000,0x7fffffff); ofltst(0x00000000,0x80000000); ofltst(0x00000000,0x00000000); ofltst(0x00000000,0x7fffffff); ofltst(0x7fffffff,0x80000000); ofltst(0x7fffffff,0x00000000); ofltst(0x7fffffff,0x7fffffff); } ofltst(a,b) int a,b; { int c; c=a+b; if( ((~a^b) & (a^c)) < 0) handle_overflow(a,b,c); else handle_nooverflow(a,b,c); } handle_overflow(a,b,c) int a,b,c; { printf("%11d = %11d + %-11d overflow\n",c,a,b); } handle_nooverflow(a,b,c) int a,b,c; { printf("%11d = %11d + %-11d no overflow\n",c,a,b); } Bruce Karsh karsh@sgi.com