xref: /rockchip-linux_mpp/osal/inc/mpp_common.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2015 Rockchip Electronics Co., Ltd.
4  */
5 
6 #ifndef __MPP_COMMON_H__
7 #define __MPP_COMMON_H__
8 
9 #include "rk_type.h"
10 
11 #define MPP_TAG_SIZE            32
12 
13 #define MPP_ABS(x)              ((x) < (0) ? -(x) : (x))
14 
15 #define MPP_MAX(a, b)           ((a) > (b) ? (a) : (b))
16 #define MPP_MAX3(a, b, c)       MPP_MAX(MPP_MAX(a,b),c)
17 #define MPP_MAX4(a, b, c, d)    MPP_MAX((a), MPP_MAX3((b), (c), (d)))
18 
19 #define MPP_MIN(a,b)            ((a) > (b) ? (b) : (a))
20 #define MPP_MIN3(a,b,c)         MPP_MIN(MPP_MIN(a,b),c)
21 #define MPP_MIN4(a, b, c, d)    MPP_MIN((a), MPP_MIN3((b), (c), (d)))
22 
23 #define MPP_DIV(a, b)           ((b) ? (a) / (b) : (a))
24 
25 #define MPP_CLIP3(l, h, v)      ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
26 #define MPP_SIGN(a)             ((a) < (0) ? (-1) : (1))
27 #define MPP_DIV_SIGN(a, b)      (((a) + (MPP_SIGN(a) * (b)) / 2) / (b))
28 
29 #define MPP_SWAP(type, a, b)    do {type SWAP_tmp = b; b = a; a = SWAP_tmp;} while(0)
30 #define MPP_ARRAY_ELEMS(a)      (sizeof(a) / sizeof((a)[0]))
31 #define MPP_ALIGN(x, a)         (((x)+(a)-1)&~((a)-1))
32 #define MPP_ALIGN_DOWN(x, a)    ((x)&~((a)-1))
33 #define MPP_ALIGN_GEN(x, a)     (((x)+(a)-1)/(a)*(a))
34 #define MPP_VSWAP(a, b)         { a ^= b; b ^= a; a ^= b; }
35 
36 #define MPP_GENMASK(h, l) (((1ULL << ((h) + 1)) - 1) & ~((1ULL << (l)) - 1))
37 
38 #define MPP_RB16(x)  ((((const RK_U8*)(x))[0] << 8) | ((const RK_U8*)(x))[1])
39 #define MPP_WB16(p, d) do { \
40         ((RK_U8*)(p))[1] = (d); \
41         ((RK_U8*)(p))[0] = (d)>>8; } while(0)
42 
43 #define MPP_RL16(x)  ((((const RK_U8*)(x))[1] << 8) | \
44                      ((const RK_U8*)(x))[0])
45 #define MPP_WL16(p, d) do { \
46         ((RK_U8*)(p))[0] = (d); \
47         ((RK_U8*)(p))[1] = (d)>>8; } while(0)
48 
49 #define MPP_RB32(x)  ((((const RK_U8*)(x))[0] << 24) | \
50                      (((const RK_U8*)(x))[1] << 16) | \
51                      (((const RK_U8*)(x))[2] <<  8) | \
52                      ((const RK_U8*)(x))[3])
53 #define MPP_WB32(p, d) do { \
54         ((RK_U8*)(p))[3] = (d); \
55         ((RK_U8*)(p))[2] = (d)>>8; \
56         ((RK_U8*)(p))[1] = (d)>>16; \
57         ((RK_U8*)(p))[0] = (d)>>24; } while(0)
58 
59 #define MPP_RL32(x) ((((const RK_U8*)(x))[3] << 24) | \
60                     (((const RK_U8*)(x))[2] << 16) | \
61                     (((const RK_U8*)(x))[1] <<  8) | \
62                     ((const RK_U8*)(x))[0])
63 #define MPP_WL32(p, d) do { \
64         ((RK_U8*)(p))[0] = (d); \
65         ((RK_U8*)(p))[1] = (d)>>8; \
66         ((RK_U8*)(p))[2] = (d)>>16; \
67         ((RK_U8*)(p))[3] = (d)>>24; } while(0)
68 
69 #define MPP_RB64(x)  (((RK_U64)((const RK_U8*)(x))[0] << 56) | \
70                      ((RK_U64)((const RK_U8*)(x))[1] << 48) | \
71                      ((RK_U64)((const RK_U8*)(x))[2] << 40) | \
72                      ((RK_U64)((const RK_U8*)(x))[3] << 32) | \
73                      ((RK_U64)((const RK_U8*)(x))[4] << 24) | \
74                      ((RK_U64)((const RK_U8*)(x))[5] << 16) | \
75                      ((RK_U64)((const RK_U8*)(x))[6] <<  8) | \
76                      (RK_U64)((const RK_U8*)(x))[7])
77 #define MPP_WB64(p, d) do { \
78         ((RK_U8*)(p))[7] = (d);     \
79         ((RK_U8*)(p))[6] = (d)>>8;  \
80         ((RK_U8*)(p))[5] = (d)>>16; \
81         ((RK_U8*)(p))[4] = (d)>>24; \
82         ((RK_U8*)(p))[3] = (d)>>32; \
83         ((RK_U8*)(p))[2] = (d)>>40; \
84         ((RK_U8*)(p))[1] = (d)>>48; \
85         ((RK_U8*)(p))[0] = (d)>>56; } while(0)
86 
87 #define MPP_RL64(x)  (((RK_U64)((const RK_U8*)(x))[7] << 56) | \
88                      ((RK_U64)((const RK_U8*)(x))[6] << 48) | \
89                      ((RK_U64)((const RK_U8*)(x))[5] << 40) | \
90                      ((RK_U64)((const RK_U8*)(x))[4] << 32) | \
91                      ((RK_U64)((const RK_U8*)(x))[3] << 24) | \
92                      ((RK_U64)((const RK_U8*)(x))[2] << 16) | \
93                      ((RK_U64)((const RK_U8*)(x))[1] <<  8) | \
94                      (RK_U64)((const RK_U8*)(x))[0])
95 #define MPP_WL64(p, d) do { \
96         ((RK_U8*)(p))[0] = (d);     \
97         ((RK_U8*)(p))[1] = (d)>>8;  \
98         ((RK_U8*)(p))[2] = (d)>>16; \
99         ((RK_U8*)(p))[3] = (d)>>24; \
100         ((RK_U8*)(p))[4] = (d)>>32; \
101         ((RK_U8*)(p))[5] = (d)>>40; \
102         ((RK_U8*)(p))[6] = (d)>>48; \
103         ((RK_U8*)(p))[7] = (d)>>56; } while(0)
104 
105 #define MPP_RB24(x)  ((((const RK_U8*)(x))[0] << 16) | \
106                      (((const RK_U8*)(x))[1] <<  8) | \
107                      ((const RK_U8*)(x))[2])
108 #define MPP_WB24(p, d) do { \
109         ((RK_U8*)(p))[2] = (d); \
110         ((RK_U8*)(p))[1] = (d)>>8; \
111         ((RK_U8*)(p))[0] = (d)>>16; } while(0)
112 
113 #define MPP_RL24(x)  ((((const RK_U8*)(x))[2] << 16) | \
114                      (((const RK_U8*)(x))[1] <<  8) | \
115                      ((const RK_U8*)(x))[0])
116 
117 #define MPP_WL24(p, d) do { \
118         ((RK_U8*)(p))[0] = (d); \
119         ((RK_U8*)(p))[1] = (d)>>8; \
120         ((RK_U8*)(p))[2] = (d)>>16; } while(0)
121 
122 #include <stdio.h>
123 #if defined(_WIN32) && !defined(__MINGW32CE__)
124 #define snprintf                _snprintf
125 #define fseeko                  _fseeki64
126 
127 #include <direct.h>
128 #include <io.h>
129 #include <sys/stat.h>
130 
131 #define chdir                   _chdir
132 #define mkdir                   _mkdir
133 #define access                  _access
134 #define off_t                   _off_t
135 
136 #define R_OK                    4 /* Test for read permission. */
137 #define W_OK                    2 /* Test for write permission. */
138 #define X_OK                    1 /* Test for execute permission. */
139 #define F_OK                    0 /* Test for existence. */
140 
141 #elif defined(__MINGW32CE__)
142 #define fseeko                  fseeko64
143 #else
144 #include <unistd.h>
145 #include <stddef.h>
146 #include <fcntl.h>
147 #include <sys/stat.h>
148 #include <sys/types.h>
149 #define mkdir(x)                mkdir(x, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
150 #endif
151 
152 #define container_of(ptr, type, member) \
153     ((type *)((char *)(ptr) - offsetof(type, member)))
154 
155 #define __RETURN                __Return
156 #define __FAILED                __failed
157 
158 #define ARG_T(t)                t
159 #define ARG_N(a,b,c,d,N,...)    N
160 #define ARG_N_HELPER(...)       ARG_T(ARG_N(__VA_ARGS__))
161 #define COUNT_ARG(...)          ARG_N_HELPER(__VA_ARGS__,4,3,2,1,0)
162 
163 #define SZ_1K                   (1024)
164 #define SZ_2K                   (SZ_1K*2)
165 #define SZ_4K                   (SZ_1K*4)
166 #define SZ_8K                   (SZ_1K*8)
167 #define SZ_16K                  (SZ_1K*16)
168 #define SZ_32K                  (SZ_1K*32)
169 #define SZ_64K                  (SZ_1K*64)
170 #define SZ_128K                 (SZ_1K*128)
171 #define SZ_256K                 (SZ_1K*256)
172 #define SZ_512K                 (SZ_1K*512)
173 #define SZ_1M                   (SZ_1K*SZ_1K)
174 #define SZ_2M                   (SZ_1M*2)
175 #define SZ_4M                   (SZ_1M*4)
176 #define SZ_8M                   (SZ_1M*8)
177 #define SZ_16M                  (SZ_1M*16)
178 #define SZ_32M                  (SZ_1M*32)
179 #define SZ_64M                  (SZ_1M*64)
180 #define SZ_80M                  (SZ_1M*80)
181 #define SZ_128M                 (SZ_1M*128)
182 
183 #ifdef __cplusplus
184 extern "C" {
185 #endif
186 
187 RK_S32 mpp_log2(RK_U32 v);
188 RK_S32 mpp_log2_16bit(RK_U32 v);
189 
mpp_ceil_log2(RK_S32 x)190 static __inline RK_S32 mpp_ceil_log2(RK_S32 x)
191 {
192     return mpp_log2((x - 1) << 1);
193 }
194 
mpp_clip(RK_S32 a,RK_S32 amin,RK_S32 amax)195 static __inline RK_S32 mpp_clip(RK_S32 a, RK_S32 amin, RK_S32 amax)
196 {
197     if      (a < amin) return amin;
198     else if (a > amax) return amax;
199     else               return a;
200 }
201 
mpp_is_32bit()202 static __inline RK_U32 mpp_is_32bit()
203 {
204     return ((sizeof(void *) == 4) ? (1) : (0));
205 }
206 
mpp_dup(RK_S32 fd)207 static __inline RK_S32 mpp_dup(RK_S32 fd)
208 {
209     /* avoid stdin / stdout / stderr so start from 3 */
210 #ifdef F_DUPFD_CLOEXEC
211     return fcntl(fd, F_DUPFD_CLOEXEC, 3);
212 #else
213     RK_S32 new_fd = -1;
214 
215     new_fd = fcntl(fd, F_DUPFD, 3);
216     if (new_fd == -1)
217         return -1;
218 
219     if (fcntl(new_fd, F_SETFD, FD_CLOEXEC) == -1)
220         return -1;
221 
222     return new_fd;
223 #endif
224 }
225 
226 RK_S32 axb_div_c(RK_S32 a, RK_S32 b, RK_S32 c);
227 RK_U32 mpp_align_16(RK_U32 val);
228 RK_U32 mpp_align_64(RK_U32 val);
229 RK_U32 mpp_align_128(RK_U32 val);
230 RK_U32 mpp_align_256_odd(RK_U32 val);
231 RK_U32 mpp_align_128_odd_plus_64(RK_U32 val);
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 #endif /*__MPP_COMMON_H__*/
238 
239