blob: 075530b0e0d0019448367b266e7865b61576d7db [file] [log] [blame]
Imre Kis3b32e7b2020-11-24 00:33:01 +01001/* SPDX-License-Identifier: BSD-2-Clause */
2/*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
Imre Kisf6d55a62021-10-18 11:45:31 +02004 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
Imre Kis3b32e7b2020-11-24 00:33:01 +01005 */
6
7#ifndef COMPILER_H
8#define COMPILER_H
9
Gyorgy Szing8fad9bc2021-12-12 03:56:41 +010010/* Some standard library implementations define some macros defined in this
11 * file without protection against redefinition. Depending on inclusion order
12 * through other file this results in compiler warnings being triggered.
13 * Including cdefs.h here makes the standard library implementation the
14 * definitive owner and thus solves the problem.
15 */
16#ifdef ENABLE_CDEFSH_FIX
Imre Kisf6d55a62021-10-18 11:45:31 +020017#include <sys/cdefs.h>
Gyorgy Szing8fad9bc2021-12-12 03:56:41 +010018#endif
Imre Kisf6d55a62021-10-18 11:45:31 +020019
Imre Kis3b32e7b2020-11-24 00:33:01 +010020/*
21 * Macros that should be used instead of using __attribute__ directly to
22 * ease portability and make the code easier to read.
23 *
24 * Some of the defines below is known to sometimes cause conflicts when
25 * this file is included from xtest in normal world. It is assumed that
26 * the conflicting defines has the same meaning in that environment.
27 * Surrounding the troublesome defines with #ifndef should be enough.
28 */
Imre Kisf6d55a62021-10-18 11:45:31 +020029#ifndef __deprecated
Imre Kis3b32e7b2020-11-24 00:33:01 +010030#define __deprecated __attribute__((deprecated))
Imre Kisf6d55a62021-10-18 11:45:31 +020031#endif
Imre Kis3b32e7b2020-11-24 00:33:01 +010032#ifndef __packed
33#define __packed __attribute__((packed))
34#endif
Imre Kisf6d55a62021-10-18 11:45:31 +020035#ifndef __weak
Imre Kis3b32e7b2020-11-24 00:33:01 +010036#define __weak __attribute__((weak))
Imre Kisf6d55a62021-10-18 11:45:31 +020037#endif
Imre Kis3b32e7b2020-11-24 00:33:01 +010038#ifndef __noreturn
39#define __noreturn __attribute__((__noreturn__))
40#endif
Imre Kisf6d55a62021-10-18 11:45:31 +020041#ifndef __pure
Imre Kis3b32e7b2020-11-24 00:33:01 +010042#define __pure __attribute__((pure))
Imre Kisf6d55a62021-10-18 11:45:31 +020043#endif
44#ifndef __aligned
Imre Kis3b32e7b2020-11-24 00:33:01 +010045#define __aligned(x) __attribute__((aligned(x)))
Imre Kisf6d55a62021-10-18 11:45:31 +020046#endif
47#ifndef __printf
Imre Kis3b32e7b2020-11-24 00:33:01 +010048#define __printf(a, b) __attribute__((format(printf, a, b)))
Imre Kisf6d55a62021-10-18 11:45:31 +020049#endif
50#ifndef __noinline
Imre Kis3b32e7b2020-11-24 00:33:01 +010051#define __noinline __attribute__((noinline))
Imre Kisf6d55a62021-10-18 11:45:31 +020052#endif
53#ifndef __attr_const
Imre Kis3b32e7b2020-11-24 00:33:01 +010054#define __attr_const __attribute__((__const__))
Imre Kisf6d55a62021-10-18 11:45:31 +020055#endif
Imre Kis3b32e7b2020-11-24 00:33:01 +010056#ifndef __unused
57#define __unused __attribute__((unused))
58#endif
59#define __maybe_unused __attribute__((unused))
60#ifndef __used
61#define __used __attribute__((__used__))
62#endif
Imre Kisf6d55a62021-10-18 11:45:31 +020063#ifndef __must_check
Imre Kis3b32e7b2020-11-24 00:33:01 +010064#define __must_check __attribute__((warn_unused_result))
Imre Kisf6d55a62021-10-18 11:45:31 +020065#endif
66#ifndef __cold
Imre Kis3b32e7b2020-11-24 00:33:01 +010067#define __cold __attribute__((__cold__))
Imre Kisf6d55a62021-10-18 11:45:31 +020068#endif
69#ifndef __section
Imre Kis3b32e7b2020-11-24 00:33:01 +010070#define __section(x) __attribute__((section(x)))
Imre Kisf6d55a62021-10-18 11:45:31 +020071#endif
Imre Kis3b32e7b2020-11-24 00:33:01 +010072#define __data __section(".data")
73#define __bss __section(".bss")
74#ifdef __clang__
75#define __SECTION_FLAGS_RODATA
76#else
77/*
78 * Override sections flags/type generated by the C compiler to make sure they
79 * are: "a",%progbits (thus creating an allocatable, non-writeable, non-
80 * executable data section).
81 * The trailing '//' comments out the flags generated by the compiler.
82 * This avoids a harmless warning with GCC.
83 */
84#define __SECTION_FLAGS_RODATA ",\"a\",%progbits //"
85#endif
86#define __rodata __section(".rodata" __SECTION_FLAGS_RODATA)
87#define __rodata_unpaged __section(".rodata.__unpaged" __SECTION_FLAGS_RODATA)
88#ifdef CFG_VIRTUALIZATION
89#define __nex_bss __section(".nex_bss")
90#define __nex_data __section(".nex_data")
91#else /* CFG_VIRTUALIZATION */
92#define __nex_bss
93#define __nex_data
94#endif /* CFG_VIRTUALIZATION */
95#define __noprof __attribute__((no_instrument_function))
96#define __nostackcheck __attribute__((no_instrument_function))
97
98#define __compiler_bswap64(x) __builtin_bswap64((x))
99#define __compiler_bswap32(x) __builtin_bswap32((x))
100#define __compiler_bswap16(x) __builtin_bswap16((x))
101
102#define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
103 __GNUC_PATCHLEVEL__)
104
105#if __GCC_VERSION >= 50100 && !defined(__CHECKER__)
106#define __HAVE_BUILTIN_OVERFLOW 1
107#endif
108
109#ifdef __HAVE_BUILTIN_OVERFLOW
110#define __compiler_add_overflow(a, b, res) \
111 __builtin_add_overflow((a), (b), (res))
112
113#define __compiler_sub_overflow(a, b, res) \
114 __builtin_sub_overflow((a), (b), (res))
115
116#define __compiler_mul_overflow(a, b, res) \
117 __builtin_mul_overflow((a), (b), (res))
118#else /*!__HAVE_BUILTIN_OVERFLOW*/
119
120/*
121 * Copied/inspired from https://www.fefe.de/intof.html
122 */
123
124#define __INTOF_ASSIGN(dest, src) (__extension__({ \
125 typeof(src) __intof_x = (src); \
126 typeof(dest) __intof_y = __intof_x; \
127 (((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \
128 ((__intof_x < 1) == (__intof_y < 1)) ? \
129 (void)((dest) = __intof_y) , 0 : 1); \
130}))
131
132#define __INTOF_ADD(c, a, b) (__extension__({ \
133 typeof(a) __intofa_a = (a); \
134 typeof(b) __intofa_b = (b); \
135 intmax_t __intofa_a_signed = __intofa_a; \
136 uintmax_t __intofa_a_unsigned = __intofa_a; \
137 intmax_t __intofa_b_signed = __intofa_b; \
138 uintmax_t __intofa_b_unsigned = __intofa_b; \
139 \
140 __intofa_b < 1 ? \
141 __intofa_a < 1 ? \
142 ((INTMAX_MIN - __intofa_b_signed <= \
143 __intofa_a_signed)) ? \
144 __INTOF_ASSIGN((c), __intofa_a_signed + \
145 __intofa_b_signed) : 1 \
146 : \
147 ((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \
148 __INTOF_ASSIGN((c), __intofa_a_unsigned + \
149 __intofa_b_signed) \
150 : \
151 __INTOF_ASSIGN((c), \
152 (intmax_t)(__intofa_a_unsigned + \
153 __intofa_b_signed))) \
154 : \
155 __intofa_a < 1 ? \
156 ((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \
157 __INTOF_ASSIGN((c), __intofa_a_signed + \
158 __intofa_b_unsigned) \
159 : \
160 __INTOF_ASSIGN((c), \
161 (intmax_t)(__intofa_a_signed + \
162 __intofa_b_unsigned))) \
163 : \
164 ((UINTMAX_MAX - __intofa_b_unsigned >= \
165 __intofa_a_unsigned) ? \
166 __INTOF_ASSIGN((c), __intofa_a_unsigned + \
167 __intofa_b_unsigned) : 1); \
168}))
169
170#define __INTOF_SUB(c, a, b) (__extension__({ \
171 typeof(a) __intofs_a = a; \
172 typeof(b) __intofs_b = b; \
173 intmax_t __intofs_a_signed = __intofs_a; \
174 uintmax_t __intofs_a_unsigned = __intofs_a; \
175 intmax_t __intofs_b_signed = __intofs_b; \
176 uintmax_t __intofs_b_unsigned = __intofs_b; \
177 \
178 __intofs_b < 1 ? \
179 __intofs_a < 1 ? \
180 ((INTMAX_MAX + __intofs_b_signed >= \
181 __intofs_a_signed) ? \
182 __INTOF_ASSIGN((c), __intofs_a_signed - \
183 __intofs_b_signed) : 1) \
184 : \
185 (((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \
186 __intofs_a_unsigned) ? \
187 __INTOF_ASSIGN((c), __intofs_a - \
188 __intofs_b) : 1) \
189 : \
190 __intofs_a < 1 ? \
191 (((intmax_t)(INTMAX_MIN + __intofs_b) <= \
192 __intofs_a_signed) ? \
193 __INTOF_ASSIGN((c), \
194 (intmax_t)(__intofs_a_signed - \
195 __intofs_b_unsigned)) : 1) \
196 : \
197 ((__intofs_b_unsigned <= __intofs_a_unsigned) ? \
198 __INTOF_ASSIGN((c), __intofs_a_unsigned - \
199 __intofs_b_unsigned) \
200 : \
201 __INTOF_ASSIGN((c), \
202 (intmax_t)(__intofs_a_unsigned - \
203 __intofs_b_unsigned))); \
204}))
205
206/*
207 * Dealing with detecting overflow in multiplication of integers.
208 *
209 * First step is to remove two corner cases with the minum signed integer
210 * which can't be represented as a positive integer + sign.
211 * Multiply with 0 or 1 can't overflow, no checking needed of the operation,
212 * only if it can be assigned to the result.
213 *
214 * After the corner cases are eliminated we convert the two factors to
215 * positive unsigned values, keeping track of the original in another
216 * variable which is used at the end to determine the sign of the product.
217 *
218 * The two terms (a and b) are divided into upper and lower half (x1 upper
219 * and x0 lower), so the product is:
220 * ((a1 << hshift) + a0) * ((b1 << hshift) + b0)
221 * which also is:
222 * ((a1 * b1) << (hshift * 2)) + (T1)
223 * ((a1 * b0 + a0 * b1) << hshift) + (T2)
224 * (a0 * b0) (T3)
225 *
226 * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that
227 * is, at least one of a1 or b1 has to be 0. Once this has been checked the
228 * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift)
229 * isn't an addition as one of the terms will be 0.
230 *
231 * Since each factor in: (a0 * b0)
232 * only uses half the capicity of the underlaying type it can't overflow
233 *
234 * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to
235 * perform that addition. If the addition succeeds without overflow the
236 * result is assigned the required sign and checked for overflow again.
237 */
238
239#define __intof_mul_negate ((__intof_oa < 1) != (__intof_ob < 1))
240#define __intof_mul_hshift (sizeof(uintmax_t) * 8 / 2)
241#define __intof_mul_hmask (UINTMAX_MAX >> __intof_mul_hshift)
242#define __intof_mul_a0 ((uintmax_t)(__intof_a) >> __intof_mul_hshift)
243#define __intof_mul_b0 ((uintmax_t)(__intof_b) >> __intof_mul_hshift)
244#define __intof_mul_a1 ((uintmax_t)(__intof_a) & __intof_mul_hmask)
245#define __intof_mul_b1 ((uintmax_t)(__intof_b) & __intof_mul_hmask)
246#define __intof_mul_t (__intof_mul_a1 * __intof_mul_b0 + \
247 __intof_mul_a0 * __intof_mul_b1)
248
249#define __INTOF_MUL(c, a, b) (__extension__({ \
250 typeof(a) __intof_oa = (a); \
251 typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \
252 typeof(b) __intof_ob = (b); \
253 typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \
254 typeof(c) __intof_c; \
255 \
256 __intof_oa == 0 || __intof_ob == 0 || \
257 __intof_oa == 1 || __intof_ob == 1 ? \
258 __INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \
259 (__intof_mul_a0 && __intof_mul_b0) || \
260 __intof_mul_t > __intof_mul_hmask ? 1 : \
261 __INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \
262 __intof_mul_a1 * __intof_mul_b1) ? 1 : \
263 __intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \
264 __INTOF_ASSIGN((c), __intof_c); \
265}))
266
267#define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b))
268#define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b))
269#define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b))
270
271#endif /*!__HAVE_BUILTIN_OVERFLOW*/
272
273#define __compiler_compare_and_swap(p, oval, nval) \
274 __atomic_compare_exchange_n((p), (oval), (nval), true, \
275 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \
276
277#define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED)
278#define __compiler_atomic_store(p, val) \
279 __atomic_store_n((p), (val), __ATOMIC_RELAXED)
280
281#endif /*COMPILER_H*/