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 #ifdef COMPILER_RT_ARMHF_TARGET 25 #define COMPILER_RT_ABI 26 #else 27 #define COMPILER_RT_ABI __attribute__((__pcs__("aapcs"))) 28 #endif 29 #else 30 #define COMPILER_RT_ABI 31 #endif 32 33 #define AEABI_RTABI __attribute__((__pcs__("aapcs"))) 34 35 #if defined(_MSC_VER) && !defined(__clang__) 36 #define ALWAYS_INLINE __forceinline 37 #define NOINLINE __declspec(noinline) 38 #define NORETURN __declspec(noreturn) 39 #define UNUSED 40 #else 41 #define ALWAYS_INLINE __attribute__((always_inline)) 42 #define NOINLINE __attribute__((noinline)) 43 #define NORETURN __attribute__((noreturn)) 44 #define UNUSED __attribute__((unused)) 45 #endif 46 47 #define STR(a) #a 48 #define XSTR(a) STR(a) 49 #define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name 50 51 #if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) 52 #define COMPILER_RT_ALIAS(name, aliasname) \ 53 COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name))); 54 #elif defined(__APPLE__) 55 #define COMPILER_RT_ALIAS(name, aliasname) \ 56 __asm__(".globl " SYMBOL_NAME(aliasname)); \ 57 __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \ 58 COMPILER_RT_ABI __typeof(name) aliasname; 59 #elif defined(_WIN32) 60 #define COMPILER_RT_ALIAS(name, aliasname) 61 #else 62 #error Unsupported target 63 #endif 64 65 #if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE)) 66 // 67 // Kernel and boot environment can't use normal headers, 68 // so use the equivalent system headers. 69 // 70 #include <machine/limits.h> 71 #include <sys/stdint.h> 72 #include <sys/types.h> 73 #else 74 // Include the standard compiler builtin headers we use functionality from. 75 #include <float.h> 76 #include <limits.h> 77 #include <stdbool.h> 78 #include <stdint.h> 79 #endif 80 81 // Include the commonly used internal type definitions. 82 #include "int_types.h" 83 84 // Include internal utility function declarations. 85 #include "int_util.h" 86 87 COMPILER_RT_ABI si_int __paritysi2(si_int a); 88 COMPILER_RT_ABI si_int __paritydi2(di_int a); 89 90 COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b); 91 COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b); 92 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d); 93 94 COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem); 95 COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem); 96 #ifdef CRT_HAS_128BIT 97 COMPILER_RT_ABI si_int __clzti2(ti_int a); 98 COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem); 99 #endif 100 101 // Definitions for builtins unavailable on MSVC 102 #if defined(_MSC_VER) && !defined(__clang__) 103 #include <intrin.h> 104 105 uint32_t __inline __builtin_ctz(uint32_t value) { 106 unsigned long trailing_zero = 0; 107 if (_BitScanForward(&trailing_zero, value)) 108 return trailing_zero; 109 return 32; 110 } 111 112 uint32_t __inline __builtin_clz(uint32_t value) { 113 unsigned long leading_zero = 0; 114 if (_BitScanReverse(&leading_zero, value)) 115 return 31 - leading_zero; 116 return 32; 117 } 118 119 #if defined(_M_ARM) || defined(_M_X64) 120 uint32_t __inline __builtin_clzll(uint64_t value) { 121 unsigned long leading_zero = 0; 122 if (_BitScanReverse64(&leading_zero, value)) 123 return 63 - leading_zero; 124 return 64; 125 } 126 #else 127 uint32_t __inline __builtin_clzll(uint64_t value) { 128 if (value == 0) 129 return 64; 130 uint32_t msh = (uint32_t)(value >> 32); 131 uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF); 132 if (msh != 0) 133 return __builtin_clz(msh); 134 return 32 + __builtin_clz(lsh); 135 } 136 #endif 137 138 #define __builtin_clzl __builtin_clzll 139 #endif // defined(_MSC_VER) && !defined(__clang__) 140 141 #endif // INT_LIB_H 142