summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan G Rennison <j.g.rennison@gmail.com>2016-03-10 00:13:58 +0000
committerCharles Pigott <charlespigott@googlemail.com>2020-12-27 10:30:55 +0000
commitfc52d3df5010994083c858fef01fa2926de951ad (patch)
treee8a7a4aa583baa9c5a52dbd879126406cec8a985 /src
parent5cf28be742581d335ecb0c270e8039ee8ed9ccf0 (diff)
downloadopenttd-fc52d3df5010994083c858fef01fa2926de951ad.tar.xz
Codechange: Use likely/__builtin_expect for assertion macros
Diffstat (limited to 'src')
-rw-r--r--src/stdafx.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/stdafx.h b/src/stdafx.h
index 00dd1322a..332f1a3ee 100644
--- a/src/stdafx.h
+++ b/src/stdafx.h
@@ -417,6 +417,14 @@ assert_compile(SIZE_MAX >= UINT32_MAX);
# define CloseConnection OTTD_CloseConnection
#endif /* __APPLE__ */
+#ifdef __GNUC__
+# define likely(x) __builtin_expect(!!(x), 1)
+# define unlikely(x) __builtin_expect(!!(x), 0)
+#else
+# define likely(x) (x)
+# define unlikely(x) (x)
+#endif
+
void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
#define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
@@ -424,13 +432,13 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
/* For non-debug builds with assertions enabled use the special assertion handler. */
#if defined(NDEBUG) && defined(WITH_ASSERT)
# undef assert
-# define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
+# define assert(expression) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
#endif
/* Asserts are enabled if NDEBUG isn't defined or WITH_ASSERT is defined. */
#if !defined(NDEBUG) || defined(WITH_ASSERT)
# define OTTD_ASSERT
-# define assert_msg(expression, msg, ...) if (!(expression)) error("Assertion failed at line %i of %s: %s\n\t" msg, __LINE__, __FILE__, #expression, __VA_ARGS__);
+# define assert_msg(expression, msg, ...) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s\n\t" msg, __LINE__, __FILE__, #expression, __VA_ARGS__);
#else
# define assert_msg(expression, msg, ...)
#endif