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