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/properties/architectures.h"
18
19#ifndef LLVM_LIBC_FUNCTION_ATTR
20#define LLVM_LIBC_FUNCTION_ATTR
21#endif
22
23// MacOS needs to be excluded because it does not support aliasing.
24#if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
25#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
26 LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) \
27 __##name##_impl__ __asm__(#name); \
28 decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \
29 type __##name##_impl__ arglist
30#else
31#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
32#endif
33
34// This extra layer of macro allows `name` to be a macro to rename a function.
35#define LLVM_LIBC_FUNCTION(type, name, arglist) \
36 LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
37
38namespace LIBC_NAMESPACE {
39namespace internal {
40LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
41 for (; *lhs || *rhs; ++lhs, ++rhs)
42 if (*lhs != *rhs)
43 return false;
44 return true;
45}
46} // namespace internal
47} // namespace LIBC_NAMESPACE
48
49#define __LIBC_MACRO_TO_STRING(str) #str
50#define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)
51
52// LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
53// Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
54//
55// This works by comparing the stringified version of the macro with and without
56// evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
57// is defined, one stringification yields "FOO" while the other yields its
58// stringified value "1".
59#define LLVM_LIBC_IS_DEFINED(macro) \
60 !LIBC_NAMESPACE::internal::same_string( \
61 LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
62#define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
63
64#endif // LLVM_LIBC_SRC___SUPPORT_COMMON_H
65

source code of libc/src/__support/common.h