Skip to content

Commit

Permalink
Address issue #229 and fix missing noexcept
Browse files Browse the repository at this point in the history
The conversion operators from layout_stride for both layout_left
and layout_right were missing a static_cast and also were not marked
noexcept. I now guard the precondition check with NDEBUG.

Note: the throw in there is legal: it will result in a termination
if triggered since the function is marked noexcept.
  • Loading branch information
crtrott committed Jan 24, 2023
1 parent 8b64a76 commit bd59063
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
19 changes: 11 additions & 8 deletions include/experimental/__p0009_bits/layout_left.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,23 @@ class layout_left::mapping {
)
MDSPAN_CONDITIONAL_EXPLICIT((extents_type::rank() > 0))
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
mapping(layout_stride::mapping<OtherExtents> const& other) // NOLINT(google-explicit-constructor)
mapping(layout_stride::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
:__extents(other.extents())
{
/*
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#ifndef __CUDA_ARCH__
size_t stride = 1;
for(rank_type r=0; r<__extents.rank(); r++) {
if(stride != other.stride(r))
throw std::runtime_error("Assigning layout_stride to layout_left with invalid strides.");
stride *= __extents.extent(r);
}
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP)
assert([&]()->bool {
index_type stride = 1;
for(rank_type r=0; r<__extents.rank(); r++) {
if(stride != static_cast<index_type>(other.stride(r)))
return false;
stride *= __extents.extent(r);
}
return true;
}() && "Assigning layout_stride to layout_left with invalid strides.");
#endif
}

Expand Down
19 changes: 11 additions & 8 deletions include/experimental/__p0009_bits/layout_right.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,23 @@ class layout_right::mapping {
)
MDSPAN_CONDITIONAL_EXPLICIT((extents_type::rank() > 0))
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
mapping(layout_stride::mapping<OtherExtents> const& other) // NOLINT(google-explicit-constructor)
mapping(layout_stride::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
:__extents(other.extents())
{
/*
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#ifndef __CUDA_ARCH__
size_t stride = 1;
for(rank_type r=__extents.rank(); r>0; r--) {
if(stride != other.stride(r-1))
throw std::runtime_error("Assigning layout_stride to layout_right with invalid strides.");
stride *= __extents.extent(r-1);
}
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP)
assert([&]()->bool {
index_type stride = 1;
for(rank_type r=__extents.rank(); r>0; r--) {
if(stride != static_cast<index_type>(other.stride(r-1)))
return false;
stride *= __extents.extent(r-1);
}
return true;
}() && "Assigning layout_stride to layout_left with invalid strides.");
#endif
}

Expand Down

0 comments on commit bd59063

Please sign in to comment.