Subject: Re: detect arithmetic overflow
To: David Laight <>
From: Johnny Billquist <>
List: current-users
Date: 08/06/2003 10:59:04
On Wed, 6 Aug 2003, David Laight wrote:

> On Tue, Aug 05, 2003 at 02:18:08PM -0600, Ciju John wrote:
> > Hi all, I need to detect arithmetic overflow when using signed integers
> > (64-bit). I read that a system signal is generated upon an overflow event.
> > However using signals is not an option and therefore I would like some
> > other solution such as bit checks etc (something I can write a simple
> > macros for).
> For addition of positive numbers, just check for the result being
> less than either operand.

It's even easier than that. For signed addition, just check if the result
is negative. If so, then you have overflow. For unsigned addition, you'll
have to test it as you described, though.

> For multiply it is all rather harder...

Impossible, actually. You might be able to predict it, but it's costly,
and not exact.


Johnny Billquist                  || "I'm on a bus
                                  ||  on a psychedelic trip
email:           ||  Reading murder books
pdp is alive!                     ||  tryin' to stay hip" - B. Idol