1 //===-- Portable optimization macros ----------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // This header file defines portable macros for performance optimization. 9 10 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H 11 #define LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H 12 13 #include "src/__support/macros/attributes.h" // LIBC_INLINE 14 #include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG 15 16 // We use a template to implement likely/unlikely to make sure that we don't 17 // accidentally pass an integer. 18 namespace LIBC_NAMESPACE::details { 19 template <typename T> expects_bool_condition(T value,T expected)20LIBC_INLINE constexpr bool expects_bool_condition(T value, T expected) { 21 return __builtin_expect(value, expected); 22 } 23 } // namespace LIBC_NAMESPACE::details 24 #define LIBC_LIKELY(x) LIBC_NAMESPACE::details::expects_bool_condition(x, true) 25 #define LIBC_UNLIKELY(x) \ 26 LIBC_NAMESPACE::details::expects_bool_condition(x, false) 27 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") 32 #else 33 #error "Unhandled compiler" 34 #endif 35 36 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H 37