Mercurial > cpython-lru_cache-weakref
diff Objects/longobject.c @ 250:b9986009d7dc legacy-trunk
Fix off-by-one error in long_repr.
Implement long_pow.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 28 May 1991 21:58:16 +0000 |
| parents | 316a0d9c0096 |
| children | 71b0bd5ed162 |
line wrap: on
line diff
--- a/Objects/longobject.c Tue May 28 21:57:39 1991 +0000 +++ b/Objects/longobject.c Tue May 28 21:58:16 1991 +0000 @@ -278,6 +278,7 @@ assert(p > q); do { } while ((*q++ = *p++) != '\0'); + q--; resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str))); } return str; @@ -844,16 +845,60 @@ } static object * -long_pow(v, w) - longobject *v; - register object *w; +long_pow(a, w) + longobject *a; + object *w; { + register longobject *b; + longobject *z; + int size_b, i; + if (!is_longobject(w)) { err_badarg(); return NULL; } - err_setstr(SystemError, "long power not implemented"); - return NULL; + + b = (longobject *)w; + size_b = b->ob_size; + if (size_b < 0) { + err_setstr(RuntimeError, "long integer to the negative power"); + return NULL; + } + + z = (longobject *)newlongobject(1L); + + INCREF(a); + for (i = 0; i < size_b; ++i) { + digit bi = b->ob_digit[i]; + int j; + + for (j = 0; j < SHIFT; ++j) { + longobject *temp; + + if (bi & 1) { + temp = (longobject *)long_mul(z, (object *)a); + DECREF(z); + z = temp; + if (z == NULL) + break; + } + bi >>= 1; + if (bi == 0 && i+1 == size_b) + break; + temp = (longobject *)long_mul(a, (object *)a); + DECREF(a); + a = temp; + if (a == NULL) { + DECREF(z); + z = NULL; + break; + } + } + if (a == NULL) + break; + } + XDECREF(a); + return (object *)z; } static object *
