1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2021-2024, Arm Limited and Contributors. All rights reserved. 5 */ 6 7 #ifndef COMPILER_H 8 #define COMPILER_H 9 10 /* 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 17 #include <cdefs.h> 18 #endif 19 20 /* 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 */ 29 #ifndef __deprecated 30 #define __deprecated __attribute__((deprecated)) 31 #endif 32 #ifndef __packed 33 #define __packed __attribute__((packed)) 34 #endif 35 #ifndef __weak 36 #define __weak __attribute__((weak)) 37 #endif 38 #ifndef __noreturn 39 #define __noreturn __attribute__((__noreturn__)) 40 #endif 41 #ifndef __pure 42 #define __pure __attribute__((pure)) 43 #endif 44 #ifndef __aligned 45 #define __aligned(x) __attribute__((aligned(x))) 46 #endif 47 #ifndef __printf 48 #define __printf(a, b) __attribute__((format(printf, a, b))) 49 #endif 50 #ifndef __noinline 51 #define __noinline __attribute__((noinline)) 52 #endif 53 #ifndef __attr_const 54 #define __attr_const __attribute__((__const__)) 55 #endif 56 #ifndef __unused 57 #define __unused __attribute__((unused)) 58 #endif 59 #ifndef __maybe_unused 60 #define __maybe_unused __attribute__((unused)) 61 #endif 62 #ifndef __used 63 #define __used __attribute__((__used__)) 64 #endif 65 #ifndef __must_check 66 #define __must_check __attribute__((warn_unused_result)) 67 #endif 68 #ifndef __cold 69 #define __cold __attribute__((__cold__)) 70 #endif 71 #ifndef __section 72 #define __section(x) __attribute__((section(x))) 73 #endif 74 #define __data __section(".data") 75 #define __bss __section(".bss") 76 #ifdef __clang__ 77 #define __SECTION_FLAGS_RODATA 78 #else 79 /* 80 * Override sections flags/type generated by the C compiler to make sure they 81 * are: "a",%progbits (thus creating an allocatable, non-writeable, non- 82 * executable data section). 83 * The trailing '//' comments out the flags generated by the compiler. 84 * This avoids a harmless warning with GCC. 85 */ 86 #define __SECTION_FLAGS_RODATA ",\"a\",%progbits //" 87 #endif 88 #define __rodata __section(".rodata" __SECTION_FLAGS_RODATA) 89 #define __rodata_unpaged __section(".rodata.__unpaged" __SECTION_FLAGS_RODATA) 90 #ifdef CFG_VIRTUALIZATION 91 #define __nex_bss __section(".nex_bss") 92 #define __nex_data __section(".nex_data") 93 #else /* CFG_VIRTUALIZATION */ 94 #define __nex_bss 95 #define __nex_data 96 #endif /* CFG_VIRTUALIZATION */ 97 #define __noprof __attribute__((no_instrument_function)) 98 #define __nostackcheck __attribute__((no_instrument_function)) 99 100 #define __compiler_bswap64(x) __builtin_bswap64((x)) 101 #define __compiler_bswap32(x) __builtin_bswap32((x)) 102 #define __compiler_bswap16(x) __builtin_bswap16((x)) 103 104 #define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \ 105 __GNUC_PATCHLEVEL__) 106 107 #if __GCC_VERSION >= 50100 && !defined(__CHECKER__) 108 #define __HAVE_BUILTIN_OVERFLOW 1 109 #endif 110 111 #ifdef __HAVE_BUILTIN_OVERFLOW 112 #define __compiler_add_overflow(a, b, res) \ 113 __builtin_add_overflow((a), (b), (res)) 114 115 #define __compiler_sub_overflow(a, b, res) \ 116 __builtin_sub_overflow((a), (b), (res)) 117 118 #define __compiler_mul_overflow(a, b, res) \ 119 __builtin_mul_overflow((a), (b), (res)) 120 #else /*!__HAVE_BUILTIN_OVERFLOW*/ 121 122 /* 123 * Copied/inspired from https://www.fefe.de/intof.html 124 */ 125 126 #define __INTOF_ASSIGN(dest, src) (__extension__({ \ 127 typeof(src) __intof_x = (src); \ 128 typeof(dest) __intof_y = __intof_x; \ 129 (((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \ 130 ((__intof_x < 1) == (__intof_y < 1)) ? \ 131 (void)((dest) = __intof_y) , 0 : 1); \ 132 })) 133 134 #define __INTOF_ADD(c, a, b) (__extension__({ \ 135 typeof(a) __intofa_a = (a); \ 136 typeof(b) __intofa_b = (b); \ 137 intmax_t __intofa_a_signed = __intofa_a; \ 138 uintmax_t __intofa_a_unsigned = __intofa_a; \ 139 intmax_t __intofa_b_signed = __intofa_b; \ 140 uintmax_t __intofa_b_unsigned = __intofa_b; \ 141 \ 142 __intofa_b < 1 ? \ 143 __intofa_a < 1 ? \ 144 ((INTMAX_MIN - __intofa_b_signed <= \ 145 __intofa_a_signed)) ? \ 146 __INTOF_ASSIGN((c), __intofa_a_signed + \ 147 __intofa_b_signed) : 1 \ 148 : \ 149 ((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \ 150 __INTOF_ASSIGN((c), __intofa_a_unsigned + \ 151 __intofa_b_signed) \ 152 : \ 153 __INTOF_ASSIGN((c), \ 154 (intmax_t)(__intofa_a_unsigned + \ 155 __intofa_b_signed))) \ 156 : \ 157 __intofa_a < 1 ? \ 158 ((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \ 159 __INTOF_ASSIGN((c), __intofa_a_signed + \ 160 __intofa_b_unsigned) \ 161 : \ 162 __INTOF_ASSIGN((c), \ 163 (intmax_t)(__intofa_a_signed + \ 164 __intofa_b_unsigned))) \ 165 : \ 166 ((UINTMAX_MAX - __intofa_b_unsigned >= \ 167 __intofa_a_unsigned) ? \ 168 __INTOF_ASSIGN((c), __intofa_a_unsigned + \ 169 __intofa_b_unsigned) : 1); \ 170 })) 171 172 #define __INTOF_SUB(c, a, b) (__extension__({ \ 173 typeof(a) __intofs_a = a; \ 174 typeof(b) __intofs_b = b; \ 175 intmax_t __intofs_a_signed = __intofs_a; \ 176 uintmax_t __intofs_a_unsigned = __intofs_a; \ 177 intmax_t __intofs_b_signed = __intofs_b; \ 178 uintmax_t __intofs_b_unsigned = __intofs_b; \ 179 \ 180 __intofs_b < 1 ? \ 181 __intofs_a < 1 ? \ 182 ((INTMAX_MAX + __intofs_b_signed >= \ 183 __intofs_a_signed) ? \ 184 __INTOF_ASSIGN((c), __intofs_a_signed - \ 185 __intofs_b_signed) : 1) \ 186 : \ 187 (((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \ 188 __intofs_a_unsigned) ? \ 189 __INTOF_ASSIGN((c), __intofs_a - \ 190 __intofs_b) : 1) \ 191 : \ 192 __intofs_a < 1 ? \ 193 (((intmax_t)(INTMAX_MIN + __intofs_b) <= \ 194 __intofs_a_signed) ? \ 195 __INTOF_ASSIGN((c), \ 196 (intmax_t)(__intofs_a_signed - \ 197 __intofs_b_unsigned)) : 1) \ 198 : \ 199 ((__intofs_b_unsigned <= __intofs_a_unsigned) ? \ 200 __INTOF_ASSIGN((c), __intofs_a_unsigned - \ 201 __intofs_b_unsigned) \ 202 : \ 203 __INTOF_ASSIGN((c), \ 204 (intmax_t)(__intofs_a_unsigned - \ 205 __intofs_b_unsigned))); \ 206 })) 207 208 /* 209 * Dealing with detecting overflow in multiplication of integers. 210 * 211 * First step is to remove two corner cases with the minum signed integer 212 * which can't be represented as a positive integer + sign. 213 * Multiply with 0 or 1 can't overflow, no checking needed of the operation, 214 * only if it can be assigned to the result. 215 * 216 * After the corner cases are eliminated we convert the two factors to 217 * positive unsigned values, keeping track of the original in another 218 * variable which is used at the end to determine the sign of the product. 219 * 220 * The two terms (a and b) are divided into upper and lower half (x1 upper 221 * and x0 lower), so the product is: 222 * ((a1 << hshift) + a0) * ((b1 << hshift) + b0) 223 * which also is: 224 * ((a1 * b1) << (hshift * 2)) + (T1) 225 * ((a1 * b0 + a0 * b1) << hshift) + (T2) 226 * (a0 * b0) (T3) 227 * 228 * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that 229 * is, at least one of a1 or b1 has to be 0. Once this has been checked the 230 * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift) 231 * isn't an addition as one of the terms will be 0. 232 * 233 * Since each factor in: (a0 * b0) 234 * only uses half the capicity of the underlaying type it can't overflow 235 * 236 * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to 237 * perform that addition. If the addition succeeds without overflow the 238 * result is assigned the required sign and checked for overflow again. 239 */ 240 241 #define __intof_mul_negate ((__intof_oa < 1) != (__intof_ob < 1)) 242 #define __intof_mul_hshift (sizeof(uintmax_t) * 8 / 2) 243 #define __intof_mul_hmask (UINTMAX_MAX >> __intof_mul_hshift) 244 #define __intof_mul_a0 ((uintmax_t)(__intof_a) >> __intof_mul_hshift) 245 #define __intof_mul_b0 ((uintmax_t)(__intof_b) >> __intof_mul_hshift) 246 #define __intof_mul_a1 ((uintmax_t)(__intof_a) & __intof_mul_hmask) 247 #define __intof_mul_b1 ((uintmax_t)(__intof_b) & __intof_mul_hmask) 248 #define __intof_mul_t (__intof_mul_a1 * __intof_mul_b0 + \ 249 __intof_mul_a0 * __intof_mul_b1) 250 251 #define __INTOF_MUL(c, a, b) (__extension__({ \ 252 typeof(a) __intof_oa = (a); \ 253 typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \ 254 typeof(b) __intof_ob = (b); \ 255 typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \ 256 typeof(c) __intof_c; \ 257 \ 258 __intof_oa == 0 || __intof_ob == 0 || \ 259 __intof_oa == 1 || __intof_ob == 1 ? \ 260 __INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \ 261 (__intof_mul_a0 && __intof_mul_b0) || \ 262 __intof_mul_t > __intof_mul_hmask ? 1 : \ 263 __INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \ 264 __intof_mul_a1 * __intof_mul_b1) ? 1 : \ 265 __intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \ 266 __INTOF_ASSIGN((c), __intof_c); \ 267 })) 268 269 #define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b)) 270 #define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b)) 271 #define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b)) 272 273 #endif /*!__HAVE_BUILTIN_OVERFLOW*/ 274 275 #define __compiler_compare_and_swap(p, oval, nval) \ 276 __atomic_compare_exchange_n((p), (oval), (nval), true, \ 277 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \ 278 279 #define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED) 280 #define __compiler_atomic_store(p, val) \ 281 __atomic_store_n((p), (val), __ATOMIC_RELAXED) 282 283 #endif /*COMPILER_H*/ 284