Alignment helpers for possibly any sizes

This commit is contained in:
Paul Ferrand 2020-05-30 20:41:25 +02:00 committed by Jean Pierre Cimalando
parent b74e376e16
commit 6e811f92b2

View file

@ -74,30 +74,30 @@ enum class SIMDOps {
void setSIMDOpStatus(SIMDOps op, bool status); void setSIMDOpStatus(SIMDOps op, bool status);
bool getSIMDOpStatus(SIMDOps op); bool getSIMDOpStatus(SIMDOps op);
constexpr uintptr_t ByteAlignmentMask { config::defaultAlignment - 1 }; constexpr uintptr_t ByteAlignmentMask(unsigned N) { return N - 1; }
template<class T> template<unsigned N = config::defaultAlignment, class T>
T* nextAligned(const T* ptr) T* nextAligned(const T* ptr)
{ {
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(ptr) + ByteAlignmentMask & (~ByteAlignmentMask)); return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(ptr) + ByteAlignmentMask(N) & (~ByteAlignmentMask(N)));
} }
template<class T> template<unsigned N = config::defaultAlignment, class T>
T* prevAligned(const T* ptr) T* prevAligned(const T* ptr)
{ {
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(ptr) & (~ByteAlignmentMask)); return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(ptr) & (~ByteAlignmentMask(N)));
} }
template<class T> template<unsigned N = config::defaultAlignment, class T>
bool unaligned(const T* ptr) bool unaligned(const T* ptr)
{ {
return (reinterpret_cast<uintptr_t>(ptr) & ByteAlignmentMask )!= 0; return (reinterpret_cast<uintptr_t>(ptr) & ByteAlignmentMask(N) )!= 0;
} }
template<class T, class... Args> template<unsigned N = config::defaultAlignment, class T, class... Args>
bool unaligned(const T* ptr1, Args... rest) bool unaligned(const T* ptr1, Args... rest)
{ {
return unaligned(ptr1) || unaligned(rest...); return unaligned<N>(ptr1) || unaligned<N>(rest...);
} }
/** /**