瀏覽代碼

core: add CE_LIKELY() and CE_UNLIKELY()

Daniele Bartolini 7 年之前
父節點
當前提交
ba3f03ce44
共有 2 個文件被更改,包括 8 次插入1 次删除
  1. 1 1
      src/core/error/error.h
  2. 7 0
      src/core/types.h

+ 1 - 1
src/core/error/error.h

@@ -28,7 +28,7 @@ namespace error
 	#define CE_ASSERT(condition, msg, ...)                   \
 		do                                                   \
 		{                                                    \
-			if (!(condition))                                \
+			if (CE_UNLIKELY(!(condition)))                   \
 			{                                                \
 				crown::error::abort("Assertion failed: %s\n" \
 					"    In: %s:%d\n"                        \

+ 7 - 0
src/core/types.h

@@ -57,6 +57,13 @@ typedef double   f64;
 #define CE_UNUSED(x) do { (void)(x); } while (0)
 #define CE_STATIC_ASSERT(condition, ...) static_assert(condition, "" # __VA_ARGS__)
 
+#if defined(__GNUC__)
+	#define CE_LIKELY(x) __builtin_expect((x), 1)
+	#define CE_UNLIKELY(x) __builtin_expect((x), 0)
+#else
+	#define CE_LIKELY(x) (x)
+	#define CE_UNLIKELY(x) (x)
+#endif
 #if defined(__GNUC__)
 	#define CE_THREAD __thread
 #elif defined(_MSC_VER)