xref: /optee_os/lib/libutils/ext/include/util.h (revision c98faf4daaa22b4a5dfce221fb95540df3d78ff3)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 #ifndef UTIL_H
6 #define UTIL_H
7 
8 #include <compiler.h>
9 #include <inttypes.h>
10 
11 #ifndef __ASSEMBLER__
12 #include <assert.h>
13 #include <stddef.h>
14 #endif
15 
16 #define SIZE_4K	UINTPTR_C(0x1000)
17 #define SIZE_1M	UINTPTR_C(0x100000)
18 #define SIZE_2M	UINTPTR_C(0x200000)
19 #define SIZE_4M	UINTPTR_C(0x400000)
20 #define SIZE_8M	UINTPTR_C(0x800000)
21 #define SIZE_2G	UINTPTR_C(0x80000000)
22 
23 #ifndef MAX
24 #ifndef __ASSEMBLER__
25 #define MAX(a, b) \
26 	(__extension__({ __typeof__(a) _a = (a); \
27 	   __typeof__(b) _b = (b); \
28 	 _a > _b ? _a : _b; }))
29 
30 #define MIN(a, b) \
31 	(__extension__({ __typeof__(a) _a = (a); \
32 	   __typeof__(b) _b = (b); \
33 	 _a < _b ? _a : _b; }))
34 #else
35 #define MAX(a, b)	(((a) > (b)) ? (a) : (b))
36 #define MIN(a, b)	(((a) < (b)) ? (a) : (b))
37 #endif
38 #endif
39 
40 /*
41  * In some particular conditions MAX and MIN macros fail to
42  * build from C source file implmentation. In such case one
43  * need to use MAX_UNSAFE/MIN_UNSAFE instead.
44  */
45 #define MAX_UNSAFE(a, b)	(((a) > (b)) ? (a) : (b))
46 #define MIN_UNSAFE(a, b)	(((a) < (b)) ? (a) : (b))
47 
48 #ifndef ARRAY_SIZE
49 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
50 #endif
51 
52 #ifndef __ASSEMBLER__
53 /* Round up the even multiple of size, size has to be a power of 2 */
54 #define ROUNDUP(v, size) (((v) + ((__typeof__(v))(size) - 1)) & \
55 			  ~((__typeof__(v))(size) - 1))
56 
57 /*
58  * ROUNDUP_OVERFLOW(v, size, res)
59  *
60  * @v: Input value to round
61  * @size: Rounding operand
62  * @res: Pointer where boolean overflow status (0/false or 1/true) is stored
63  * @return: boolean overflow status of the resulting rounded value
64  *
65  * Round up value @v to the even multiple of @size and return if result
66  * overflows the output value range pointed by @res. The rounded value is
67  * stored in the memory address pointed by @res.
68  */
69 #define ROUNDUP_OVERFLOW(v, size, res) \
70 	(__extension__({ \
71 		typeof(v) __roundup_mod = 0; \
72 		typeof(v) __roundup_add = 0; \
73 		\
74 		__roundup_mod = (v) % (typeof(v))(size); \
75 		if (__roundup_mod) \
76 			__roundup_add = (typeof(v))(size) - __roundup_mod; \
77 		ADD_OVERFLOW((v), __roundup_add, (res)); \
78 	}))
79 
80 /*
81  * ROUNDUP2_OVERFLOW(v, size, res)
82  *
83  * @v: Input value to round
84  * @size: Rounding operand, must be a power of 2
85  * @res: Pointer where boolean overflow status (0/false or 1/true) is stored
86  * @return: boolean overflow status of the resulting rounded value
87  *
88  * Round up value @v to the even multiple of @size and return if result
89  * overflows the output value range pointed by @res. The rounded value is
90  * stored in the memory address pointed by @res.
91  */
92 #define ROUNDUP2_OVERFLOW(v, size, res) \
93 	(__extension__({ \
94 		typeof(*(res)) __roundup_tmp = 0; \
95 		typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
96 		\
97 		assert(IS_POWER_OF_TWO(size)); \
98 		ADD_OVERFLOW((v), __roundup_mask, &__roundup_tmp) ? 1 : \
99 			((void)(*(res) = __roundup_tmp & ~__roundup_mask), 0); \
100 	}))
101 
102 /*
103  * ROUNDUP2_DIV(x, y)
104  *
105  * Rounds up to the nearest multiple of y and then divides by y. Safe
106  * against overflow, y has to be a power of 2.
107  *
108  * This macro is intended to be used to convert from "number of bytes" to
109  * "number of pages" or similar units. Example:
110  * num_pages = ROUNDUP2_DIV(num_bytes, SMALL_PAGE_SIZE);
111  */
112 #define ROUNDUP2_DIV(x, y) \
113 	(__extension__({ \
114 		typeof(x) __roundup_x = (x); \
115 		typeof(y) __roundup_mask = (typeof(x))(y) - 1; \
116 		\
117 		assert(IS_POWER_OF_TWO(y)); \
118 		(__roundup_x / (y)) + (__roundup_x & __roundup_mask ? 1 : 0); \
119 	}))
120 
121 /*
122  * ROUNDUP_DIV(x, y)
123  *
124  * Rounds up to the nearest multiple of y and then divides by y. Safe
125  * against overflow.
126  */
127 #define ROUNDUP_DIV(x, y) (ROUNDUP((x), (y)) / (__typeof__(x))(y))
128 
129 /* Round down the even multiple of size, size has to be a power of 2 */
130 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1))
131 
132 /*
133  * Round up the result of x / y to the nearest upper integer if result is not
134  * already an integer.
135  */
136 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y))
137 
138 /* Unsigned integer division with nearest rounding variant */
139 #define UDIV_ROUND_NEAREST(x, y) \
140 	(__extension__ ({ __typeof__(x) _x = (x); \
141 	  __typeof__(y) _y = (y); \
142 	  (_x + (_y / 2)) / _y; }))
143 #else
144 #define ROUNDUP(x, y)			((((x) + (y) - 1) / (y)) * (y))
145 #define ROUNDDOWN(x, y)		(((x) / (y)) * (y))
146 #define UDIV_ROUND_NEAREST(x, y)	(((x) + ((y) / 2)) / (y))
147 #endif
148 
149 /* x has to be of an unsigned type */
150 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x)))
151 
152 #define IS_ALIGNED(x, a)		(((x) & ((a) - 1)) == 0)
153 #define IS_ALIGNED_WITH_TYPE(x, type) \
154         (__extension__({ \
155                 type __is_aligned_y; \
156                 IS_ALIGNED((uintptr_t)(x), __alignof__(__is_aligned_y)); \
157         }))
158 
159 #define TO_STR(x) _TO_STR(x)
160 #define _TO_STR(x) #x
161 
162 #define CONCAT(x, y) _CONCAT(x, y)
163 #define _CONCAT(x, y) x##y
164 
165 #define container_of(ptr, type, member) \
166 	(__extension__({ \
167 		const typeof(((type *)0)->member) *__ptr = (ptr); \
168 		(type *)((unsigned long)(__ptr) - offsetof(type, member)); \
169 	}))
170 
171 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
172 
173 #ifdef __ASSEMBLER__
174 #define BIT32(nr)		(1 << (nr))
175 #define BIT64(nr)		(1 << (nr))
176 #define SHIFT_U32(v, shift)	((v) << (shift))
177 #define SHIFT_U64(v, shift)	((v) << (shift))
178 #else
179 #define BIT32(nr)		(UINT32_C(1) << (nr))
180 #define BIT64(nr)		(UINT64_C(1) << (nr))
181 #define SHIFT_U32(v, shift)	((uint32_t)(v) << (shift))
182 #define SHIFT_U64(v, shift)	((uint64_t)(v) << (shift))
183 #endif
184 #define BIT(nr)			BIT32(nr)
185 
186 /*
187  * Create a contiguous bitmask starting at bit position @l and ending at
188  * position @h. For example
189  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
190  */
191 #define GENMASK_32(h, l) \
192 	((UINT32_C(0xffffffff) << (l)) & \
193 	 (UINT32_C(0xffffffff) >> (32 - 1 - (h))))
194 
195 #define GENMASK_64(h, l) \
196 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
197 
198 /*
199  * Checking overflow for addition, subtraction and multiplication. Result
200  * of operation is stored in res which is a pointer to some kind of
201  * integer.
202  *
203  * The macros return true if an overflow occurred and *res is undefined.
204  */
205 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res))
206 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res))
207 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res))
208 
209 /* Return a signed +1, 0 or -1 value based on data comparison */
210 #define CMP_TRILEAN(a, b) \
211 	(__extension__({ \
212 		__typeof__(a) _a = (a); \
213 		__typeof__(b) _b = (b); \
214 		\
215 		_a > _b ? 1 : _a < _b ? -1 : 0; \
216 	}))
217 
218 #ifndef __ASSEMBLER__
219 static inline uint64_t reg_pair_to_64(uint32_t reg0, uint32_t reg1)
220 {
221 	return (uint64_t)reg0 << 32 | reg1;
222 }
223 
224 static inline uint32_t high32_from_64(uint64_t val)
225 {
226 	return val >> 32;
227 }
228 
229 static inline uint32_t low32_from_64(uint64_t val)
230 {
231 	return val;
232 }
233 
234 static inline void reg_pair_from_64(uint64_t val, uint32_t *reg0,
235 				    uint32_t *reg1)
236 {
237 	*reg0 = high32_from_64(val);
238 	*reg1 = low32_from_64(val);
239 }
240 
241 /* Get and set bit fields  */
242 static inline uint32_t get_field_u32(uint32_t reg, uint32_t mask)
243 {
244 	return (reg & mask) / (mask & ~(mask - 1));
245 }
246 
247 static inline uint32_t set_field_u32(uint32_t reg, uint32_t mask, uint32_t val)
248 {
249 	return (reg & ~mask) | (val * (mask & ~(mask - 1)));
250 }
251 
252 static inline uint64_t get_field_u64(uint64_t reg, uint64_t mask)
253 {
254 	return (reg & mask) / (mask & ~(mask - 1));
255 }
256 
257 static inline uint64_t set_field_u64(uint64_t reg, uint64_t mask, uint64_t val)
258 {
259 	return (reg & ~mask) | (val * (mask & ~(mask - 1)));
260 }
261 
262 /* Helper function for qsort with standard types */
263 void qsort_int(int *aa, size_t n);
264 void qsort_uint(unsigned int *aa, size_t n);
265 void qsort_long(long int *aa, size_t n);
266 void qsort_ul(unsigned long int *aa, size_t n);
267 void qsort_ll(long long int *aa, size_t n);
268 void qsort_ull(unsigned long long int *aa, size_t n);
269 void qsort_s8(int8_t *aa, size_t n);
270 void qsort_u8(uint8_t *aa, size_t n);
271 void qsort_s16(int16_t *aa, size_t n);
272 void qsort_u16(uint16_t *aa, size_t n);
273 void qsort_s32(int32_t *aa, size_t n);
274 void qsort_u32(uint32_t *aa, size_t n);
275 void qsort_s64(int64_t *aa, size_t n);
276 void qsort_u64(uint64_t *aa, size_t n);
277 #endif
278 
279 #endif /*UTIL_H*/
280