Likely.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #if __GNUC__
  18. #define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) (__builtin_expect(b, t))
  19. #else
  20. #define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) b
  21. #endif
  22. // Likeliness annotations
  23. //
  24. // Useful when the author has better knowledge than the compiler of whether
  25. // the branch condition is overwhelmingly likely to take a specific value.
  26. //
  27. // Useful when the author has better knowledge than the compiler of which code
  28. // paths are designed as the fast path and which are designed as the slow path,
  29. // and to force the compiler to optimize for the fast path, even when it is not
  30. // overwhelmingly likely.
  31. #define FOLLY_LIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 1)
  32. #define FOLLY_UNLIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 0)
  33. // Un-namespaced annotations
  34. #undef LIKELY
  35. #undef UNLIKELY
  36. #if defined(__GNUC__)
  37. #define LIKELY(x) (__builtin_expect((x), 1))
  38. #define UNLIKELY(x) (__builtin_expect((x), 0))
  39. #else
  40. #define LIKELY(x) (x)
  41. #define UNLIKELY(x) (x)
  42. #endif