xref: /optee_os/lib/libutils/ext/include/util.h (revision 5b25c76ac40f830867e3d60800120ffd7874e8dc)
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 #define SIZE_4K	UINTPTR_C(0x1000)
12 #define SIZE_1M	UINTPTR_C(0x100000)
13 #define SIZE_2M	UINTPTR_C(0x200000)
14 #define SIZE_4M	UINTPTR_C(0x400000)
15 #define SIZE_8M	UINTPTR_C(0x800000)
16 #define SIZE_2G	UINTPTR_C(0x80000000)
17 
18 #ifndef MAX
19 #ifndef __ASSEMBLER__
20 #define MAX(a, b) \
21 	(__extension__({ __typeof__(a) _a = (a); \
22 	   __typeof__(b) _b = (b); \
23 	 _a > _b ? _a : _b; }))
24 
25 #define MIN(a, b) \
26 	(__extension__({ __typeof__(a) _a = (a); \
27 	   __typeof__(b) _b = (b); \
28 	 _a < _b ? _a : _b; }))
29 #else
30 #define MAX(a, b)	(((a) > (b)) ? (a) : (b))
31 #define MIN(a, b)	(((a) < (b)) ? (a) : (b))
32 #endif
33 #endif
34 
35 /*
36  * In some particular conditions MAX and MIN macros fail to
37  * build from C source file implmentation. In such case one
38  * need to use MAX_UNSAFE/MIN_UNSAFE instead.
39  */
40 #define MAX_UNSAFE(a, b)	(((a) > (b)) ? (a) : (b))
41 #define MIN_UNSAFE(a, b)	(((a) < (b)) ? (a) : (b))
42 
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
44 
45 #ifndef __ASSEMBLER__
46 /* Round up the even multiple of size, size has to be a multiple of 2 */
47 #define ROUNDUP(v, size) (((v) + ((__typeof__(v))(size) - 1)) & \
48 			  ~((__typeof__(v))(size) - 1))
49 
50 #define ROUNDUP_OVERFLOW(v, size, res) (__extension__({ \
51 	typeof(*(res)) __roundup_tmp = 0; \
52 	typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
53 	\
54 	ADD_OVERFLOW((v), __roundup_mask, &__roundup_tmp) ? 1 : \
55 		(void)(*(res) = __roundup_tmp & ~__roundup_mask), 0; \
56 }))
57 
58 /*
59  * Rounds up to the nearest multiple of y and then divides by y. Safe
60  * against overflow, y has to be a multiple of 2.
61  *
62  * This macro is intended to be used to convert from "number of bytes" to
63  * "number of pages" or similar units. Example:
64  * num_pages = ROUNDUP_DIV(num_bytes, SMALL_PAGE_SIZE);
65  */
66 #define ROUNDUP_DIV(x, y) (__extension__({ \
67 	typeof(x) __roundup_x = (x); \
68 	typeof(y) __roundup_mask = (typeof(x))(y) - 1; \
69 	\
70 	(__roundup_x / (y)) + (__roundup_x & __roundup_mask ? 1 : 0); \
71 }))
72 
73 /* Round down the even multiple of size, size has to be a multiple of 2 */
74 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1))
75 
76 /* Unsigned integer division with nearest rounding variant */
77 #define UDIV_ROUND_NEAREST(x, y) \
78 	(__extension__ ({ __typeof__(x) _x = (x); \
79 	  __typeof__(y) _y = (y); \
80 	  (_x + (_y / 2)) / _y; }))
81 #else
82 #define ROUNDUP(x, y)			((((x) + (y) - 1) / (y)) * (y))
83 #define ROUNDDOWN(x, y)		(((x) / (y)) * (y))
84 #define UDIV_ROUND_NEAREST(x, y)	(((x) + ((y) / 2)) / (y))
85 #endif
86 
87 /* x has to be of an unsigned type */
88 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x)))
89 
90 #define ALIGNMENT_IS_OK(p, type) \
91 	(((uintptr_t)(p) & (__alignof__(type) - 1)) == 0)
92 
93 #define TO_STR(x) _TO_STR(x)
94 #define _TO_STR(x) #x
95 
96 #define CONCAT(x, y) _CONCAT(x, y)
97 #define _CONCAT(x, y) x##y
98 
99 #define container_of(ptr, type, member) \
100 	(__extension__({ \
101 		const typeof(((type *)0)->member) *__ptr = (ptr); \
102 		(type *)((unsigned long)(__ptr) - offsetof(type, member)); \
103 	}))
104 
105 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
106 
107 #ifdef __ASSEMBLER__
108 #define BIT32(nr)		(1 << (nr))
109 #define BIT64(nr)		(1 << (nr))
110 #define SHIFT_U32(v, shift)	((v) << (shift))
111 #define SHIFT_U64(v, shift)	((v) << (shift))
112 #else
113 #define BIT32(nr)		(UINT32_C(1) << (nr))
114 #define BIT64(nr)		(UINT64_C(1) << (nr))
115 #define SHIFT_U32(v, shift)	((uint32_t)(v) << (shift))
116 #define SHIFT_U64(v, shift)	((uint64_t)(v) << (shift))
117 #endif
118 #define BIT(nr)			BIT32(nr)
119 
120 /*
121  * Create a contiguous bitmask starting at bit position @l and ending at
122  * position @h. For example
123  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
124  */
125 #define GENMASK_32(h, l) \
126 	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
127 
128 #define GENMASK_64(h, l) \
129 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
130 
131 /*
132  * Checking overflow for addition, subtraction and multiplication. Result
133  * of operation is stored in res which is a pointer to some kind of
134  * integer.
135  *
136  * The macros return true if an overflow occurred and *res is undefined.
137  */
138 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res))
139 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res))
140 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res))
141 
142 /* Return a signed +1, 0 or -1 value based on data comparison */
143 #define CMP_TRILEAN(a, b) \
144 	(__extension__({ \
145 		__typeof__(a) _a = (a); \
146 		__typeof__(b) _b = (b); \
147 		\
148 		_a > _b ? 1 : _a < _b ? -1 : 0; \
149 	}))
150 
151 #ifndef __ASSEMBLER__
152 static inline uint64_t reg_pair_to_64(uint32_t reg0, uint32_t reg1)
153 {
154 	return (uint64_t)reg0 << 32 | reg1;
155 }
156 
157 static inline void reg_pair_from_64(uint64_t val, uint32_t *reg0,
158 				    uint32_t *reg1)
159 {
160 	*reg0 = val >> 32;
161 	*reg1 = val;
162 }
163 #endif
164 
165 #endif /*UTIL_H*/
166