Namespacing and comments in folly/Likely.h
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 27 Dec 2017 01:17:08 +0000 (17:17 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 27 Dec 2017 01:34:03 +0000 (17:34 -0800)
Summary:
[Folly] Namespacing and comments in `folly/Likely.h`.

This adds `FOLLY_LIKELY` and `FOLLY_UNLIKELY`.

Reviewed By: Orvid

Differential Revision: D6636136

fbshipit-source-id: da93220201cabca91b4477ab98269a0febb735db

folly/Likely.h

index 5c7da5e48859dd6a4d4ca0668b4d84ee64f73cee..bdab72b817c2d621649c6bc28f27127aab6715be 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2012-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-/**
- * Compiler hints to indicate the fast path of an "if" branch: whether
- * the if condition is likely to be true or false.
- *
- * @author Tudor Bosman (tudorb@fb.com)
- */
-
 #pragma once
 
+#if __GNUC__
+#define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) (__builtin_expect(b, t))
+#else
+#define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) b
+#endif
+
+//  Likeliness annotations
+//
+//  Useful when the author has better knowledge than the compiler of whether
+//  the branch condition is overwhelmingly likely to take a specific value.
+//
+//  Useful when the author has better knowledge than the compiler of which code
+//  paths are designed as the fast path and which are designed as the slow path,
+//  and to force the compiler to optimize for the fast path, even when it is not
+//  overwhelmingly likely.
+
+#define FOLLY_LIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 1)
+#define FOLLY_UNLIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 0)
+
+//  Un-namespaced annotations
+
 #undef LIKELY
 #undef UNLIKELY
 
-#if defined(__GNUC__) && __GNUC__ >= 4
+#if defined(__GNUC__)
 #define LIKELY(x)   (__builtin_expect((x), 1))
 #define UNLIKELY(x) (__builtin_expect((x), 0))
 #else