Exemple #1
0
CNum::Counter CNum::Counter::pow(const Counter &rhs) const {
    assert(isNormalized());
    if (*this == 0 && rhs == 0)
        throw;
    if (*this == 0)
        return 0;
    if (rhs == 0)
        return 1;
    // *this >=1 && rhs >=1 here
    // Do repeated square algorithm
    const Unit SQ_SIZE = rhs.bitSize();
    assert(SQ_SIZE >= 1);
    std::vector<Counter> sq(SQ_SIZE);
    sq[0] = *this;
    for (Unit i = 1; i < SQ_SIZE; ++i) {
        sq[i] = sq[i - 1] * sq[i - 1];
    }
    Counter rv(1);
    for (Unit i = 0; i < SQ_SIZE; ++i) {
        if (rhs.isSet(i)) {
            rv *= sq[i];
        }
    }
    return rv;
}