/* * Assigns the scalar value to every element of the destination raw array. * * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int raw_array_assign_scalar(int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data) { int idim; npy_intp shape_it[NPY_MAXDIMS], dst_strides_it[NPY_MAXDIMS]; npy_intp coord[NPY_MAXDIMS]; PyArray_StridedUnaryOp *stransfer = NULL; NpyAuxData *transferdata = NULL; int aligned, needs_api = 0; npy_intp src_itemsize = src_dtype->elsize; NPY_BEGIN_THREADS_DEF; /* Check alignment */ aligned = raw_array_is_aligned(ndim, dst_data, dst_strides, dst_dtype->alignment); if (!npy_is_aligned(src_data, src_dtype->alignment)) { aligned = 0; } /* Use raw iteration with no heap allocation */ if (PyArray_PrepareOneRawArrayIter( ndim, shape, dst_data, dst_strides, &ndim, shape_it, &dst_data, dst_strides_it) < 0) { return -1; } /* Get the function to do the casting */ if (PyArray_GetDTypeTransferFunction(aligned, 0, dst_strides_it[0], src_dtype, dst_dtype, 0, &stransfer, &transferdata, &needs_api) != NPY_SUCCEED) { return -1; } if (!needs_api) { npy_intp nitems = 1, i; for (i = 0; i < ndim; i++) { nitems *= shape_it[i]; } NPY_BEGIN_THREADS_THRESHOLDED(nitems); } NPY_RAW_ITER_START(idim, ndim, coord, shape_it) { /* Process the innermost dimension */ stransfer(dst_data, dst_strides_it[0], src_data, 0, shape_it[0], src_itemsize, transferdata); } NPY_RAW_ITER_ONE_NEXT(idim, ndim, coord,
/* * Assigns the array from 'src' to 'dst'. The strides must already have * been broadcast. * * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int raw_array_assign_array(int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data, npy_intp *src_strides) { int idim; npy_intp shape_it[NPY_MAXDIMS]; npy_intp dst_strides_it[NPY_MAXDIMS]; npy_intp src_strides_it[NPY_MAXDIMS]; npy_intp coord[NPY_MAXDIMS]; PyArray_StridedUnaryOp *stransfer = NULL; NpyAuxData *transferdata = NULL; int aligned, needs_api = 0; npy_intp src_itemsize = src_dtype->elsize; NPY_BEGIN_THREADS_DEF; /* Check alignment */ aligned = raw_array_is_aligned(ndim, dst_data, dst_strides, dst_dtype->alignment) && raw_array_is_aligned(ndim, src_data, src_strides, src_dtype->alignment); /* Use raw iteration with no heap allocation */ if (PyArray_PrepareTwoRawArrayIter( ndim, shape, dst_data, dst_strides, src_data, src_strides, &ndim, shape_it, &dst_data, dst_strides_it, &src_data, src_strides_it) < 0) { return -1; } /* * Overlap check for the 1D case. Higher dimensional arrays and * opposite strides cause a temporary copy before getting here. */ if (ndim == 1 && src_data < dst_data && src_data + shape_it[0] * src_strides_it[0] > dst_data) { src_data += (shape_it[0] - 1) * src_strides_it[0]; dst_data += (shape_it[0] - 1) * dst_strides_it[0]; src_strides_it[0] = -src_strides_it[0]; dst_strides_it[0] = -dst_strides_it[0]; } /* Get the function to do the casting */ if (PyArray_GetDTypeTransferFunction(aligned, src_strides_it[0], dst_strides_it[0], src_dtype, dst_dtype, 0, &stransfer, &transferdata, &needs_api) != NPY_SUCCEED) { return -1; } if (!needs_api) { NPY_BEGIN_THREADS; } NPY_RAW_ITER_START(idim, ndim, coord, shape_it) { /* Process the innermost dimension */ stransfer(dst_data, dst_strides_it[0], src_data, src_strides_it[0], shape_it[0], src_itemsize, transferdata); } NPY_RAW_ITER_TWO_NEXT(idim, ndim, coord, shape_it,