Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
layout_transpose.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_LAYOUT_TRANSPOSE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_LAYOUT_TRANSPOSE_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
11#include "arene/base/mdspan/detail/do_submdspan_mapping.hpp"
12#include "arene/base/mdspan/detail/reverse_extents.hpp"
13#include "arene/base/mdspan/is_sliceable_mapping.hpp" // IWYU pragma: keep
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/move.hpp"
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
23// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
24
25namespace arene {
26namespace base {
27namespace linalg {
28
29namespace transpose_detail {
30
31/// @brief Verify the strided precondition of @c layout_transpose::mapping::stride for an always-strided mapping
32/// @tparam NestedMapping The wrapped mapping type; selected when it is always strided
33///
34/// An always-strided mapping satisfies the precondition statically, so there is nothing to check at runtime
35template <class NestedMapping, constraints<std::enable_if_t<NestedMapping::is_always_strided()>> = nullptr>
36constexpr auto check_nested_mapping_is_strided(NestedMapping const&) noexcept -> void {}
37
38/// @brief Verify the strided precondition of @c layout_transpose::mapping::stride for a sometimes-strided mapping
39/// @tparam NestedMapping The wrapped mapping type; selected when it is not always strided
40/// @param nested The wrapped mapping whose @c is_strided() is checked
41/// @pre @c nested.is_strided() is true, else @c ARENE_PRECONDITION violation
42template <class NestedMapping, constraints<std::enable_if_t<!NestedMapping::is_always_strided()>> = nullptr>
43constexpr auto check_nested_mapping_is_strided(NestedMapping const& nested) noexcept -> void {
44 ARENE_PRECONDITION(nested.is_strided());
45}
46
47} // namespace transpose_detail
48
49/// @brief A layout mapping policy that swaps the two indices, extents, and strides of a rank-2 mapping
50/// @tparam NestedLayout The layout mapping policy being transposed; its rank-2 @c mapping is wrapped by this policy
51///
52/// Given a @c NestedLayout mapping over extents <c>(R, C)</c>, the @c layout_transpose::mapping presents extents
53/// <c>(C, R)</c> and maps index <c>(i, j)</c> to the nested mapping's <c>(j, i)</c>.
54template <class NestedLayout>
56 public:
57 /// @brief The wrapped (un-transposed) layout type
58 using nested_layout_type = NestedLayout;
59
60 /// @brief The mapping type produced by @c layout_transpose for a given set of extents
61 /// @tparam The extents of the space used for this mapping
62 ///
63 /// @note Mandates <br>
64 /// * @c Extents is a specialization of @c arene::base::extents
65 /// * @c Extents::rank() equals 2
66 template <class Extents>
67 // parasoft-begin-suppress AUTOSAR-A2_10_1 "False positive: There is no identifier 'mapping' being hidden"
68 class mapping : private NestedLayout::template mapping<mdspan_detail::reverse_extents_t<Extents>> {
69 // parasoft-end-suppress AUTOSAR-A2_10_1
70 public:
71 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
72 // side-effects"
73 static_assert(Extents::rank() == 2U, "Extents must be rank 2.");
74 // parasoft-end-suppress CERT_C-PRE31-c-3
75
76 /// @brief This mapping's extents type
77 using extents_type = Extents;
78 /// @brief The size type of this mapping
79 using size_type = typename extents_type::size_type;
80 /// @brief The index type of this mapping
81 using index_type = typename extents_type::index_type;
82 /// @brief The rank type of this mapping
83 using rank_type = typename extents_type::rank_type;
84 /// @brief The tag type of the layout used by this @c mapping
86 /// @brief The wrapped (un-transposed) mapping type
88
89 /// @brief Construct a transposed mapping from a nested mapping
90 /// @param nested The nested mapping to transpose
91 constexpr explicit mapping(nested_mapping_type const& nested) noexcept
92 : nested_mapping_type{nested},
93 extents_{nested.extents().extent(1U), nested.extents().extent(0U)} {}
94
95 /// @brief Obtain the (transposed) extents of this mapping
96 /// @return The extents, with the two dimensions of the nested mapping swapped
97 constexpr auto extents() const noexcept -> extents_type const& { return extents_; }
98
99 /// @brief Obtain the wrapped (un-transposed) mapping
100 /// @return The nested mapping before the transpose is applied
101 constexpr auto nested_mapping() const noexcept -> nested_mapping_type const& {
102 return static_cast<nested_mapping_type const&>(*this);
103 }
104
105 /// @brief Obtain the required span size
106 /// @return @c nested_mapping().required_span_size()
107 constexpr auto required_span_size() const noexcept -> index_type { return nested_mapping().required_span_size(); }
108
109 /// @brief Access the flat element index for the given two-dimensional index
110 /// @tparam Index0 The type of the first index
111 /// @tparam Index1 The type of the second index
112 /// @param index0 The index into the first (transposed) dimension
113 /// @param index1 The index into the second (transposed) dimension
114 /// @return @c nested_mapping()(index1, index0), i.e. the underlying element with the indices reversed
115 template <class Index0, class Index1>
116 constexpr auto operator()(Index0 index0, Index1 index1) const noexcept -> index_type {
117 return nested_mapping()(index1, index0);
118 }
119
120 /// @brief Determine if this mapping is always unique
121 /// @return @c nested_mapping_type::is_always_unique()
122 static constexpr auto is_always_unique() noexcept -> bool { return nested_mapping_type::is_always_unique(); }
123
124 /// @brief Determine if this mapping is always exhaustive
125 /// @return @c nested_mapping_type::is_always_exhaustive()
126 static constexpr auto is_always_exhaustive() noexcept -> bool {
127 return nested_mapping_type::is_always_exhaustive();
128 }
129
130 /// @brief Determine if this mapping is always strided
131 /// @return @c nested_mapping_type::is_always_strided()
132 static constexpr auto is_always_strided() noexcept -> bool { return nested_mapping_type::is_always_strided(); }
133
134 /// @brief Determine if this mapping is unique
135 /// @return @c nested_mapping().is_unique()
136 constexpr auto is_unique() const noexcept -> bool { return nested_mapping().is_unique(); }
137
138 /// @brief Determine if this mapping is exhaustive
139 /// @return @c nested_mapping().is_exhaustive()
140 constexpr auto is_exhaustive() const noexcept -> bool { return nested_mapping().is_exhaustive(); }
141
142 /// @brief Determine if this mapping is strided
143 /// @return @c nested_mapping().is_strided()
144 constexpr auto is_strided() const noexcept -> bool { return nested_mapping().is_strided(); }
145
146 /// @brief Obtain the stride along the specified (transposed) dimension
147 /// @param rank_index The (transposed) dimension index; 0 maps to the nested dimension 1 and vice versa
148 /// @return The stride of the nested mapping along the swapped dimension
149 /// @pre @c is_strided() is true, else @c ARENE_PRECONDITION violation
150 /// @pre <c>rank_index < 2</c>, else @c ARENE_PRECONDITION violation
151 constexpr auto stride(rank_type rank_index) const noexcept -> index_type {
152 ARENE_PRECONDITION(rank_index < rank_type{2});
153 transpose_detail::check_nested_mapping_is_strided(nested_mapping());
154 // rank_index is 0 or 1 (guaranteed above), so the swap to the nested dimension is rank_type{1} - rank_index.
155 return nested_mapping().stride(static_cast<rank_type>(rank_type{1} - rank_index));
156 }
157
158 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
159 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Mixed comparisons permitted by A13-5-5 Permit #1"
160 /// @brief Compare two transposed mappings for equality
161 /// @tparam OtherExtents The extents type of the right-hand side mapping
162 /// @param left A transposed mapping
163 /// @param right A transposed mapping with possibly different extents
164 /// @return @c true if the wrapped nested mappings compare equal
165 template <
166 class OtherExtents,
169 extents_type::rank() == OtherExtents::rank(),
170 bool> = true>
171 friend constexpr auto operator==(mapping const left, mapping<OtherExtents> const right) noexcept -> bool {
172 return left.nested_mapping() == right.nested_mapping();
173 }
174
175 /// @brief Compare two transposed mappings for inequality
176 /// @tparam OtherExtents The extents type of the right-hand side mapping
177 /// @param left A transposed mapping
178 /// @param right A transposed mapping with possibly different extents
179 /// @return @c true if the wrapped nested mappings compare unequal
180 template <
181 class OtherExtents,
184 extents_type::rank() == OtherExtents::rank(),
185 bool> = true>
186 friend constexpr auto operator!=(mapping const left, mapping<OtherExtents> const right) noexcept -> bool {
187 return !(left == right);
188 }
189 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
190 // parasoft-end-suppress AUTOSAR-A11_3_1-a
191
192 private:
193 /// @brief The transposed mapping type wrapping a rank-2 sub-mapping @c Mapping
194 /// @tparam Mapping A rank-2 sub-mapping produced by slicing the nested mapping
195 template <class Mapping>
196 using transposed_sub_mapping_t = typename layout_transpose<typename Mapping::layout_type>::template mapping<
197 mdspan_detail::reverse_extents_t<typename Mapping::extents_type>>;
198
199 /// @brief Re-apply the transpose to a rank-2 sub-mapping result of the nested mapping
200 /// @tparam Mapping The sub-mapping type produced by slicing the nested mapping; selected when it is rank 2
201 /// @param result The @c submdspan_mapping_result from slicing the nested mapping
202 /// @return A @c submdspan_mapping_result whose mapping is @c result 's mapping wrapped in a @c layout_transpose,
203 /// preserving the offset
204 ///
205 /// A rank-2 slice of the nested mapping is transposed back so that the sub-mapping matches the transposed indexing.
206 template <class Mapping, constraints<std::enable_if_t<Mapping::extents_type::rank() == 2U>> = nullptr>
207 static constexpr auto transpose_result(submdspan_mapping_result<Mapping> result) noexcept
209 return {transposed_sub_mapping_t<Mapping>{result.mapping}, result.offset};
210 }
211
212 /// @brief Pass a rank-reduced sub-mapping result through unchanged
213 /// @tparam Mapping The sub-mapping type produced by slicing the nested mapping; selected when its rank is below 2
214 /// @param result The @c submdspan_mapping_result from slicing the nested mapping
215 /// @return @c result unchanged
216 ///
217 /// A slice that drops a rank produces a rank-1 (or rank-0) sub-mapping, which has no transpose to re-apply.
218 template <class Mapping, constraints<std::enable_if_t<Mapping::extents_type::rank() < 2U>> = nullptr>
219 static constexpr auto transpose_result(submdspan_mapping_result<Mapping> result) noexcept
220 -> submdspan_mapping_result<Mapping> {
221 return result;
222 }
223
224 public:
225 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
226 /// @brief Compute the sub-mapping and offset for a transposed mapping
227 /// @tparam CanonicalSlice0 The type of the slice specifier for the first (transposed) dimension
228 /// @tparam CanonicalSlice1 The type of the slice specifier for the second (transposed) dimension
229 /// @tparam Mapping Defaulted to @c mapping so the trailing return type can be SFINAE-friendly
230 /// @param src The source transposed mapping to slice
231 /// @param slice0 The slice specifier for the first (transposed) dimension
232 /// @param slice1 The slice specifier for the second (transposed) dimension
233 /// @return A @c submdspan_mapping_result obtained by slicing the nested mapping with the slices reversed, then
234 /// re-transposing a rank-2 result
235 ///
236 /// Because @c layout_transpose swaps the two dimensions, slicing dimension 0 of the transpose is equivalent to
237 /// slicing dimension 1 of the nested mapping. The slices are therefore reversed and forwarded to the nested
238 /// mapping's own @c submdspan_mapping via @c do_submdspan_mapping. The resulting sub-mapping is re-transposed if it
239 /// is still rank 2.
240 template <class CanonicalSlice0, class CanonicalSlice1, class Mapping = mapping>
241 friend constexpr auto submdspan_mapping(mapping const src, CanonicalSlice0 slice0, CanonicalSlice1 slice1) noexcept(
242 noexcept(transpose_result(mdspan_detail::do_submdspan_mapping(
246 )))
247 )
248 -> decltype(transpose_result(mdspan_detail::do_submdspan_mapping(
250 std::move(slice1),
251 std::move(slice0)
252 ))) {
253 return transpose_result(
254 mdspan_detail::do_submdspan_mapping(src.nested_mapping(), std::move(slice1), std::move(slice0))
255 );
256 }
257 // parasoft-end-suppress AUTOSAR-A11_3_1-a
258
259 private:
260 /// @brief The transposed extents, stored so that @c extents() can return a reference
261 extents_type extents_{};
262 };
263 // parasoft-end-suppress AUTOSAR-A10_2_1-a AUTOSAR-A10_2_1-b
264 // parasoft-end-suppress AUTOSAR-A2_10_1
265};
266
267} // namespace linalg
268
269/// @brief Specialization of @c is_layout_mapping_v indicating a @c layout_transpose::mapping is a layout mapping
270/// @tparam LayoutMapping A layout mapping whose @c layout_type is a @c layout_transpose instantiation
271///
272/// This short-circuits the recursive instantiation cycle that would otherwise occur while completing the mapping type,
273/// which gcc parses as always being false
274template <class LayoutMapping>
275extern constexpr bool is_layout_mapping_v<
276 LayoutMapping,
278
279} // namespace base
280} // namespace arene
281
282// parasoft-end-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
283// parasoft-end-suppress AUTOSAR-A7_1_5-a-2
284
285#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_LAYOUT_TRANSPOSE_HPP_
The mapping type produced by layout_transpose for a given set of extents.
Definition layout_transpose.hpp:68
static constexpr auto is_always_exhaustive() noexcept -> bool
Determine if this mapping is always exhaustive.
Definition layout_transpose.hpp:126
friend constexpr auto operator!=(mapping const left, mapping< OtherExtents > const right) noexcept -> bool
Compare two transposed mappings for inequality.
Definition layout_transpose.hpp:186
constexpr auto stride(rank_type rank_index) const noexcept -> index_type
Obtain the stride along the specified (transposed) dimension.
Definition layout_transpose.hpp:151
constexpr mapping(nested_mapping_type const &nested) noexcept
Construct a transposed mapping from a nested mapping.
Definition layout_transpose.hpp:91
constexpr auto required_span_size() const noexcept -> index_type
Obtain the required span size.
Definition layout_transpose.hpp:107
typename extents_type::rank_type rank_type
The rank type of this mapping.
Definition layout_transpose.hpp:83
typename extents_type::size_type size_type
The size type of this mapping.
Definition layout_transpose.hpp:79
typename NestedLayout::template mapping< mdspan_detail::reverse_extents_t< extents_type > > nested_mapping_type
The wrapped (un-transposed) mapping type.
Definition layout_transpose.hpp:87
constexpr auto is_strided() const noexcept -> bool
Determine if this mapping is strided.
Definition layout_transpose.hpp:144
friend constexpr auto operator==(mapping const left, mapping< OtherExtents > const right) noexcept -> bool
Compare two transposed mappings for equality.
Definition layout_transpose.hpp:171
constexpr auto extents() const noexcept -> extents_type const &
Obtain the (transposed) extents of this mapping.
Definition layout_transpose.hpp:97
constexpr auto nested_mapping() const noexcept -> nested_mapping_type const &
Obtain the wrapped (un-transposed) mapping.
Definition layout_transpose.hpp:101
constexpr auto is_exhaustive() const noexcept -> bool
Determine if this mapping is exhaustive.
Definition layout_transpose.hpp:140
static constexpr auto is_always_strided() noexcept -> bool
Determine if this mapping is always strided.
Definition layout_transpose.hpp:132
friend constexpr auto submdspan_mapping(mapping const src, CanonicalSlice0 slice0, CanonicalSlice1 slice1) noexcept(noexcept(transpose_result(mdspan_detail::do_submdspan_mapping(std::declval< Mapping const & >().nested_mapping(), std::move(std::declval< CanonicalSlice1 >()), std::move(std::declval< CanonicalSlice0 >()))))) ->
Compute the sub-mapping and offset for a transposed mapping.
Definition layout_transpose.hpp:241
typename extents_type::index_type index_type
The index type of this mapping.
Definition layout_transpose.hpp:81
static constexpr auto is_always_unique() noexcept -> bool
Determine if this mapping is always unique.
Definition layout_transpose.hpp:122
layout_transpose layout_type
The tag type of the layout used by this mapping.
Definition layout_transpose.hpp:85
constexpr auto operator()(Index0 index0, Index1 index1) const noexcept -> index_type
Access the flat element index for the given two-dimensional index.
Definition layout_transpose.hpp:116
constexpr auto is_unique() const noexcept -> bool
Determine if this mapping is unique.
Definition layout_transpose.hpp:136
Extents extents_type
This mapping's extents type.
Definition layout_transpose.hpp:77
A layout mapping policy that swaps the two indices, extents, and strides of a rank-2 mapping.
Definition layout_transpose.hpp:55
NestedLayout nested_layout_type
The wrapped (un-transposed) layout type.
Definition layout_transpose.hpp:58
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
Definition accumulate.hpp:37
constexpr bool is_layout_mapping_v
Helper trait to indicate if the given type has all the basic properties required of a layout mapping:
constexpr bool is_equality_comparable_v
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
Specializations of submdspan_mapping_result are returned by overloads of submdspan_mapping.
Definition submdspan_mapping_result.hpp:18