Commit 7fc33f75 authored by Stefan Behnel's avatar Stefan Behnel

calculate 2**N in PY_LONG_LONG if it's larger than long and the value fits

parent bcbdbb10
...@@ -470,6 +470,9 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject ...@@ -470,6 +470,9 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject
if ((size_t)shiftby <= sizeof(long) * 8 - 2) { if ((size_t)shiftby <= sizeof(long) * 8 - 2) {
long value = 1L << shiftby; long value = 1L << shiftby;
return PyInt_FromLong(value); return PyInt_FromLong(value);
} else if ((size_t)shiftby <= sizeof(unsigned PY_LONG_LONG) * 8 - 1) {
unsigned PY_LONG_LONG value = ((unsigned PY_LONG_LONG)1) << shiftby;
return PyLong_FromUnsignedLongLong(value);
} else { } else {
PyObject *one = PyInt_FromLong(1L); PyObject *one = PyInt_FromLong(1L);
if (unlikely(!one)) return NULL; if (unlikely(!one)) return NULL;
......
...@@ -107,6 +107,10 @@ def optimised_pow2(n): ...@@ -107,6 +107,10 @@ def optimised_pow2(n):
1073741824 1073741824
>>> print(repr(optimised_pow2(32)).rstrip('L')) >>> print(repr(optimised_pow2(32)).rstrip('L'))
4294967296 4294967296
>>> print(repr(optimised_pow2(60)).rstrip('L'))
1152921504606846976
>>> print(repr(optimised_pow2(64)).rstrip('L'))
18446744073709551616
>>> print(repr(optimised_pow2(100)).rstrip('L')) >>> print(repr(optimised_pow2(100)).rstrip('L'))
1267650600228229401496703205376 1267650600228229401496703205376
>>> optimised_pow2(30000) == 2 ** 30000 >>> optimised_pow2(30000) == 2 ** 30000
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment