github.com/moontrade/nogc@v0.1.7/collections/tree/Likely.h (about) 1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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 // Notes: 36 // * All supported compilers treat unconditionally-noreturn blocks as unlikely. 37 // This is true for blocks which unconditionally throw exceptions and for 38 // blocks which unconditionally call [[noreturn]]-annotated functions. Such 39 // cases do not require likeliness annotations. 40 41 #define FOLLY_LIKELY(...) FOLLY_DETAIL_BUILTIN_EXPECT((__VA_ARGS__), 1) 42 #define FOLLY_UNLIKELY(...) FOLLY_DETAIL_BUILTIN_EXPECT((__VA_ARGS__), 0) 43 44 // Un-namespaced annotations 45 46 #undef LIKELY 47 #undef UNLIKELY 48 49 #if defined(__GNUC__) 50 #define LIKELY(x) (__builtin_expect((x), 1)) 51 #define UNLIKELY(x) (__builtin_expect((x), 0)) 52 #else 53 #define LIKELY(x) (x) 54 #define UNLIKELY(x) (x) 55 #endif