git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] add likely/unlikely hints

From
Dmitry Potapov <dpotapov@gmail.com>
Date
Apr 28, 2011, 16:34 UTC
Message-ID
<20110428163418.GA6455@dpotapov.dyndns.org>

Usually modern CPUs work fine even when the most likely branch is not local, thus these hints do not provide noticeable wins, but in a situation of some loop that usually executed only once, the compiler may aggressively unroll the loop making the code bigger and slower when the loop is executed only once. So, for such rare situations, these hints may be helpful.

Signed-off-by: Dmitry Potapov <dpotapov@gmail.com>
---
 git-compat-util.h |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 40498b3..f690b29 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -60,6 +60,15 @@
 /* Approximation of the length of the decimal representation of this type. */
 #define decimal_length(x)	((int)(sizeof(x) * 2.56 + 0.5) + 1)
 
+/* Hints for branch prediction */
+#if defined(__GNUC__) && (__GNUC__ >= 3)
+#define likely(x)	__builtin_expect(!!(x), 1)
+#define unlikely(x)	__builtin_expect(!!(x), 0)
+#else
+#define likely(x)	(!!(x))
+#define unlikely(x)	(!!(x))
+#endif
+
 #if defined(__sun__)
  /*
   * On Solaris, when _XOPEN_EXTENDED is set, its header file
-- 
1.7.5
Message 1 of 1 in “add likely/unlikely hints”
  1. add likely/unlikely hintsDmitry Potapov, Apr 28, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.