a Big Number module

bearophile bearophileHUGS at lycos.com
Sun Nov 4 05:32:48 PST 2007


Nice. Can it made to work with Phobos alone too?

Maybe the function bigintLLCountOnes() can be speed up with this:

/*********************************
Quickly return the number of set bits in the given uint
(functions similar to this one can work on 64 bits too).
*/
int countBitUint(TyNumber)(TyNumber v) {
    // This templating trick is useful to avoid processing ulongs
    static if( is(TyNumber == uint) ) {
        v = v - ((v >> 1) & 0x55555555);    // reuse input as temporary
        v = (v & 0x33333333) + ((v >> 2) & 0x33333333);        // temp
        return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
    } else
        assert(false, "countBitUint() works only with uint, not with " ~ typeid(TyNumber).toString);
}

Bye,
bearophile



More information about the Digitalmars-d mailing list