From 7a1388d55faa47d80be19a4b050ca58d2343cc0a Mon Sep 17 00:00:00 2001 From: Anton Protopopov Date: Mon, 31 Mar 2025 20:36:18 +0000 Subject: [PATCH] libbpf: Add likely/unlikely macros and use them in selftests A few selftests and, more importantly, consequent changes to the bpf_helpers.h file, use likely/unlikely macros, so define them here and remove duplicate definitions from existing selftests. Signed-off-by: Anton Protopopov Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20250331203618.1973691-3-a.s.protopopov@gmail.com --- src/bpf_helpers.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bpf_helpers.h b/src/bpf_helpers.h index 686824b..a50773d 100644 --- a/src/bpf_helpers.h +++ b/src/bpf_helpers.h @@ -15,6 +15,14 @@ #define __array(name, val) typeof(val) *name[] #define __ulong(name, val) enum { ___bpf_concat(__unique_value, __COUNTER__) = val } name +#ifndef likely +#define likely(x) (__builtin_expect(!!(x), 1)) +#endif + +#ifndef unlikely +#define unlikely(x) (__builtin_expect(!!(x), 0)) +#endif + /* * Helper macro to place programs, maps, license in * different sections in elf_bpf file. Section names