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