Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2008-08-02 13:48:14


On Aug 2, 2008, at 7:47 AM, Beman Dawes wrote:

> Isaac Dupree wrote:
>> Is there interested in a library to check for overflow in (primarily)
>> the built-in integers?
>
> Yes. This has been on my personal wish-list for a long time.

I've been interested in something related. I don't need to know of
the existence of overflow, but what exactly the overflow is. I want
functions like:

1. + lhs, rhs: word -> wrapped_sum: word, carry: Boolean
2. - lhs, rhs: word -> wrapped_difference: word, borrow: Boolean
3. * lhs, rhs: word -> product: word[2]
4. /% lhs: word[2], rhs: word -> quotient, remainder: word

(4) assumes that the dividend is small enough to only need a single-
word quotient, i.e. lhs[1] < rhs. The general case could divide lhs
[1] by rhs first then compose the answer:

function 4_full( lhs: word[2], rhs: word )
     {lq, lr} = lhs[1] FULL_DIV rhs;
     lhs[1] = lr;
     {result.quotient, result.remainder} = 4( lhs, rhs ); // now lhs
meets preconditions
     result.extended_quotient = lq;
     return result;

The key is that, AFAIK, processors for the past few decades have
these functions built-in. It's just that C, C++, etc. have not given
us access to these optimized routines. The only access is either
assembly language or compiler optimizations.

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk