1 /*!
2 * \copy
3 * Copyright (c) 2009-2013, Cisco Systems
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
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * \file macros.h
33 *
34 * \brief MACRO based tool utilization
35 *
36 * \date 3/13/2009 Created
37 *
38 *************************************************************************************
39 */
40 #ifndef WELS_MACRO_UTILIZATIONS_H__
41 #define WELS_MACRO_UTILIZATIONS_H__
42
43 #include <math.h>
44 #include <assert.h>
45 #include <string.h>
46 #include "typedefs.h"
47
48
49 /*
50 * ENFORCE_STACK_ALIGN_1D: force 1 dimension local data aligned in stack
51 * _tp: type
52 * _nm: var name
53 * _sz: size
54 * _al: align bytes
55 * auxiliary var: _nm ## _tEmP
56 */
57 #define ENFORCE_STACK_ALIGN_1D(_tp, _nm, _sz, _al) \
58 _tp _nm ## _tEmP[(_sz)+(_al)-1]; \
59 _tp *_nm = _nm ## _tEmP + ((_al)-1) - (((uintptr_t)(_nm ## _tEmP + ((_al)-1)) & ((_al)-1))/sizeof(_tp));
60
61
62 #define ENFORCE_STACK_ALIGN_2D(_tp, _nm, _cx, _cy, _al) \
63 assert( ((_al) && !((_al) & ((_al) - 1))) && ((_al) >= sizeof(_tp)) ); /*_al should be power-of-2 and >= sizeof(_tp)*/\
64 _tp _nm ## _tEmP[(_cx)*(_cy)+(_al)/sizeof(_tp)-1]; \
65 _tp *_nm ## _tEmP_al = _nm ## _tEmP + ((_al)/sizeof(_tp)-1); \
66 _nm ## _tEmP_al -= (((uintptr_t)_nm ## _tEmP_al & ((_al)-1))/sizeof(_tp)); \
67 _tp (*_nm)[(_cy)] = (_tp (*)[(_cy)])_nm ## _tEmP_al;
68
69
70 #if defined(_MSC_VER)
71
72 #if(_MSC_VER < 1700)
73 #define inline __inline
74 #endif
75
76 #define ALIGNED_DECLARE( type, var, n ) __declspec(align(n)) type var
77
78 #elif defined(__GNUC__)
79
80 #define ALIGNED_DECLARE( type, var, n ) type var __attribute__((aligned(n)))
81 #endif//_MSC_VER
82
83
84 #ifndef WELS_ALIGN
85 #define WELS_ALIGN(x, n) (((x)+(n)-1)&~((n)-1))
86 #endif//WELS_ALIGN
87
88
89 #if 1 // Alternative implementation of WELS_MAX and WELS_MIN
90 #ifndef WELS_MAX
91 #define WELS_MAX(x, y) ((x) > (y) ? (x) : (y))
92 #endif//WELS_MAX
93
94 #ifndef WELS_MIN
95 #define WELS_MIN(x, y) ((x) < (y) ? (x) : (y))
96 #endif//WELS_MIN
97 #ifndef WELS_MIN_POSITIVE
98 #define WELS_MIN_POSITIVE(x, y) (x >= 0 && y >= 0) ? WELS_MIN(x, y) : WELS_MAX(x, y);
99 #endif//WELS_MIN_POSITIVE
100 #else // Alternative implementation of WELS_MAX and WELS_MIN
101 #ifndef WELS_MAX
102 #define WELS_MAX(x, y) ((x) - (((x)-(y))&(((x)-(y))>>31)))
103 #endif//WELS_MAX
104
105 #ifndef WELS_MIN
106 #define WELS_MIN(x, y) ((y) + (((x)-(y))&(((x)-(y))>>31)))
107 #endif//WELS_MIN
108 #endif // Alternative implementation of WELS_MAX and WELS_MIN
109
110
111 #ifndef WELS_CEIL
112 #define WELS_CEIL(x) ceil(x) // FIXME: low complexity instead of math library used
113 #endif//WELS_CEIL
114
115 #ifndef WELS_FLOOR
116 #define WELS_FLOOR(x) floor(x) // FIXME: low complexity instead of math library used
117 #endif//WELS_FLOOR
118
119 #ifndef WELS_ROUND
120 #define WELS_ROUND(x) ((int32_t)(0.5+(x)))
121 #endif//WELS_ROUND
122
123 #ifndef WELS_ROUND64
124 #define WELS_ROUND64(x) ((int64_t)(0.5+(x)))
125 #endif//WELS_ROUND
126
127 #ifndef WELS_DIV_ROUND
128 #define WELS_DIV_ROUND(x,y) ((int32_t)((y)==0?((x)/((y)+1)):(((y)/2+(x))/(y))))
129 #endif//WELS_DIV_ROUND
130
131 #ifndef WELS_DIV_ROUND64
132 #define WELS_DIV_ROUND64(x,y) ((int64_t)((y)==0?((x)/((y)+1)):(((y)/2+(x))/(y))))
133 #endif//WELS_DIV_ROUND64
134
135 #define WELS_NON_ZERO_COUNT_AVERAGE(nC,nA,nB) { \
136 nC = nA + nB + 1; \
137 nC >>= (uint8_t)( nA != -1 && nB != -1); \
138 nC += (uint8_t)(nA == -1 && nB == -1); \
139 }
140
CeilLog2(int32_t i)141 static inline int32_t CeilLog2 (int32_t i) {
142 int32_t s = 0;
143 i--;
144 while (i > 0) {
145 s++;
146 i >>= 1;
147 }
148 return s;
149 }
150 /*
151 the second path will degrades the performance
152 */
153 #if 1
WelsMedian(int32_t iX,int32_t iY,int32_t iZ)154 static inline int32_t WelsMedian (int32_t iX, int32_t iY, int32_t iZ) {
155 int32_t iMin = iX, iMax = iX;
156
157 if (iY < iMin)
158 iMin = iY;
159 else
160 iMax = iY;
161
162 if (iZ < iMin)
163 iMin = iZ;
164 else if (iZ > iMax)
165 iMax = iZ;
166
167 return (iX + iY + iZ) - (iMin + iMax);
168 }
169 #else
WelsMedian(int32_t iX,int32_t iY,int32_t iZ)170 static inline int32_t WelsMedian (int32_t iX, int32_t iY, int32_t iZ) {
171 int32_t iTmp = (iX - iY) & ((iX - iY) >> 31);
172 iX -= iTmp;
173 iY += iTmp;
174 iY -= (iY - iZ) & ((iY - iZ) >> 31);
175 iY += (iX - iY) & ((iX - iY) >> 31);
176 return iY;
177 }
178
179 #endif
180
181 #ifndef NEG_NUM
182 //#define NEG_NUM( num ) (-num)
183 #define NEG_NUM(iX) (1+(~(iX)))
184 #endif// NEG_NUM
185
WelsClip1(int32_t iX)186 static inline uint8_t WelsClip1 (int32_t iX) {
187 uint8_t uiTmp = (uint8_t) (((iX) & ~255) ? (- (iX) >> 31) : (iX));
188 return uiTmp;
189 }
190
191 #ifndef WELS_SIGN
192 #define WELS_SIGN(iX) ((int32_t)(iX) >> 31)
193 #endif //WELS_SIGN
194 #ifndef WELS_ABS
195 #if 1
196 #define WELS_ABS(iX) ((iX)>0 ? (iX) : -(iX))
197 #else
198 #define WELS_ABS(iX) ((WELS_SIGN(iX) ^ (int32_t)(iX)) - WELS_SIGN(iX))
199 #endif
200 #endif //WELS_ABS
201
202 // WELS_CLIP3
203 #ifndef WELS_CLIP3
204 #define WELS_CLIP3(iX, iY, iZ) ((iX) < (iY) ? (iY) : ((iX) > (iZ) ? (iZ) : (iX)))
205 #endif //WELS_CLIP3
206
WelsClip3(T iX,T iY,T iZ)207 template<typename T> T WelsClip3(T iX, T iY, T iZ) {
208 if (iX < iY)
209 return iY;
210 if (iX > iZ)
211 return iZ;
212 return iX;
213 }
214
215 #define DISALLOW_COPY_AND_ASSIGN(cclass) \
216 private: \
217 cclass(const cclass &); \
218 cclass& operator=(const cclass &);
219
220 /*
221 * Description: to check variable validation and return the specified result
222 * iResult: value to be checked
223 * iExpected: the expected value
224 */
225 #ifndef WELS_VERIFY_RETURN_IFNEQ
226 #define WELS_VERIFY_RETURN_IFNEQ(iResult, iExpected) \
227 if (iResult != iExpected) { \
228 return iResult; \
229 }
230 #endif//#if WELS_VERIFY_RETURN_IF
231
232 /*
233 * Description: to check variable validation and return the specified result
234 * iResult: value to be return
235 * bCaseIf: negative condition to be verified
236 */
237 #ifndef WELS_VERIFY_RETURN_IF
238 #define WELS_VERIFY_RETURN_IF(iResult, bCaseIf) \
239 if (bCaseIf) { \
240 return iResult; \
241 }
242 #endif//#if WELS_VERIFY_RETURN_IF
243
244 /*
245 * Description: to check variable validation and return the specified result
246 * with correspoinding process advance.
247 * result: value to be return
248 * case_if: negative condition to be verified
249 * proc: process need perform
250 */
251 #ifndef WELS_VERIFY_RETURN_PROC_IF
252 #define WELS_VERIFY_RETURN_PROC_IF(iResult, bCaseIf, fProc) \
253 if (bCaseIf) { \
254 fProc; \
255 return iResult; \
256 }
257 #endif//#if WELS_VERIFY_RETURN_PROC_IF
258
WELS_LOG2(uint32_t v)259 static inline int32_t WELS_LOG2 (uint32_t v) {
260 int32_t r = 0;
261 while (v >>= 1) {
262 ++r;
263 }
264 return r;
265
266 }
267
268 #define CLIP3_QP_0_51(q) WELS_CLIP3(q, 0, 51) // ((q) < (0) ? (0) : ((q) > (51) ? (51) : (q)))
269 #define CALC_BI_STRIDE(width,bitcount) ((((width * bitcount) + 31) & ~31) >> 3)
270
271
272
273
274 #ifndef BUTTERFLY1x2
275 #define BUTTERFLY1x2(b) (((b)<<8) | (b))
276 #endif//BUTTERFLY1x2
277
278 #ifndef BUTTERFLY2x4
279 #define BUTTERFLY2x4(wd) (((uint32_t)(wd)<<16) |(wd))
280 #endif//BUTTERFLY2x4
281
282 #ifndef BUTTERFLY4x8
283 #define BUTTERFLY4x8(dw) (((uint64_t)(dw)<<32) | (dw))
284 #endif//BUTTERFLY4x8
285
WELS_POWER2_IF(uint32_t v)286 static inline bool WELS_POWER2_IF (uint32_t v) {
287 return (v && ! (v & (v - 1)));
288 }
289
290 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
291 #define WELS_GCC_UNUSED __attribute__((__unused__))
292 #else
293 #define WELS_GCC_UNUSED
294 #endif
295
CheckInRangeCloseOpen(const int16_t kiCurrent,const int16_t kiMin,const int16_t kiMax)296 inline bool CheckInRangeCloseOpen (const int16_t kiCurrent, const int16_t kiMin, const int16_t kiMax) {
297 return ((kiCurrent >= kiMin) && (kiCurrent < kiMax));
298 }
299
WelsSetMemUint32_c(uint32_t * pDst,uint32_t iValue,int32_t iSizeOfData)300 static inline void WelsSetMemUint32_c (uint32_t* pDst, uint32_t iValue, int32_t iSizeOfData) {
301 for (int i = 0; i < iSizeOfData; i++) {
302 pDst[i] = iValue;
303 }
304 }
305
WelsSetMemUint16_c(uint16_t * pDst,uint16_t iValue,int32_t iSizeOfData)306 static inline void WelsSetMemUint16_c (uint16_t* pDst, uint16_t iValue, int32_t iSizeOfData) {
307 for (int i = 0; i < iSizeOfData; i++) {
308 pDst[i] = iValue;
309 }
310 }
311
WelsSetMemMultiplebytes_c(void * pDst,uint32_t iValue,int32_t iSizeOfData,int32_t iDataLengthOfData)312 inline void WelsSetMemMultiplebytes_c (void* pDst, uint32_t iValue, int32_t iSizeOfData, int32_t iDataLengthOfData) {
313 assert (4 == iDataLengthOfData || 2 == iDataLengthOfData || 1 == iDataLengthOfData);
314
315 // TODO: consider add assembly for these functions
316 if (0 != iValue) {
317 if (4 == iDataLengthOfData) {
318 WelsSetMemUint32_c (static_cast<uint32_t*> (pDst), static_cast<uint32_t> (iValue), iSizeOfData);
319 } else if (2 == iDataLengthOfData) {
320 WelsSetMemUint16_c (static_cast<uint16_t*> (pDst), static_cast<uint16_t> (iValue), iSizeOfData);
321 } else {
322 memset (static_cast<uint8_t*> (pDst), static_cast<uint8_t> (iValue), iSizeOfData);
323 }
324 } else {
325 memset (static_cast<uint8_t*> (pDst), 0, iSizeOfData * iDataLengthOfData);
326 }
327 }
328
329 #endif//WELS_MACRO_UTILIZATIONS_H__
330