VCTR
Loading...
Searching...
No Matches
Span.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
25
38template <class ElementType, bool isDataSIMDAligned, bool isStorageSIMDExtended = false>
39using SpanStorageInfo = StaticStorageInfo<isDataSIMDAligned, isStorageSIMDExtended, alignof (std::span<ElementType>)>;
40
46template <class ElementType>
48
49// clang-format off
50
64template <class ElementType, size_t extent = std::dynamic_extent, class StorageInfoType = StorageInfoWithMemberAlignment<alignof (std::span<ElementType, extent>), StorageInfo<std::span<ElementType, extent> > > > // Extra whitespaces are needed for proper doxygen rendering
65class Span : public VctrBase<ElementType, std::span<ElementType, extent>, extent, StorageInfoType>
66{
67// clang-format on
68private:
69 //==============================================================================
71 using StdSpanType = std::span<ElementType, extent>;
72
73public:
74 //==============================================================================
75 using value_type = typename Vctr::value_type;
76
77 using element_type = ElementType;
78
79 //==============================================================================
80 // Constructors
81 //==============================================================================
82
84 constexpr Span (ElementType* ptr, size_t size)
85 : Vctr (StdSpanType (ptr, size))
86 {
87 VCTR_ASSERT (extent == size || extent == std::dynamic_extent);
88
89 if (Vctr::getStorageInfo().dataIsSIMDAligned)
90 {
91 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
92 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
93 VCTR_ASSERT (isPtrAligned (ptr));
94 }
95 }
96
102 template <bool isDataSIMDAligned, bool isStorageSIMDExtended>
103 constexpr Span (ElementType* ptr, size_t size, const StaticStorageInfo<isDataSIMDAligned, isStorageSIMDExtended, alignof (StdSpanType)>& info)
104 : Vctr (StdSpanType (ptr, size), info)
105 {
106 VCTR_ASSERT (extent == size || extent == std::dynamic_extent);
107
108 if constexpr (isDataSIMDAligned)
109 {
110 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
111 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
112 VCTR_ASSERT (isPtrAligned (ptr));
113 }
114 }
115
120 template <has::sizeAndDataWithElementType<ElementType> Container>
121 constexpr Span (Container&& containerToView)
122 : Vctr (StdSpanType (containerToView.data(), containerToView.size()), makeStorageInfo (containerToView))
123 {
124 VCTR_ASSERT (extent == containerToView.size() || extent == std::dynamic_extent);
125 }
126
128 template <std::same_as<ElementType> T, size_t n>
129 requires (n != std::dynamic_extent)
130 constexpr Span (T (&cStyleArray)[n])
131 requires (n == extent)
132 : Vctr (StdSpanType (cStyleArray))
133 {
134 if (Vctr::getStorageInfo().dataIsSIMDAligned)
135 {
136 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
137 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
138 VCTR_ASSERT (isPtrAligned (cStyleArray));
139 }
140 }
141
143 constexpr Span (ElementType* ptr)
144 requires (extent != std::dynamic_extent)
145 : Vctr (StdSpanType (ptr, extent))
146 {
147 if (Vctr::getStorageInfo().dataIsSIMDAligned)
148 {
149 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
150 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
151 VCTR_ASSERT (isPtrAligned (ptr));
152 }
153 }
154
155 //==============================================================================
156 // Operators
157 //==============================================================================
158 // Deleting the default copy operator forces the usage of the intended operator below
159 Span& operator= (const Span&) = delete;
160
162 template <has::sizeAndDataWithElementType<ElementType> Container>
163 constexpr Span& operator= (Container&& containerToCopyDataFrom)
164 {
165 if constexpr (Vctr::template shouldMoveFromOtherContainer<Container>)
166 {
167 VCTR_ASSERT (containerToCopyDataFrom.size() == Vctr::size());
168 std::copy (std::make_move_iterator (containerToCopyDataFrom.begin()), std::make_move_iterator (containerToCopyDataFrom.end()), Vctr::storage.begin());
169 }
170 else
171 {
172 Vctr::copyFrom (containerToCopyDataFrom.data(), containerToCopyDataFrom.size());
173 }
174
175 return *this;
176 }
177
179 constexpr Span& operator= (std::initializer_list<ElementType> elementsToAssign)
180 {
181 Vctr::assign (std::move (elementsToAssign));
182 return *this;
183 }
184
189 template <is::expression E>
190 constexpr void operator= (const E& expression)
191 {
192 VCTR_ASSERT (expression.size() == Vctr::size());
193
194 Vctr::assignExpressionTemplate (expression);
195 }
196
203 template <is::derivedFromSplitComplexBase SplitComplex>
204 requires std::same_as<ElementType, std::complex<typename SplitComplex::value_type>>
205 constexpr Span& operator= (const SplitComplex& splitComplex)
206 {
207 *this = realImagToComplex (splitComplex);
208 return *this;
209 }
210
212 operator const StdSpanType&() { return Vctr::storage; }
213
214 //==============================================================================
220 template <class DstType>
221 constexpr auto castTo()
222 {
223 using SizeRatio = std::ratio<sizeof (ElementType), sizeof (DstType)>;
224 auto* dstData = reinterpret_cast<DstType*> (Vctr::data());
225
226 if constexpr (extent == std::dynamic_extent)
227 {
228 auto dstSize = detail::expectNoRemainderMultiplyBy<SizeRatio> (Vctr::size());
229 return Span<DstType> (dstData, dstSize);
230 }
231 else
232 {
233 constexpr auto dstSize = detail::expectNoRemainderMultiplyBy<SizeRatio> (Vctr::size());
234 return Span<DstType, dstSize> (dstData, dstSize);
235 }
236 }
237
238private:
239 //==============================================================================
240 template <has::sizeAndData Container>
241 static constexpr auto makeStorageInfo (const Container& container)
242 {
243 constexpr size_t memberAlignment = alignof (StdSpanType);
244
245 if constexpr (is::anyVctr<Container>)
246 {
247 const auto& info = container.getStorageInfo();
248 return StorageInfoWithMemberAlignment<memberAlignment, std::remove_cvref_t<decltype (info)>> (info);
249 }
250 else
251 {
252 return StorageInfoWithMemberAlignment<memberAlignment, StorageInfo<Container>>().init (container.data(), container.size());
253 }
254 }
255};
256
257//==============================================================================
258// Deduction guides
259//==============================================================================
260
261template <class ElementType, bool isDataSIMDAligned, bool isStorageSIMDExtended>
262Span (ElementType*, size_t, const StaticStorageInfo<isDataSIMDAligned, isStorageSIMDExtended, alignof (std::span<ElementType>)>&) -> Span<ElementType, std::dynamic_extent, StaticStorageInfo<isDataSIMDAligned, isStorageSIMDExtended, alignof (std::span<ElementType>)>>;
263
264template <class Container>
265Span (Container&&) -> Span<DataType<Container>, extentOf<Container>, StorageInfoWithMemberAlignment<alignof (std::span<DataType<Container>, extentOf<Container>>), StorageInfoType<Container>>>;
266
267template <class ElementType, size_t size>
268Span (ElementType (&)[size]) -> Span<ElementType, size, StorageInfoWithMemberAlignment<alignof (std::span<ElementType, size>), StorageInfo<std::span<ElementType, size>>>>;
269
271template <class ElementType>
272auto makeSimdAlignedSpan (ElementType* data, size_t size)
273{
274 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
275 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
276 VCTR_ASSERT (isPtrAligned (data));
277
278 return Span<ElementType, std::dynamic_extent, StaticStorageInfo<true, false, alignof (std::span<ElementType>)>> (data, size);
279}
280
282template <size_t extent, class ElementType>
283requires (extent != std::dynamic_extent)
284auto makeSimdAlignedSpan (ElementType* data)
285{
286 // If you hit this assertion, you passed a StaticStorageInfo that specifies SIMD aligned
287 // memory, but it is not aligned. Required alignment is 32 byte on x64 and 16 byte on ARM.
288 VCTR_ASSERT (isPtrAligned (data));
289
290 return Span<ElementType, extent, StaticStorageInfo<true, false, alignof (std::span<ElementType>)>> (data, extent);
291}
292
293} // namespace vctr
The view type.
Definition: Span.h:66
constexpr Span(ElementType *ptr, size_t size, const StaticStorageInfo< isDataSIMDAligned, isStorageSIMDExtended, alignof(StdSpanType)> &info)
Creates a Span with a given size that views externally managed data, accessed by ptr.
Definition: Span.h:103
constexpr Span(ElementType *ptr, size_t size)
Creates a Span with a given size that views externally managed data, accessed by ptr.
Definition: Span.h:84
constexpr Span(Container &&containerToView)
Creates a Span that acts as a view to the given container.
Definition: Span.h:121
constexpr Span(ElementType *ptr)
Creates a Span with a static extent that views externally managed data, accessed by ptr.
Definition: Span.h:143
constexpr auto castTo()
Casts this Span to a span of a different type by applying a reinterpret cast to the data.
Definition: Span.h:221
The base class to all one dimensional containers and views in the VCTR project.
Definition: VctrBase.h:38
constexpr size_t size() const noexcept
Returns the number of elements.
Definition: VctrBase.h:61
constexpr void assign(std::initializer_list< ElementType > elements)
Assigns elements from the initializer list to this instance.
Definition: VctrBase.h:263
constexpr void copyFrom(const ElementType *otherData, size_t otherSize)
Copies the content from otherData to this instance.
Definition: VctrBase.h:282
VCTR_FORCEDINLINE constexpr auto * data()
Returns a raw pointer to the underlying storage.
Definition: VctrBase.h:123
Constrains a type to be any derived instance of VctrBase.
Definition: ContainerAndExpressionConcepts.h:247
constexpr auto realImagToComplex(SrcType &&src)
Returns an expression that converts a split-complex source into interleaved std::complex values.
Definition: SplitComplexMath.h:206
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::StorageInfoType< std::remove_cvref_t< T > >::Type StorageInfoType
If t is a type derived from VctrBase, this will equal the return value of T::getStorageInfo,...
Definition: Traits.h:227
constexpr bool isPtrAligned(const void *ptr)
Helper function to check if a pointer is aligned to the required alignment value.
Definition: SIMDHelpers.h:57
auto makeSimdAlignedSpan(ElementType *data, size_t size)
Creates a span with dynamic extent pointing to a memory location that is expected to be SIMD aligned.
Definition: Span.h:272
consteval auto simdAlignedSpanStorageInfo()
Creates a StaticStorageInfo instance suitable to be passed to a Span constructor.
Definition: Span.h:47
A storage info type especially used to pass compile time constant traits when viewing externally owne...
Definition: SIMDHelpers.h:159
A storage info type especially used for vctr::Span.
Definition: SIMDHelpers.h:173