Skip to content

Commit

Permalink
BUG: fix for modifying the index arg in ufunc_at
Browse files Browse the repository at this point in the history
  • Loading branch information
mattip authored and charris committed Jul 30, 2023
1 parent 5c964bb commit fa3a023
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 33 deletions.
99 changes: 72 additions & 27 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,19 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 int
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
@type@ *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (@type@ *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;
}
indexed = (@type@ *)(ip1 + is1 * indx);
*indexed = *indexed @OP@ *(@type@ *)value;
}
return 0;
Expand Down Expand Up @@ -1241,14 +1246,19 @@ NPY_NO_EXPORT int
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
@type@ *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (@type@ *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1259 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1259

Added line #L1259 was not covered by tests
}
indexed = (@type@ *)(ip1 + is1 * indx);
*indexed = npy_floor_divide@c@(*indexed, *(@type@ *)value);
}
return 0;
Expand Down Expand Up @@ -1395,14 +1405,19 @@ LONGDOUBLE_@kind@_indexed(PyArrayMethod_Context *NPY_UNUSED(context),
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
npy_longdouble *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (npy_longdouble *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1418 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1418

Added line #L1418 was not covered by tests
}
indexed = (npy_longdouble *)(ip1 + is1 * indx);
*indexed = *indexed @OP@ *(npy_longdouble *)value;
}
return 0;
Expand Down Expand Up @@ -1520,14 +1535,19 @@ HALF_@kind@_indexed(void *NPY_UNUSED(context),
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
npy_half *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (npy_half *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1548 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1548

Added line #L1548 was not covered by tests
}
indexed = (npy_half *)(ip1 + is1 * indx);
const float v = npy_half_to_float(*(npy_half *)value);
*indexed = npy_float_to_half(npy_half_to_float(*indexed) @OP@ v);
}
Expand Down Expand Up @@ -1641,14 +1661,19 @@ HALF_@kind@_indexed(PyArrayMethod_Context *NPY_UNUSED(context),
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
npy_half *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (npy_half *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1674 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1674

Added line #L1674 was not covered by tests
}
indexed = (npy_half *)(ip1 + is1 * indx);
npy_half v = *(npy_half *)value;
*indexed = (@OP@(*indexed, v) || npy_half_isnan(*indexed)) ? *indexed : v;
}
Expand Down Expand Up @@ -1679,14 +1704,19 @@ HALF_@kind@_indexed(PyArrayMethod_Context *NPY_UNUSED(context),
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
npy_half *indexed;
for (i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (npy_half *)(ip1 + is1 * *(npy_intp *)indx);
for (i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1717 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1717

Added line #L1717 was not covered by tests
}
indexed = (npy_half *)(ip1 + is1 * indx);
npy_half v = *(npy_half *)value;
*indexed = (@OP@(*indexed, v) || npy_half_isnan(v)) ? *indexed: v;
}
Expand Down Expand Up @@ -1717,14 +1747,19 @@ HALF_floor_divide_indexed(PyArrayMethod_Context *NPY_UNUSED(context),
void *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
npy_half *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (npy_half *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1760 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1760

Added line #L1760 was not covered by tests
}
indexed = (npy_half *)(ip1 + is1 * indx);
float v = npy_half_to_float(*(npy_half *)value);
float div = npy_floor_dividef(npy_half_to_float(*indexed), v);
*indexed = npy_float_to_half(div);
Expand Down Expand Up @@ -1947,14 +1982,19 @@ NPY_NO_EXPORT int @TYPE@_@kind@_indexed
(PyArrayMethod_Context *NPY_UNUSED(context), char * const*args, npy_intp const *dimensions, npy_intp const *steps, NpyAuxData *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
@ftype@ *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (@ftype@ *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 1995 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L1995

Added line #L1995 was not covered by tests
}
indexed = (@ftype@ *)(ip1 + is1 * indx);
const @ftype@ b_r = ((@ftype@ *)value)[0];
const @ftype@ b_i = ((@ftype@ *)value)[1];
indexed[0] @OP@= b_r;
Expand All @@ -1981,14 +2021,19 @@ NPY_NO_EXPORT int @TYPE@_multiply_indexed
(PyArrayMethod_Context *NPY_UNUSED(context), char * const*args, npy_intp const *dimensions, npy_intp const *steps, NpyAuxData *NPY_UNUSED(func))
{
char *ip1 = args[0];
char *indx = args[1];
char *indxp = args[1];
char *value = args[2];
npy_intp is1 = steps[0], isindex = steps[1], isb = steps[2];
npy_intp shape = steps[3];
npy_intp n = dimensions[0];
npy_intp i;
@ftype@ *indexed;
for(i = 0; i < n; i++, indx += isindex, value += isb) {
indexed = (@ftype@ *)(ip1 + is1 * *(npy_intp *)indx);
for(i = 0; i < n; i++, indxp += isindex, value += isb) {
npy_intp indx = *(npy_intp *)indxp;
if (indx < 0) {
indx += shape;

Check warning on line 2034 in numpy/core/src/umath/loops.c.src

View check run for this annotation

Codecov / codecov/patch

numpy/core/src/umath/loops.c.src#L2034

Added line #L2034 was not covered by tests
}
indexed = (@ftype@ *)(ip1 + is1 * indx);
const @ftype@ a_r = indexed[0];
const @ftype@ a_i = indexed[1];
const @ftype@ b_r = ((@ftype@ *)value)[0];
Expand Down
13 changes: 7 additions & 6 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -5939,7 +5939,7 @@ trivial_at_loop(PyArrayMethodObject *ufuncimpl, NPY_ARRAYMETHOD_FLAGS flags,
int buffersize=0, errormask = 0;
int res;
char *args[3];
npy_intp steps[3];
npy_intp steps[4];
args[0] = (char *) iter->baseoffset;
steps[0] = iter->fancy_strides[0];
if (ufuncimpl->nin == 1) {
Expand All @@ -5962,16 +5962,17 @@ trivial_at_loop(PyArrayMethodObject *ufuncimpl, NPY_ARRAYMETHOD_FLAGS flags,
do {
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
npy_intp * indxP = (npy_intp *)iter->outer_ptrs[0];
for (npy_intp i=0; i < *inner_size; i++) {
if (indxP[i] < 0) {
indxP[i] += iter->fancy_dims[0];
}
}
args[1] = (char *)indxP;
steps[1] = iter->outer_strides[0];
/*
* The value of iter->fancy_dims[0] is added to negative indexes
* inside the inner loop
*/
steps[3] = iter->fancy_dims[0];

res = ufuncimpl->contiguous_indexed_loop(
context, args, inner_size, steps, NULL);

if (args[2] != NULL) {
args[2] += (*inner_size) * steps[2];
}
Expand Down

0 comments on commit fa3a023

Please sign in to comment.