1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /**
19 *******************************************************************************
20 * @file
21 *  ihevc_platform_macros.h
22 *
23 * @brief
24 *  Platform specific Macro definitions used in the codec
25 *
26 * @author
27 *  Ittiam
28 *
29 * @remarks
30 *  None
31 *
32 *******************************************************************************
33 */
34 
35 
36 #ifndef _IHEVC_PLATFORM_MACROS_H_
37 #define _IHEVC_PLATFORM_MACROS_H_
38 
39 //#include <immintrin.h>
40 
41 
42 #define CLIP_U8(x) CLIP3((x), 0,     255)
43 #define CLIP_S8(x) CLIP3((x), -128,  127)
44 
45 #define CLIP_U10(x) CLIP3((x), 0,     1023);
46 #define CLIP_S10(x) CLIP3((x), -512,  511);
47 
48 #define CLIP_U12(x) CLIP3((x), 0,     4095);
49 #define CLIP_S12(x) CLIP3((x), -2048,  2047);
50 
51 #define CLIP_U16(x) CLIP3((x), 0,        65535)
52 #define CLIP_S16(x) CLIP3((x), -32768,   32767)
53 
54 
55 
56 #define SHL(x,y) (((y) < 32) ? ((x) << (y)) : 0)
57 #define SHR(x,y) (((y) < 32) ? ((x) >> (y)) : 0)
58 
59 #define SHR_NEG(val,shift)  ((shift>0)?(val>>shift):(val<<(-shift)))
60 #define SHL_NEG(val,shift)  ((shift<0)?(val>>(-shift)):(val<<shift))
61 
62 
63 #define ITT_BIG_ENDIAN(x)       ((x << 24))                |   \
64                             ((x & 0x0000ff00) << 8)    |   \
65                             ((x & 0x00ff0000) >> 8)    |   \
66                             ((UWORD32)x >> 24);
67 
68 
69 #define NOP(nop_cnt)    {UWORD32 nop_i; for (nop_i = (nop_cnt) ; nop_i > 0 ; nop_i--) asm("nop");}
70 
71 #define POPCNT_U32(x)       __builtin_popcount(x)
72 
73 #define PLD(a)
74 #define INLINE inline
75 
CLZ(UWORD32 u4_word)76 static INLINE UWORD32 CLZ(UWORD32 u4_word)
77 {
78     if(u4_word)
79         return (__builtin_clz(u4_word));
80     else
81         return 32;
82 }
CLZNZ(UWORD32 u4_word)83 static INLINE UWORD32 CLZNZ(UWORD32 u4_word)
84 {
85    return (__builtin_clz(u4_word));
86 }
CTZ(UWORD32 u4_word)87 static INLINE UWORD32 CTZ(UWORD32 u4_word)
88 {
89     if(0 == u4_word)
90         return 31;
91     else
92     {
93         unsigned int index;
94         index = __builtin_ctz(u4_word);
95         return (UWORD32)index;
96     }
97 }
98 
99 #define DATA_SYNC()  __sync_synchronize()
100 
101 /**
102 ******************************************************************************
103  *  @brief  returns postion of msb bit for 32bit input
104 ******************************************************************************
105  */
106 #define GET_POS_MSB_32(r,word)                         \
107 {                                                       \
108     if(word)                                           \
109     {                                                   \
110         r = 31 - __builtin_clz(word);                  \
111     }                                                   \
112     else                                                \
113     {                                                   \
114         r = -1;                                         \
115     }                                                   \
116 }
117 
118 /**
119 ******************************************************************************
120  *  @brief  returns postion of msb bit for 64bit input
121 ******************************************************************************
122  */
123 #define GET_POS_MSB_64(r,word)                          \
124 {                                                       \
125     if(word)                                            \
126     {                                                   \
127         r = 63 - __builtin_clzll(word);                 \
128     }                                                   \
129     else                                                \
130     {                                                   \
131         r = -1;                                         \
132     }                                                   \
133 }
134 
135 
136 /**
137 ******************************************************************************
138  *  @brief  returns max number of bits required to represent input word (max 32bits)
139 ******************************************************************************
140  */
141 #define GETRANGE(r,word)                                \
142 {                                                       \
143     if(word)                                            \
144     {                                                   \
145         r = 32 - __builtin_clz(word);                   \
146     }                                                   \
147     else                                                \
148     {                                                   \
149         r = 1;                                          \
150     }                                                   \
151 }
152 #define GCC_ENABLE 0
153 
154 #if GCC_ENABLE
155 #define _mm256_loadu2_m128i(X,Y) _mm256_insertf128_si256(_mm256_castsi128_si256(_mm_loadu_si128((Y))), _mm_loadu_si128((X)),1);
156 
157 #define _mm256_storeu2_m128i(X,Y,Z) {_mm_storeu_si128 ((Y), _mm256_castsi256_si128((Z)));_mm_storeu_si128 ((X), _mm256_extracti128_si256((Z),1));}
158 
159 #define _mm256_set_m128i(X,Y) _mm256_insertf128_si256(_mm256_castsi128_si256((Y)),(X),1);
160 
161 #endif
162 
163 
164 #define PREFETCH_ENABLE 1
165 
166 #if PREFETCH_ENABLE
167 #define PREFETCH(ptr, type) _mm_prefetch(ptr, type);
168 #else
169 #define PREFETCH(ptr, type)
170 #endif
171 
172 #define MEM_ALIGN8 __attribute__ ((aligned (8)))
173 #define MEM_ALIGN16 __attribute__ ((aligned (16)))
174 #define MEM_ALIGN32 __attribute__ ((aligned (32)))
175 
176 #endif /* _IHEVC_PLATFORM_MACROS_H_ */
177