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 f533312
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions include/experimental/__p0009_bits/layout_left.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,20 @@ 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;
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
index_type stride = 1;
for(rank_type r=0; r<__extents.rank(); r++) {
if(stride != other.stride(r))
if(stride != static_cast<index_type>(other.stride(r))) {
// Note this throw will lead to a terminate if triggered since this function is marked noexcept
throw std::runtime_error("Assigning layout_stride to layout_left with invalid strides.");
}
stride *= __extents.extent(r);
}
#endif
Expand Down
10 changes: 6 additions & 4 deletions include/experimental/__p0009_bits/layout_right.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,20 @@ 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;
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
index_type stride = 1;
for(rank_type r=__extents.rank(); r>0; r--) {
if(stride != other.stride(r-1))
if(stride != static_cast<index_type>(other.stride(r-1))) {
// Note this throw will lead to a terminate if triggered since this function is marked noexcept
throw std::runtime_error("Assigning layout_stride to layout_right with invalid strides.");
}
stride *= __extents.extent(r-1);
}
#endif
Expand Down

0 comments on commit f533312

Please sign in to comment.