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>
67#ifdef Vc_RECURSIVE_MEMORY
68class Memory :
public MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
69 Memory<V, Size2, 0, InitPadding>>
72 public MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
73 Memory<V, Size2, 0, false>>
77 typedef typename V::EntryType EntryType;
80#ifdef Vc_RECURSIVE_MEMORY
87 friend class MemoryDimensionBase<V,
Memory<V, Size1, Size2, InitPadding>, 2,
91 PaddedSize2 = _MemorySizeCalculation<V, Size2>::PaddedSize
93 alignas(
static_cast<size_t>(Alignment))
96#ifdef Vc_RECURSIVE_MEMORY
99 EntryType m_mem[Size1][PaddedSize2];
106 VectorsCount = PaddedSize2 / V::Size
109#ifdef Vc_RECURSIVE_MEMORY
116 for (
size_t i = 0; i < Size1; ++i) {
128 static constexpr size_t rowsCount() {
return RowCount; }
143 static constexpr size_t vectorsCount() {
return VectorsCount * Size1; }
154 template<
typename Parent,
typename RM>
157 Detail::copyVectors(*
this, rhs);
162 Detail::copyVectors(*
this, rhs);
174 for (
size_t i = 0; i < vectorsCount(); ++i) {
224template <
typename V,
size_t Size,
bool InitPadding>
226#ifndef Vc_RECURSIVE_MEMORY
229 public MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>
232 typedef typename V::EntryType EntryType;
236 friend class MemoryDimensionBase<V,
Memory<V, Size, 0u, InitPadding>, 1, void>;
239 MaskedSize = Size & (V::Size - 1),
241 Padding = V::Size - MaskedSize,
242 PaddedSize = MaskedSize == 0 ? Size : Size + Padding
244 alignas(
static_cast<size_t>(Alignment))
247 EntryType m_mem[PaddedSize];
253 VectorsCount = PaddedSize / V::Size
259 Base::lastVector() =
V::Zero();
263 Memory(std::initializer_list<EntryType> init)
265 Vc_ASSERT(init.size() <= Size);
266 Base::lastVector() =
V::Zero();
267 std::copy(init.begin(), init.end(), &m_mem[0]);
297 char *addr =
reinterpret_cast<char *
>(ptr);
299 addr -= Vc_OFFSETOF(MM, m_mem);
300 return *
new(addr) MM;
319 Detail::copyVectors(*
this, rhs);
325 Detail::copyVectors(*
this, rhs);
328 inline Memory &operator=(
const Memory &rhs)
330 Detail::copyVectors(*
this, rhs);
334 template <
size_t S>
inline Memory &operator=(
const Memory<V, S> &rhs)
336 assert(vectorsCount() == rhs.vectorsCount());
337 Detail::copyVectors(*
this, rhs);
341 Vc_ALWAYS_INLINE Memory &operator=(
const EntryType *rhs) {
342 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
345 inline Memory &operator=(
const V &v) {
346 for (
size_t i = 0; i < vectorsCount(); ++i) {
393 template<
typename V>
class Memory<V, 0u, 0u, true> :
public MemoryBase<V, Memory<V, 0u, 0u, true>, 1, void>
396 typedef typename V::EntryType EntryType;
400 friend class MemoryDimensionBase<V,
Memory<V>, 1, void>;
401 enum InternalConstants {
403 AlignmentMask = Alignment - 1
405 size_t m_entriesCount;
406 size_t m_vectorsCount;
408 size_t calcPaddedEntriesCount(
size_t x)
410 size_t masked = x & AlignmentMask;
411 return (masked == 0 ? x : x + (Alignment - masked));
424 : m_entriesCount(size),
425 m_vectorsCount(calcPaddedEntriesCount(m_entriesCount)),
428 m_vectorsCount /= V::Size;
429 Base::lastVector() =
V::Zero();
439 template<
typename Parent,
typename RM>
441 : m_entriesCount(rhs.entriesCount()),
442 m_vectorsCount(rhs.vectorsCount()),
445 Detail::copyVectors(*
this, rhs);
456 : m_entriesCount(rhs.entriesCount()),
457 m_vectorsCount(rhs.vectorsCount()),
460 Detail::copyVectors(*
this, rhs);
478 std::swap(m_entriesCount, rhs.m_entriesCount);
479 std::swap(m_vectorsCount, rhs.m_vectorsCount);
485 Vc_ALWAYS_INLINE Vc_PURE
size_t entriesCount()
const {
return m_entriesCount; }
490 Vc_ALWAYS_INLINE Vc_PURE
size_t vectorsCount()
const {
return m_vectorsCount; }
501 template<
typename Parent,
typename RM>
504 Detail::copyVectors(*
this, rhs);
510 Detail::copyVectors(*
this, rhs);
524 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
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 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...
constexpr StreamingTag Streaming
Use this object for a flags parameter to request streaming loads and stores.
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.
Helper class to ensure a given alignment.