-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the semantics of Aligned<T> to not guarantee that T will be a…
…lone on the aligned memory block of size alignment. Aligned<T[]> still guarantees that inherently as an array. CacheAligned<T> is to be used to when the guarantee of being alone on that aligned memory block is needed. CacheAligned classes also automatically determine the cache line size at run-time.
- Loading branch information
1 parent
20309ae
commit 6d430c3
Showing
6 changed files
with
124 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef CACHEALIGNED_HPP_INCLUDED | ||
#define CACHEALIGNED_HPP_INCLUDED | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include "CacheAlignedBase.hpp" | ||
#include "Aligned.hpp" | ||
#include "RoundUp.hpp" | ||
|
||
template<typename T> | ||
class CacheAligned : protected Aligned<T>, CacheAlignedBase { | ||
public: | ||
CacheAligned() : Aligned(cacheLineSize(), nullptr) {} | ||
CacheAligned(T const & value) : CacheAligned() { ref() = value; } | ||
T & ref() { return *reinterpret_cast<T *>(&Aligned::ref()); } | ||
T const & ref() const { return ref(); } | ||
}; | ||
|
||
template<typename T, std::size_t Size> | ||
class CacheAligned<T[Size]> : Aligned<T[]>, CacheAlignedBase{ | ||
public: | ||
CacheAligned() : Aligned(cacheLineSize(), Size) {} | ||
T & operator[](std::size_t index) { return *reinterpret_cast<T *>(&Aligned::operator[](index)); } | ||
T const & operator[](std::size_t index) const { return operator[](index); } | ||
}; | ||
|
||
template<typename T> | ||
class CacheAligned<T[]> : Aligned<T[]>, CacheAlignedBase { | ||
public: | ||
CacheAligned(std::size_t size) : Aligned(cacheLineSize(), size) {} | ||
T & operator[](std::size_t index) { return *reinterpret_cast<T *>(&Aligned::operator[](index)); } | ||
T const & operator[](std::size_t index) const { return operator[](index); } | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef CACHEALIGNEDBASE_HPP_INCLUDED | ||
#define CACHEALIGNEDBASE_HPP_INCLUDED | ||
|
||
#include <cstddef> | ||
#include "CacheLineSize/cacheLineSize.h" | ||
|
||
class CacheAlignedBase { | ||
protected: | ||
static std::size_t cacheLineSize() { | ||
static std::size_t cacheLineSize = cache_line_size(); | ||
return cacheLineSize; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef ISPOWEROFTWO_HPP_INCLUDED | ||
#define ISPOWEROFTWO_HPP_INCLUDED | ||
|
||
#define ISPOWEROFTWO(x) (x != 0) && ((x & (x - 1)) == 0) | ||
|
||
template<typename T> | ||
inline bool isPowerOfTwo(T x) { | ||
return ISPOWEROFTWO(x); | ||
} | ||
|
||
template<typename T, T X> | ||
struct IsPowerOfTwo { | ||
static const bool Result = ISPOWEROFTWO(X); | ||
}; | ||
|
||
#undef ISPOWEROFTWO | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
#ifndef ROUNDUP_HPP_INCLUDED | ||
#define ROUNDUP_HPP_INCLUDED | ||
|
||
#define ROUNDUP(from, to) from + to - 1 - (from - 1) % to | ||
|
||
template<typename T> | ||
inline T roundUp(T from, T to) { | ||
return from + to - 1 - (from - 1) % to; | ||
return ROUNDUP(from, to); | ||
} | ||
|
||
template<size_t From, size_t To> | ||
template<typename T, T From, T To> | ||
struct RoundUp { | ||
enum { value = From + To - 1 - (From -1) % To }; | ||
static const T Result = ROUNDUP(From, To); | ||
}; | ||
|
||
#undef ROUNDUP | ||
|
||
#endif |