From 08b79b9bbfa96ee6dcbc4d58bf082c9e6df911df Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 20 Dec 2018 11:46:40 -0800 Subject: Add an assume_aligned helper --- common/almalloc.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/almalloc.h b/common/almalloc.h index d9b285fe..7ec07004 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -68,6 +68,21 @@ struct allocator : public std::allocator { { } }; +template +inline T* assume_aligned(T *ptr) noexcept +{ + static_assert((alignment & (alignment-1)) == 0, "alignment must be a power of 2"); +#ifdef __GNUC__ + return reinterpret_cast(__builtin_assume_aligned(ptr, alignment)); +#elif defined(_MSC_VER) + auto ptrval = reinterpret_cast(ptr); + if((ptrval&(alignment-1)) != 0) __assume(0); + return reinterpret_cast(ptrval); +#else + return ptr; +#endif +} + } // namespace al #endif /* AL_MALLOC_H */ -- cgit v1.2.3