29 #ifndef VC_COMMON_MEMORY_H_
30 #define VC_COMMON_MEMORY_H_
32 #include "memorybase.h"
37 #include <initializer_list>
38 #include "memoryfwd.h"
42 namespace Vc_VERSIONED_NAMESPACE
75 template<
typename T, Vc::MallocAlignment A>
78 return static_cast<T *
>(Common::malloc<A>(n *
sizeof(T)));
103 Vc_ALWAYS_INLINE
void free(T *p)
110 template<
typename V,
size_t Size>
struct _MemorySizeCalculation
112 enum AlignmentCalculations {
114 AlignmentMask = Alignment - 1,
115 MaskedSize = Size & AlignmentMask,
116 Padding = Alignment - MaskedSize,
117 PaddedSize = MaskedSize == 0 ? Size : Size + Padding
131 template <
typename V,
size_t Size1,
size_t Size2,
bool InitPadding>
133 public MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
134 Memory<V, Size2, 0, false>>
137 typedef typename V::EntryType EntryType;
140 friend class MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, Memory<V, Size2, 0, false> >;
141 friend class MemoryDimensionBase<V, Memory<V, Size1, Size2, InitPadding>, 2, Memory<V, Size2, 0, false> >;
144 PaddedSize2 = _MemorySizeCalculation<V, Size2>::PaddedSize
146 alignas(
static_cast<size_t>(Alignment))
149 EntryType m_mem[Size1][PaddedSize2];
155 VectorsCount = PaddedSize2 / V::Size
162 for (
size_t i = 0; i < Size1; ++i) {
173 static constexpr
size_t rowsCount() {
return RowCount; }
188 static constexpr
size_t vectorsCount() {
return VectorsCount * Size1; }
199 template<
typename Parent,
typename RM>
202 Detail::copyVectors(*
this, rhs);
206 Vc_ALWAYS_INLINE Memory &operator=(
const Memory &rhs) {
207 Detail::copyVectors(*
this, rhs);
219 for (
size_t i = 0; i < vectorsCount(); ++i) {
269 template <
typename V,
size_t Size,
bool InitPadding>
272 public MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>
275 typedef typename V::EntryType EntryType;
278 friend class MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>;
279 friend class MemoryDimensionBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>;
282 MaskedSize = Size & (V::Size - 1),
284 Padding = V::Size - MaskedSize,
285 PaddedSize = MaskedSize == 0 ? Size : Size + Padding
287 alignas(
static_cast<size_t>(Alignment))
290 EntryType m_mem[PaddedSize];
296 VectorsCount = PaddedSize / V::Size
302 Base::lastVector() =
V::Zero();
306 Memory(std::initializer_list<EntryType> init)
308 Vc_ASSERT(init.size() <= Size);
309 Base::lastVector() =
V::Zero();
310 std::copy(init.begin(), init.end(), &m_mem[0]);
340 char *addr =
reinterpret_cast<char *
>(ptr);
342 addr -= Vc_OFFSETOF(MM, m_mem);
343 return *
new(addr) MM;
360 inline Memory(
const Memory &rhs)
362 Detail::copyVectors(*
this, rhs);
365 template <
size_t S>
inline Memory(
const Memory<V, S> &rhs)
367 assert(vectorsCount() == rhs.vectorsCount());
368 Detail::copyVectors(*
this, rhs);
371 inline Memory &operator=(
const Memory &rhs)
373 Detail::copyVectors(*
this, rhs);
377 template <
size_t S>
inline Memory &operator=(
const Memory<V, S> &rhs)
379 assert(vectorsCount() == rhs.vectorsCount());
380 Detail::copyVectors(*
this, rhs);
384 Vc_ALWAYS_INLINE Memory &operator=(
const EntryType *rhs) {
385 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
388 inline Memory &operator=(
const V &v) {
389 for (
size_t i = 0; i < vectorsCount(); ++i) {
436 template<
typename V>
class Memory<V, 0u, 0u, true> :
public MemoryBase<V, Memory<V, 0u, 0u, true>, 1, void>
439 typedef typename V::EntryType EntryType;
442 friend class MemoryBase<V, Memory<V>, 1, void>;
443 friend class MemoryDimensionBase<V, Memory<V>, 1, void>;
444 enum InternalConstants {
446 AlignmentMask = Alignment - 1
448 size_t m_entriesCount;
449 size_t m_vectorsCount;
451 size_t calcPaddedEntriesCount(
size_t x)
453 size_t masked = x & AlignmentMask;
454 return (masked == 0 ? x : x + (Alignment - masked));
467 : m_entriesCount(size),
468 m_vectorsCount(calcPaddedEntriesCount(m_entriesCount)),
471 m_vectorsCount /= V::Size;
472 Base::lastVector() =
V::Zero();
482 template<
typename Parent,
typename RM>
484 : m_entriesCount(rhs.entriesCount()),
485 m_vectorsCount(rhs.vectorsCount()),
488 Detail::copyVectors(*
this, rhs);
498 Vc_ALWAYS_INLINE
Memory(
const Memory &rhs)
499 : m_entriesCount(rhs.entriesCount()),
500 m_vectorsCount(rhs.vectorsCount()),
503 Detail::copyVectors(*
this, rhs);
519 inline void swap(Memory &rhs) {
521 std::swap(m_entriesCount, rhs.m_entriesCount);
522 std::swap(m_vectorsCount, rhs.m_vectorsCount);
528 Vc_ALWAYS_INLINE Vc_PURE
size_t entriesCount()
const {
return m_entriesCount; }
533 Vc_ALWAYS_INLINE Vc_PURE
size_t vectorsCount()
const {
return m_vectorsCount; }
544 template<
typename Parent,
typename RM>
547 Detail::copyVectors(*
this, rhs);
551 Vc_ALWAYS_INLINE Memory &operator=(
const Memory &rhs) {
553 Detail::copyVectors(*
this, rhs);
566 Vc_ALWAYS_INLINE Memory &
operator=(
const EntryType *rhs) {
567 std::memcpy(m_mem, rhs, entriesCount() *
sizeof(EntryType));
650 using Common::Memory;
660 template<
typename V> Vc_ALWAYS_INLINE
void swap(Vc::Memory<V> &a, Vc::Memory<V> &b) { a.swap(b); }
663 #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.
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()
size_t vectorsCount() const
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...
Common::AdaptSubscriptOperator< std::vector< T, Allocator >> vector
An adapted std::vector container with an additional subscript operator which implements gather and sc...
Helper class to ensure a given alignment.
Memory & operator=(const V &v)
Initialize all data with the given vector.
void prefetchForOneRead(const void *addr)
Prefetch the cacheline containing addr for a single read access.
size_t vectorsCount() const
void prefetchFar(const void *addr)
Prefetch the cacheline containing addr to L3 cache.
Memory(const Memory &rhs)
Overload of the above function.
size_t entriesCount() const
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...
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.
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.