1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #include <stdbool.h>
22 #include <common/bk_include.h>
23 #include <common/bk_assert.h>
24
25 typedef void (*FUNCPTR)(void);
26 typedef void (*FUNC_1PARAM_PTR)(void *ctxt);
27 typedef void (*FUNC_2PARAM_PTR)(void *arg, uint8_t vif_idx);
28
29 #ifndef MAX
30 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
31 #endif
32
33 #ifndef MIN
34 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
35 #endif
36
37 #ifndef max
38 #define max(x, y) (((x) > (y)) ? (x) : (y))
39 #endif
40
41 #ifndef min
42 #define min(x, y) (((x) < (y)) ? (x) : (y))
43 #endif
44
45 #define min3(x, y, z) ({ \
46 typeof(x) _min1 = (x); \
47 typeof(y) _min2 = (y); \
48 typeof(z) _min3 = (z); \
49 (void) (&_min1 == &_min2); \
50 (void) (&_min1 == &_min3); \
51 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
52 (_min2 < _min3 ? _min2 : _min3); })
53
54 #define max3(x, y, z) ({ \
55 typeof(x) _max1 = (x); \
56 typeof(y) _max2 = (y); \
57 typeof(z) _max3 = (z); \
58 (void) (&_max1 == &_max2); \
59 (void) (&_max1 == &_max3); \
60 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
61 (_max2 > _max3 ? _max2 : _max3); })
62
63 /*
64 * ..and if you can't take the strict
65 * types, you can specify one yourself.
66 *
67 * Or not use min/max/clamp at all, of course.
68 */
69 #define min_t(type, x, y) ({ \
70 type __min1 = (x); \
71 type __min2 = (y); \
72 __min1 < __min2 ? __min1: __min2; })
73
74 #define max_t(type, x, y) ({ \
75 type __max1 = (x); \
76 type __max2 = (y); \
77 __max1 > __max2 ? __max1: __max2; })
78
79
80 /*
81 * swap - swap value of @a and @b
82 */
83 #define swap(a, b) \
84 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
85
86 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
87
88 //##define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
89 #ifndef ARRAY_SIZE
90 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
91 #endif
92 /*
93 * This looks more complex than it should be. But we need to
94 * get the type for the ~ right in round_down (it needs to be
95 * as wide as the result!), and we want to evaluate the macro
96 * arguments just once each.
97 */
98 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
99 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
100 #define round_down(x, y) ((x) & ~__round_mask(x, y))
101
102 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
103 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
104
105 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
106 #define roundup(x, y) ( \
107 { \
108 const typeof(y) __y = y; \
109 (((x) + (__y - 1)) / __y) * __y; \
110 } \
111 )
112 #define rounddown(x, y) ( \
113 { \
114 typeof(x) __x = (x); \
115 __x - (__x % (y)); \
116 } \
117 )
118 #define DIV_ROUND_CLOSEST(x, divisor)( \
119 { \
120 typeof(divisor) __divisor = divisor; \
121 (((x) + ((__divisor) / 2)) / (__divisor)); \
122 } \
123 )
124
125 /**
126 * container_of - cast a member of a structure out to the containing structure
127 * @ptr: the pointer to the member.
128 * @type: the type of the container struct this is embedded in.
129 * @member: the name of the member within the struct.
130 *
131 */
132 #ifdef container_of
133 #undef container_of
134 #endif
135 #define container_of(ptr, type, member) \
136 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
137
138
139 #ifndef NULL
140 #define NULL (0L)
141 #endif
142
143 #ifndef NULLPTR
144 #define NULLPTR ((void *)0)
145 #endif
146
147 //#define BIT(i) (1UL << (i))
148 #define BIT64(i) (1LL << (i))
149
__bswap16(__uint16_t _x)150 static inline __uint16_t __bswap16(__uint16_t _x)
151 {
152
153 return ((__uint16_t)((_x >> 8) | ((_x << 8) & 0xff00)));
154 }
155
__bswap32(__uint32_t _x)156 static inline __uint32_t __bswap32(__uint32_t _x)
157 {
158
159 return ((__uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) |
160 ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000)));
161 }
162
__bswap64(__uint64_t _x)163 static inline __uint64_t __bswap64(__uint64_t _x)
164 {
165
166 return ((__uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) |
167 ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) |
168 ((_x << 8) & ((__uint64_t)0xff << 32)) |
169 ((_x << 24) & ((__uint64_t)0xff << 40)) |
170 ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))));
171 }
172
173 #define __swab16(x) __bswap16((__u8 *)&(x))
174 #define __swab32(x) __bswap32((__u8 *)&(x))
175
176 #define cpu_to_le16(x) (x)
177 #define cpu_to_le32(x) (x)
178
179 #define __cpu_to_be32(x) __swab32((x))
180 #define __be32_to_cpu(x) __swab32((x))
181 #define __cpu_to_be16(x) __swab16((x))
182 #define __be16_to_cpu(x) __swab16((x))
183
184 #define __htonl(_x) __bswap32(_x)
185 #define __htons(_x) __bswap16(_x)
186 #define __ntohl(_x) __bswap32(_x)
187 #define __ntohs(_x) __bswap16(_x)
188
189 #define ___htonl(x) __cpu_to_be32(x)
190 #define ___htons(x) __cpu_to_be16(x)
191 #define ___ntohl(x) __be32_to_cpu(x)
192 #define ___ntohs(x) __be16_to_cpu(x)
193
194 #if (!CONFIG_RTT)
195 #define htons(x) __htons(x)
196 #define ntohs(x) __ntohs(x)
197 #define htonl(x) __htonl(x)
198 #define ntohl(x) __ntohl(x)
199 #endif
200
201 #ifndef __maybe_unused
202 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
203 #define __maybe_unused __attribute__((unused))
204 #else
205 #define __maybe_unused
206 #endif /* __GNUC__ */
207 #endif /* __maybe_unused */
208
209 #ifndef __maybe_unused_var
210 #define __maybe_unused_var(_var) do {\
211 (void)(_var);\
212 } while(0)
213 #endif
214
215 #ifdef __cplusplus
216 /* Following Macro are all defined in standard c++ header. So undef them when compiling in c++ project. */
217 #undef min
218 #undef max
219 #undef MAX
220 #undef MIN
221 #undef swap
222 }
223 #endif
224