28 #ifndef VC_COMMON_MEMORY_H_
29 #define VC_COMMON_MEMORY_H_
31 #include "memorybase.h"
36 #include <initializer_list>
37 #include "memoryfwd.h"
41 namespace Vc_VERSIONED_NAMESPACE
45 template<
typename V,
size_t Size>
struct _MemorySizeCalculation
47 enum AlignmentCalculations {
49 AlignmentMask = Alignment - 1,
50 MaskedSize = Size & AlignmentMask,
51 Padding = Alignment - MaskedSize,
52 PaddedSize = MaskedSize == 0 ? Size : Size + Padding
66 template <
typename V,
size_t Size1,
size_t Size2,
bool InitPadding>
68 Memory<V, Size2, 0, InitPadding>>
71 typedef typename V::EntryType EntryType;
77 friend class MemoryDimensionBase<V,
Memory<V, Size1, Size2, InitPadding>, 2,
81 PaddedSize2 = _MemorySizeCalculation<V, Size2>::PaddedSize
83 alignas(
static_cast<size_t>(Alignment))
92 VectorsCount = PaddedSize2 / V::Size
102 static constexpr
size_t rowsCount() {
return RowCount; }
117 static constexpr
size_t vectorsCount() {
return VectorsCount * Size1; }
128 template<
typename Parent,
typename RM>
131 Detail::copyVectors(*
this, rhs);
136 Detail::copyVectors(*
this, rhs);
148 for (
size_t i = 0; i < vectorsCount(); ++i) {
198 template <
typename V,
size_t Size,
bool InitPadding>
200 public MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>
203 typedef typename V::EntryType EntryType;
207 friend class MemoryDimensionBase<V,
Memory<V, Size, 0u, InitPadding>, 1, void>;
210 MaskedSize = Size & (V::Size - 1),
212 Padding = V::Size - MaskedSize,
213 PaddedSize = MaskedSize == 0 ? Size : Size + Padding
215 alignas(
static_cast<size_t>(Alignment))
218 EntryType m_mem[PaddedSize];
224 VectorsCount = PaddedSize / V::Size
230 Base::lastVector() =
V::Zero();
234 Memory(std::initializer_list<EntryType> init)
236 Vc_ASSERT(init.size() <= Size);
237 Base::lastVector() =
V::Zero();
238 std::copy(init.begin(), init.end(), &m_mem[0]);
268 char *addr =
reinterpret_cast<char *
>(ptr);
270 addr -= offsetof(MM, m_mem);
271 return *
new(addr) MM;
290 Detail::copyVectors(*
this, rhs);
296 Detail::copyVectors(*
this, rhs);
299 inline Memory &operator=(
const Memory &rhs)
301 Detail::copyVectors(*
this, rhs);
305 template <
size_t S>
inline Memory &operator=(
const Memory<V, S> &rhs)
307 assert(vectorsCount() == rhs.vectorsCount());
308 Detail::copyVectors(*
this, rhs);
312 Vc_ALWAYS_INLINE Memory &operator=(
const EntryType *rhs) {
313 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
316 inline Memory &operator=(
const V &v) {
317 for (
size_t i = 0; i < vectorsCount(); ++i) {
364 template<
typename V>
class Memory<V, 0u, 0u, true> :
public MemoryBase<V, Memory<V, 0u, 0u, true>, 1, void>
367 typedef typename V::EntryType EntryType;
371 friend class MemoryDimensionBase<V,
Memory<V>, 1, void>;
372 enum InternalConstants {
374 AlignmentMask = Alignment - 1
376 size_t m_entriesCount;
377 size_t m_vectorsCount;
379 size_t calcPaddedEntriesCount(
size_t x)
381 size_t masked = x & AlignmentMask;
382 return (masked == 0 ? x : x + (Alignment - masked));
395 : m_entriesCount(size),
396 m_vectorsCount(calcPaddedEntriesCount(m_entriesCount)),
399 m_vectorsCount /= V::Size;
400 Base::lastVector() =
V::Zero();
410 template<
typename Parent,
typename RM>
412 : m_entriesCount(rhs.entriesCount()),
413 m_vectorsCount(rhs.vectorsCount()),
416 Detail::copyVectors(*
this, rhs);
427 : m_entriesCount(rhs.entriesCount()),
428 m_vectorsCount(rhs.vectorsCount()),
431 Detail::copyVectors(*
this, rhs);
449 std::swap(m_entriesCount, rhs.m_entriesCount);
450 std::swap(m_vectorsCount, rhs.m_vectorsCount);
456 Vc_ALWAYS_INLINE Vc_PURE
size_t entriesCount()
const {
return m_entriesCount; }
461 Vc_ALWAYS_INLINE Vc_PURE
size_t vectorsCount()
const {
return m_vectorsCount; }
472 template<
typename Parent,
typename RM>
475 Detail::copyVectors(*
this, rhs);
481 Detail::copyVectors(*
this, rhs);
495 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
578 using Common::Memory;
Common interface to all Memory classes, independent of allocation on the stack or heap.
size_t vectorsCount() const
Memory(const MemoryBase< V, Parent, 1, RM > &rhs)
Copy the memory into a new memory area.
Memory(const Memory &rhs)
Overload of the above function.
Memory & operator=(const MemoryBase< V, Parent, 1, RM > &rhs)
Overwrite all entries with the values stored in rhs.
size_t entriesCount() const
~Memory()
Frees the memory which was allocated in the constructor.
size_t vectorsCount() const
void swap(Memory &rhs)
Swap the contents and size information of two Memory objects.
Memory & operator=(const EntryType *rhs)
Overwrite all entries with the values stored in the memory at rhs.
Memory(size_t size)
Allocate enough memory to access size values of type V::EntryType.
static constexpr size_t entriesCount()
static Memory< V, Size, 0u, false > & fromRawData(EntryType *ptr)
Wrap existing data with the Memory convenience class.
static constexpr size_t vectorsCount()
A helper class for fixed-size two-dimensional arrays.
static constexpr size_t entriesCount()
static constexpr size_t rowsCount()
static constexpr size_t vectorsCount()
Memory & operator=(const MemoryBase< V, Parent, 2, RM > &rhs)
Copies the data from a different object.
Memory & operator=(const V &v)
Initialize all data with the given vector.
Common::AdaptSubscriptOperator< std::vector< T, Allocator > > vector
An adapted std::vector container with an additional subscript operator which implements gather and sc...
void swap(Adapter< S, T, N > &a, std::size_t i, S &x)
Swaps one scalar object x with a SIMD slot at offset i in the simdized object a.
void prefetchForModify(const void *addr)
Prefetch the cacheline containing addr for modification.
T * malloc(size_t n)
Allocates memory on the Heap with alignment and padding suitable for vectorized access.
void prefetchClose(const void *addr)
Prefetch the cacheline containing addr to L1 cache.
void prefetchFar(const void *addr)
Prefetch the cacheline containing addr to L3 cache.
void prefetchMid(const void *addr)
Prefetch the cacheline containing addr to L2 cache.
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
void prefetchForOneRead(const void *addr)
Prefetch the cacheline containing addr for a single read access.
void free(T *p)
Frees memory that was allocated with Vc::malloc.
@ AlignOnVector
Align on boundary of vector sizes (e.g.
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Vector Classes Namespace.