-
Notifications
You must be signed in to change notification settings - Fork 82
Add strided_slice updates with integral_constant_like support (P3663 piece 2 of N)
#450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6bc21e
Initial plan
Copilot ef17e31
Add strided_slice updates with integral_constant_like support
Copilot f2188f5
Apply suggestions from code review
crtrott 0489fe2
Update include/experimental/__p0009_bits/utility.hpp
crtrott 7a1c1df
Remove MDSPAN_IMPL_ENABLE_P3663 guards and operator() block from test…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
include/experimental/__p2630_bits/integral_constant_like.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,3 +113,4 @@ endif() | |
| endif() | ||
|
|
||
| mdspan_add_test(test_constant_wrapper) | ||
| mdspan_add_test(test_strided_slice) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.