]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/builtins/int_lib.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / builtins / int_lib.h
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 #define COMPILER_RT_ALIAS(name, aliasname) \
55   COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name)));
56 #elif defined(__APPLE__)
57 #define COMPILER_RT_ALIAS(name, aliasname) \
58   __asm__(".globl " SYMBOL_NAME(aliasname)); \
59   __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \
60   COMPILER_RT_ABI __typeof(name) aliasname;
61 #elif defined(_WIN32)
62 #define COMPILER_RT_ALIAS(name, aliasname)
63 #else
64 #error Unsupported target
65 #endif
66
67 #if (defined(__FreeBSD__) || defined(__NetBSD__)) && (defined(_KERNEL) || defined(_STANDALONE))
68 //
69 // Kernel and boot environment can't use normal headers,
70 // so use the equivalent system headers.
71 //
72 #ifdef __FreeBSD__
73 #include <sys/limits.h>
74 #else
75 #include <machine/limits.h>
76 #endif
77 #include <sys/stdint.h>
78 #include <sys/types.h>
79 #else
80 // Include the standard compiler builtin headers we use functionality from.
81 #include <float.h>
82 #include <limits.h>
83 #include <stdbool.h>
84 #include <stdint.h>
85 #endif
86
87 // Include the commonly used internal type definitions.
88 #include "int_types.h"
89
90 // Include internal utility function declarations.
91 #include "int_util.h"
92
93 /*
94  * Workaround for LLVM bug 11663.  Prevent endless recursion in
95  * __c?zdi2(), where calls to __builtin_c?z() are expanded to
96  * __c?zdi2() instead of __c?zsi2().
97  *
98  * Instead of placing this workaround in c?zdi2.c, put it in this
99  * global header to prevent other C files from making the detour
100  * through __c?zdi2() as well.
101  *
102  * This problem has been observed on FreeBSD for sparc64 and
103  * mips64 with GCC 4.2.1, and for riscv with GCC 5.2.0.
104  * Presumably it's any version of GCC, and targeting an arch that
105  * does not have dedicated bit counting instructions.
106  */
107 #if defined(__FreeBSD__) && (defined(__sparc64__) || \
108     defined(__mips_n32) || defined(__mips_n64) || defined(__mips_o64) || \
109     defined(__riscv))
110 si_int __clzsi2(si_int);
111 si_int __ctzsi2(si_int);
112 #define __builtin_clz __clzsi2
113 #define __builtin_ctz __ctzsi2
114 #endif /* FreeBSD && (sparc64 || mips_n32 || mips_n64 || mips_o64 || riscv) */
115
116 COMPILER_RT_ABI si_int __paritysi2(si_int a);
117 COMPILER_RT_ABI si_int __paritydi2(di_int a);
118
119 COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
120 COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
121 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
122
123 COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem);
124 COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem);
125 #ifdef CRT_HAS_128BIT
126 COMPILER_RT_ABI si_int __clzti2(ti_int a);
127 COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem);
128 #endif
129
130 // Definitions for builtins unavailable on MSVC
131 #if defined(_MSC_VER) && !defined(__clang__)
132 #include <intrin.h>
133
134 uint32_t __inline __builtin_ctz(uint32_t value) {
135   unsigned long trailing_zero = 0;
136   if (_BitScanForward(&trailing_zero, value))
137     return trailing_zero;
138   return 32;
139 }
140
141 uint32_t __inline __builtin_clz(uint32_t value) {
142   unsigned long leading_zero = 0;
143   if (_BitScanReverse(&leading_zero, value))
144     return 31 - leading_zero;
145   return 32;
146 }
147
148 #if defined(_M_ARM) || defined(_M_X64)
149 uint32_t __inline __builtin_clzll(uint64_t value) {
150   unsigned long leading_zero = 0;
151   if (_BitScanReverse64(&leading_zero, value))
152     return 63 - leading_zero;
153   return 64;
154 }
155 #else
156 uint32_t __inline __builtin_clzll(uint64_t value) {
157   if (value == 0)
158     return 64;
159   uint32_t msh = (uint32_t)(value >> 32);
160   uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
161   if (msh != 0)
162     return __builtin_clz(msh);
163   return 32 + __builtin_clz(lsh);
164 }
165 #endif
166
167 #define __builtin_clzl __builtin_clzll
168 #endif // defined(_MSC_VER) && !defined(__clang__)
169
170 #endif // INT_LIB_H