static final long pow(final long x, final long n) {
        /* Overflow check: overflow will occur if log2(abs(x)) * n > nbits. */
        final int nbits = 63;
        long r = 1;
        long p = x;
        double logtwox;
        long mask = 1;
        if (n < 0) { throw Py.ValueError("Integer to a negative power"); }
        if (x != 0) {
            logtwox = Math.log(Math.abs((double) x)) / Math.log(2.0);
            if (logtwox * n > nbits) { throw new PyException(
                    Py.ArithmeticError,
                    "Integer overflow in power."); }
        }
        while (mask > 0 && n >= mask) {
            if ((n & mask) != 0) {
                r *= p;
            }
            mask <<= 1;
            p *= p;
        }
        return r;
    }