Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions include/experimental/__p0009_bits/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

// Backport of std::remove_cvref / std::remove_cvref_t (C++20)
#if (__cplusplus >= 202002L)
using std::remove_cvref_t;
#else
template<class T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
#endif // __cplusplus >= 202002L

// type alias used for rank-based tag dispatch
//
// this is used to enable alternatives to constexpr if when building for C++14
Expand Down
157 changes: 157 additions & 0 deletions include/experimental/__p2630_bits/integral_constant_like.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#pragma once

#include "../__p0009_bits/utility.hpp"
#include <type_traits>
#if defined(__cpp_lib_concepts)
# include <concepts>
#endif // __cpp_lib_concepts

// ============================================================
// equality_comparable back-port (used only by integral_constant_like)
// ============================================================

#if defined(__cpp_lib_concepts)

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {
template<class T, class = void>
struct is_equality_comparable : std::bool_constant<std::equality_comparable<T>> {};

template<class T, class U, class = void>
struct is_equality_comparable_with : std::bool_constant<std::equality_comparable_with<T, U>> {};
} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#else

#include <utility>

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

template<typename T, typename = void>
struct is_equality_comparable : std::false_type {};

template<typename T>
struct is_equality_comparable<
T,
std::void_t<
decltype(std::declval<const T&>() == std::declval<const T&>()),
decltype(std::declval<const T&>() != std::declval<const T&>())
>
> : std::bool_constant<
std::is_convertible_v<
decltype(std::declval<const T&>() == std::declval<const T&>()),
bool
> &&
std::is_convertible_v<
decltype(std::declval<const T&>() != std::declval<const T&>()),
bool
>
> {};

template<typename T, typename U, typename = void>
struct is_equality_comparable_with : std::false_type {};

template<typename T, typename U>
struct is_equality_comparable_with<
T, U,
std::void_t<
decltype(std::declval<const T&>() == std::declval<const U&>()),
decltype(std::declval<const T&>() != std::declval<const U&>()),
decltype(std::declval<const U&>() == std::declval<const T&>()),
decltype(std::declval<const U&>() != std::declval<const T&>())
>
> : std::bool_constant<
is_equality_comparable<T>::value &&
is_equality_comparable<U>::value &&
std::is_convertible_v<
decltype(std::declval<const T&>() == std::declval<const U&>()),
bool
> &&
std::is_convertible_v<
decltype(std::declval<const T&>() != std::declval<const U&>()),
bool
> &&
std::is_convertible_v<
decltype(std::declval<const U&>() == std::declval<const T&>()),
bool
> &&
std::is_convertible_v<
decltype(std::declval<const U&>() != std::declval<const T&>()),
bool
>
> {};

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#endif // defined(__cpp_lib_concepts)

// ============================================================
// integral_constant_like concept / trait
// ============================================================

#if defined(__cpp_lib_concepts)

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

template<class T>
concept integral_constant_like =
std::is_integral_v<std::remove_cvref_t<decltype(T::value)>> &&
!std::is_same_v<bool, std::remove_cvref_t<decltype(T::value)>> &&
std::convertible_to<T, decltype(T::value)> &&
std::equality_comparable_with<T, decltype(T::value)> &&
std::bool_constant<T() == T::value>::value &&
std::bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value;

template<class T>
constexpr bool is_integral_constant_like_v = integral_constant_like<T>;

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#else

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

template<class T, class = void>
struct is_integral_constant_like_impl : std::false_type {};

template<class T>
struct is_integral_constant_like_impl<T, std::void_t<decltype(T::value), decltype(T())>> :
std::bool_constant<
std::is_integral_v<remove_cvref_t<decltype(T::value)>> &&
! std::is_same_v<bool, remove_cvref_t<decltype(T::value)>> &&
std::is_convertible_v<T, decltype(T::value)> &&
is_equality_comparable_with<T, decltype(T::value)>::value &&
std::bool_constant<T() == T::value>::value &&
std::bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value
>
{};

template<class T>
constexpr bool is_integral_constant_like_v = is_integral_constant_like_impl<T>::value;

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#endif // __cpp_lib_concepts
33 changes: 26 additions & 7 deletions include/experimental/__p2630_bits/strided_slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@

#pragma once

#include "../__p0009_bits/config.hpp"
#include "constant_wrapper.hpp"
#include "integral_constant_like.hpp"

#include <type_traits>

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {

namespace detail {
template<class T, class = void>
struct is_signed_or_unsigned_integral_constant_like : std::false_type {};

template<class T>
struct mdspan_is_integral_constant: std::false_type {};
struct is_signed_or_unsigned_integral_constant_like<
T, std::enable_if_t<is_integral_constant_like_v<T>>
> : std::bool_constant<
std::is_integral_v<remove_cvref_t<decltype(T::value)>> &&
! std::is_same_v<bool, remove_cvref_t<decltype(T::value)>>
>
{};

template<class T, T val>
struct mdspan_is_integral_constant<std::integral_constant<T,val>>: std::true_type {};
}
template<class T>
constexpr bool is_signed_or_unsigned_integral_constant_like_v =
is_signed_or_unsigned_integral_constant_like<T>::value;

template<class T>
constexpr bool mdspan_is_index_like_v =
(std::is_integral_v<T> && ! std::is_same_v<bool, T>) ||
is_signed_or_unsigned_integral_constant_like_v<T>;
} // namespace detail

// Slice Specifier allowing for strides and compile time extent
template <class OffsetType, class ExtentType, class StrideType>
Expand All @@ -40,9 +59,9 @@ struct strided_slice {
MDSPAN_IMPL_NO_UNIQUE_ADDRESS ExtentType extent{};
MDSPAN_IMPL_NO_UNIQUE_ADDRESS StrideType stride{};

static_assert(std::is_integral_v<OffsetType> || detail::mdspan_is_integral_constant<OffsetType>::value);
static_assert(std::is_integral_v<ExtentType> || detail::mdspan_is_integral_constant<ExtentType>::value);
static_assert(std::is_integral_v<StrideType> || detail::mdspan_is_integral_constant<StrideType>::value);
static_assert(detail::mdspan_is_index_like_v<OffsetType>);
static_assert(detail::mdspan_is_index_like_v<ExtentType>);
static_assert(detail::mdspan_is_index_like_v<StrideType>);
};

} // MDSPAN_IMPL_STANDARD_NAMESPACE
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ endif()
endif()

mdspan_add_test(test_constant_wrapper)
mdspan_add_test(test_strided_slice)
94 changes: 94 additions & 0 deletions tests/test_strided_slice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <mdspan/mdspan.hpp>
#include <type_traits>

#include <gtest/gtest.h>

namespace {

template<class OffsetType, class ExtentType, class StrideType>
void test_strided_slice(OffsetType offset, ExtentType extent, StrideType stride)
{
// Some compilers are bad at CTAD for aggregates.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this worked! It's just that Clang is somehow bad at this.

Kokkos::strided_slice<OffsetType, ExtentType, StrideType> s{offset, extent, stride};

static_assert(std::is_same_v<decltype(s), Kokkos::strided_slice<OffsetType, ExtentType, StrideType>>);
auto offset2 = s.offset;
static_assert(std::is_same_v<OffsetType, decltype(offset2)>);
auto extent2 = s.extent;
static_assert(std::is_same_v<ExtentType, decltype(extent2)>);
auto stride2 = s.stride;
static_assert(std::is_same_v<StrideType, decltype(stride2)>);

ASSERT_EQ(offset2, offset);
ASSERT_EQ(extent2, extent);
ASSERT_EQ(stride2, stride);
}

template<class T, T Value>
constexpr auto IC = std::integral_constant<T, Value>{};

MDSPAN_TEMPLATE_REQUIRES(
class T,
T Value,
/* requires */ (
std::is_integral_v<T> && ! std::is_same_v<T, bool>
)
)
struct my_integral_constant {
static constexpr T value = Value;
constexpr operator T () const { return value; }
};

template<class T, T Value>
constexpr auto IC2 = my_integral_constant<T, Value>{};

static_assert(
std::is_convertible_v<
my_integral_constant<int, 1>,
decltype(my_integral_constant<int, 1>::value)>);

static_assert(
Kokkos::detail::is_equality_comparable_with<
my_integral_constant<int, 1>,
decltype(my_integral_constant<int, 1>::value)>::value);

static_assert(
Kokkos::detail::is_integral_constant_like_v<
my_integral_constant<int, 1>
>);

TEST(StridedSlice, WellFormed) {
test_strided_slice(int(1), unsigned(10), long(3));
test_strided_slice((signed char)(1), (unsigned short)(10), (unsigned long long)(3));

test_strided_slice(IC<int, 1>, unsigned(10), long(3));
test_strided_slice(int(1), IC<unsigned, 10>, long(3));
test_strided_slice(int(1), unsigned(10), IC<long, 3>);

using MDSPAN_IMPL_STANDARD_NAMESPACE::detail::cw;

test_strided_slice(cw<1>, unsigned(10), long(3));
test_strided_slice(int(1), cw<unsigned(10)>, long(3));
test_strided_slice(int(1), unsigned(10), cw<long(3)>);

test_strided_slice(IC2<int, 1>, unsigned(10), long(3));
test_strided_slice(int(1), IC2<unsigned, 10>, long(3));
test_strided_slice(int(1), unsigned(10), IC2<long, 3>);
}

} // namespace (anonymous)