checking for overflow in C

Griff Smith ggs at ulysses.homer.nj.att.com
Thu May 18 06:14:19 AEST 1989


In article <472 at chem.ucsd.EDU>, tps at chem.ucsd.edu (Tom Stockfisch) writes:
> In article <10256 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
> >... the Gould hardware tied integer
> >and floating exceptions to the same enable bit.  Consequently non-floating
> >applications started dying due to unexpected SIGFPEs.  We were not amused.
> 
> Aren't unsigned int operations guaranteed not to generate exceptions?

It depends on the architecture.  On many machines there is no
difference between unsigned integer arithmetic and signed integer
arithmetic.  After doing the operation, the program tests different
bits in the condition code depending on the type.

> It seems like the C compiler could/should have done things differently.

It may have no choice.  After doing an `add' that causes overflow, it's
a bit late to tell the hardware that you were doing an unsigned add.
Preventing the overflow is often an expensive operation.

As an example of how gross it can get, consider how DEC's TOPS-10 OS
separated integer and floating exceptions: if either condition was enabled,
the processor generated a trap to the kernel.  The kernel then tested
the `enable' bits for both conditions, and quietly returned control to
the user program if the trap wasn't enabled.  As you might imagine, this 
had strange effects on execution time.

> -- 
> || Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu

-- 
Griff Smith	AT&T (Bell Laboratories), Murray Hill
Phone:		1-201-582-7736
UUCP:		{most AT&T sites}!ulysses!ggs
Internet:	ggs at ulysses.att.com



More information about the Comp.lang.c mailing list