summaryrefslogtreecommitdiff
path: root/libc/src/__support/macros/optimization.h
blob: ba4441090cde5a020191774fa23b5c44501d0faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//===-- Portable optimization macros ----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This header file defines portable macros for performance optimization.

#ifndef LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H
#define LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H

#include "src/__support/macros/attributes.h"          // LIBC_INLINE
#include "src/__support/macros/config.h"              // LIBC_HAS_BUILTIN
#include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG

// We use a template to implement likely/unlikely to make sure that we don't
// accidentally pass an integer.
namespace __llvm_libc::details {
template <typename T>
LIBC_INLINE constexpr bool expects_bool_condition(T value, T expected) {
  return __builtin_expect(value, expected);
}
} // namespace __llvm_libc::details
#define LIBC_LIKELY(x) __llvm_libc::details::expects_bool_condition(x, true)
#define LIBC_UNLIKELY(x) __llvm_libc::details::expects_bool_condition(x, false)

#if defined(LIBC_COMPILER_IS_CLANG)
#define LIBC_LOOP_NOUNROLL _Pragma("nounroll")
#elif defined(LIBC_COMPILER_IS_GCC)
#define LIBC_LOOP_NOUNROLL _Pragma("GCC unroll 0")
#else
#error "Unhandled compiler"
#endif

#endif /* LLVM_LIBC_SRC_SUPPORT_MACROS_OPTIMIZATION_H */