xref: /aosp_15_r20/external/llvm-libc/src/__support/macros/optimization.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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>
expects_bool_condition(T value,T expected)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 #elif defined(LIBC_COMPILER_IS_GCC)
34 #define LIBC_LOOP_NOUNROLL _Pragma("GCC unroll 0")
35 #else
36 #error "Unhandled compiler"
37 #endif
38 
39 // Defining optimization options for math functions.
40 // TODO: Exporting this to public generated headers?
41 #define LIBC_MATH_SKIP_ACCURATE_PASS 0x01
42 #define LIBC_MATH_SMALL_TABLES 0x02
43 #define LIBC_MATH_NO_ERRNO 0x04
44 #define LIBC_MATH_NO_EXCEPT 0x08
45 #define LIBC_MATH_FAST                                                         \
46   (LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES |                     \
47    LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)
48 
49 #ifndef LIBC_MATH
50 #define LIBC_MATH 0
51 #else
52 
53 #if (LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS)
54 #define LIBC_MATH_HAS_SKIP_ACCURATE_PASS
55 #endif
56 
57 #if (LIBC_MATH & LIBC_MATH_SMALL_TABLES)
58 #define LIBC_MATH_HAS_SMALL_TABLES
59 #endif
60 
61 #endif // LIBC_MATH
62 
63 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H
64