diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-06-27 17:11:12 -0400 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-06-27 17:11:12 -0400 |
commit | 38d6f099bf97f46024346112f94881575bb420f0 (patch) | |
tree | c2d3738be04327fa8f029156a020ea34a5108eec | |
parent | 16cdeff7520819322209f89003dcc282903ca34e (diff) | |
parent | 5c3c04537a76099aa7ac9cc5db9d797287e44612 (diff) | |
download | numpy-38d6f099bf97f46024346112f94881575bb420f0.tar.gz |
Merge pull request #5955 from jaimefrio/faster_revert
MANT: Cleanup of _pyarray_revert
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 56 |
1 files changed, 25 insertions, 31 deletions
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 4a29bb45e..d2dc4469a 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1258,47 +1258,41 @@ clean_ret: static int _pyarray_revert(PyArrayObject *ret) { - npy_intp length; - npy_intp i; - PyArray_CopySwapFunc *copyswap; - char *tmp = NULL, *sw1, *sw2; - npy_intp os; - char *op; - - length = PyArray_DIMS(ret)[0]; - copyswap = PyArray_DESCR(ret)->f->copyswap; - - tmp = PyArray_malloc(PyArray_DESCR(ret)->elsize); - if (tmp == NULL) { - return -1; + npy_intp length = PyArray_DIM(ret, 0); + npy_intp os = PyArray_DESCR(ret)->elsize; + char *op = PyArray_DATA(ret); + char *sw1 = op; + char *sw2; + + if (PyArray_ISNUMBER(ret) && !PyArray_ISCOMPLEX(ret)) { + /* Optimization for unstructured dtypes */ + PyArray_CopySwapNFunc *copyswapn = PyArray_DESCR(ret)->f->copyswapn; + sw2 = op + length * os - 1; + /* First reverse the whole array byte by byte... */ + while(sw1 < sw2) { + const char tmp = *sw1; + *sw1++ = *sw2; + *sw2-- = tmp; + } + /* ...then swap in place every item */ + copyswapn(op, os, NULL, 0, length, 1, NULL); } - - os = PyArray_DESCR(ret)->elsize; - op = PyArray_DATA(ret); - sw1 = op; - sw2 = op + (length - 1) * os; - if (PyArray_ISFLEXIBLE(ret) || PyArray_ISOBJECT(ret)) { - for(i = 0; i < length/2; ++i) { - memmove(tmp, sw1, os); - copyswap(tmp, NULL, 0, NULL); - memmove(sw1, sw2, os); - copyswap(sw1, NULL, 0, NULL); - memmove(sw2, tmp, os); - copyswap(sw2, NULL, 0, NULL); - sw1 += os; - sw2 -= os; + else { + char *tmp = PyArray_malloc(PyArray_DESCR(ret)->elsize); + if (tmp == NULL) { + return -1; } - } else { - for(i = 0; i < length/2; ++i) { + sw2 = op + (length - 1) * os; + while (sw1 < sw2) { memcpy(tmp, sw1, os); memcpy(sw1, sw2, os); memcpy(sw2, tmp, os); sw1 += os; sw2 -= os; } + PyArray_free(tmp); } - PyArray_free(tmp); return 0; } |