1 //===-- Portable optimization macros ----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // This header file defines portable macros for performance optimization.
10 #ifndef LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H
11 #define LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H
13 #include "src/__support/macros/attributes.h" // LIBC_INLINE
14 #include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
15 #include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG
17 // We use a template to implement likely/unlikely to make sure that we don't
18 // accidentally pass an integer.
19 namespace __llvm_libc::details
{
21 LIBC_INLINE
constexpr bool expects_bool_condition(T value
, T expected
) {
22 return __builtin_expect(value
, expected
);
24 } // namespace __llvm_libc::details
25 #define LIBC_LIKELY(x) __llvm_libc::details::expects_bool_condition(x, true)
26 #define LIBC_UNLIKELY(x) __llvm_libc::details::expects_bool_condition(x, false)
28 #if defined(LIBC_COMPILER_IS_CLANG)
29 #define LIBC_LOOP_NOUNROLL _Pragma("nounroll")
30 #elif defined(LIBC_COMPILER_IS_GCC)
31 #define LIBC_LOOP_NOUNROLL _Pragma("GCC unroll 0")
33 #error "Unhandled compiler"
36 #endif /* LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H */