{"thread":{"id":"27210","subject":"[PATCH] add likely/unlikely hints","startedAt":"2011-04-28T16:34:18Z","lastAt":"2011-04-28T16:34:18Z","messageCount":1,"participants":["Dmitry Potapov"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"166656","messageId":"20110428163418.GA6455@dpotapov.dyndns.org","threadId":"27210","inReplyTo":null,"subject":"[PATCH] add likely/unlikely hints","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2011-04-28T16:34:18Z","receivedAt":"2011-04-28T16:34:18Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"Usually modern CPUs work fine even when the most likely branch is not local,\nthus these hints do not provide noticeable wins, but in a situation of some\nloop that usually executed only once, the compiler may aggressively unroll\nthe loop making the code bigger and slower when the loop is executed only\nonce. So, for such rare situations, these hints may be helpful.\n\nSigned-off-by: Dmitry Potapov <dpotapov@gmail.com>\n---\n\n git-compat-util.h |    9 +++++++++\n 1 files changed, 9 insertions(+), 0 deletions(-)\n\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 40498b3..f690b29 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -60,6 +60,15 @@\n /* Approximation of the length of the decimal representation of this type. */\n #define decimal_length(x)\t((int)(sizeof(x) * 2.56 + 0.5) + 1)\n \n+/* Hints for branch prediction */\n+#if defined(__GNUC__) && (__GNUC__ >= 3)\n+#define likely(x)\t__builtin_expect(!!(x), 1)\n+#define unlikely(x)\t__builtin_expect(!!(x), 0)\n+#else\n+#define likely(x)\t(!!(x))\n+#define unlikely(x)\t(!!(x))\n+#endif\n+\n #if defined(__sun__)\n  /*\n   * On Solaris, when _XOPEN_EXTENDED is set, its header file\n-- \n1.7.5\n"}]}