28#ifndef VC_COMMON_MEMORY_H_
29#define VC_COMMON_MEMORY_H_
31#include "memorybase.h"
36#include <initializer_list>
41namespace Vc_VERSIONED_NAMESPACE
45template<
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
66template <
typename V,
size_t Size1,
size_t Size2,
bool InitPadding>
67class Memory :
public MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
68 Memory<V, Size2, 0, InitPadding>>
71 typedef typename V::EntryType EntryType;
74 using RowMemory = Memory<V, Size2, 0, InitPadding>;
75 typedef MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, RowMemory> Base;
76 friend class MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, RowMemory>;
77 friend class MemoryDimensionBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
80 Alignment = V::MemoryAlignment,
81 PaddedSize2 = _MemorySizeCalculation<V, Size2>::PaddedSize
83 alignas(
static_cast<size_t>(Alignment))
86 RowMemory m_mem[Size1];
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) {
198template <
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>;
209 Alignment = V::MemoryAlignment,
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);
295 assert(vectorsCount() == rhs.vectorsCount());
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);
448 std::swap(m_mem, rhs.m_mem);
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);
480 assert(vectorsCount() == rhs.vectorsCount());
481 Detail::copyVectors(*
this, rhs);
495 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
510Vc_ALWAYS_INLINE
void prefetchForOneRead(
const void *addr)
512 Vc::Detail::prefetchForOneRead(addr, VectorAbi::Best<float>());
527Vc_ALWAYS_INLINE
void prefetchForModify(
const void *addr)
529 Vc::Detail::prefetchForModify(addr, VectorAbi::Best<float>());
542Vc_ALWAYS_INLINE
void prefetchClose(
const void *addr)
544 Vc::Detail::prefetchClose(addr, VectorAbi::Best<float>());
557Vc_ALWAYS_INLINE
void prefetchMid(
const void *addr)
559 Vc::Detail::prefetchMid(addr, VectorAbi::Best<float>());
572Vc_ALWAYS_INLINE
void prefetchFar(
const void *addr)
574 Vc::Detail::prefetchFar(addr, VectorAbi::Best<float>());
579using Common::prefetchForOneRead;
580using Common::prefetchForModify;
581using Common::prefetchClose;
582using Common::prefetchMid;
583using Common::prefetchFar;
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.
size_t entriesCount() const
~Memory()
Frees the memory which was allocated in the constructor.
size_t vectorsCount() const
Memory & operator=(const EntryType *rhs)
Overwrite all entries with the values stored in the memory at rhs.
void swap(Memory &rhs)
Swap the contents and size information of two Memory objects.
Memory(size_t size)
Allocate enough memory to access size values of type V::EntryType.
Memory & operator=(const MemoryBase< V, Parent, 1, RM > &rhs)
Overwrite all entries with the values stored in rhs.
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.
Memory & operator=(const V &v)
Initialize all data with the given vector.
static constexpr size_t entriesCount()
Memory & operator=(const MemoryBase< V, Parent, 2, RM > &rhs)
Copies the data from a different object.
static constexpr size_t rowsCount()
static constexpr size_t vectorsCount()
Common::AdaptSubscriptOperator< std::vector< T, Allocator > > vector
An adapted std::vector container with an additional subscript operator which implements gather and sc...
void free(T *p)
Frees memory that was allocated with Vc::malloc.
@ AlignOnVector
Align on boundary of vector sizes (e.g.
Vector Classes Namespace.