Move some macros to a common header

This commit is contained in:
Chris Robinson
2019-01-07 01:12:09 -08:00
parent 20a5306bdf
commit 648b76ed65
3 changed files with 41 additions and 35 deletions
+1
View File
@@ -696,6 +696,7 @@ SET(COMMON_OBJS
common/almalloc.h
common/atomic.h
common/math_defs.h
common/opthelpers.h
common/threads.cpp
common/threads.h
common/vecmat.h
+1 -35
View File
@@ -32,6 +32,7 @@
#include "almalloc.h"
#include "threads.h"
#include "ambidefs.h"
#include "opthelpers.h"
template<typename T, size_t N>
@@ -40,41 +41,6 @@ constexpr inline size_t countof(const T(&)[N]) noexcept
#define COUNTOF countof
#ifdef __has_builtin
#define HAS_BUILTIN __has_builtin
#else
#define HAS_BUILTIN(x) (0)
#endif
#ifdef __GNUC__
/* LIKELY optimizes 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.
*/
#define LIKELY(x) __builtin_expect(!!(x), !0)
/* The opposite of LIKELY, optimizing the case where the condition is false. */
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
/* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
* undefined behavior. It's essentially an assert without actually checking the
* condition at run-time, allowing for stronger optimizations than LIKELY.
*/
#if HAS_BUILTIN(__builtin_assume)
#define ASSUME __builtin_assume
#else
#define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
#endif
#else
#define LIKELY(x) (!!(x))
#define UNLIKELY(x) (!!(x))
#ifdef _MSC_VER
#define ASSUME __assume
#else
#define ASSUME(x) ((void)0)
#endif
#endif
#ifndef UNUSED
#if defined(__cplusplus)
#define UNUSED(x)
+39
View File
@@ -0,0 +1,39 @@
#ifndef OPTHELPERS_H
#define OPTHELPERS_H
#ifdef __has_builtin
#define HAS_BUILTIN __has_builtin
#else
#define HAS_BUILTIN(x) (0)
#endif
#ifdef __GNUC__
/* LIKELY optimizes 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.
*/
#define LIKELY(x) __builtin_expect(!!(x), !0)
/* The opposite of LIKELY, optimizing the case where the condition is false. */
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
/* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
* undefined behavior. It's essentially an assert without actually checking the
* condition at run-time, allowing for stronger optimizations than LIKELY.
*/
#if HAS_BUILTIN(__builtin_assume)
#define ASSUME __builtin_assume
#else
#define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
#endif
#else /* __GNUC__ */
#define LIKELY(x) (!!(x))
#define UNLIKELY(x) (!!(x))
#ifdef _MSC_VER
#define ASSUME __assume
#else
#define ASSUME(x) ((void)0)
#endif /* _MSC_VER */
#endif /* __GNUC__ */
#endif /* OPTHELPERS_H */