All pastes #2069561 Raw Edit

Unnamed

public text v1 · immutable
#2069561 ·published 2011-05-26 18:24 UTC
rendered paste body
int64_t div64( int64_t p, int64_t q ) {
    uint64_t up = (p<0)?-p:p;
    uint64_t uq = (q<0)?-q:q;

    uint64_t k = 1;
    int64_t res = 0;

    if( uq == 0 ) {
        return 0; /* FIXME: division by zero */
    }

    while( uq <= up ) {
        uq <<= 1;
        k <<= 1;
    }
    while( k ) {
        while( up >= uq ) {
            up -= uq;
            res += k;
        }
        k >>= 1;
        uq >>= 1;
    }
    if( p < 0 ) res = -res;
    if( q < 0 ) res = -res;

    return res;
}