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 | |
17 | #pragma once |
18 | |
19 | #if __GNUC__ |
20 | #define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) (__builtin_expect(b, t)) |
21 | #else |
22 | #define FOLLY_DETAIL_BUILTIN_EXPECT(b, t) b |
23 | #endif |
24 | |
25 | // Likeliness annotations |
26 | // |
27 | // Useful when the author has better knowledge than the compiler of whether |
28 | // the branch condition is overwhelmingly likely to take a specific value. |
29 | // |
30 | // Useful when the author has better knowledge than the compiler of which code |
31 | // paths are designed as the fast path and which are designed as the slow path, |
32 | // and to force the compiler to optimize for the fast path, even when it is not |
33 | // overwhelmingly likely. |
34 | |
35 | #define FOLLY_LIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 1) |
36 | #define FOLLY_UNLIKELY(x) FOLLY_DETAIL_BUILTIN_EXPECT((x), 0) |
37 | |
38 | // Un-namespaced annotations |
39 | |
40 | #undef LIKELY |
41 | #undef UNLIKELY |
42 | |
43 | #if defined(__GNUC__) |
44 | #define LIKELY(x) (__builtin_expect((x), 1)) |
45 | #define UNLIKELY(x) (__builtin_expect((x), 0)) |
46 | #else |
47 | #define LIKELY(x) (x) |
48 | #define UNLIKELY(x) (x) |
49 | #endif |
50 | |