From a8a40ac03289cd76c22b0bef42473b293da16b00 Mon Sep 17 00:00:00 2001 From: Matheus Date: Sat, 5 Oct 2024 11:31:22 -0300 Subject: [PATCH] util: likely() and unlikely() macros --- kernel-src/include/logging.h | 5 +++-- kernel-src/include/util.h | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel-src/include/logging.h b/kernel-src/include/logging.h index 1a0f8996..fadd22a0 100644 --- a/kernel-src/include/logging.h +++ b/kernel-src/include/logging.h @@ -5,6 +5,7 @@ #include #include #include +#include void _putchar(char c); void logging_sethook(void (*fun)(char)); @@ -16,7 +17,7 @@ extern mutex_t printf_mutex; #endif #define printf(...) { \ - if (current_thread()) {\ + if (likely(current_thread())) {\ MUTEX_ACQUIRE(&printf_mutex, false); \ } else { \ while (MUTEX_TRY(&printf_mutex) == false) CPU_PAUSE(); \ @@ -26,7 +27,7 @@ extern mutex_t printf_mutex; } #define __assert(x) \ - if (!(x)){ \ + if (unlikely(!(x))){ \ printf("\e[91m%s:%s:%d: %s failed\n\e[0m", __FILE__, __func__, __LINE__, #x); \ _panic(NULL, NULL); \ }; diff --git a/kernel-src/include/util.h b/kernel-src/include/util.h index a44491d8..2802e2e9 100644 --- a/kernel-src/include/util.h +++ b/kernel-src/include/util.h @@ -9,6 +9,9 @@ #define ROUND_DOWN(v, n) ((v) - ((v) % (n))) #define ROUND_UP(v, n) ROUND_DOWN((v) + (n) - 1, n) +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + static inline long abs(long x) { return x < 0 ? -x : x; }