xref: /optee_os/lib/libutils/ext/include/compiler.h (revision 62f21181c547da3bd098908300e5699e9ae5cca9)
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 /*
44  * Override sections flags/type generated by the C compiler to make sure they
45  * are: "a",%progbits (thus creating an allocatable, non-writeable, non-
46  * executable data section).
47  * The trailing '//' comments out the flags generated by the compiler.
48  * This avoids a harmless warning with GCC 8.x.
49  */
50 #define __SECTION_FLAGS_RODATA ",\"a\",%progbits //"
51 #define __rodata	__section(".rodata" __SECTION_FLAGS_RODATA)
52 #define __rodata_unpaged __section(".rodata.__unpaged" __SECTION_FLAGS_RODATA)
53 #define __noprof	__attribute__((no_instrument_function))
54 
55 #define __compiler_bswap64(x)	__builtin_bswap64((x))
56 #define __compiler_bswap32(x)	__builtin_bswap32((x))
57 #define __compiler_bswap16(x)	__builtin_bswap16((x))
58 
59 #define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
60 		       __GNUC_PATCHLEVEL__)
61 
62 #if __GCC_VERSION >= 50100 && !defined(__CHECKER__)
63 #define __HAVE_BUILTIN_OVERFLOW 1
64 #endif
65 
66 #ifdef __HAVE_BUILTIN_OVERFLOW
67 #define __compiler_add_overflow(a, b, res) \
68 	__builtin_add_overflow((a), (b), (res))
69 
70 #define __compiler_sub_overflow(a, b, res) \
71 	__builtin_sub_overflow((a), (b), (res))
72 
73 #define __compiler_mul_overflow(a, b, res) \
74 	__builtin_mul_overflow((a), (b), (res))
75 #else /*!__HAVE_BUILTIN_OVERFLOW*/
76 
77 /*
78  * Copied/inspired from https://www.fefe.de/intof.html
79  */
80 
81 #define __INTOF_ASSIGN(dest, src) (__extension__({ \
82 	typeof(src) __intof_x = (src); \
83 	typeof(dest) __intof_y = __intof_x; \
84 	(((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \
85 	 ((__intof_x < 1) == (__intof_y < 1)) ? \
86 		(void)((dest) = __intof_y) , 0 : 1); \
87 }))
88 
89 #define __INTOF_ADD(c, a, b) (__extension__({ \
90 	typeof(a) __intofa_a = (a); \
91 	typeof(b) __intofa_b = (b); \
92 	intmax_t __intofa_a_signed = __intofa_a; \
93 	uintmax_t __intofa_a_unsigned = __intofa_a; \
94 	intmax_t __intofa_b_signed = __intofa_b; \
95 	uintmax_t __intofa_b_unsigned = __intofa_b; \
96 	\
97 	__intofa_b < 1 ? \
98 		__intofa_a < 1 ? \
99 			((INTMAX_MIN - __intofa_b_signed <= \
100 			  __intofa_a_signed)) ? \
101 				__INTOF_ASSIGN((c), __intofa_a_signed + \
102 						    __intofa_b_signed) : 1 \
103 		: \
104 			((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \
105 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
106 						    __intofa_b_signed) \
107 			: \
108 				__INTOF_ASSIGN((c), \
109 					(intmax_t)(__intofa_a_unsigned + \
110 						   __intofa_b_signed))) \
111 	: \
112 		__intofa_a < 1 ? \
113 			((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \
114 				__INTOF_ASSIGN((c), __intofa_a_signed + \
115 						    __intofa_b_unsigned) \
116 			: \
117 				__INTOF_ASSIGN((c), \
118 					(intmax_t)(__intofa_a_signed + \
119 						   __intofa_b_unsigned))) \
120 		: \
121 			((UINTMAX_MAX - __intofa_b_unsigned >= \
122 			  __intofa_a_unsigned) ? \
123 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
124 						    __intofa_b_unsigned) : 1); \
125 }))
126 
127 #define __INTOF_SUB(c, a, b) (__extension__({ \
128 	typeof(a) __intofs_a = a; \
129 	typeof(b) __intofs_b = b; \
130 	intmax_t __intofs_a_signed = __intofs_a; \
131 	uintmax_t __intofs_a_unsigned = __intofs_a; \
132 	intmax_t __intofs_b_signed = __intofs_b; \
133 	uintmax_t __intofs_b_unsigned = __intofs_b; \
134 	\
135 	__intofs_b < 1 ? \
136 		__intofs_a < 1 ? \
137 			((INTMAX_MAX + __intofs_b >= __intofs_a) ? \
138 				__INTOF_ASSIGN((c), __intofs_a_signed - \
139 						    __intofs_b_signed) : 1) \
140 		: \
141 			(((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \
142 			  __intofs_a_unsigned) ? \
143 				__INTOF_ASSIGN((c), __intofs_a - \
144 						    __intofs_b) : 1) \
145 	: \
146 		__intofs_a < 1 ? \
147 			(((INTMAX_MIN + __intofs_b <= __intofs_a)) ? \
148 				__INTOF_ASSIGN((c), \
149 					(intmax_t)(__intofs_a_signed - \
150 						   __intofs_b_unsigned)) : 1) \
151 		: \
152 			((__intofs_b_unsigned <= __intofs_a_unsigned) ? \
153 				__INTOF_ASSIGN((c), __intofs_a_unsigned - \
154 						    __intofs_b_unsigned) \
155 			: \
156 				__INTOF_ASSIGN((c), \
157 					(intmax_t)(__intofs_a_unsigned - \
158 						   __intofs_b_unsigned))); \
159 }))
160 
161 /*
162  * Dealing with detecting overflow in multiplication of integers.
163  *
164  * First step is to remove two corner cases with the minum signed integer
165  * which can't be represented as a positive integer + sign.
166  * Multiply with 0 or 1 can't overflow, no checking needed of the operation,
167  * only if it can be assigned to the result.
168  *
169  * After the corner cases are eliminated we convert the two factors to
170  * positive unsigned values, keeping track of the original in another
171  * variable which is used at the end to determine the sign of the product.
172  *
173  * The two terms (a and b) are divided into upper and lower half (x1 upper
174  * and x0 lower), so the product is:
175  * ((a1 << hshift) + a0) * ((b1 << hshift) + b0)
176  * which also is:
177  * ((a1 * b1) << (hshift * 2)) +				(T1)
178  * ((a1 * b0 + a0 * b1) << hshift) +				(T2)
179  * (a0 * b0)							(T3)
180  *
181  * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that
182  * is, at least one of a1 or b1 has to be 0. Once this has been checked the
183  * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift)
184  * isn't an addition as one of the terms will be 0.
185  *
186  * Since each factor in: (a0 * b0)
187  * only uses half the capicity of the underlaying type it can't overflow
188  *
189  * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to
190  * perform that addition. If the addition succeeds without overflow the
191  * result is assigned the required sign and checked for overflow again.
192  */
193 
194 #define __intof_mul_negate	((__intof_oa < 1) != (__intof_ob < 1))
195 #define __intof_mul_hshift	(sizeof(uintmax_t) * 8 / 2)
196 #define __intof_mul_hmask	(UINTMAX_MAX >> __intof_mul_hshift)
197 #define __intof_mul_a0		((uintmax_t)(__intof_a) >> __intof_mul_hshift)
198 #define __intof_mul_b0		((uintmax_t)(__intof_b) >> __intof_mul_hshift)
199 #define __intof_mul_a1		((uintmax_t)(__intof_a) & __intof_mul_hmask)
200 #define __intof_mul_b1		((uintmax_t)(__intof_b) & __intof_mul_hmask)
201 #define __intof_mul_t		(__intof_mul_a1 * __intof_mul_b0 + \
202 				 __intof_mul_a0 * __intof_mul_b1)
203 
204 #define __INTOF_MUL(c, a, b) (__extension__({ \
205 	typeof(a) __intof_oa = (a); \
206 	typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \
207 	typeof(b) __intof_ob = (b); \
208 	typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \
209 	typeof(c) __intof_c; \
210 	\
211 	__intof_oa == 0 || __intof_ob == 0 || \
212 	__intof_oa == 1 || __intof_ob == 1 ? \
213 		__INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \
214 	(__intof_mul_a0 && __intof_mul_b0) || \
215 	 __intof_mul_t > __intof_mul_hmask ?  1 : \
216 	__INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \
217 				 __intof_mul_a1 * __intof_mul_b1) ? 1 : \
218 	__intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \
219 			     __INTOF_ASSIGN((c), __intof_c); \
220 }))
221 
222 #define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b))
223 #define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b))
224 #define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b))
225 
226 #endif /*!__HAVE_BUILTIN_OVERFLOW*/
227 
228 #define __compiler_compare_and_swap(p, oval, nval) \
229 	__atomic_compare_exchange_n((p), (oval), (nval), true, \
230 				    __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \
231 
232 #define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED)
233 #define __compiler_atomic_store(p, val) \
234 	__atomic_store_n((p), (val), __ATOMIC_RELAXED)
235 
236 #endif /*COMPILER_H*/
237