[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / __support / common.h
blob5f2b63de2743bb6f44163744675f205d97ddda08
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 //===----------------------------------------------------------------------===//
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
17 #endif
19 // The NVPTX target does not support aliasing.
20 #if defined(LIBC_COPT_PUBLIC_PACKAGING) && defined(LIBC_TARGET_ARCH_IS_NVPTX)
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
32 #else
33 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
34 #endif
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 {
41 namespace internal {
42 constexpr bool same_string(char const *lhs, char const *rhs) {
43 for (; *lhs || *rhs; ++lhs, ++rhs)
44 if (*lhs != *rhs)
45 return false;
46 return true;
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