xref: /aosp_15_r20/external/libhevc/common/riscv64/ihevc_platform_macros.h (revision c83a76b084498d55f252f48b2e3786804cdf24b7)
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 #ifndef _IHEVC_PLATFORM_MACROS_H_
35 #define _IHEVC_PLATFORM_MACROS_H_
36 
37 #define CLIP_U8(x) CLIP3((x), 0,     255)
38 #define CLIP_S8(x) CLIP3((x), -128,  127)
39 
40 #define CLIP_U10(x) CLIP3((x), 0,     1023);
41 #define CLIP_S10(x) CLIP3((x), -512,  511);
42 
43 #define CLIP_U12(x) CLIP3((x), 0,     4095);
44 #define CLIP_S12(x) CLIP3((x), -2048,  2047);
45 
46 #define CLIP_U14(x) CLIP3((x), 0,     16383);
47 #define CLIP_S14(x) CLIP3((x), -8192,  8191);
48 
49 #define CLIP_U16(x) CLIP3((x), 0,        65535)
50 #define CLIP_S16(x) CLIP3((x), -32768,   32767)
51 
52 #define ITT_BIG_ENDIAN(x)   ((x & 0x000000ff) << 24)                |   \
53                             ((x & 0x0000ff00) << 8)    |   \
54                             ((x & 0x00ff0000) >> 8)    |   \
55                             ((UWORD32)x >> 24);
56 
57 #define SHL(x,y) (((y) < 32) ? ((x) << (y)) : 0)
58 #define SHR(x,y) (((y) < 32) ? ((x) >> (y)) : 0)
59 
60 #define SHR_NEG(val,shift)  ((shift>0)?(val>>shift):(val<<(-shift)))
61 #define SHL_NEG(val,shift)  ((shift<0)?(val>>(-shift)):(val<<shift))
62 
63 #define INLINE inline
64 
65 #define POPCNT_U32(x)       __builtin_popcount(x)
66 
CLZ(UWORD32 u4_word)67 static INLINE UWORD32 CLZ(UWORD32 u4_word)
68 {
69     if(u4_word)
70         return (__builtin_clz(u4_word));
71     else
72         return 31;
73 }
74 
CLZNZ(UWORD32 u4_word)75 static INLINE UWORD32 CLZNZ(UWORD32 u4_word)
76 {
77    return (__builtin_clz(u4_word));
78 }
79 
CTZ(UWORD32 u4_word)80 static INLINE UWORD32 CTZ(UWORD32 u4_word)
81 {
82     if(0 == u4_word)
83         return 31;
84     else
85     {
86         unsigned int index;
87         index = __builtin_ctz(u4_word);
88         return (UWORD32)index;
89     }
90 }
91 
92 #define DATA_SYNC()  __sync_synchronize()
93 
94 /**
95 ******************************************************************************
96  *  @brief  returns postion of msb bit for 32bit input
97 ******************************************************************************
98  */
99 #define GET_POS_MSB_32(r,word)                         \
100 {                                                      \
101     if(word)                                           \
102     {                                                  \
103         r = 31 - __builtin_clz(word);                  \
104     }                                                  \
105     else                                               \
106     {                                                  \
107         r = -1;                                        \
108     }                                                  \
109 }
110 
111 /**
112 ******************************************************************************
113  *  @brief  returns postion of msb bit for 64bit input
114 ******************************************************************************
115  */
116 #define GET_POS_MSB_64(r,word)                         \
117 {                                                      \
118     if(word)                                           \
119     {                                                  \
120         r = 63 - __builtin_clzll(word);                \
121     }                                                  \
122     else                                               \
123     {                                                  \
124         r = -1;                                        \
125     }                                                  \
126 }
127 
128 
129 /**
130 ******************************************************************************
131  *  @brief  returns max number of bits required to represent input word (max 32bits)
132 ******************************************************************************
133  */
134 #define GETRANGE(r,word)                               \
135 {                                                      \
136     if(word)                                           \
137     {                                                  \
138         r = 32 - __builtin_clz(word);                  \
139     }                                                  \
140     else                                               \
141     {                                                  \
142         r = 1;                                         \
143     }                                                  \
144 }
145 
146 
147 /**
148 *****************************************************************************************************
149 *  @brief  returns max number of bits required to represent input unsigned long long word (max 64bits)
150 *****************************************************************************************************
151 */
152 #define GETRANGE64(r,llword)                             \
153 {                                                        \
154     if(llword)                                           \
155     {                                                    \
156         r = 64 - __builtin_clzll(llword);                \
157     }                                                    \
158     else                                                 \
159     {                                                    \
160         r = 1;                                           \
161     }                                                    \
162 }
163 
164 
165 
166 #define NOP(nop_cnt)    {UWORD32 nop_i; for (nop_i = 0; nop_i < nop_cnt; nop_i++) asm("nop");}
167 
168 
169 
170 #define MEM_ALIGN8 __attribute__ ((aligned (8)))
171 #define MEM_ALIGN16 __attribute__ ((aligned (16)))
172 #define MEM_ALIGN32 __attribute__ ((aligned (32)))
173 
174 #endif /* _IHEVC_PLATFORM_MACROS_H_ */
175