Skip to content

Commit

Permalink
Add likely and unlikely macro
Browse files Browse the repository at this point in the history
These two macro is defined in linux `compiler.h` using GNU compiler
built-in function `__builtin_expect()`.
  • Loading branch information
millaker committed Mar 5, 2024
1 parent cdce47a commit f5407cd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ void q_shuffle(struct list_head *head)
}
}

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

static struct list_head *merge(bool (*cmp)(struct list_head *,
struct list_head *,
bool),
Expand Down Expand Up @@ -516,7 +519,7 @@ static void merge_final(bool (*cmp)(struct list_head *,
/* Finish linking remainder of list b on to tail */
tail->next = b;
do {
if (!++count)
if (unlikely(!++count))
cmp(b, b, descend);
b->prev = tail;
tail = b;
Expand Down Expand Up @@ -549,7 +552,7 @@ void list_sort(struct list_head *head,
for (bits = count; bits & 1; bits >>= 1)
tail = &(*tail)->prev;
/* Do the indicated merge */
if (bits) {
if (likely(bits)) {
struct list_head *a = *tail, *b = a->prev;

a = merge(cmp, b, a, descend);
Expand Down

0 comments on commit f5407cd

Please sign in to comment.