xref: /optee_os/lib/libutils/ext/include/compiler.h (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #ifndef COMPILER_H
7 #define COMPILER_H
8 
9 /*
10  * Macros that should be used instead of using __attribute__ directly to
11  * ease portability and make the code easier to read.
12  *
13  * Some of the defines below is known to sometimes cause conflicts when
14  * this file is included from xtest in normal world. It is assumed that
15  * the conflicting defines has the same meaning in that environment.
16  * Surrounding the troublesome defines with #ifndef should be enough.
17  */
18 #define __deprecated	__attribute__((deprecated))
19 #ifndef __packed
20 #define __packed	__attribute__((packed))
21 #endif
22 #define __weak		__attribute__((weak))
23 #ifndef __noreturn
24 #define __noreturn	__attribute__((noreturn))
25 #endif
26 #define __pure		__attribute__((pure))
27 #define __aligned(x)	__attribute__((aligned(x)))
28 #define __printf(a, b)	__attribute__((format(printf, a, b)))
29 #define __noinline	__attribute__((noinline))
30 #define __attr_const	__attribute__((__const__))
31 #ifndef __unused
32 #define __unused	__attribute__((unused))
33 #endif
34 #define __maybe_unused	__attribute__((unused))
35 #ifndef __used
36 #define __used		__attribute__((__used__))
37 #endif
38 #define __must_check	__attribute__((warn_unused_result))
39 #define __cold		__attribute__((__cold__))
40 #define __section(x)	__attribute__((section(x)))
41 #define __data		__section(".data")
42 #define __bss		__section(".bss")
43 #define __rodata	__section(".rodata")
44 #define __rodata_unpaged __section(".rodata.__unpaged")
45 #define __early_ta	__section(".rodata.early_ta")
46 #define __noprof	__attribute__((no_instrument_function))
47 
48 #define __compiler_bswap64(x)	__builtin_bswap64((x))
49 #define __compiler_bswap32(x)	__builtin_bswap32((x))
50 #define __compiler_bswap16(x)	__builtin_bswap16((x))
51 
52 #define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
53 		       __GNUC_PATCHLEVEL__)
54 
55 #if __GCC_VERSION >= 50100 && !defined(__CHECKER__)
56 #define __HAVE_BUILTIN_OVERFLOW 1
57 #endif
58 
59 #ifdef __HAVE_BUILTIN_OVERFLOW
60 #define __compiler_add_overflow(a, b, res) \
61 	__builtin_add_overflow((a), (b), (res))
62 
63 #define __compiler_sub_overflow(a, b, res) \
64 	__builtin_sub_overflow((a), (b), (res))
65 
66 #define __compiler_mul_overflow(a, b, res) \
67 	__builtin_mul_overflow((a), (b), (res))
68 #else /*!__HAVE_BUILTIN_OVERFLOW*/
69 
70 /*
71  * Copied/inspired from https://www.fefe.de/intof.html
72  */
73 
74 #define __INTOF_ASSIGN(dest, src) (__extension__({ \
75 	typeof(src) __intof_x = (src); \
76 	typeof(dest) __intof_y = __intof_x; \
77 	(((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \
78 	 ((__intof_x < 1) == (__intof_y < 1)) ? \
79 		(void)((dest) = __intof_y) , 0 : 1); \
80 }))
81 
82 #define __INTOF_ADD(c, a, b) (__extension__({ \
83 	typeof(a) __intofa_a = (a); \
84 	typeof(b) __intofa_b = (b); \
85 	intmax_t __intofa_a_signed = __intofa_a; \
86 	uintmax_t __intofa_a_unsigned = __intofa_a; \
87 	intmax_t __intofa_b_signed = __intofa_b; \
88 	uintmax_t __intofa_b_unsigned = __intofa_b; \
89 	\
90 	__intofa_b < 1 ? \
91 		__intofa_a < 1 ? \
92 			((INTMAX_MIN - __intofa_b_signed <= \
93 			  __intofa_a_signed)) ? \
94 				__INTOF_ASSIGN((c), __intofa_a_signed + \
95 						    __intofa_b_signed) : 1 \
96 		: \
97 			((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \
98 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
99 						    __intofa_b_signed) \
100 			: \
101 				__INTOF_ASSIGN((c), \
102 					(intmax_t)(__intofa_a_unsigned + \
103 						   __intofa_b_signed))) \
104 	: \
105 		__intofa_a < 1 ? \
106 			((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \
107 				__INTOF_ASSIGN((c), __intofa_a_signed + \
108 						    __intofa_b_unsigned) \
109 			: \
110 				__INTOF_ASSIGN((c), \
111 					(intmax_t)(__intofa_a_signed + \
112 						   __intofa_b_unsigned))) \
113 		: \
114 			((UINTMAX_MAX - __intofa_b_unsigned >= \
115 			  __intofa_a_unsigned) ? \
116 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
117 						    __intofa_b_unsigned) : 1); \
118 }))
119 
120 #define __INTOF_SUB(c, a, b) (__extension__({ \
121 	typeof(a) __intofs_a = a; \
122 	typeof(b) __intofs_b = b; \
123 	intmax_t __intofs_a_signed = __intofs_a; \
124 	uintmax_t __intofs_a_unsigned = __intofs_a; \
125 	intmax_t __intofs_b_signed = __intofs_b; \
126 	uintmax_t __intofs_b_unsigned = __intofs_b; \
127 	\
128 	__intofs_b < 1 ? \
129 		__intofs_a < 1 ? \
130 			((INTMAX_MAX + __intofs_b >= __intofs_a) ? \
131 				__INTOF_ASSIGN((c), __intofs_a_signed - \
132 						    __intofs_b_signed) : 1) \
133 		: \
134 			(((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \
135 			  __intofs_a_unsigned) ? \
136 				__INTOF_ASSIGN((c), __intofs_a - \
137 						    __intofs_b) : 1) \
138 	: \
139 		__intofs_a < 1 ? \
140 			(((INTMAX_MIN + __intofs_b <= __intofs_a)) ? \
141 				__INTOF_ASSIGN((c), \
142 					(intmax_t)(__intofs_a_signed - \
143 						   __intofs_b_unsigned)) : 1) \
144 		: \
145 			((__intofs_b_unsigned <= __intofs_a_unsigned) ? \
146 				__INTOF_ASSIGN((c), __intofs_a_unsigned - \
147 						    __intofs_b_unsigned) \
148 			: \
149 				__INTOF_ASSIGN((c), \
150 					(intmax_t)(__intofs_a_unsigned - \
151 						   __intofs_b_unsigned))); \
152 }))
153 
154 /*
155  * Dealing with detecting overflow in multiplication of integers.
156  *
157  * First step is to remove two corner cases with the minum signed integer
158  * which can't be represented as a positive integer + sign.
159  * Multiply with 0 or 1 can't overflow, no checking needed of the operation,
160  * only if it can be assigned to the result.
161  *
162  * After the corner cases are eliminated we convert the two factors to
163  * positive unsigned values, keeping track of the original in another
164  * variable which is used at the end to determine the sign of the product.
165  *
166  * The two terms (a and b) are divided into upper and lower half (x1 upper
167  * and x0 lower), so the product is:
168  * ((a1 << hshift) + a0) * ((b1 << hshift) + b0)
169  * which also is:
170  * ((a1 * b1) << (hshift * 2)) +				(T1)
171  * ((a1 * b0 + a0 * b1) << hshift) +				(T2)
172  * (a0 * b0)							(T3)
173  *
174  * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that
175  * is, at least one of a1 or b1 has to be 0. Once this has been checked the
176  * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift)
177  * isn't an addition as one of the terms will be 0.
178  *
179  * Since each factor in: (a0 * b0)
180  * only uses half the capicity of the underlaying type it can't overflow
181  *
182  * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to
183  * perform that addition. If the addition succeeds without overflow the
184  * result is assigned the required sign and checked for overflow again.
185  */
186 
187 #define __intof_mul_negate	((__intof_oa < 1) != (__intof_ob < 1))
188 #define __intof_mul_hshift	(sizeof(uintmax_t) * 8 / 2)
189 #define __intof_mul_hmask	(UINTMAX_MAX >> __intof_mul_hshift)
190 #define __intof_mul_a0		((uintmax_t)(__intof_a) >> __intof_mul_hshift)
191 #define __intof_mul_b0		((uintmax_t)(__intof_b) >> __intof_mul_hshift)
192 #define __intof_mul_a1		((uintmax_t)(__intof_a) & __intof_mul_hmask)
193 #define __intof_mul_b1		((uintmax_t)(__intof_b) & __intof_mul_hmask)
194 #define __intof_mul_t		(__intof_mul_a1 * __intof_mul_b0 + \
195 				 __intof_mul_a0 * __intof_mul_b1)
196 
197 #define __INTOF_MUL(c, a, b) (__extension__({ \
198 	typeof(a) __intof_oa = (a); \
199 	typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \
200 	typeof(b) __intof_ob = (b); \
201 	typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \
202 	typeof(c) __intof_c; \
203 	\
204 	__intof_oa == 0 || __intof_ob == 0 || \
205 	__intof_oa == 1 || __intof_ob == 1 ? \
206 		__INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \
207 	(__intof_mul_a0 && __intof_mul_b0) || \
208 	 __intof_mul_t > __intof_mul_hmask ?  1 : \
209 	__INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \
210 				 __intof_mul_a1 * __intof_mul_b1) ? 1 : \
211 	__intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \
212 			     __INTOF_ASSIGN((c), __intof_c); \
213 }))
214 
215 #define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b))
216 #define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b))
217 #define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b))
218 
219 #endif /*!__HAVE_BUILTIN_OVERFLOW*/
220 
221 #define __compiler_compare_and_swap(p, oval, nval) \
222 	__atomic_compare_exchange_n((p), (oval), (nval), true, \
223 				    __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \
224 
225 #define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED)
226 #define __compiler_atomic_store(p, val) \
227 	__atomic_store_n((p), (val), __ATOMIC_RELAXED)
228 
229 #endif /*COMPILER_H*/
230