1 /******************************************************************************
2 * *
3 * Copyright (C) 2018 The Android Open Source Project
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 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #ifndef IXHEAACD_BASIC_OPS16_H
21 #define IXHEAACD_BASIC_OPS16_H
22
ixheaacd_sat16(WORD32 op1)23 static PLATFORM_INLINE WORD16 ixheaacd_sat16(WORD32 op1) {
24 WORD16 var_out;
25
26 if (op1 > 0X00007fffL) {
27 var_out = MAX_16;
28 } else if (op1 < (WORD32)0xffff8000L) {
29 var_out = (WORD16)(-32768);
30 } else {
31 var_out = (WORD16)(op1);
32 }
33 return (var_out);
34 }
35
ixheaacd_add16(WORD16 op1,WORD16 op2)36 static PLATFORM_INLINE WORD16 ixheaacd_add16(WORD16 op1, WORD16 op2) {
37 WORD16 var_out;
38
39 var_out = ((WORD16)(op1 + op2));
40 return (var_out);
41 }
42
ixheaacd_add16_sat(WORD16 op1,WORD16 op2)43 static PLATFORM_INLINE WORD16 ixheaacd_add16_sat(WORD16 op1, WORD16 op2) {
44 WORD16 var_out;
45 WORD32 sum;
46
47 sum = (WORD32)op1 + (WORD32)op2;
48 var_out = ixheaacd_sat16(sum);
49 return (var_out);
50 }
51
ixheaacd_sub16(WORD16 op1,WORD16 op2)52 static PLATFORM_INLINE WORD16 ixheaacd_sub16(WORD16 op1, WORD16 op2) {
53 WORD16 var_out;
54
55 var_out = ((WORD16)(op1 - op2));
56 return (var_out);
57 }
58
ixheaacd_sub16_sat(WORD16 op1,WORD16 op2)59 static PLATFORM_INLINE WORD16 ixheaacd_sub16_sat(WORD16 op1, WORD16 op2) {
60 WORD16 var_out;
61 WORD32 diff;
62
63 diff = (WORD32)op1 - op2;
64 var_out = ixheaacd_sat16(diff);
65 return (var_out);
66 }
67
ixheaacd_mult16(WORD16 op1,WORD16 op2)68 static PLATFORM_INLINE WORD16 ixheaacd_mult16(WORD16 op1, WORD16 op2) {
69 WORD16 var_out;
70
71 var_out = ((WORD16)(((WORD32)op1 * (WORD32)op2) >> 16));
72 return (var_out);
73 }
74
ixheaacd_mult16_shl(WORD16 op1,WORD16 op2)75 static PLATFORM_INLINE WORD16 ixheaacd_mult16_shl(WORD16 op1, WORD16 op2) {
76 WORD16 var_out;
77
78 var_out = ((WORD16)(((WORD32)op1 * (WORD32)op2) >> 15));
79 return (var_out);
80 }
81
ixheaacd_mult16_shl_sat(WORD16 op1,WORD16 op2)82 static PLATFORM_INLINE WORD16 ixheaacd_mult16_shl_sat(WORD16 op1, WORD16 op2) {
83 WORD16 var_out;
84 WORD32 temp;
85
86 temp = ((WORD32)(((WORD32)op1 * (WORD32)op2) >> 15));
87 var_out = ixheaacd_sat16(temp);
88 return (var_out);
89 }
90
ixheaacd_shl16(WORD16 op1,WORD16 shift)91 static PLATFORM_INLINE WORD16 ixheaacd_shl16(WORD16 op1, WORD16 shift) {
92 WORD16 var_out;
93
94 var_out = (WORD16)(op1 << shift);
95 return (var_out);
96 }
97
ixheaacd_shl16_sat(WORD16 op1,WORD16 shift)98 static PLATFORM_INLINE WORD16 ixheaacd_shl16_sat(WORD16 op1, WORD16 shift) {
99 WORD16 var_out;
100 WORD32 temp;
101
102 if (shift > 15) {
103 shift = 15;
104 }
105 temp = (WORD32)(op1 << shift);
106 var_out = ixheaacd_sat16(temp);
107 return (var_out);
108 }
109
ixheaacd_shr16(WORD16 op1,WORD16 shift)110 static PLATFORM_INLINE WORD16 ixheaacd_shr16(WORD16 op1, WORD16 shift) {
111 WORD16 var_out;
112
113 var_out = ((WORD16)(op1 >> shift));
114 return (var_out);
115 }
116
ixheaacd_shl16_dir(WORD16 op1,WORD16 shift)117 static PLATFORM_INLINE WORD16 ixheaacd_shl16_dir(WORD16 op1, WORD16 shift) {
118 WORD16 var_out;
119 if (shift > 0) {
120 var_out = ixheaacd_shl16(op1, shift);
121 } else {
122 var_out = ixheaacd_shr16(op1, (WORD16)(-shift));
123 }
124 return (var_out);
125 }
126
ixheaacd_shr16_dir(WORD16 op1,WORD16 shift)127 static PLATFORM_INLINE WORD16 ixheaacd_shr16_dir(WORD16 op1, WORD16 shift) {
128 WORD16 var_out;
129
130 if (shift < 0) {
131 var_out = ixheaacd_shl16(op1, (WORD16)(-shift));
132 } else {
133 var_out = ixheaacd_shr16(op1, shift);
134 }
135 return (var_out);
136 }
137
ixheaacd_shl16_dir_sat(WORD16 op1,WORD16 shift)138 static PLATFORM_INLINE WORD16 ixheaacd_shl16_dir_sat(WORD16 op1, WORD16 shift) {
139 WORD16 var_out;
140 if (shift > 0) {
141 var_out = ixheaacd_shl16_sat(op1, shift);
142 } else {
143 var_out = ixheaacd_shr16(op1, (WORD16)(-shift));
144 }
145 return (var_out);
146 }
147
ixheaacd_shr16_dir_sat(WORD16 op1,WORD16 shift)148 static PLATFORM_INLINE WORD16 ixheaacd_shr16_dir_sat(WORD16 op1, WORD16 shift) {
149 WORD16 var_out;
150
151 if (shift < 0) {
152 var_out = ixheaacd_shl16_sat(op1, (WORD16)(-shift));
153 } else {
154 var_out = ixheaacd_shr16(op1, shift);
155 }
156 return (var_out);
157 }
158
ixheaacd_norm16(WORD16 op1)159 static PLATFORM_INLINE WORD16 ixheaacd_norm16(WORD16 op1) {
160 WORD16 var_out;
161
162 if (0 == op1) {
163 var_out = 0;
164 } else {
165 if ((WORD16)0xffff == op1) {
166 var_out = 15;
167 } else {
168 if (op1 < 0) {
169 op1 = (WORD16)(~op1);
170 }
171 for (var_out = 0; op1 < 0x4000; var_out++) {
172 op1 <<= 1;
173 }
174 }
175 }
176
177 return (var_out);
178 }
179
ixheaacd_abs16(WORD16 op1)180 static PLATFORM_INLINE WORD16 ixheaacd_abs16(WORD16 op1) {
181 WORD16 var_out;
182
183 if (op1 < 0) {
184 var_out = (WORD16)(-op1);
185 } else {
186 var_out = op1;
187 }
188 return (var_out);
189 }
190
ixheaacd_abs16_sat(WORD16 op1)191 static PLATFORM_INLINE WORD16 ixheaacd_abs16_sat(WORD16 op1) {
192 WORD16 var_out;
193
194 if (-32768 == op1) {
195 var_out = MAX_16;
196 } else {
197 if (op1 < 0) {
198 var_out = (WORD16)(-op1);
199 } else {
200 var_out = op1;
201 }
202 }
203 return (var_out);
204 }
205
ixheaacd_negate16(WORD16 op1)206 static PLATFORM_INLINE WORD16 ixheaacd_negate16(WORD16 op1) {
207 WORD16 var_out;
208
209 if (-32768 == op1) {
210 var_out = MAX_16;
211 } else {
212 var_out = (WORD16)(-op1);
213 }
214 return (var_out);
215 }
216
ixheaacd_min16(WORD16 op1,WORD16 op2)217 static PLATFORM_INLINE WORD16 ixheaacd_min16(WORD16 op1, WORD16 op2) {
218 WORD16 var_out;
219
220 var_out = op1 < op2 ? op1 : op2;
221 return (var_out);
222 }
223
ixheaacd_max16(WORD16 op1,WORD16 op2)224 static PLATFORM_INLINE WORD16 ixheaacd_max16(WORD16 op1, WORD16 op2) {
225 WORD16 var_out;
226
227 var_out = op1 > op2 ? op1 : op2;
228 return (var_out);
229 }
230
ixheaacd_round16(WORD32 op1)231 static PLATFORM_INLINE WORD16 ixheaacd_round16(WORD32 op1) {
232 WORD16 var_out;
233
234 var_out = (WORD16)(ixheaacd_add32_sat(op1, 0x8000) >> 16);
235 return (var_out);
236 }
237 #endif
238