1 //===-- Common internal contructs -------------------------------*- 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 
9 #ifndef LLVM_LIBC_SUPPORT_COMMON_H
10 #define LLVM_LIBC_SUPPORT_COMMON_H
11 
12 #define LIBC_INLINE_ASM __asm__ __volatile__
13 
14 #define likely(x) __builtin_expect(!!(x), 1)
15 #define unlikely(x) __builtin_expect(x, 0)
16 #define UNUSED __attribute__((unused))
17 
18 #ifndef LLVM_LIBC_FUNCTION_ATTR
19 #define LLVM_LIBC_FUNCTION_ATTR
20 #endif
21 
22 #ifdef LLVM_LIBC_PUBLIC_PACKAGING
23 #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
24   LLVM_LIBC_FUNCTION_ATTR decltype(__llvm_libc::name)                          \
25       __##name##_impl__ __asm__(#name);                                        \
26   decltype(__llvm_libc::name) name [[gnu::alias(#name)]];                      \
27   type __##name##_impl__ arglist
28 #else
29 #define LLVM_LIBC_FUNCTION(type, name, arglist) type name arglist
30 #endif
31 
32 namespace __llvm_libc {
33 namespace internal {
34 constexpr bool same_string(char const *lhs, char const *rhs) {
35   for (; *lhs || *rhs; ++lhs, ++rhs)
36     if (*lhs != *rhs)
37       return false;
38   return true;
39 }
40 } // namespace internal
41 } // namespace __llvm_libc
42 
43 // LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
44 // Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
45 //
46 // This works by comparing the stringified version of the macro with and without
47 // evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
48 // is defined, one stringification yields "FOO" while the other yields its
49 // stringified value "1".
50 #define LLVM_LIBC_IS_DEFINED(macro)                                            \
51   !__llvm_libc::internal::same_string(                                         \
52       LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
53 #define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
54 
55 #endif // LLVM_LIBC_SUPPORT_COMMON_H
56