1 //===-- int_lib.h - configuration header for compiler-rt -----------------===// 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 // This file is a configuration header for compiler-rt. 10 // This file is not part of the interface of this library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef INT_LIB_H 15 #define INT_LIB_H 16 17 // Assumption: Signed integral is 2's complement. 18 // Assumption: Right shift of signed negative is arithmetic shift. 19 // Assumption: Endianness is little or big (not mixed). 20 21 // ABI macro definitions 22 23 #if __ARM_EABI__ 24 #if defined(COMPILER_RT_ARMHF_TARGET) || (!defined(__clang__) && \ 25 defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 5)) 26 // The pcs attribute was introduced in GCC 4.5.0 27 #define COMPILER_RT_ABI 28 #else 29 #define COMPILER_RT_ABI __attribute__((__pcs__("aapcs"))) 30 #endif 31 #else 32 #define COMPILER_RT_ABI 33 #endif 34 35 #define AEABI_RTABI __attribute__((__pcs__("aapcs"))) 36 37 #if defined(_MSC_VER) && !defined(__clang__) 38 #define ALWAYS_INLINE __forceinline 39 #define NOINLINE __declspec(noinline) 40 #define NORETURN __declspec(noreturn) 41 #define UNUSED 42 #else 43 #define ALWAYS_INLINE __attribute__((always_inline)) 44 #define NOINLINE __attribute__((noinline)) 45 #define NORETURN __attribute__((noreturn)) 46 #define UNUSED __attribute__((unused)) 47 #endif 48 49 #define STR(a) #a 50 #define XSTR(a) STR(a) 51 #define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name 52 53 #if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) || \ 54 defined(_AIX) 55 #define COMPILER_RT_ALIAS(name, aliasname) \ 56 COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name))); 57 #elif defined(__APPLE__) 58 #if defined(VISIBILITY_HIDDEN) 59 #define COMPILER_RT_ALIAS_VISIBILITY(name) \ 60 __asm__(".private_extern " SYMBOL_NAME(name)); 61 #else 62 #define COMPILER_RT_ALIAS_VISIBILITY(name) 63 #endif 64 #define COMPILER_RT_ALIAS(name, aliasname) \ 65 __asm__(".globl " SYMBOL_NAME(aliasname)); \ 66 COMPILER_RT_ALIAS_VISIBILITY(aliasname) \ 67 __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \ 68 COMPILER_RT_ABI __typeof(name) aliasname; 69 #elif defined(_WIN32) 70 #define COMPILER_RT_ALIAS(name, aliasname) 71 #else 72 #error Unsupported target 73 #endif 74 75 #if (defined(__FreeBSD__) || defined(__NetBSD__)) && \ 76 (defined(_KERNEL) || defined(_STANDALONE)) 77 // 78 // Kernel and boot environment can't use normal headers, 79 // so use the equivalent system headers. 80 // NB: FreeBSD (and OpenBSD) deprecate machine/limits.h in 81 // favour of sys/limits.h, so prefer the former, but fall 82 // back on the latter if not available since NetBSD only has 83 // the latter. 84 // 85 #if defined(__has_include) && __has_include(<sys/limits.h>) 86 #include <sys/limits.h> 87 #else 88 #include <machine/limits.h> 89 #endif 90 #include <sys/stdint.h> 91 #include <sys/types.h> 92 #else 93 // Include the standard compiler builtin headers we use functionality from. 94 #include <float.h> 95 #include <limits.h> 96 #include <stdbool.h> 97 #include <stdint.h> 98 #endif 99 100 // Include the commonly used internal type definitions. 101 #include "int_types.h" 102 103 // Include internal utility function declarations. 104 #include "int_util.h" 105 106 COMPILER_RT_ABI int __paritysi2(si_int a); 107 COMPILER_RT_ABI int __paritydi2(di_int a); 108 109 COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b); 110 COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b); 111 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d); 112 113 COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem); 114 COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem); 115 #ifdef CRT_HAS_128BIT 116 COMPILER_RT_ABI int __clzti2(ti_int a); 117 COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem); 118 #endif 119 120 // Definitions for builtins unavailable on MSVC 121 #if defined(_MSC_VER) && !defined(__clang__) 122 #include <intrin.h> 123 124 int __inline __builtin_ctz(uint32_t value) { 125 unsigned long trailing_zero = 0; 126 if (_BitScanForward(&trailing_zero, value)) 127 return trailing_zero; 128 return 32; 129 } 130 131 int __inline __builtin_clz(uint32_t value) { 132 unsigned long leading_zero = 0; 133 if (_BitScanReverse(&leading_zero, value)) 134 return 31 - leading_zero; 135 return 32; 136 } 137 138 #if defined(_M_ARM) || defined(_M_X64) 139 int __inline __builtin_clzll(uint64_t value) { 140 unsigned long leading_zero = 0; 141 if (_BitScanReverse64(&leading_zero, value)) 142 return 63 - leading_zero; 143 return 64; 144 } 145 #else 146 int __inline __builtin_clzll(uint64_t value) { 147 if (value == 0) 148 return 64; 149 uint32_t msh = (uint32_t)(value >> 32); 150 uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF); 151 if (msh != 0) 152 return __builtin_clz(msh); 153 return 32 + __builtin_clz(lsh); 154 } 155 #endif 156 157 #define __builtin_clzl __builtin_clzll 158 159 bool __inline __builtin_sadd_overflow(int x, int y, int *result) { 160 if ((x < 0) != (y < 0)) { 161 *result = x + y; 162 return false; 163 } 164 int tmp = (unsigned int)x + (unsigned int)y; 165 if ((tmp < 0) != (x < 0)) 166 return true; 167 *result = tmp; 168 return false; 169 } 170 171 #endif // defined(_MSC_VER) && !defined(__clang__) 172 173 #endif // INT_LIB_H 174