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