Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2007-09-08 23:10:38


Maybe we should add a header to our detail (and/or pending) folder
encapsulating the full versions of built-in integer arithmetic
operations, that most(?) processors have, but neither C nor C++ let us
access. I'm talking about functions like:

std::pair<unsigned, bool> full_add( unsigned a1, unsigned a2 );

The processor has a "carry" flag whenever an integer addition overflows,
but we can't access it (without assembly-language hacks). A conforming
C++ workaround is to calculate the carry flag in advance before adding.
  But we could provide an implementation of this function that uses the
assembly-language hacks to compute the sum and carry at once,
encapsulating it for any other library (present or future) that needs
full addition arithmetic.

Other examples would be:

std::pair<unsigned, bool> full_subtract( unsigned m, unsigned s );
        // for quick access to the "borrow" state

boost::array<unsigned, 2> full_multiply(unsigned f1, unsigned f2);
        // no need to do piecemeal multiply with 16-bit halves
        // result[1]: high part, result[0]: low part

std::pair<boost::array<unsigned, 2>, unsigned>
   full_divide( unsigned (&dividend)[2], unsigned divisor );
        // no need to do piecemeal division one-bit at a time
        // result.first[1]: quotient, high part
        // result.first[0]: quotient, low part
        // result.second: remainder

I list only "unsigned int" here, but we could do versions for all the
built-in integer types. The assembly-language implementations would
require #segregating by processor/compiler type.

-- 
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