From 04de38d166b968a378222cd85d1c4239b31335d6 Mon Sep 17 00:00:00 2001 From: Fletcher Dunn Date: Mon, 6 Apr 2020 10:45:03 -0700 Subject: [PATCH] Promote likely()/unlikely() to public header. I have been using this in the relay to optimize some branches in inner loop code, and I want to do that more broadly. This is an idiom from the linux kernel. --- .../steamnetworkingsockets_internal.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/steamnetworkingsockets/steamnetworkingsockets_internal.h b/src/steamnetworkingsockets/steamnetworkingsockets_internal.h index 991fe9c..21e80dd 100644 --- a/src/steamnetworkingsockets/steamnetworkingsockets_internal.h +++ b/src/steamnetworkingsockets/steamnetworkingsockets_internal.h @@ -107,6 +107,24 @@ struct iovec struct iovec; #endif +// likely() and unlikely(). Branch hints +// This is an idiom from the linux kernel +#ifdef __GNUC__ + #ifndef likely + #define likely(x) __builtin_expect (!!(x), 1) + #endif + #ifndef unlikely + #define unlikely(x) __builtin_expect (!!(x), 0) + #endif +#else + #ifndef likely + #define likely(x) (x) + #endif + #ifndef unlikely + #define unlikely(x) (x) + #endif +#endif + // Internal stuff goes in a private namespace namespace SteamNetworkingSocketsLib {