xref: /aosp_15_r20/external/llvm-libc/src/__support/common.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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_SRC___SUPPORT_COMMON_H
10 #define LLVM_LIBC_SRC___SUPPORT_COMMON_H
11 
12 #ifndef LIBC_NAMESPACE
13 #error "LIBC_NAMESPACE macro is not defined."
14 #endif
15 
16 #include "src/__support/macros/attributes.h"
17 #include "src/__support/macros/config.h"
18 #include "src/__support/macros/properties/architectures.h"
19 
20 #ifndef LLVM_LIBC_FUNCTION_ATTR
21 #define LLVM_LIBC_FUNCTION_ATTR
22 #endif
23 
24 // clang-format off
25 // Allow each function `func` to have extra attributes specified by defining:
26 // `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
27 // "LLVM_LIBC_EMPTY, "
28 //
29 // For examples:
30 // #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]
31 // #define LLVM_LIBC_FUNCTION_ATTR_memchr LLVM_LIBC_EMPTY, [[gnu::weak]] [[gnu::visibility("default")]]
32 // clang-format on
33 #define LLVM_LIBC_EMPTY
34 
35 #define GET_SECOND(first, second, ...) second
36 #define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY)
37 
38 #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
39 
40 // MacOS needs to be excluded because it does not support aliasing.
41 #if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
42 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
43   LLVM_LIBC_ATTR(name)                                                         \
44   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
45       __##name##_impl__ __asm__(#name);                                        \
46   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
47   type __##name##_impl__ arglist
48 #else
49 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
50 #endif
51 
52 // This extra layer of macro allows `name` to be a macro to rename a function.
53 #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
54   LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
55 
56 namespace LIBC_NAMESPACE_DECL {
57 namespace internal {
same_string(char const * lhs,char const * rhs)58 LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
59   for (; *lhs || *rhs; ++lhs, ++rhs)
60     if (*lhs != *rhs)
61       return false;
62   return true;
63 }
64 } // namespace internal
65 } // namespace LIBC_NAMESPACE_DECL
66 
67 #define __LIBC_MACRO_TO_STRING(str) #str
68 #define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)
69 
70 // LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
71 // Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
72 //
73 // This works by comparing the stringified version of the macro with and without
74 // evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
75 // is defined, one stringification yields "FOO" while the other yields its
76 // stringified value "1".
77 #define LLVM_LIBC_IS_DEFINED(macro)                                            \
78   !LIBC_NAMESPACE::internal::same_string(                                      \
79       LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
80 #define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
81 
82 #endif // LLVM_LIBC_SRC___SUPPORT_COMMON_H
83