aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom/core
diff options
context:
space:
mode:
authorVadim Yanitskiy <vyanitskiy@sysmocom.de>2022-02-02 01:17:45 +0600
committerVadim Yanitskiy <vyanitskiy@sysmocom.de>2022-02-08 12:48:28 +0600
commite59e839dd949136d2f1b63c59df141e1b01f4763 (patch)
tree4e44e45007edea0dbaf1a4c1971813bb9e19afb4 /include/osmocom/core
parent1790f1763adcb71c6c2efd22ca733c6caff52949 (diff)
core/utils.h: add OSMO_LIKELY / OSMO_UNLIKELY macros
These macros are built on top of the __builtin_expect() function [1], which provides the compiler with branch prediction information. Similar macros exist in the Linux kernel: likely() / unlikely(). [1] https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html Change-Id: I0b029654ba050f079eed4a0574a3fa8019677067
Diffstat (limited to 'include/osmocom/core')
-rw-r--r--include/osmocom/core/utils.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index eda0e399..7625da16 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -38,6 +38,15 @@
/*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */
#define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
+/*! Branch prediction optimizations */
+#if defined(__GNUC__)
+#define OSMO_LIKELY(exp) __builtin_expect(!!(exp), 1)
+#define OSMO_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
+#else
+#define OSMO_LIKELY(exp) exp
+#define OSMO_UNLIKELY(exp) exp
+#endif
+
/*! A mapping between human-readable string and numeric value */
struct value_string {
uint32_t value; /*!< numeric value */