xref: /rk3399_ARM-atf/lib/compiler-rt/builtins/int_lib.h (revision 6d4f6aea2cd96a4a57ffa2d88b9230e2cab88f28)
1 /* ===-- int_lib.h - configuration header for compiler-rt  -----------------===
2  *
3  *                     The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9  *
10  * This file is a configuration header for compiler-rt.
11  * This file is not part of the interface of this library.
12  *
13  * ===----------------------------------------------------------------------===
14  */
15 
16 /*
17  * Portions copyright (c) 2017-2018, ARM Limited and Contributors.
18  * All rights reserved.
19  */
20 
21 #ifndef INT_LIB_H
22 #define INT_LIB_H
23 
24 /* Assumption: Signed integral is 2's complement. */
25 /* Assumption: Right shift of signed negative is arithmetic shift. */
26 /* Assumption: Endianness is little or big (not mixed). */
27 
28 #if defined(__ELF__)
29 #define FNALIAS(alias_name, original_name) \
30   void alias_name() __attribute__((alias(#original_name)))
31 #else
32 #define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")")
33 #endif
34 
35 /* ABI macro definitions */
36 
37 #if __ARM_EABI__
38 # define ARM_EABI_FNALIAS(aeabi_name, name)         \
39   void __aeabi_##aeabi_name() __attribute__((alias("__" #name)));
40 # ifdef COMPILER_RT_ARMHF_TARGET
41 #   define COMPILER_RT_ABI
42 # else
43 #   define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
44 # endif
45 #else
46 # define ARM_EABI_FNALIAS(aeabi_name, name)
47 # define COMPILER_RT_ABI
48 #endif
49 
50 #ifdef _MSC_VER
51 #define ALWAYS_INLINE __forceinline
52 #define NOINLINE __declspec(noinline)
53 #define NORETURN __declspec(noreturn)
54 #define UNUSED
55 #else
56 #define ALWAYS_INLINE __attribute__((always_inline))
57 #define NOINLINE __attribute__((noinline))
58 #define NORETURN __attribute__((noreturn))
59 #define UNUSED __attribute__((unused))
60 #endif
61 
62 /*
63  * Kernel and boot environment can't use normal headers,
64  * so use the equivalent system headers.
65  */
66 #  include <limits.h>
67 #  include <stdint.h>
68 
69 /* Include the commonly used internal type definitions. */
70 #include "int_types.h"
71 
72 COMPILER_RT_ABI si_int __paritysi2(si_int a);
73 COMPILER_RT_ABI si_int __paritydi2(di_int a);
74 
75 COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
76 COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
77 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
78 
79 COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
80 COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
81 #ifdef CRT_HAS_128BIT
82 COMPILER_RT_ABI si_int __clzti2(ti_int a);
83 COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
84 #endif
85 
86 /* Definitions for builtins unavailable on MSVC */
87 #if defined(_MSC_VER) && !defined(__clang__)
88 #include <intrin.h>
89 
90 uint32_t __inline __builtin_ctz(uint32_t value) {
91   unsigned long trailing_zero = 0;
92   if (_BitScanForward(&trailing_zero, value))
93     return trailing_zero;
94   return 32;
95 }
96 
97 uint32_t __inline __builtin_clz(uint32_t value) {
98   unsigned long leading_zero = 0;
99   if (_BitScanReverse(&leading_zero, value))
100     return 31 - leading_zero;
101   return 32;
102 }
103 
104 #if defined(_M_ARM) || defined(_M_X64)
105 uint32_t __inline __builtin_clzll(uint64_t value) {
106   unsigned long leading_zero = 0;
107   if (_BitScanReverse64(&leading_zero, value))
108     return 63 - leading_zero;
109   return 64;
110 }
111 #else
112 uint32_t __inline __builtin_clzll(uint64_t value) {
113   if (value == 0)
114     return 64;
115   uint32_t msh = (uint32_t)(value >> 32);
116   uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
117   if (msh != 0)
118     return __builtin_clz(msh);
119   return 32 + __builtin_clz(lsh);
120 }
121 #endif
122 
123 #define __builtin_clzl __builtin_clzll
124 #endif /* defined(_MSC_VER) && !defined(__clang__) */
125 
126 #endif /* INT_LIB_H */
127