diff options
| author | Eric Wieser <wieser.eric@gmail.com> | 2018-01-04 07:54:47 -0800 |
|---|---|---|
| committer | Eric Wieser <wieser.eric@gmail.com> | 2018-01-04 08:19:25 -0800 |
| commit | 0398d44d0c6622853730f34df2d5271e3c2ee3d5 (patch) | |
| tree | 1c4414fd1bf48c1527452c5f19a12db13615df1a /numpy/core/src | |
| parent | 9a902b775c12be99708a0a33a244ace3521855de (diff) | |
| download | numpy-0398d44d0c6622853730f34df2d5271e3c2ee3d5.tar.gz | |
MAINT: Replace manual expansion of PyArray_MinScalarType with an inner helper function
Diffstat (limited to 'numpy/core/src')
| -rw-r--r-- | numpy/core/src/multiarray/convert_datatype.c | 59 |
1 files changed, 23 insertions, 36 deletions
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index 9d28e5c15..6ff381862 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -1610,16 +1610,12 @@ static int min_scalar_type_num(char *valueptr, int type_num, return type_num; } -/*NUMPY_API - * If arr is a scalar (has 0 dimensions) with a built-in number data type, - * finds the smallest type size/kind which can still represent its data. - * Otherwise, returns the array's data type. - * - */ + NPY_NO_EXPORT PyArray_Descr * -PyArray_MinScalarType(PyArrayObject *arr) +PyArray_MinScalarType_internal(PyArrayObject *arr, int *is_small_unsigned) { PyArray_Descr *dtype = PyArray_DESCR(arr); + *is_small_unsigned = 0; /* * If the array isn't a numeric scalar, just return the array's dtype. */ @@ -1630,18 +1626,30 @@ PyArray_MinScalarType(PyArrayObject *arr) else { char *data = PyArray_BYTES(arr); int swap = !PyArray_ISNBO(dtype->byteorder); - int is_small_unsigned = 0; /* An aligned memory buffer large enough to hold any type */ npy_longlong value[4]; dtype->f->copyswap(&value, data, swap, NULL); return PyArray_DescrFromType( min_scalar_type_num((char *)&value, - dtype->type_num, &is_small_unsigned)); + dtype->type_num, is_small_unsigned)); } } +/*NUMPY_API + * If arr is a scalar (has 0 dimensions) with a built-in number data type, + * finds the smallest type size/kind which can still represent its data. + * Otherwise, returns the array's data type. + * + */ +NPY_NO_EXPORT PyArray_Descr * +PyArray_MinScalarType(PyArrayObject *arr) +{ + int is_small_unsigned; + return PyArray_MinScalarType_internal(arr, &is_small_unsigned); +} + /* * Provides an ordering for the dtype 'kind' character codes, to help * determine when to use the min_scalar_type function. This groups @@ -1773,33 +1781,12 @@ PyArray_ResultType(npy_intp narrs, PyArrayObject **arr, } else { for (i = 0; i < narrs; ++i) { - /* Get the min scalar type for the array */ - PyArray_Descr *tmp = PyArray_DESCR(arr[i]); - int tmp_is_small_unsigned = 0; - /* - * If it's a scalar, find the min scalar type. The function - * is expanded here so that we can flag whether we've got an - * unsigned integer which would fit an a signed integer - * of the same size, something not exposed in the public API. - */ - if (PyArray_NDIM(arr[i]) == 0 && - PyTypeNum_ISNUMBER(tmp->type_num)) { - char *data = PyArray_BYTES(arr[i]); - int swap = !PyArray_ISNBO(tmp->byteorder); - int type_num; - /* An aligned memory buffer large enough to hold any type */ - npy_longlong value[4]; - tmp->f->copyswap(&value, data, swap, NULL); - type_num = min_scalar_type_num((char *)&value, - tmp->type_num, &tmp_is_small_unsigned); - tmp = PyArray_DescrFromType(type_num); - if (tmp == NULL) { - Py_XDECREF(ret); - return NULL; - } - } - else { - Py_INCREF(tmp); + int tmp_is_small_unsigned; + PyArray_Descr *tmp = PyArray_MinScalarType_internal( + arr[i], &tmp_is_small_unsigned); + if (tmp == NULL) { + Py_XDECREF(ret); + return NULL; } /* Combine it with the existing type */ if (ret == NULL) { |
