xref: /optee_os/lib/libutils/ext/include/util.h (revision 62f21181c547da3bd098908300e5699e9ae5cca9)
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 ASM
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 ASM
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 /* Round down the even multiple of size, size has to be a multiple of 2 */
51 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1))
52 #else
53 #define ROUNDUP(x, y)			((((x) + (y) - 1) / (y)) * (y))
54 #define ROUNDDOWN(x, y)		(((x) / (y)) * (y))
55 #endif
56 
57 /* x has to be of an unsigned type */
58 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x)))
59 
60 #define ALIGNMENT_IS_OK(p, type) \
61 	(((uintptr_t)(p) & (__alignof__(type) - 1)) == 0)
62 
63 #define TO_STR(x) _TO_STR(x)
64 #define _TO_STR(x) #x
65 
66 #define CONCAT(x, y) _CONCAT(x, y)
67 #define _CONCAT(x, y) x##y
68 
69 #define container_of(ptr, type, member) \
70 	(__extension__({ \
71 		const typeof(((type *)0)->member) *__ptr = (ptr); \
72 		(type *)((unsigned long)(__ptr) - offsetof(type, member)); \
73 	}))
74 
75 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
76 
77 #ifdef ASM
78 #define BIT32(nr)		(1 << (nr))
79 #define BIT64(nr)		(1 << (nr))
80 #define SHIFT_U32(v, shift)	((v) << (shift))
81 #define SHIFT_U64(v, shift)	((v) << (shift))
82 #else
83 #define BIT32(nr)		(UINT32_C(1) << (nr))
84 #define BIT64(nr)		(UINT64_C(1) << (nr))
85 #define SHIFT_U32(v, shift)	((uint32_t)(v) << (shift))
86 #define SHIFT_U64(v, shift)	((uint64_t)(v) << (shift))
87 #endif
88 #define BIT(nr)			BIT32(nr)
89 
90 /*
91  * Create a contiguous bitmask starting at bit position @l and ending at
92  * position @h. For example
93  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
94  */
95 #define GENMASK_32(h, l) \
96 	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
97 
98 #define GENMASK_64(h, l) \
99 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
100 
101 /*
102  * Checking overflow for addition, subtraction and multiplication. Result
103  * of operation is stored in res which is a pointer to some kind of
104  * integer.
105  *
106  * The macros return true if an overflow occurred and *res is undefined.
107  */
108 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res))
109 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res))
110 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res))
111 
112 /* Return a signed +1, 0 or -1 value based on data comparison */
113 #define CMP_TRILEAN(a, b) \
114 	(__extension__({ \
115 		__typeof__(a) _a = (a); \
116 		__typeof__(b) _b = (b); \
117 		\
118 		_a > _b ? 1 : _a < _b ? -1 : 0; \
119 	}))
120 
121 #endif /*UTIL_H*/
122