5#ifndef OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_
6#define OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_
10#if !defined(__ASSEMBLER__) && !defined(NOSTDINC)
45#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
50#define OT_FALLTHROUGH_INTENDED __attribute__((fallthrough))
55#define OT_ALWAYS_INLINE __attribute__((always_inline)) inline
63#define OT_RESTRICT __restrict__
68#define OT_STRINGIFY(a) OT_STRINGIFY_(a)
69#define OT_STRINGIFY_(a) #a
85#define OT_VA_ARGS_COUNT(dummy, ...) \
86 OT_SHIFT_N_VARIABLE_ARGS_(dummy, ##__VA_ARGS__, 31, 30, 29, 28, 27, 26, 25, \
87 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, \
88 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
91#define OT_SHIFT_N_VARIABLE_ARGS_(...) OT_GET_NTH_VARIABLE_ARG_(__VA_ARGS__)
92#define OT_GET_NTH_VARIABLE_ARG_(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, \
93 x11, x12, x13, x14, x15, x16, x17, x18, x19, \
94 x20, x21, x22, x23, x24, x25, x26, x27, x28, \
95 x29, x30, x31, n, ...) \
104#define OT_CAT(a, ...) OT_PRIMITIVE_CAT(a, __VA_ARGS__)
105#define OT_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
110#define OT_GET_ARG(n, ...) OT_CAT(OT_CAT(OT_GET_ARG_, n), _)(__VA_ARGS__)
119#define OT_GET_LAST_ARG(...) \
120 OT_GET_ARG(OT_VA_ARGS_COUNT(__VA_ARGS__), ##__VA_ARGS__)
126#define OT_GET_ARG_0_(x0, ...) x0
127#define OT_GET_ARG_1_(x1, x0, ...) x0
128#define OT_GET_ARG_2_(x2, x1, x0, ...) x0
129#define OT_GET_ARG_3_(x3, x2, x1, x0, ...) x0
130#define OT_GET_ARG_4_(x4, x3, x2, x1, x0, ...) x0
131#define OT_GET_ARG_5_(x5, x4, x3, x2, x1, x0, ...) x0
132#define OT_GET_ARG_6_(x6, x5, x4, x3, x2, x1, x0, ...) x0
133#define OT_GET_ARG_7_(x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
134#define OT_GET_ARG_8_(x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
135#define OT_GET_ARG_9_(x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
136#define OT_GET_ARG_10_(x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
137#define OT_GET_ARG_11_(x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
138#define OT_GET_ARG_12_(x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
141#define OT_GET_ARG_13_(x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, \
144#define OT_GET_ARG_14_(x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, \
147#define OT_GET_ARG_15_(x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, \
148 x3, x2, x1, x0, ...) \
150#define OT_GET_ARG_16_(x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, \
151 x4, x3, x2, x1, x0, ...) \
153#define OT_GET_ARG_17_(x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, \
154 x5, x4, x3, x2, x1, x0, ...) \
156#define OT_GET_ARG_18_(x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, \
157 x7, x6, x5, x4, x3, x2, x1, x0, ...) \
159#define OT_GET_ARG_19_(x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, \
160 x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
162#define OT_GET_ARG_20_(x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, \
163 x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
165#define OT_GET_ARG_21_(x21, x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, \
166 x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
168#define OT_GET_ARG_22_(x22, x21, x20, x19, x18, x17, x16, x15, x14, x13, x12, \
169 x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
171#define OT_GET_ARG_23_(x23, x22, x21, x20, x19, x18, x17, x16, x15, x14, x13, \
172 x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
175#define OT_GET_ARG_24_(x24, x23, x22, x21, x20, x19, x18, x17, x16, x15, x14, \
176 x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, \
179#define OT_GET_ARG_25_(x25, x24, x23, x22, x21, x20, x19, x18, x17, x16, x15, \
180 x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, \
183#define OT_GET_ARG_26_(x26, x25, x24, x23, x22, x21, x20, x19, x18, x17, x16, \
184 x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, \
185 x3, x2, x1, x0, ...) \
187#define OT_GET_ARG_27_(x27, x26, x25, x24, x23, x22, x21, x20, x19, x18, x17, \
188 x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, \
189 x4, x3, x2, x1, x0, ...) \
191#define OT_GET_ARG_28_(x28, x27, x26, x25, x24, x23, x22, x21, x20, x19, x18, \
192 x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, \
193 x5, x4, x3, x2, x1, x0, ...) \
195#define OT_GET_ARG_29_(x29, x28, x27, x26, x25, x24, x23, x22, x21, x20, x19, \
196 x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, \
197 x7, x6, x5, x4, x3, x2, x1, x0, ...) \
199#define OT_GET_ARG_30_(x30, x29, x28, x27, x26, x25, x24, x23, x22, x21, x20, \
200 x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, \
201 x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
203#define OT_GET_ARG_31_(x31, x30, x29, x28, x27, x26, x25, x24, x23, x22, x21, \
204 x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, \
205 x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
215#define OT_ASSERT_MEMBER_OFFSET(type, member, offset) \
216 static_assert(offsetof(type, member) == UINT32_C(offset), \
217 "Unexpected offset for " #type "." #member)
226#define OT_ASSERT_MEMBER_OFFSET_AS_ENUM(type, member, enum_offset) \
227 static_assert(offsetof(type, member) == enum_offset, \
228 "Unexpected offset for " #type "." #member)
237#define OT_ASSERT_MEMBER_SIZE(type, member, size) \
238 static_assert(sizeof(((type *)0)->member) == UINT32_C(size), \
239 "Unexpected size for " #type)
251#define OT_ASSERT_MEMBER_SIZE_AS_ENUM(type, member, enum_size) \
252 static_assert(sizeof(((type *)0)->member) == enum_size, \
253 "Unexpected size for " #type)
261#define OT_ASSERT_SIZE(type, size) \
262 static_assert(sizeof(type) == UINT32_C(size), "Unexpected size for " #type)
270#define OT_ASSERT_ENUM_VALUE(var, expected_value) \
271 static_assert(var == expected_value, "Unexpected value for " #var)
288#define OT_VA_FOR_EACH(action, ...) \
290 OT_CAT(OT_CAT(OT_VA_FOR_EACH_, OT_VA_ARGS_COUNT(0, ##__VA_ARGS__)), _) \
291 (action, ##__VA_ARGS__) \
298#define OT_VA_FOR_EACH_0_(action)
299#define OT_VA_FOR_EACH_1_(action, item, ...) \
301 OT_VA_FOR_EACH_0_(action, ##__VA_ARGS__)
302#define OT_VA_FOR_EACH_2_(action, item, ...) \
304 OT_VA_FOR_EACH_1_(action, ##__VA_ARGS__)
305#define OT_VA_FOR_EACH_3_(action, item, ...) \
307 OT_VA_FOR_EACH_2_(action, ##__VA_ARGS__)
308#define OT_VA_FOR_EACH_4_(action, item, ...) \
310 OT_VA_FOR_EACH_3_(action, ##__VA_ARGS__)
311#define OT_VA_FOR_EACH_5_(action, item, ...) \
313 OT_VA_FOR_EACH_4_(action, ##__VA_ARGS__)
314#define OT_VA_FOR_EACH_6_(action, item, ...) \
316 OT_VA_FOR_EACH_5_(action, ##__VA_ARGS__)
317#define OT_VA_FOR_EACH_7_(action, item, ...) \
319 OT_VA_FOR_EACH_6_(action, ##__VA_ARGS__)
320#define OT_VA_FOR_EACH_8_(action, item, ...) \
322 OT_VA_FOR_EACH_7_(action, ##__VA_ARGS__)
323#define OT_VA_FOR_EACH_9_(action, item, ...) \
325 OT_VA_FOR_EACH_8_(action, ##__VA_ARGS__)
326#define OT_VA_FOR_EACH_10_(action, item, ...) \
328 OT_VA_FOR_EACH_9_(action, ##__VA_ARGS__)
329#define OT_VA_FOR_EACH_11_(action, item, ...) \
331 OT_VA_FOR_EACH_10_(action, ##__VA_ARGS__)
332#define OT_VA_FOR_EACH_12_(action, item, ...) \
334 OT_VA_FOR_EACH_11_(action, ##__VA_ARGS__)
335#define OT_VA_FOR_EACH_13_(action, item, ...) \
337 OT_VA_FOR_EACH_12_(action, ##__VA_ARGS__)
338#define OT_VA_FOR_EACH_14_(action, item, ...) \
340 OT_VA_FOR_EACH_13_(action, ##__VA_ARGS__)
341#define OT_VA_FOR_EACH_15_(action, item, ...) \
343 OT_VA_FOR_EACH_14_(action, ##__VA_ARGS__)
344#define OT_VA_FOR_EACH_16_(action, item, ...) \
346 OT_VA_FOR_EACH_15_(action, ##__VA_ARGS__)
347#define OT_VA_FOR_EACH_17_(action, item, ...) \
349 OT_VA_FOR_EACH_16_(action, ##__VA_ARGS__)
350#define OT_VA_FOR_EACH_18_(action, item, ...) \
352 OT_VA_FOR_EACH_17_(action, ##__VA_ARGS__)
353#define OT_VA_FOR_EACH_19_(action, item, ...) \
355 OT_VA_FOR_EACH_18_(action, ##__VA_ARGS__)
356#define OT_VA_FOR_EACH_20_(action, item, ...) \
358 OT_VA_FOR_EACH_19_(action, ##__VA_ARGS__)
359#define OT_VA_FOR_EACH_21_(action, item, ...) \
361 OT_VA_FOR_EACH_20_(action, ##__VA_ARGS__)
362#define OT_VA_FOR_EACH_22_(action, item, ...) \
364 OT_VA_FOR_EACH_21_(action, ##__VA_ARGS__)
365#define OT_VA_FOR_EACH_23_(action, item, ...) \
367 OT_VA_FOR_EACH_22_(action, ##__VA_ARGS__)
368#define OT_VA_FOR_EACH_24_(action, item, ...) \
370 OT_VA_FOR_EACH_23_(action, ##__VA_ARGS__)
371#define OT_VA_FOR_EACH_25_(action, item, ...) \
373 OT_VA_FOR_EACH_24_(action, ##__VA_ARGS__)
374#define OT_VA_FOR_EACH_26_(action, item, ...) \
376 OT_VA_FOR_EACH_25_(action, ##__VA_ARGS__)
377#define OT_VA_FOR_EACH_27_(action, item, ...) \
379 OT_VA_FOR_EACH_26_(action, ##__VA_ARGS__)
380#define OT_VA_FOR_EACH_28_(action, item, ...) \
382 OT_VA_FOR_EACH_27_(action, ##__VA_ARGS__)
383#define OT_VA_FOR_EACH_29_(action, item, ...) \
385 OT_VA_FOR_EACH_28_(action, ##__VA_ARGS__)
386#define OT_VA_FOR_EACH_30_(action, item, ...) \
388 OT_VA_FOR_EACH_29_(action, ##__VA_ARGS__)
389#define OT_VA_FOR_EACH_31_(action, item, ...) \
391 OT_VA_FOR_EACH_30_(action, ##__VA_ARGS__)
400#define OT_CHECK_NOT_INT64(arg) \
401 _Generic((arg), int64_t: false, uint64_t: false, default: true)
412#define OT_FAIL_IF_64_BIT(arg, func_name) \
414 static_assert(OT_CHECK_NOT_INT64(arg), \
415 "Argument '" #arg "' passed to the " #func_name \
416 " function must be no wider than 32 bits. " \
417 "Hint: maybe cast with '(uint32_t) " #arg "'?"); \
423#if defined(__riscv_xlen) && __riscv_xlen == 32
424#define OT_PLATFORM_RV32 1
431#ifdef OT_IS_SCAFI_DEPRECATED_REDUCED_SUPPORT_FOR_INTERNAL_USE_ONLY_
432#define OT_IS_SCAFI_DEPRECATED 1
449#define OT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
455#define OT_NONNULL __attribute__((nonnull))
460#define OT_WEAK __attribute__((weak))
470#define OT_NAKED __attribute__((naked))
478#define OT_SECTION(name) __attribute__((section(name)))
494#define OT_SET_BSS_SECTION(name, ...) \
495 OT_SET_BSS_SECTION_(clang section bss = name) \
497 _Pragma("clang section bss = \"\"")
498#define OT_SET_BSS_SECTION_(section) _Pragma(#section)
505#define OT_NOINLINE __attribute__((noinline))
512#define OT_FRAME_ADDR() __builtin_frame_address(0)
524#define OT_UNREACHABLE() __builtin_unreachable()
530#define OT_ALIAS(name) __attribute__((alias(name)))
611#define OT_ADDRESSABLE_LABEL(kName_) \
612 extern const char kName_[]; \
613 asm volatile(".local " #kName_ "; " #kName_ ":;"); \
624#define OT_DISCARD(expr_) \
632#define OT_USED __attribute__((used))
638#if defined(__clang__) && __clang_major__ >= 21
639#define OT_NONSTRING __attribute__((nonstring))
640#elif defined(__GNUC__) && !defined(__clang__)
641#define OT_NONSTRING __attribute__((nonstring))
651#ifdef __clang_analyzer__
652#define OT_BUILD_FOR_STATIC_ANALYZER 1
654#define OT_BUILD_FOR_STATIC_ANALYZER 0
660#define OT_ALIGN_MEM(x) (uint32_t)(4 + (((uintptr_t)(x) - 1) & ~3u))
662#if !defined(__ASSEMBLER__) && !defined(NOSTDINC) && \
663 !defined(RUST_PREPROCESSOR_EMIT)
672#define OT_SIGNED(value) \
674 unsigned char: (signed char)(value), \
675 unsigned short: (short)(value), \
676 unsigned int: (int)(value), \
677 unsigned long: (long)(value), \
678 unsigned long long: (long long)(value), \
679 default: (struct OtSignConversionUnsupportedType){.err = 1})
684#define OT_UNSIGNED(value) \
686 signed char: (unsigned char)(value), \
687 short: (unsigned short)(value), \
688 int: (unsigned int)(value), \
689 long: (unsigned long)(value), \
690 long long: (unsigned long long)(value), \
691 default: (struct OtSignConversionUnsupportedType){.err = 1})
702 using SignedT =
typename std::make_signed<T>::type;
703 using UnsignedT =
typename std::make_unsigned<T>::type;
704 static constexpr SignedT as_signed(T value) {
705 return static_cast<SignedT
>(value);
707 static constexpr UnsignedT as_unsigned(T value) {
708 return static_cast<UnsignedT
>(value);
713#define OT_SIGNED(value) (SignConverter<__typeof__(value)>::as_signed((value)))
714#define OT_UNSIGNED(value) \
715 (SignConverter<__typeof__(value)>::as_unsigned((value)))
723#define ATOMIC_WAIT_FOR_INTERRUPT(_volatile_condition) \
725 irq_global_ctrl(false); \
726 if ((_volatile_condition)) { \
729 wait_for_interrupt(); \
730 irq_global_ctrl(true); \
732 irq_global_ctrl(true)
737#define OT_WORD_SIZE 4
738#define OT_HALF_WORD_SIZE (OT_WORD_SIZE / 2)
741#define OT_CONTEXT_SIZE (OT_WORD_SIZE * 30)
752 typeof(a) _a = (a); \
753 typeof(b) _b = (b); \
768 typeof(a) _a = (a); \
769 typeof(b) _b = (b); \