xref: /aosp_15_r20/external/coreboot/src/commonlib/bsd/include/commonlib/bsd/helpers.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only */
2 
3 #ifndef COMMONLIB_BSD_HELPERS_H
4 #define COMMONLIB_BSD_HELPERS_H
5 
6 #ifndef __ASSEMBLER__
7 #include <commonlib/bsd/compiler.h>
8 #include <stddef.h>
9 #endif
10 
11 #ifndef ARRAY_SIZE
12 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
13 #endif
14 
15 #define ALIGN(x, a)             __ALIGN_MASK(x, (__typeof__(x))(a)-1UL)
16 #define __ALIGN_MASK(x, mask)   (((x)+(mask))&~(mask))
17 #define ALIGN_UP(x, a)          ALIGN((x), (a))
18 #define ALIGN_DOWN(x, a)        ((x) & ~((__typeof__(x))(a)-1UL))
19 #define IS_ALIGNED(x, a)        (((x) & ((__typeof__(x))(a)-1UL)) == 0)
20 
21 /* Double-evaluation unsafe min/max, for bitfields and outside of functions */
22 #define __CMP_UNSAFE(a, b, op) ((a) op (b) ? (a) : (b))
23 #define MIN_UNSAFE(a, b) __CMP_UNSAFE(a, b, <)
24 #define MAX_UNSAFE(a, b) __CMP_UNSAFE(a, b, >)
25 
26 #define __CMP_SAFE(a, b, op, var_a, var_b) ({ \
27 	__TYPEOF_UNLESS_CONST(a, b) var_a = (a); \
28 	__TYPEOF_UNLESS_CONST(b, a) var_b = (b); \
29 	var_a op var_b ? var_a : var_b; \
30 })
31 
32 #define __CMP(a, b, op) __builtin_choose_expr( \
33 	__builtin_constant_p(a) && __builtin_constant_p(b), \
34 	__CMP_UNSAFE(a, b, op), __CMP_SAFE(a, b, op, __TMPNAME, __TMPNAME))
35 
36 #ifndef MIN
37 #define MIN(a, b) __CMP(a, b, <)
38 #endif
39 #ifndef MAX
40 #define MAX(a, b) __CMP(a, b, >)
41 #endif
42 
43 #ifndef ABS
44 #define ABS(a) ({ \
45 	__typeof__(a) _abs_local_a = (a); \
46 	(_abs_local_a < 0) ? (-_abs_local_a) : _abs_local_a; \
47 })
48 #endif
49 
50 #define IS_POWER_OF_2(x) ({ \
51 	__typeof__(x) _power_local_x = (x); \
52 	(_power_local_x & (_power_local_x - 1)) == 0; \
53 })
54 
55 #define POWER_OF_2(x)		(1ULL << (x))
56 
57 /* Set bits from `high` to `low` (inclusive). */
58 #define GENMASK(high, low) (((~0ULL) << (low)) & (~0ULL >> (63 - (high))))
59 
60 #define DIV_ROUND_UP(x, y) ({ \
61 	__typeof__(x) _div_local_x = (x); \
62 	__typeof__(y) _div_local_y = (y); \
63 	(_div_local_x + _div_local_y - 1) / _div_local_y; \
64 })
65 
66 #define SWAP(a, b) do { \
67 	__typeof__(&(a)) _swap_local_a = &(a); \
68 	__typeof__(&(b)) _swap_local_b = &(b); \
69 	__typeof__(a) _swap_local_tmp = *_swap_local_a; \
70 	*_swap_local_a = *_swap_local_b; \
71 	*_swap_local_b = _swap_local_tmp; \
72 } while (0)
73 
74 /* Standard units. */
75 #define KiB (1<<10)
76 #define MiB (1<<20)
77 #define GiB (1<<30)
78 
79 #define KHz (1000)
80 #define MHz (1000 * KHz)
81 #define GHz (1000 * MHz)
82 
83 #ifndef offsetof
84 #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
85 #endif
86 
87 #define check_member(structure, member, offset) _Static_assert( \
88 	offsetof(struct structure, member) == offset, \
89 	"`struct " #structure "` offset for `" #member "` is not " #offset)
90 
91 /* Calculate size of structure member. */
92 #define member_size(type, member)	(sizeof(((type *)0)->member))
93 
94 #define _retry_impl(attempts, condition, expr, ...)	\
95 ({							\
96 	__typeof__(condition) _retry_ret =		\
97 		(__typeof__(condition))0;		\
98 	int _retry_attempts = (attempts);		\
99 	do {						\
100 		_retry_ret = (condition);		\
101 		if (_retry_ret)				\
102 			break;				\
103 		if (--_retry_attempts > 0) {		\
104 			expr;				\
105 		} else {				\
106 			break;				\
107 		}					\
108 	} while (1);					\
109 	_retry_ret;					\
110 })
111 
112 /*
113  * Helper macro to retry until a condition becomes true or the maximum number
114  * of attempts is reached. Two forms are supported:
115  *
116  * 1. retry(attempts, condition)
117  * 2. retry(attempts, condition, expr)
118  *
119  * @param attempts	Maximum attempts.
120  * @param condition	Condition to retry for.
121  * @param expr		Procedure to run between each evaluation to "condition".
122  *
123  * @return Condition value if it evaluates to true within the maximum attempts;
124  *	   0 otherwise.
125  */
126 #define retry(attempts, condition, ...) \
127 	_retry_impl(attempts, condition, __VA_ARGS__)
128 
129 /* Stringify a token */
130 #ifndef STRINGIFY
131 #define _STRINGIFY(x)	#x
132 #define STRINGIFY(x)	_STRINGIFY(x)
133 #endif
134 
135 #endif /* COMMONLIB_BSD_HELPERS_H */
136