80 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8// This header file defines portable macros for performance optimization.9 10#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H11#define LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H12 13#include "src/__support/macros/attributes.h" // LIBC_INLINE14#include "src/__support/macros/config.h"15#include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG16 17// We use a template to implement likely/unlikely to make sure that we don't18// accidentally pass an integer.19namespace LIBC_NAMESPACE_DECL {20namespace details {21template <typename T>22LIBC_INLINE constexpr bool expects_bool_condition(T value, T expected) {23 return __builtin_expect(value, expected);24}25} // namespace details26} // namespace LIBC_NAMESPACE_DECL27#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#elif defined(LIBC_COMPILER_IS_MSVC)38#define LIBC_LOOP_NOUNROLL39#define LIBC_LOOP_UNROLL40#else41#error "Unhandled compiler"42#endif43 44// Defining optimization options for math functions.45// TODO: Exporting this to public generated headers?46#define LIBC_MATH_SKIP_ACCURATE_PASS 0x0147#define LIBC_MATH_SMALL_TABLES 0x0248#define LIBC_MATH_NO_ERRNO 0x0449#define LIBC_MATH_NO_EXCEPT 0x0850#define LIBC_MATH_FAST \51 (LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | \52 LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)53#define LIBC_MATH_INTERMEDIATE_COMP_IN_FLOAT 0x1054 55#ifndef LIBC_MATH56#define LIBC_MATH 057#endif // LIBC_MATH58 59#if (LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS)60#define LIBC_MATH_HAS_SKIP_ACCURATE_PASS61#endif62 63#if (LIBC_MATH & LIBC_MATH_SMALL_TABLES)64#define LIBC_MATH_HAS_SMALL_TABLES65#endif66 67#if (LIBC_MATH & LIBC_MATH_INTERMEDIATE_COMP_IN_FLOAT)68#define LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT69#endif70 71#if (LIBC_MATH & LIBC_MATH_NO_ERRNO)72#define LIBC_MATH_HAS_NO_ERRNO73#endif74 75#if (LIBC_MATH & LIBC_MATH_NO_EXCEPT)76#define LIBC_MATH_HAS_NO_EXCEPT77#endif78 79#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H80