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>
67 #ifdef Vc_RECURSIVE_MEMORY 68 class 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 86 friend class MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, RowMemory>;
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 97 RowMemory m_mem[Size1];
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);
161 Vc_ALWAYS_INLINE Memory &operator=(
const Memory &rhs) {
162 Detail::copyVectors(*
this, rhs);
174 for (
size_t i = 0; i < vectorsCount(); ++i) {
224 template <
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;
235 friend class MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>;
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;
317 inline Memory(
const Memory &rhs)
319 Detail::copyVectors(*
this, rhs);
322 template <
size_t S>
inline Memory(
const Memory<V, S> &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)
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;
399 friend class MemoryBase<V, Memory<V>, 1, void>;
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);
455 Vc_ALWAYS_INLINE
Memory(
const Memory &rhs)
456 : m_entriesCount(rhs.entriesCount()),
457 m_vectorsCount(rhs.vectorsCount()),
460 Detail::copyVectors(*
this, rhs);
476 inline void swap(Memory &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);
508 Vc_ALWAYS_INLINE Memory &operator=(
const Memory &rhs) {
510 Detail::copyVectors(*
this, rhs);
523 Vc_ALWAYS_INLINE Memory &
operator=(
const EntryType *rhs) {
524 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
620 #endif // VC_COMMON_MEMORY_H_ void free(T *p)
Frees memory that was allocated with Vc::malloc.
void prefetchClose(const void *addr)
Prefetch the cacheline containing addr to L1 cache.
size_t vectorsCount() const
Memory & operator=(const EntryType *rhs)
Overwrite all entries with the values stored in the memory at rhs.
void prefetchMid(const void *addr)
Prefetch the cacheline containing addr to L2 cache.
Memory(size_t size)
Allocate enough memory to access size values of type V::EntryType.
static constexpr size_t entriesCount()
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.
static constexpr size_t vectorsCount()
A helper class for fixed-size two-dimensional arrays.
Memory(const MemoryBase< V, Parent, 1, RM > &rhs)
Copy the memory into a new memory area.
Memory & operator=(const MemoryBase< V, Parent, 1, RM > &rhs)
Overwrite all entries with the values stored in rhs.
Common interface to all Memory classes, independent of allocation on the stack or heap...
Helper class to ensure a given alignment.
Memory & operator=(const V &v)
Initialize all data with the given vector.
size_t entriesCount() const
void prefetchForOneRead(const void *addr)
Prefetch the cacheline containing addr for a single read access.
void prefetchFar(const void *addr)
Prefetch the cacheline containing addr to L3 cache.
Memory(const Memory &rhs)
Overload of the above function.
static constexpr size_t entriesCount()
T * malloc(size_t n)
Allocates memory on the Heap with alignment and padding suitable for vectorized access.
constexpr StreamingTag Streaming
Use this object for a flags parameter to request streaming loads and stores.
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Common::AdaptSubscriptOperator< std::vector< T, Allocator > > vector
An adapted std::vector container with an additional subscript operator which implements gather and sc...
Vector Classes Namespace.
void swap(Memory &rhs)
Swap the contents and size information of two Memory objects.
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
static Memory< V, Size, 0u, false > & fromRawData(EntryType *ptr)
Wrap existing data with the Memory convenience class.
static constexpr size_t vectorsCount()
Align on boundary of vector sizes (e.g.
size_t vectorsCount() const
static constexpr size_t rowsCount()
Memory & operator=(const MemoryBase< V, Parent, 2, RM > &rhs)
Copies the data from a different object.
~Memory()
Frees the memory which was allocated in the constructor.