8000 BUG: Don't produce undefined behavior for a << b if b >= bitsof(a) by eric-wieser · Pull Request #13739 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Don't produce undefined behavior for a << b if b >= bitsof(a) #13739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT: Respond to review comments on gh-7473
This:
* Inlines the macros in loops.c.src
* Replaces 8 with `CHAR_BIT `. The `NPY_SIZEOF_*` macros are not used here because it's too much work to apply them to the signed types, and they expand to the same thing anyway.
* Removes the reduce loop specializations which likely no one cares about
* Uses pytest.mark.parametrize to shorten the test
  • Loading branch information
eric-wieser committed Sep 13, 2019
commit fca077c7e2fc8f21c639ba479267a34eb16a2810
104 changes: 27 additions & 77 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
/**begin repeat2
* Arithmetic
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor#
* #OP = +, -,*, &, |, ^#
* #OP = +, -, *, &, |, ^#
*/

#if @CHK@
Expand Down Expand Up @@ -808,98 +808,48 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
* which is undefined in C.
*/

#define LEFT_SHIFT_OP \
do { \
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \
*out = in1 << in2; \
} \
else { \
*out = 0; \
} \
} while (0)


NPY_NO_EXPORT NPY_GCC_OPT_3 void
@TYPE@_left_shift@isa@(char **args, npy_intp *dimensions, npy_intp *steps,
void *NPY_UNUSED(func))
{
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
@type@ ip2_val = *(@type@ *)ip2;

if (NPY_LIKELY(ip2_val < sizeof(@type@) * 8)) {
io1 <<= ip2_val;
}
else {
io1 = 0;
}
BINARY_LOOP_FAST(@type@, @type@,
if (NPY_LIKELY(in2 < sizeof(@type@) * CHAR_BIT)) {
*out = in1 << in2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP_FAST(@type@, @type@, LEFT_SHIFT_OP);
}
}

#undef LEFT_SHIFT_OP

#define RIGHT_SHIFT_OP_SIGNED \
do { \
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \
*out = in1 >> in2; \
} \
else if (in1 < 0) { \
*out = -1; \
} \
else { \
*out = 0; \
} \
} while (0)

#define RIGHT_SHIFT_OP_UNSIGNED \
do { \
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \
*out = in1 >> in2; \
} \
else { \
*out = 0; \
} \
} while (0)
else {
*out = 0;
}
);
}

NPY_NO_EXPORT NPY_GCC_OPT_3 void
@TYPE@_right_shift@isa@(char **args, npy_intp *dimensions, npy_intp *steps,
void *NPY_UNUSED(func))
{
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
@type@ ip2_val = *(@type@ *)ip2;

if (NPY_LIKELY(ip2_val < sizeof(@type@) * 8)) {
io1 >>= ip2_val;
}
#if @SIGNED@
else if (io1 < 0) {
io1 = -1;
}
#endif
else {
io1 = 0;
}
BINARY_LOOP_FAST(@type@, @type@, {
if (NPY_LIKELY(in2 < sizeof(@type@) * CHAR_BIT)) {
*out = in1 >> in2;
}
*((@type@ *)iop1) = io1;
}
else {
#if @SIGNED@
BINARY_LOOP_FAST(@type@, @type@, RIGHT_SHIFT_OP_SIGNED);
else if (in1 < 0) {
*out = (@type@)-1; /* shift right preserves the sign bit */
}
else {
*out = 0;
}
});
#else
BINARY_LOOP_FAST(@type@, @type@, RIGHT_SHIFT_OP_UNSIGNED);
BINARY_LOOP_FAST(@type@, @type@, {
if (NPY_LIKELY(in2 < sizeof(@type@) * CHAR_BIT)) {
*out = in1 >> in2;
}
else {
*out = 0;
}
});
#endif
}
}

#undef RIGHT_SHIFT_OP_SIGNED
#undef RIGHT_SHIFT_OP_UNSIGNED


/**begin repeat2
* #kind = equal, not_equal, greater, greater_equal, less, less_equal,
Expand Down
56 changes: 29 additions & 27 deletions numpy/core/src/umath/scalarmath.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -263,38 +263,40 @@ static void

/**end repeat1**/

#define @name@_ctype_lshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * 8)) { \
*(out) = (arg1) << (arg2); \
} \
else { \
*(out) = 0; \
} \
/* Note: these need to be kept in sync with the shift ufuncs */

#define @name@_ctype_lshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * CHAR_BIT)) { \
*(out) = (arg1) << (arg2); \
} \
else { \
*(out) = 0; \
} \
} while (0)

#if @issigned@
#define @name@_ctype_rshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * 8)) { \
*(out) = (arg1) >> (arg2); \
} \
else if ((arg1) < 0) { \
*(out) = -1; \
} \
else { \
*(out) = 0; \
} \
#define @name@_ctype_rshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * CHAR_BIT)) { \
*(out) = (arg1) >> (arg2); \
} \
else if ((arg1) < 0) { \
*(out) = -1; \
} \
else { \
*(out) = 0; \
} \
} while (0)
#else
#define @name@_ctype_rshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * 8)) { \
*(out) = (arg1) >> (arg2); \
} \
else { \
*(out) = 0; \
} \
#define @name@_ctype_rshift(arg1, arg2, out) \
do { \
if (NPY_LIKELY((arg2) < sizeof(@type@) * CHAR_BIT)) { \
*(out) = (arg1) >> (arg2); \
} \
else { \
*(out) = 0; \
} \
} while (0)
#endif

Expand Down
46 changes: 21 additions & 25 deletions numpy/core/tests/test_scalarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,31 +668,27 @@ def test_numpy_abs(self):

class TestBitShifts(object):

def test_left_shift(self):
@pytest.mark.parametrize('type_code', np.typecodes['AllInteger'])
@pytest.mark.parametrize('op',
[operator.rshift, operator.lshift], ids=['>>', '<<'])
def test_shift_all_bits(self, type_code, op):
""" Shifts where the shift amount is the width of the type or wider """
# gh-2449
for dt in np.typecodes['AllInteger']:
arr = np.array([5, -5], dtype=dt)
scl_pos, scl_neg = arr
for shift in np.array([arr.dtype.itemsize * 8], dtype=dt):
res_pos = scl_pos << shift
res_neg = scl_neg << shift
assert_equal(res_pos, 0)
assert_equal(res_neg, 0)
# Result on scalars should be the same as on arrays
assert_array_equal(arr << shift, [res_pos, res_neg])

def test_right_shift(self):
# gh-2449
for dt in np.typecodes['AllInteger']:
arr = np.array([5, -5], dtype=dt)
scl_pos, scl_neg = arr
for shift in np.array([arr.dtype.itemsize * 8], dtype=dt):
res_pos = scl_pos >> shift
res_neg = scl_neg >> shift
assert_equal(res_pos, 0)
if dt in np.typecodes['UnsignedInteger']:
assert_equal(res_neg, 0)
dt = np.dtype(type_code)
nbits = dt.itemsize * 8
for val in [5, -5]:
for shift in [nbits, nbits + 4]:
val_scl = dt.type(val)
shift_scl = dt.type(shift)
res_scl = op(val_scl, shift_scl)
if val_scl < 0 and op is operator.rshift:
# sign bit is preserved
assert_equal(res_scl, -1)
else:
assert_equal(res_neg, -1)
assert_equal(res_scl, 0)

# Result on scalars should be the same as on arrays
assert_array_equal(arr >> shift, [res_pos, res_neg], dt)
val_arr = np.array([val]*32, dtype=dt)
shift_arr = np.array([shift]*32, dtype=dt)
res_arr = op(val_arr, shift_arr)
assert_equal(res_arr, res_scl)
0