tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
layout_transpose.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_LINALG_LAYOUT_TRANSPOSE_HPP
4#define TETL_LINALG_LAYOUT_TRANSPOSE_HPP
5
8
9namespace etl::linalg {
10
11namespace detail {
12
13template <typename Extents>
14using transpose_extents_t = extents<typename Extents::index_type, Extents::static_extent(1), Extents::static_extent(0)>;
15
16template <typename Extents>
17 requires(Extents::rank() == 2)
18[[nodiscard]] constexpr auto transpose_extents(Extents const& e) -> transpose_extents_t<Extents>
19{
20 constexpr auto isDynamicE0 = Extents::static_extent(0) == dynamic_extent;
21 constexpr auto isDynamicE1 = Extents::static_extent(1) == dynamic_extent;
22
23 if constexpr (isDynamicE0) {
24 if constexpr (isDynamicE1) {
25 return transpose_extents_t<Extents>{e.extent(1), e.extent(0)};
26 } else {
27 return transpose_extents_t<Extents>{e.extent(0)};
28 }
29 } else {
30 if constexpr (isDynamicE1) {
31 return transpose_extents_t<Extents>{e.extent(1)};
32 } else {
33 return transpose_extents_t<Extents>{};
34 }
35 }
36}
37
38} // namespace detail
39
41template <typename Layout>
43 template <typename Extents>
44 requires(Extents::rank() == 2)
45 struct mapping {
46 private:
47 using nested_mapping_t = typename Layout::template mapping<detail::transpose_extents_t<Extents>>;
48 nested_mapping_t _nestedMapping;
49
50 public:
51 using extents_type = Extents;
52 using size_type = typename extents_type::size_type;
54
55 constexpr explicit mapping(nested_mapping_t const& map)
56 : _nestedMapping{map}
57 {
58 }
59
60 [[nodiscard]] constexpr auto extents() const noexcept(noexcept(_nestedMapping.extents())) -> extents_type
61 {
62 return detail::transpose_extents(_nestedMapping.extents());
63 }
64
65 [[nodiscard]] constexpr auto required_span_size() const noexcept(noexcept(_nestedMapping.required_span_size()))
66 {
67 return _nestedMapping.required_span_size();
68 }
69
70 template <typename IndexType, typename... Indices>
71 [[nodiscard]] constexpr auto operator()(Indices... rest, IndexType i, IndexType j) const
72 noexcept(noexcept(_nestedMapping(rest..., j, i))) -> typename Extents::size_type
73 {
74 return _nestedMapping(rest..., j, i);
75 }
76
77 [[nodiscard]] constexpr auto nested_mapping() const -> nested_mapping_t { return _nestedMapping; }
78
79 [[nodiscard]] static constexpr auto is_always_unique() -> bool { return nested_mapping_t::is_always_unique(); }
80
81 [[nodiscard]] static constexpr auto is_always_contiguous() -> bool
82 {
83 return nested_mapping_t::is_always_contiguous();
84 }
85
86 [[nodiscard]] static constexpr auto is_always_strided() -> bool
87 {
88 return nested_mapping_t::is_always_strided();
89 }
90
91 [[nodiscard]] constexpr auto is_unique() const noexcept(noexcept(_nestedMapping.is_unique())) -> bool
92 {
93 return _nestedMapping.is_unique();
94 }
95
96 [[nodiscard]] constexpr auto is_contiguous() const noexcept(noexcept(_nestedMapping.is_contiguous())) -> bool
97 {
98 return _nestedMapping.is_contiguous();
99 }
100
101 [[nodiscard]] constexpr auto is_strided() const noexcept(noexcept(_nestedMapping.is_strided())) -> bool
102 {
103 return _nestedMapping.is_strided();
104 }
105
106 [[nodiscard]] constexpr auto stride(size_t r) const noexcept(noexcept(_nestedMapping.stride(r))) -> size_type
107 requires(is_always_strided())
108 {
109 if (r == Extents::rank() - 1) {
110 return _nestedMapping.stride(r - 2);
111 }
112 if (r == Extents::rank() - 2) {
113 return _nestedMapping.stride(r - 1);
114 }
115 return _nestedMapping.stride(r);
116 }
117
118 template <typename OtherExtents>
119 requires(Extents::rank() == OtherExtents::rank())
120 friend constexpr auto operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept -> bool
121 {
122 return lhs._nestedMapping == rhs._nestedMapping;
123 }
124 };
125};
126
127} // namespace etl::linalg
128
129#endif // TETL_LINALG_LAYOUT_TRANSPOSE_HPP
constexpr double e
Definition constants.hpp:29
constexpr auto dynamic_extent
etl::dynamic_extent is a constant of type etl::size_t that is used to differentiate etl::span of stat...
Definition dynamic_extent.hpp:14
Definition accessor_conjugate.hpp:12
extents(Integrals...) -> extents< etl::size_t, etl::size_t((Integrals(), etl::dynamic_extent))... >
static constexpr auto is_always_unique() -> bool
Definition layout_transpose.hpp:79
constexpr auto nested_mapping() const -> nested_mapping_t
Definition layout_transpose.hpp:77
static constexpr auto is_always_contiguous() -> bool
Definition layout_transpose.hpp:81
Extents extents_type
Definition layout_transpose.hpp:51
constexpr auto is_contiguous() const noexcept(noexcept(_nestedMapping.is_contiguous())) -> bool
Definition layout_transpose.hpp:96
constexpr mapping(nested_mapping_t const &map)
Definition layout_transpose.hpp:55
constexpr auto is_unique() const noexcept(noexcept(_nestedMapping.is_unique())) -> bool
Definition layout_transpose.hpp:91
constexpr auto required_span_size() const noexcept(noexcept(_nestedMapping.required_span_size()))
Definition layout_transpose.hpp:65
typename extents_type::size_type size_type
Definition layout_transpose.hpp:52
constexpr auto operator()(Indices... rest, IndexType i, IndexType j) const noexcept(noexcept(_nestedMapping(rest..., j, i))) -> typename Extents::size_type
Definition layout_transpose.hpp:71
layout_transpose layout_type
Definition layout_transpose.hpp:53
constexpr auto is_strided() const noexcept(noexcept(_nestedMapping.is_strided())) -> bool
Definition layout_transpose.hpp:101
constexpr auto extents() const noexcept(noexcept(_nestedMapping.extents())) -> extents_type
Definition layout_transpose.hpp:60
constexpr auto stride(size_t r) const noexcept(noexcept(_nestedMapping.stride(r))) -> size_type requires(is_always_strided())
Definition layout_transpose.hpp:106
static constexpr auto is_always_strided() -> bool
Definition layout_transpose.hpp:86
Definition layout_transpose.hpp:42