xref: /optee_os/lib/libutils/ext/include/util.h (revision 2770e2422a28f1e7b3a68d05fd5a3aa3c23428be)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef UTIL_H
29 #define UTIL_H
30 
31 #include <compiler.h>
32 #include <stdint.h>
33 
34 #define SIZE_4K	UINTPTR_C(0x1000)
35 #define SIZE_1M	UINTPTR_C(0x100000)
36 #define SIZE_2M	UINTPTR_C(0x200000)
37 #define SIZE_4M	UINTPTR_C(0x400000)
38 #define SIZE_8M	UINTPTR_C(0x800000)
39 #define SIZE_2G	UINTPTR_C(0x80000000)
40 
41 #ifndef MAX
42 #ifndef ASM
43 #define MAX(a, b) \
44 	(__extension__({ __typeof__(a) _a = (a); \
45 	   __typeof__(b) _b = (b); \
46 	 _a > _b ? _a : _b; }))
47 
48 #define MIN(a, b) \
49 	(__extension__({ __typeof__(a) _a = (a); \
50 	   __typeof__(b) _b = (b); \
51 	 _a < _b ? _a : _b; }))
52 #else
53 #define MAX(a, b)	(((a) > (b)) ? (a) : (b))
54 #define MIN(a, b)	(((a) < (b)) ? (a) : (b))
55 #endif
56 #endif
57 
58 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
59 
60 #ifndef ASM
61 /* Round up the even multiple of size, size has to be a multiple of 2 */
62 #define ROUNDUP(v, size) (((v) + ((__typeof__(v))(size) - 1)) & \
63 			  ~((__typeof__(v))(size) - 1))
64 
65 /* Round down the even multiple of size, size has to be a multiple of 2 */
66 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1))
67 #else
68 #define ROUNDUP(x, y)			((((x) + (y) - 1) / (y)) * (y))
69 #define ROUNDDOWN(x, y)		(((x) / (y)) * (y))
70 #endif
71 
72 /* x has to be of an unsigned type */
73 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x)))
74 
75 #define ALIGNMENT_IS_OK(p, type) \
76 	(((uintptr_t)(p) & (__alignof__(type) - 1)) == 0)
77 
78 #define TO_STR(x) _TO_STR(x)
79 #define _TO_STR(x) #x
80 
81 #define CONCAT(x, y) _CONCAT(x, y)
82 #define _CONCAT(x, y) x##y
83 
84 #define container_of(ptr, type, member) \
85 	(__extension__({ \
86 		const typeof(((type *)0)->member) *__ptr = (ptr); \
87 		(type *)((unsigned long)(__ptr) - offsetof(type, member)); \
88 	}))
89 
90 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
91 
92 #ifdef ASM
93 #define BIT32(nr)		(1 << (nr))
94 #define BIT64(nr)		(1 << (nr))
95 #define SHIFT_U32(v, shift)	((v) << (shift))
96 #define SHIFT_U64(v, shift)	((v) << (shift))
97 #else
98 #define BIT32(nr)		(UINT32_C(1) << (nr))
99 #define BIT64(nr)		(UINT64_C(1) << (nr))
100 #define SHIFT_U32(v, shift)	((uint32_t)(v) << (shift))
101 #define SHIFT_U64(v, shift)	((uint64_t)(v) << (shift))
102 #endif
103 #define BIT(nr)			BIT32(nr)
104 
105 /*
106  * Create a contiguous bitmask starting at bit position @l and ending at
107  * position @h. For example
108  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
109  */
110 #define GENMASK_32(h, l) \
111 	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
112 
113 #define GENMASK_64(h, l) \
114 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
115 
116 /*
117  * Checking overflow for addition, subtraction and multiplication. Result
118  * of operation is stored in res which is a pointer to some kind of
119  * integer.
120  *
121  * The macros return true if an overflow occurred and *res is undefined.
122  */
123 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res))
124 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res))
125 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res))
126 
127 /* Return a signed +1, 0 or -1 value based on data comparison */
128 #define CMP_TRILEAN(a, b) \
129 	(__extension__({ \
130 		__typeof__(a) _a = (a); \
131 		__typeof__(b) _b = (b); \
132 		\
133 		_a > _b ? 1 : _a < _b ? -1 : 0; \
134 	}))
135 
136 #endif /*UTIL_H*/
137