xref: /aosp_15_r20/external/libxaac/common/ixheaac_basic_ops.h (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 IXHEAAC_BASIC_OPS_H
21 #define IXHEAAC_BASIC_OPS_H
22 
23 #define MIN(a, b) ((a) < (b) ? (a) : (b))
24 #define MAX(a, b) ((a) > (b) ? (a) : (b))
25 
ixheaac_extract16h(WORD32 var)26 static PLATFORM_INLINE WORD16 ixheaac_extract16h(WORD32 var) {
27   WORD16 var_out;
28 
29   var_out = (WORD16)(var >> 16);
30   return (var_out);
31 }
32 
ixheaac_extract16l(WORD32 var)33 static PLATFORM_INLINE WORD16 ixheaac_extract16l(WORD32 var) {
34   WORD16 var_out;
35 
36   var_out = (WORD16)var;
37   return (var_out);
38 }
39 
ixheaac_deposit16h_in32(WORD16 var)40 static PLATFORM_INLINE WORD32 ixheaac_deposit16h_in32(WORD16 var) {
41   WORD32 var_out;
42 
43   var_out = (WORD32)var << 16;
44   return (var_out);
45 }
46 
ixheaac_deposit16l_in32(WORD16 var)47 static PLATFORM_INLINE WORD32 ixheaac_deposit16l_in32(WORD16 var) {
48   WORD32 var_out;
49 
50   var_out = (WORD32)var;
51   return (var_out);
52 }
53 
ixheaac_extu(UWORD32 a,WORD32 shift_left,WORD32 shift_right)54 static PLATFORM_INLINE UWORD32 ixheaac_extu(UWORD32 a, WORD32 shift_left, WORD32 shift_right) {
55   UWORD32 x;
56   x = (UWORD32)a << shift_left;
57   x = (UWORD32)x >> shift_right;
58 
59   return x;
60 }
61 
ixheaac_mult32x16h_in32_shl_sat(WORD32 a,WORD32 b)62 static PLATFORM_INLINE WORD32 ixheaac_mult32x16h_in32_shl_sat(WORD32 a, WORD32 b) {
63   WORD32 result;
64 
65   if (a == (WORD32)0x80000000 && b == (WORD16)0x8000) {
66     result = (WORD32)0x7fffffff;
67   } else {
68     result = ixheaac_mult32x16in32_shl(a, ixheaac_extract16h(b));
69   }
70 
71   return (result);
72 }
73 
ixheaac_div32_pos_normb(WORD32 a,WORD32 b)74 static PLATFORM_INLINE WORD32 ixheaac_div32_pos_normb(WORD32 a, WORD32 b) {
75   WORD32 quotient;
76   UWORD32 mantissa_nr = a;
77   UWORD32 mantissa_dr = b;
78 
79   LOOPINDEX i;
80 
81   if (a == b) {
82     quotient = MAX_32;
83   } else {
84     quotient = 0;
85 
86     for (i = 0; i < 32; i++) {
87       quotient = quotient << 1;
88 
89       if (mantissa_nr >= mantissa_dr) {
90         mantissa_nr = mantissa_nr - mantissa_dr;
91         quotient += 1;
92       }
93 
94       mantissa_nr = (UWORD32)mantissa_nr << 1;
95     }
96   }
97 
98   return quotient;
99 }
100 
ixheaac_shr32_dir_sat_limit(WORD32 a,WORD b)101 static PLATFORM_INLINE WORD32 ixheaac_shr32_dir_sat_limit(WORD32 a, WORD b) {
102   WORD32 out_val;
103 
104   if (b < 0) {
105     out_val = ixheaac_shl32_sat(a, -b);
106   } else {
107     b = ixheaac_min32(b, 31);
108     out_val = ixheaac_shr32(a, b);
109   }
110 
111   return out_val;
112 }
113 
ixheaac_shl32_dir_sat_limit(WORD32 a,WORD b)114 static PLATFORM_INLINE WORD32 ixheaac_shl32_dir_sat_limit(WORD32 a, WORD b) {
115   WORD32 out_val;
116 
117   if (b < 0) {
118     b = -b;
119     b = ixheaac_min32(b, 31);
120     out_val = ixheaac_shr32(a, b);
121   } else {
122     out_val = ixheaac_shl32_sat(a, b);
123   }
124 
125   return out_val;
126 }
127 
ixheaac_mac32x32in64_dual(WORD32 a,WORD32 b,WORD64 c)128 static PLATFORM_INLINE WORD64 ixheaac_mac32x32in64_dual(WORD32 a, WORD32 b, WORD64 c) {
129   WORD64 result;
130   WORD64 temp_result;
131 
132   temp_result = (WORD64)a * (WORD64)b;
133   result = ixheaac_add64_sat(c, temp_result);
134   return (result);
135 }
136 
137 #endif /* IXHEAAC_BASIC_OPS_H */
138