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/config.h" 15 #include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG 16 17 // We use a template to implement likely/unlikely to make sure that we don't 18 // accidentally pass an integer. 19 namespace LIBC_NAMESPACE_DECL { 20 namespace details { 21 template <typename T> 22 LIBC_INLINE constexpr bool expects_bool_condition(T value, T expected) { 23 return __builtin_expect(value, expected); 24 } 25 } // namespace details 26 } // namespace LIBC_NAMESPACE_DECL 27 #define LIBC_LIKELY(x) LIBC_NAMESPACE::details::expects_bool_condition(x, true) 28 #define LIBC_UNLIKELY(x) \ 29 LIBC_NAMESPACE::details::expects_bool_condition(x, false) 30 31 #if defined(LIBC_COMPILER_IS_CLANG) 32 #define LIBC_LOOP_NOUNROLL _Pragma("nounroll") 33 #define LIBC_LOOP_UNROLL _Pragma("unroll") 34 #elif defined(LIBC_COMPILER_IS_GCC) 35 #define LIBC_LOOP_NOUNROLL _Pragma("GCC unroll 0") 36 #define LIBC_LOOP_UNROLL _Pragma("GCC unroll 2048") 37 #else 38 #error "Unhandled compiler" 39 #endif 40 41 // Defining optimization options for math functions. 42 // TODO: Exporting this to public generated headers? 43 #define LIBC_MATH_SKIP_ACCURATE_PASS 0x01 44 #define LIBC_MATH_SMALL_TABLES 0x02 45 #define LIBC_MATH_NO_ERRNO 0x04 46 #define LIBC_MATH_NO_EXCEPT 0x08 47 #define LIBC_MATH_FAST \ 48 (LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | \ 49 LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT) 50 #define LIBC_MATH_INTERMEDIATE_COMP_IN_FLOAT 0x10 51 52 #ifndef LIBC_MATH 53 #define LIBC_MATH 0 54 #endif // LIBC_MATH 55 56 #if (LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS) 57 #define LIBC_MATH_HAS_SKIP_ACCURATE_PASS 58 #endif 59 60 #if (LIBC_MATH & LIBC_MATH_SMALL_TABLES) 61 #define LIBC_MATH_HAS_SMALL_TABLES 62 #endif 63 64 #if (LIBC_MATH & LIBC_MATH_INTERMEDIATE_COMP_IN_FLOAT) 65 #define LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT 66 #endif 67 68 #if (LIBC_MATH & LIBC_MATH_NO_ERRNO) 69 #define LIBC_MATH_HAS_NO_ERRNO 70 #endif 71 72 #if (LIBC_MATH & LIBC_MATH_NO_EXCEPT) 73 #define LIBC_MATH_HAS_NO_EXCEPT 74 #endif 75 76 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H 77