Skip to content

Commit

Permalink
ENH: remove overflow check to inline pos_to_index
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Jevnik authored and llllllllll committed May 20, 2019
1 parent d22139c commit 78f83d2
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions include/libpy/ndarray_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,9 @@ class ndarray_view {

std::ptrdiff_t pos_to_index(const std::array<std::size_t, ndim>& pos) const {
std::ptrdiff_t ix = 0;

for (std::size_t n = 0; n < ndim; ++n) {
std::ptrdiff_t along_axis;
if (__builtin_mul_overflow(pos[n], m_strides[n], &along_axis)) {
throw std::overflow_error("pos * m_strides overflows std::ptrdiff_t");
}
if (__builtin_add_overflow(ix, along_axis, &ix)) {
throw std::overflow_error("ix + along_axis overflows std::ptrdiff_t");
}
ix += pos[n] * m_strides[n];
}

return ix;
}

Expand Down Expand Up @@ -471,17 +463,9 @@ class any_ref_ndarray_view {

std::ptrdiff_t pos_to_index(const std::array<std::size_t, ndim>& pos) const {
std::ptrdiff_t ix = 0;

for (std::size_t n = 0; n < ndim; ++n) {
std::ptrdiff_t along_axis;
if (__builtin_mul_overflow(pos[n], m_strides[n], &along_axis)) {
throw std::overflow_error("pos * m_strides overflows std::ptrdiff_t");
}
if (__builtin_add_overflow(ix, along_axis, &ix)) {
throw std::overflow_error("ix + along_axis overflows std::ptrdiff_t");
}
ix += pos[n] * m_strides[n];
}

return ix;
}

Expand Down

0 comments on commit 78f83d2

Please sign in to comment.