1 //===-- Common internal contructs -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SUPPORT_COMMON_H
10 #define LLVM_LIBC_SUPPORT_COMMON_H
12 #include "src/__support/macros/attributes.h"
13 #include "src/__support/macros/properties/architectures.h"
15 #ifndef LLVM_LIBC_FUNCTION_ATTR
16 #define LLVM_LIBC_FUNCTION_ATTR
19 // GPU targets do not support aliasing.
20 #if defined(LIBC_COPT_PUBLIC_PACKAGING) && defined(LIBC_TARGET_ARCH_IS_GPU)
21 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
22 LLVM_LIBC_FUNCTION_ATTR decltype(__llvm_libc::name) \
23 __##name##_impl__ __asm__(#name); \
24 type __##name##_impl__ arglist
25 // MacOS needs to be excluded because it does not support aliasing.
26 #elif defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
27 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
28 LLVM_LIBC_FUNCTION_ATTR decltype(__llvm_libc::name) \
29 __##name##_impl__ __asm__(#name); \
30 decltype(__llvm_libc::name) name [[gnu::alias(#name)]]; \
31 type __##name##_impl__ arglist
33 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
36 // This extra layer of macro allows `name` to be a macro to rename a function.
37 #define LLVM_LIBC_FUNCTION(type, name, arglist) \
38 LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
40 namespace __llvm_libc
{
42 constexpr bool same_string(char const *lhs
, char const *rhs
) {
43 for (; *lhs
|| *rhs
; ++lhs
, ++rhs
)
48 } // namespace internal
49 } // namespace __llvm_libc
51 // LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
52 // Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
54 // This works by comparing the stringified version of the macro with and without
55 // evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
56 // is defined, one stringification yields "FOO" while the other yields its
57 // stringified value "1".
58 #define LLVM_LIBC_IS_DEFINED(macro) \
59 !__llvm_libc::internal::same_string( \
60 LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
61 #define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
63 #endif // LLVM_LIBC_SUPPORT_COMMON_H