diff options
author | Chris Robinson <[email protected]> | 2022-03-30 01:33:32 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2022-03-30 01:33:32 -0700 |
commit | 51796ccbedd6b4f76dd47964ad9241e300c9f1fe (patch) | |
tree | 7208f7c167722c0b1f9c6d0d37b93857526dce2c | |
parent | 325c992bea3bfaf204c493357523817816ba44db (diff) |
Move assume_aligned to opthelpers.h and define force_inline
-rw-r--r-- | common/almalloc.h | 36 | ||||
-rw-r--r-- | common/opthelpers.h | 42 |
2 files changed, 38 insertions, 40 deletions
diff --git a/common/almalloc.h b/common/almalloc.h index ddbd72f4..711d02fd 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -101,42 +101,6 @@ constexpr bool operator==(const allocator<T,N>&, const allocator<U,M>&) noexcept template<typename T, std::size_t N, typename U, std::size_t M> constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; } -#ifdef __cpp_lib_assume_aligned -template<std::size_t alignment, typename T> -using assume_aligned = std::assume_aligned<alignment,T>; - -#elif defined(__clang__) || (defined(__GNUC__) && !defined(__ICC)) - -template<std::size_t alignment, typename T> -[[gnu::always_inline/*,nodiscard*/]] constexpr T* assume_aligned(T *ptr) noexcept -{ return static_cast<T*>(__builtin_assume_aligned(ptr, alignment)); } - -#elif defined(_MSC_VER) - -template<std::size_t alignment, typename T> -__forceinline /*[[nodiscard]]*/ constexpr T* assume_aligned(T *ptr) noexcept -{ - static constexpr std::size_t alignment_mask{(1<<alignment) - 1}; - if((reinterpret_cast<std::uintptr_t>(ptr)&alignment_mask) == 0) - return ptr; - __assume(0); -} - -#elif defined(__ICC) - -template<std::size_t alignment, typename T> -/*[[nodiscard]]*/ constexpr T* assume_aligned(T *ptr) noexcept -{ - __assume_aligned(ptr, alignment); - return ptr; -} - -#else - -template<std::size_t alignment, typename T> -/*[[nodiscard]]*/ constexpr T* assume_aligned(T *ptr) noexcept { return ptr; } -#endif - template<typename T, typename ...Args> constexpr T* construct_at(T *ptr, Args&& ...args) diff --git a/common/opthelpers.h b/common/opthelpers.h index 726179de..e4945c2b 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -10,27 +10,37 @@ #define HAS_BUILTIN(x) (0) #endif +#ifdef __GNUC__ +#define force_inline [[gnu::always_inline]] +#elif defined(_MSC_VER) +#define force_inline __forceinline +#else +#define force_inline inline +#endif + #if defined(__GNUC__) || HAS_BUILTIN(__builtin_expect) /* likely() optimizes for the case where the condition is true. The condition * is not required to be true, but it can result in more optimal code for the * true path at the expense of a less optimal false path. */ template<typename T> -constexpr bool likely(T&& expr) noexcept +force_inline constexpr bool likely(T&& expr) noexcept { return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), true); } /* The opposite of likely(), optimizing for the case where the condition is * false. */ template<typename T> -constexpr bool unlikely(T&& expr) noexcept +force_inline constexpr bool unlikely(T&& expr) noexcept { return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), false); } #else template<typename T> -constexpr bool likely(T&& expr) noexcept { return static_cast<bool>(std::forward<T>(expr)); } +force_inline constexpr bool likely(T&& expr) noexcept +{ return static_cast<bool>(std::forward<T>(expr)); } template<typename T> -constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(std::forward<T>(expr)); } +force_inline constexpr bool unlikely(T&& expr) noexcept +{ return static_cast<bool>(std::forward<T>(expr)); } #endif #define LIKELY(x) (likely(x)) #define UNLIKELY(x) (unlikely(x)) @@ -49,4 +59,28 @@ constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(std::forwa #define ASSUME(x) ((void)0) #endif +namespace al { + +template<std::size_t alignment, typename T> +force_inline constexpr auto assume_aligned(T *ptr) noexcept +{ +#ifdef __cpp_lib_assume_aligned + return std::assume_aligned<alignment,T>(ptr); +#elif defined(__clang__) || (defined(__GNUC__) && !defined(__ICC)) + return static_cast<T*>(__builtin_assume_aligned(ptr, alignment)); +#elif defined(_MSC_VER) + constexpr std::size_t alignment_mask{(1<<alignment) - 1}; + if((reinterpret_cast<std::uintptr_t>(ptr)&alignment_mask) == 0) + return ptr; + __assume(0); +#elif defined(__ICC) + __assume_aligned(ptr, alignment); + return ptr; +#else + return ptr; +#endif +} + +} // namespace al + #endif /* OPTHELPERS_H */ |