#pragma once #include #include #include #include namespace MDSPAN_IMPL_STANDARD_NAMESPACE { namespace detail { // type alias used for rank-based tag dispatch // // this is used to enable alternatives to constexpr if when building for C++14 // template using with_rank = std::integral_constant; template MDSPAN_INLINE_FUNCTION constexpr bool common_integral_compare(I1 x, I2 y) { static_assert(std::is_integral::value && std::is_integral::value, ""); using I = std::common_type_t; return static_cast(x) == static_cast(y); } template MDSPAN_INLINE_FUNCTION constexpr bool rankwise_equal(with_rank<0>, const T1&, const T2&, F) { return true; } template MDSPAN_INLINE_FUNCTION constexpr bool rankwise_equal(with_rank, const T1& x, const T2& y, F func) { bool match = true; for (std::size_t r = 0; r < N; r++) { match = match && common_integral_compare(func(x, r), func(y, r)); } return match; } constexpr struct { template MDSPAN_INLINE_FUNCTION constexpr auto operator()(const T& x, I i) const { return x.extent(i); } } extent; constexpr struct { template MDSPAN_INLINE_FUNCTION constexpr auto operator()(const T& x, I i) const { return x.stride(i); } } stride; // same as std::integral_constant but with __host__ __device__ annotations on // the implicit conversion function and the call operator template struct integral_constant { using value_type = T; using type = integral_constant; static constexpr T value = v; MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr integral_constant() = default; // These interop functions work, because other than the value_type operator // everything of std::integral_constant works on device (defaulted functions) MDSPAN_FUNCTION constexpr integral_constant(std::integral_constant) {}; MDSPAN_FUNCTION constexpr operator std::integral_constant() const noexcept { return std::integral_constant{}; } MDSPAN_FUNCTION constexpr operator value_type() const noexcept { return value; } MDSPAN_FUNCTION constexpr value_type operator()() const noexcept { return value; } }; // The tuple implementation only comes in play when using capabilities // such as submdspan which require C++17 anyway #if MDSPAN_HAS_CXX_17 template struct tuple_member { using type = T; static constexpr size_t idx = Idx; T val; MDSPAN_FUNCTION constexpr T& get() { return val; } MDSPAN_FUNCTION constexpr const T& get() const { return val; } }; // A helper class which will be used via a fold expression to // select the type with the correct Idx in a pack of tuple_member template struct tuple_idx_matcher { using type = tuple_member; template MDSPAN_FUNCTION constexpr auto operator | (Other v) const { if constexpr (Idx == SearchIdx) { return *this; } else { return v; } } }; template struct tuple_impl; template struct tuple_impl, Elements...>: public tuple_member ... { MDSPAN_FUNCTION constexpr tuple_impl(Elements ... vals):tuple_member{vals}... {} template MDSPAN_FUNCTION constexpr auto& get() { using base_t = decltype((tuple_idx_matcher() | ...) ); return base_t::type::get(); } template MDSPAN_FUNCTION constexpr const auto& get() const { using base_t = decltype((tuple_idx_matcher() | ...) ); return base_t::type::get(); } }; // A simple tuple-like class for representing slices internally and is compatible with device code // This doesn't support type access since we don't need it // This is not meant as an external API template struct tuple: public tuple_impl()), Elements...> { MDSPAN_FUNCTION constexpr tuple(Elements ... vals):tuple_impl()), Elements ...>(vals ...) {} }; template MDSPAN_FUNCTION constexpr auto& get(tuple& vals) { return vals.template get(); } template MDSPAN_FUNCTION constexpr const auto& get(const tuple& vals) { return vals.template get(); } template tuple(Elements ...) -> tuple; #endif } // namespace detail constexpr struct mdspan_non_standard_tag { } mdspan_non_standard; } // namespace MDSPAN_IMPL_STANDARD_NAMESPACE