1 /*
2  * Copyright (c) 2010-2014,2017 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
8 #define ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_
9 
10 #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_
11 #error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead
12 #endif
13 
14 /**
15  * @file
16  * @brief GCC toolchain abstraction
17  *
18  * Macros to abstract compiler capabilities for GCC toolchain.
19  */
20 
21 #define TOOLCHAIN_GCC_VERSION \
22 	((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
23 
24 /* GCC supports #pragma diagnostics since 4.6.0 */
25 #if !defined(TOOLCHAIN_HAS_PRAGMA_DIAG) && (TOOLCHAIN_GCC_VERSION >= 40600)
26 #define TOOLCHAIN_HAS_PRAGMA_DIAG 1
27 #endif
28 
29 #if !defined(TOOLCHAIN_HAS_C_GENERIC) && (TOOLCHAIN_GCC_VERSION >= 40900)
30 #define TOOLCHAIN_HAS_C_GENERIC 1
31 #endif
32 
33 #if !defined(TOOLCHAIN_HAS_C_AUTO_TYPE) && (TOOLCHAIN_GCC_VERSION >= 40900)
34 #define TOOLCHAIN_HAS_C_AUTO_TYPE 1
35 #endif
36 
37 #define TOOLCHAIN_HAS_ZLA 1
38 
39 /*
40  * Older versions of GCC do not define __BYTE_ORDER__, so it must be manually
41  * detected and defined using arch-specific definitions.
42  */
43 
44 #ifndef _LINKER
45 
46 #ifndef __ORDER_BIG_ENDIAN__
47 #define __ORDER_BIG_ENDIAN__            (1)
48 #endif
49 
50 #ifndef __ORDER_LITTLE_ENDIAN__
51 #define __ORDER_LITTLE_ENDIAN__         (2)
52 #endif
53 
54 #ifndef __BYTE_ORDER__
55 #if defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
56     defined(__THUMBEB__) || defined(__AARCH64EB__) || \
57     defined(__MIPSEB__) || defined(__TC32EB__)
58 
59 #define __BYTE_ORDER__                  __ORDER_BIG_ENDIAN__
60 
61 #elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
62       defined(__THUMBEL__) || defined(__AARCH64EL__) || \
63       defined(__MIPSEL__) || defined(__TC32EL__)
64 
65 #define __BYTE_ORDER__                  __ORDER_LITTLE_ENDIAN__
66 
67 #else
68 #error "__BYTE_ORDER__ is not defined and cannot be automatically resolved"
69 #endif
70 #endif
71 
72 
73 #undef BUILD_ASSERT /* clear out common version */
74 /* C++11 has static_assert built in */
75 #if defined(__cplusplus) && (__cplusplus >= 201103L)
76 #define BUILD_ASSERT(EXPR, MSG...) static_assert(EXPR, "" MSG)
77 
78 /*
79  * GCC 4.6 and higher have the C11 _Static_assert built in and its
80  * output is easier to understand than the common BUILD_ASSERT macros.
81  * Don't use this in C++98 mode though (which we can hit, as
82  * static_assert() is not available)
83  */
84 #elif !defined(__cplusplus) && \
85 	(((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))) ||	\
86 	 (__STDC_VERSION__) >= 201100)
87 #define BUILD_ASSERT(EXPR, MSG...) _Static_assert((EXPR), "" MSG)
88 #else
89 #define BUILD_ASSERT(EXPR, MSG...)
90 #endif
91 
92 #ifdef __cplusplus
93 #define ZRESTRICT __restrict
94 #else
95 #define ZRESTRICT restrict
96 #endif
97 
98 #include <zephyr/toolchain/common.h>
99 #include <stdbool.h>
100 
101 #define ALIAS_OF(of) __attribute__((alias(#of)))
102 
103 #define FUNC_ALIAS(real_func, new_alias, return_type) \
104 	return_type new_alias() ALIAS_OF(real_func)
105 
106 #if defined(CONFIG_ARCH_POSIX) && !defined(_ASMLANGUAGE)
107 #include <zephyr/arch/posix/posix_trace.h>
108 
109 /*let's not segfault if this were to happen for some reason*/
110 #define CODE_UNREACHABLE \
111 {\
112 	posix_print_error_and_exit("CODE_UNREACHABLE reached from %s:%d\n",\
113 		__FILE__, __LINE__);\
114 	__builtin_unreachable(); \
115 }
116 #else
117 #define CODE_UNREACHABLE __builtin_unreachable()
118 #endif
119 #define FUNC_NORETURN    __attribute__((__noreturn__))
120 
121 /* The GNU assembler for Cortex-M3 uses # for immediate values, not
122  * comments, so the @nobits# trick does not work.
123  */
124 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
125 #define _NODATA_SECTION(segment)  __attribute__((section(#segment)))
126 #else
127 #define _NODATA_SECTION(segment)				\
128 	__attribute__((section(#segment ",\"wa\",@nobits#")))
129 #endif
130 
131 /* Unaligned access */
132 #define UNALIGNED_GET(g)						\
133 __extension__ ({							\
134 	struct  __attribute__((__packed__)) {				\
135 		__typeof__(*(g)) __v;					\
136 	} *__g = (__typeof__(__g)) (g);					\
137 	__g->__v;							\
138 })
139 
140 
141 #if (__GNUC__ >= 7) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
142 
143 /* Version of UNALIGNED_PUT() which issues a compiler_barrier() after
144  * the store. It is required to workaround an apparent optimization
145  * bug in GCC for ARM Cortex-M3 and higher targets, when multiple
146  * byte, half-word and word stores (strb, strh, str instructions),
147  * which support unaligned access, can be coalesced into store double
148  * (strd) instruction, which doesn't support unaligned access (the
149  * compilers in question do this optimization ignoring __packed__
150  * attribute).
151  */
152 #define UNALIGNED_PUT(v, p)                                             \
153 do {                                                                    \
154 	struct __attribute__((__packed__)) {                            \
155 		__typeof__(*p) __v;                                     \
156 	} *__p = (__typeof__(__p)) (p);                                 \
157 	__p->__v = (v);                                                 \
158 	compiler_barrier();                                             \
159 } while (false)
160 
161 #else
162 
163 #define UNALIGNED_PUT(v, p)                                             \
164 do {                                                                    \
165 	struct __attribute__((__packed__)) {                            \
166 		__typeof__(*p) __v;                                     \
167 	} *__p = (__typeof__(__p)) (p);                                 \
168 	__p->__v = (v);                                               \
169 } while (false)
170 
171 #endif
172 
173 /* Double indirection to ensure section names are expanded before
174  * stringification
175  */
176 #define __GENERIC_SECTION(segment) __attribute__((section(STRINGIFY(segment))))
177 #define Z_GENERIC_SECTION(segment) __GENERIC_SECTION(segment)
178 
179 #define __GENERIC_DOT_SECTION(segment) \
180 	__attribute__((section("." STRINGIFY(segment))))
181 #define Z_GENERIC_DOT_SECTION(segment) __GENERIC_DOT_SECTION(segment)
182 
183 #define ___in_section(a, b, c) \
184 	__attribute__((section("." Z_STRINGIFY(a)			\
185 				"." Z_STRINGIFY(b)			\
186 				"." Z_STRINGIFY(c))))
187 #define __in_section(a, b, c) ___in_section(a, b, c)
188 
189 #ifndef __in_section_unique
190 #define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__)
191 #endif
192 
193 #ifndef __in_section_unique_named
194 #define __in_section_unique_named(seg, name) \
195 	___in_section(seg, __FILE__, name)
196 #endif
197 
198 /* When using XIP, using '__ramfunc' places a function into RAM instead
199  * of FLASH. Make sure '__ramfunc' is defined only when
200  * CONFIG_ARCH_HAS_RAMFUNC_SUPPORT is defined, so that the compiler can
201  * report an error if '__ramfunc' is used but the architecture does not
202  * support it.
203  */
204 #if !defined(CONFIG_XIP)
205 #define __ramfunc
206 #elif defined(CONFIG_ARCH_HAS_RAMFUNC_SUPPORT)
207 #if defined(CONFIG_ARM)
208 #define __ramfunc	__attribute__((noinline))			\
209 			__attribute__((long_call, section(".ramfunc")))
210 #else
211 #define __ramfunc	__attribute__((noinline))			\
212 			__attribute__((section(".ramfunc")))
213 #endif
214 #endif /* !CONFIG_XIP */
215 
216 #ifndef __fallthrough
217 #if __GNUC__ >= 7
218 #define __fallthrough        __attribute__((fallthrough))
219 #else
220 #define __fallthrough
221 #endif	/* __GNUC__ >= 7 */
222 #endif
223 
224 #ifndef __packed
225 #define __packed        __attribute__((__packed__))
226 #endif
227 
228 #ifndef __aligned
229 #define __aligned(x)	__attribute__((__aligned__(x)))
230 #endif
231 
232 #ifndef __noinline
233 #define __noinline      __attribute__((noinline))
234 #endif
235 
236 #define __may_alias     __attribute__((__may_alias__))
237 
238 #ifndef __printf_like
239 #ifdef CONFIG_ENFORCE_ZEPHYR_STDINT
240 #define __printf_like(f, a)   __attribute__((format (printf, f, a)))
241 #else
242 /*
243  * The Zephyr stdint convention enforces int32_t = int, int64_t = long long,
244  * and intptr_t = long so that short string format length modifiers can be
245  * used universally across ILP32 and LP64 architectures. Without that it
246  * is possible for ILP32 toolchains to have int32_t = long and intptr_t = int
247  * clashing with the Zephyr convention and generating pointless warnings
248  * as they're still the same size. Inhibit the format argument type
249  * validation in that case and let the other configs do it.
250  */
251 #define __printf_like(f, a)
252 #endif
253 #endif
254 
255 #define __used		__attribute__((__used__))
256 #define __unused	__attribute__((__unused__))
257 #define __maybe_unused	__attribute__((__unused__))
258 
259 #ifndef __deprecated
260 #define __deprecated	__attribute__((deprecated))
261 /* When adding this, remember to follow the instructions in
262  * https://docs.zephyrproject.org/latest/develop/api/api_lifecycle.html#deprecated
263  */
264 #endif
265 
266 #ifndef __attribute_const__
267 #define __attribute_const__ __attribute__((__const__))
268 #endif
269 
270 #ifndef __must_check
271 #define __must_check __attribute__((warn_unused_result))
272 #endif
273 
274 #define ARG_UNUSED(x) (void)(x)
275 
276 #define likely(x)   (__builtin_expect((bool)!!(x), true) != 0L)
277 #define unlikely(x) (__builtin_expect((bool)!!(x), false) != 0L)
278 #define POPCOUNT(x) __builtin_popcount(x)
279 
280 #ifndef __no_optimization
281 #define __no_optimization __attribute__((optimize("-O0")))
282 #endif
283 
284 #ifndef __weak
285 #define __weak __attribute__((__weak__))
286 #endif
287 
288 #ifndef __attribute_nonnull
289 #define __attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
290 #endif
291 
292 /* Builtins with availability that depend on the compiler version. */
293 #if __GNUC__ >= 5
294 #define HAS_BUILTIN___builtin_add_overflow 1
295 #define HAS_BUILTIN___builtin_sub_overflow 1
296 #define HAS_BUILTIN___builtin_mul_overflow 1
297 #define HAS_BUILTIN___builtin_div_overflow 1
298 #endif
299 #if __GNUC__ >= 4
300 #define HAS_BUILTIN___builtin_clz 1
301 #define HAS_BUILTIN___builtin_clzl 1
302 #define HAS_BUILTIN___builtin_clzll 1
303 #define HAS_BUILTIN___builtin_ctz 1
304 #define HAS_BUILTIN___builtin_ctzl 1
305 #define HAS_BUILTIN___builtin_ctzll 1
306 #endif
307 
308 /*
309  * Be *very* careful with these. You cannot filter out __DEPRECATED_MACRO with
310  * -wno-deprecated, which has implications for -Werror.
311  */
312 
313 /*
314  * Expands to nothing and generates a warning. Used like
315  *
316  *   #define FOO __WARN("Please use BAR instead") ...
317  *
318  * The warning points to the location where the macro is expanded.
319  */
320 #define __WARN(msg) __WARN1(GCC warning msg)
321 #define __WARN1(s) _Pragma(#s)
322 
323 /* Generic message */
324 #ifndef __DEPRECATED_MACRO
325 #define __DEPRECATED_MACRO __WARN("Macro is deprecated")
326 /* When adding this, remember to follow the instructions in
327  * https://docs.zephyrproject.org/latest/develop/api/api_lifecycle.html#deprecated
328  */
329 #endif
330 
331 /* These macros allow having ARM asm functions callable from thumb */
332 
333 #if defined(_ASMLANGUAGE)
334 
335 #if defined(CONFIG_ARM)
336 
337 #if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
338 
339 #define FUNC_CODE() .thumb;
340 #define FUNC_INSTR(a)
341 
342 #else
343 
344 #define FUNC_CODE() .code 32;
345 #define FUNC_INSTR(a)
346 
347 #endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
348 
349 #else
350 
351 #define FUNC_CODE()
352 #define FUNC_INSTR(a)
353 
354 #endif /* CONFIG_ARM */
355 
356 #endif /* _ASMLANGUAGE */
357 
358 /*
359  * These macros are used to declare assembly language symbols that need
360  * to be typed properly(func or data) to be visible to the OMF tool.
361  * So that the build tool could mark them as an entry point to be linked
362  * correctly.  This is an elfism. Use #if 0 for a.out.
363  */
364 
365 #if defined(_ASMLANGUAGE)
366 
367 #if defined(CONFIG_ARM) || defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) \
368 	|| defined(CONFIG_XTENSA) || defined(CONFIG_ARM64) \
369 	|| defined(CONFIG_MIPS)
370 #define GTEXT(sym) .global sym; .type sym, %function
371 #define GDATA(sym) .global sym; .type sym, %object
372 #define WTEXT(sym) .weak sym; .type sym, %function
373 #define WDATA(sym) .weak sym; .type sym, %object
374 #elif defined(CONFIG_ARC)
375 /*
376  * Need to use assembly macros because ';' is interpreted as the start of
377  * a single line comment in the ARC assembler.
378  */
379 
380 .macro glbl_text symbol
381 	.globl \symbol
382 	.type \symbol, %function
383 .endm
384 
385 .macro glbl_data symbol
386 	.globl \symbol
387 	.type \symbol, %object
388 .endm
389 
390 .macro weak_data symbol
391 	.weak \symbol
392 	.type \symbol, %object
393 .endm
394 
395 #define GTEXT(sym) glbl_text sym
396 #define GDATA(sym) glbl_data sym
397 #define WDATA(sym) weak_data sym
398 
399 #else  /* !CONFIG_ARM && !CONFIG_ARC */
400 #define GTEXT(sym) .globl sym; .type sym, @function
401 #define GDATA(sym) .globl sym; .type sym, @object
402 #endif
403 
404 /*
405  * These macros specify the section in which a given function or variable
406  * resides.
407  *
408  * - SECTION_FUNC	allows only one function to reside in a sub-section
409  * - SECTION_SUBSEC_FUNC allows multiple functions to reside in a sub-section
410  *   This ensures that garbage collection only discards the section
411  *   if all functions in the sub-section are not referenced.
412  */
413 
414 #if defined(CONFIG_ARC)
415 /*
416  * Need to use assembly macros because ';' is interpreted as the start of
417  * a single line comment in the ARC assembler.
418  *
419  * Also, '\()' is needed in the .section directive of these macros for
420  * correct substitution of the 'section' variable.
421  */
422 
423 .macro section_var section, symbol
424 	.section .\section\().\symbol
425 	\symbol :
426 .endm
427 
428 .macro section_func section, symbol
429 	.section .\section\().\symbol, "ax"
430 	FUNC_CODE()
431 	PERFOPT_ALIGN
432 	\symbol :
433 	FUNC_INSTR(\symbol)
434 .endm
435 
436 .macro section_subsec_func section, subsection, symbol
437 	.section .\section\().\subsection, "ax"
438 	PERFOPT_ALIGN
439 	\symbol :
440 .endm
441 
442 #define SECTION_VAR(sect, sym) section_var sect, sym
443 #define SECTION_FUNC(sect, sym) section_func sect, sym
444 #define SECTION_SUBSEC_FUNC(sect, subsec, sym) \
445 	section_subsec_func sect, subsec, sym
446 #else /* !CONFIG_ARC */
447 
448 #define SECTION_VAR(sect, sym)  .section .sect.sym; sym:
449 #define SECTION_FUNC(sect, sym)						\
450 	.section .sect.sym, "ax";					\
451 				FUNC_CODE()				\
452 				PERFOPT_ALIGN; sym :		\
453 							FUNC_INSTR(sym)
454 #define SECTION_SUBSEC_FUNC(sect, subsec, sym)				\
455 		.section .sect.subsec, "ax"; PERFOPT_ALIGN; sym :
456 
457 #endif /* CONFIG_ARC */
458 
459 #endif /* _ASMLANGUAGE */
460 
461 #if defined(_ASMLANGUAGE)
462 #if defined(CONFIG_ARM)
463 #if defined(CONFIG_ASSEMBLER_ISA_THUMB2)
464 /* '.syntax unified' is a gcc-ism used in thumb-2 asm files */
465 #define _ASM_FILE_PROLOGUE .text; .syntax unified; .thumb
466 #else
467 #define _ASM_FILE_PROLOGUE .text; .code 32
468 #endif /* CONFIG_ASSEMBLER_ISA_THUMB2 */
469 #elif defined(CONFIG_ARM64)
470 #define _ASM_FILE_PROLOGUE .text
471 #endif /* CONFIG_ARM64 || CONFIG_ARM */
472 #endif /* _ASMLANGUAGE */
473 
474 /*
475  * These macros generate absolute symbols for GCC
476  */
477 
478 /* create an extern reference to the absolute symbol */
479 
480 #define GEN_OFFSET_EXTERN(name) extern const char name[]
481 
482 #define GEN_ABS_SYM_BEGIN(name) \
483 	EXTERN_C void name(void); \
484 	void name(void)         \
485 	{
486 
487 #define GEN_ABS_SYM_END }
488 
489 /*
490  * Note that GEN_ABSOLUTE_SYM(), depending on the architecture
491  * and toolchain, may restrict the range of values permitted
492  * for assignment to the named symbol.
493  *
494  * For example, on x86, "value" is interpreted as signed
495  * 32-bit integer. Passing in an unsigned 32-bit integer
496  * with MSB set would result in a negative integer.
497  * Moreover, GCC would error out if an integer larger
498  * than 2^32-1 is passed as "value".
499  */
500 
501 /*
502  * GEN_ABSOLUTE_SYM_KCONFIG() is outputted by the build system
503  * to generate named symbol/value pairs for kconfigs.
504  */
505 
506 #if defined(CONFIG_ARM)
507 
508 /*
509  * GNU/ARM backend does not have a proper operand modifier which does not
510  * produces prefix # followed by value, such as %0 for PowerPC, Intel, and
511  * MIPS. The workaround performed here is using %B0 which converts
512  * the value to ~(value). Thus "n"(~(value)) is set in operand constraint
513  * to output (value) in the ARM specific GEN_OFFSET macro.
514  */
515 
516 #define GEN_ABSOLUTE_SYM(name, value)               \
517 	__asm__(".globl\t" #name "\n\t.equ\t" #name \
518 		",%B0"                              \
519 		"\n\t.type\t" #name ",%%object" :  : "n"(~(value)))
520 
521 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
522 	__asm__(".globl\t" #name                    \
523 		"\n\t.equ\t" #name "," #value       \
524 		"\n\t.type\t" #name ",%object")
525 
526 #elif defined(CONFIG_X86)
527 
528 #define GEN_ABSOLUTE_SYM(name, value)               \
529 	__asm__(".globl\t" #name "\n\t.equ\t" #name \
530 		",%c0"                              \
531 		"\n\t.type\t" #name ",@object" :  : "n"(value))
532 
533 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
534 	__asm__(".globl\t" #name                    \
535 		"\n\t.equ\t" #name "," #value       \
536 		"\n\t.type\t" #name ",@object")
537 
538 #elif defined(CONFIG_ARC) || defined(CONFIG_ARM64)
539 
540 #define GEN_ABSOLUTE_SYM(name, value)               \
541 	__asm__(".globl\t" #name "\n\t.equ\t" #name \
542 		",%c0"                              \
543 		"\n\t.type\t" #name ",@object" :  : "n"(value))
544 
545 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
546 	__asm__(".globl\t" #name                    \
547 		"\n\t.equ\t" #name "," #value       \
548 		"\n\t.type\t" #name ",@object")
549 
550 #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
551 	defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
552 
553 /* No special prefixes necessary for constants in this arch AFAICT */
554 #define GEN_ABSOLUTE_SYM(name, value)		\
555 	__asm__(".globl\t" #name "\n\t.equ\t" #name \
556 		",%0"                              \
557 		"\n\t.type\t" #name ",%%object" :  : "n"(value))
558 
559 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
560 	__asm__(".globl\t" #name                    \
561 		"\n\t.equ\t" #name "," #value       \
562 		"\n\t.type\t" #name ",%object")
563 
564 #elif defined(CONFIG_ARCH_POSIX)
565 #define GEN_ABSOLUTE_SYM(name, value)               \
566 	__asm__(".globl\t" #name "\n\t.equ\t" #name \
567 		",%c0"                              \
568 		"\n\t.type\t" #name ",@object" :  : "n"(value))
569 
570 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
571 	__asm__(".globl\t" #name                    \
572 		"\n\t.equ\t" #name "," #value       \
573 		"\n\t.type\t" #name ",@object")
574 
575 #elif defined(CONFIG_SPARC)
576 #define GEN_ABSOLUTE_SYM(name, value)			\
577 	__asm__(".global\t" #name "\n\t.equ\t" #name	\
578 		",%0"					\
579 		"\n\t.type\t" #name ",#object" : : "n"(value))
580 
581 #define GEN_ABSOLUTE_SYM_KCONFIG(name, value)       \
582 	__asm__(".globl\t" #name                    \
583 		"\n\t.equ\t" #name "," #value       \
584 		"\n\t.type\t" #name ",#object")
585 
586 #else
587 #error processor architecture not supported
588 #endif
589 
590 #define compiler_barrier() do { \
591 	__asm__ __volatile__ ("" ::: "memory"); \
592 } while (false)
593 
594 /** @brief Return larger value of two provided expressions.
595  *
596  * Macro ensures that expressions are evaluated only once.
597  *
598  * @note Macro has limited usage compared to the standard macro as it cannot be
599  *	 used:
600  *	 - to generate constant integer, e.g. __aligned(Z_MAX(4,5))
601  *	 - static variable, e.g. array like static uint8_t array[Z_MAX(...)];
602  */
603 #define Z_MAX(a, b) ({ \
604 		/* random suffix to avoid naming conflict */ \
605 		__typeof__(a) _value_a_ = (a); \
606 		__typeof__(b) _value_b_ = (b); \
607 		(_value_a_ > _value_b_) ? _value_a_ : _value_b_; \
608 	})
609 
610 /** @brief Return smaller value of two provided expressions.
611  *
612  * Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
613  * macro limitations.
614  */
615 #define Z_MIN(a, b) ({ \
616 		/* random suffix to avoid naming conflict */ \
617 		__typeof__(a) _value_a_ = (a); \
618 		__typeof__(b) _value_b_ = (b); \
619 		(_value_a_ < _value_b_) ? _value_a_ : _value_b_; \
620 	})
621 
622 /** @brief Return a value clamped to a given range.
623  *
624  * Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
625  * macro limitations.
626  */
627 #define Z_CLAMP(val, low, high) ({                                             \
628 		/* random suffix to avoid naming conflict */                   \
629 		__typeof__(val) _value_val_ = (val);                           \
630 		__typeof__(low) _value_low_ = (low);                           \
631 		__typeof__(high) _value_high_ = (high);                        \
632 		(_value_val_ < _value_low_)  ? _value_low_ :                   \
633 		(_value_val_ > _value_high_) ? _value_high_ :                  \
634 					       _value_val_;                    \
635 	})
636 
637 /**
638  * @brief Calculate power of two ceiling for some nonzero value
639  *
640  * @param x Nonzero unsigned long value
641  * @return X rounded up to the next power of two
642  */
643 #define Z_POW2_CEIL(x) \
644 	((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) - 1))))
645 
646 /**
647  * @brief Check whether or not a value is a power of 2
648  *
649  * @param x The value to check
650  * @return true if x is a power of 2, false otherwise
651  */
652 #define Z_IS_POW2(x) (((x) != 0) && (((x) & ((x)-1)) == 0))
653 
654 #if defined(CONFIG_ASAN) && defined(__clang__)
655 #define __noasan __attribute__((no_sanitize("address")))
656 #else
657 #define __noasan /**/
658 #endif
659 
660 #if defined(CONFIG_UBSAN)
661 #define __noubsan __attribute__((no_sanitize("undefined")))
662 #else
663 #define __noubsan
664 #endif
665 
666 /**
667  * @brief Function attribute to disable stack protector.
668  *
669  * @note Only supported for GCC >= 11.0.0 or Clang >= 7.
670  */
671 #if (TOOLCHAIN_GCC_VERSION >= 110000) || \
672 	(defined(TOOLCHAIN_CLANG_VERSION) && (TOOLCHAIN_CLANG_VERSION >= 70000))
673 #define FUNC_NO_STACK_PROTECTOR __attribute__((no_stack_protector))
674 #else
675 #define FUNC_NO_STACK_PROTECTOR
676 #endif
677 
678 #define TOOLCHAIN_IGNORE_WSHADOW_BEGIN \
679 	_Pragma("GCC diagnostic push") \
680 	_Pragma("GCC diagnostic ignored \"-Wshadow\"")
681 
682 #define TOOLCHAIN_IGNORE_WSHADOW_END \
683 	_Pragma("GCC diagnostic pop")
684 
685 #endif /* !_LINKER */
686 #endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
687