On Thu, Mar 08, 2001 at 01:55:57PM -0500, Dan Sugalski wrote:
> At 06:49 PM 3/8/2001 +0000, Nicholas Clark wrote:
> >On Thu, Mar 08, 2001 at 11:43:31AM -0500, Dan Sugalski wrote:
> > > I was thinking maybe (length/4)*31-bit 2s complement to make portable
> > > overflow detection easier, but that would be only if there wasn't a good C
> > > library for this available to snag.
> >
> >The only portable integer overflow in ANSI C is unsigned integers.
> >signed integer overflow does not always do what you might expect, even
> >on a 2s complement machine.
> 
> That's what the 32nd bit of each word is for. If we do addition on a 
> word-by-word basis and the high bit of the result is set, we know we've 
> overflowed, and we can set it as a carry bit for subtraction.

pp_add and pp_subtract in perl5 currently do it OK on 32 bits.
You can detect overflow in unsigned 32 bits because your result
(a+b) is smaller than a (or b for that matter)
The explanation for all subtraction is more complex, but still fits on
the back of an envelope (Should I scan the original envelope in?) or about
7 lines of comments in pp_subtract

Unless I'm much mistaken, ANSI C defined for unsigned integers that
arithmetic wraps. Which is what pp_add and pp_subtract are taking advantage
of. I see no reason why their current overflow can't be converted to
carry.

[It's doing maths in software, which Ilya doesn't like, as software is more
likely to contain bugs than hardware. I start to get cagey at the idea of
carry]

Nicholas Clark

Reply via email to