Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / include / mpy_32.h
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30
31  Filename: mpy_32.h
32
33 ------------------------------------------------------------------------------
34  INCLUDE DESCRIPTION
35
36  This file contains all the constant definitions and prototype definitions
37  needed by the Mpy_32 function.
38
39 ------------------------------------------------------------------------------
40 */
41
42 /*----------------------------------------------------------------------------
43 ; CONTINUE ONLY IF NOT ALREADY DEFINED
44 ----------------------------------------------------------------------------*/
45 #ifndef MPY_32_H
46 #define MPY_32_H
47
48 /*----------------------------------------------------------------------------
49 ; INCLUDES
50 ----------------------------------------------------------------------------*/
51 #include    "basicop_malloc.h"
52
53 /*--------------------------------------------------------------------------*/
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif
58
59     /*----------------------------------------------------------------------------
60     ; MACROS
61     ; Define module specific macros here
62     ----------------------------------------------------------------------------*/
63
64
65     /*----------------------------------------------------------------------------
66     ; DEFINES
67     ; Include all pre-processor statements here.
68     ----------------------------------------------------------------------------*/
69
70     /*----------------------------------------------------------------------------
71     ; EXTERNAL VARIABLES REFERENCES
72     ; Declare variables used in this module but defined elsewhere
73     ----------------------------------------------------------------------------*/
74
75     /*----------------------------------------------------------------------------
76     ; SIMPLE TYPEDEF'S
77     ----------------------------------------------------------------------------*/
78
79     /*----------------------------------------------------------------------------
80     ; ENUMERATED TYPEDEF'S
81     ----------------------------------------------------------------------------*/
82
83     /*----------------------------------------------------------------------------
84     ; STRUCTURES TYPEDEF'S
85     ----------------------------------------------------------------------------*/
86
87     /*----------------------------------------------------------------------------
88     ; GLOBAL FUNCTION DEFINITIONS
89     ; Function Prototype declaration
90     ----------------------------------------------------------------------------*/
91 #if defined(PV_ARM_V5) /* Instructions for ARM Assembly on ADS*/
92
93     __inline Word32 Mpy_32(Word16 L_var1_hi,
94     Word16 L_var1_lo,
95     Word16 L_var2_hi,
96     Word16 L_var2_lo,
97     Flag   *pOverflow)
98
99     {
100         /*----------------------------------------------------------------------------
101         ; Define all local variables
102         ----------------------------------------------------------------------------*/
103         Word32 L_product;
104         Word32 L_sum;
105         Word32 product32;
106
107         OSCL_UNUSED_ARG(pOverflow);
108         /*----------------------------------------------------------------------------
109         ; Function body here
110         ----------------------------------------------------------------------------*/
111         /* L_product = L_mult (L_var1_hi, L_var2_hi, pOverflow);*/
112
113         __asm {SMULBB L_product, L_var1_hi, L_var2_hi}
114         __asm {QDADD L_product, 0, L_product}
115         __asm {SMULBB product32, L_var1_hi, L_var2_lo}
116         product32 >>= 15;
117         __asm {QDADD L_sum, L_product, product32}
118         L_product = L_sum;
119         __asm {SMULBB product32, L_var1_lo, L_var2_hi}
120         product32 >>= 15;
121         __asm {QDADD L_sum, L_product, product32}
122         return (L_sum);
123     }
124
125 #elif defined(PV_ARM_GCC_V5) /* Instructions for ARM-linux cross-compiler*/
126
127     static inline Word32 Mpy_32(Word16 L_var1_hi,
128                                 Word16 L_var1_lo,
129                                 Word16 L_var2_hi,
130                                 Word16 L_var2_lo,
131                                 Flag   *pOverflow)
132     {
133         register Word32 product32;
134         register Word32 L_sum;
135         register Word32 L_product, result;
136         register Word32 ra = L_var1_hi;
137         register Word32 rb = L_var1_lo;
138         register Word32 rc = L_var2_hi;
139         register Word32 rd = L_var2_lo;
140
141
142
143         OSCL_UNUSED_ARG(pOverflow);
144
145         asm volatile("smulbb %0, %1, %2"
146              : "=r"(L_product)
147                              : "r"(ra), "r"(rc)
148                             );
149         asm volatile("mov %0, #0"
150              : "=r"(result)
151                     );
152
153         asm volatile("qdadd %0, %1, %2"
154              : "=r"(L_sum)
155                              : "r"(result), "r"(L_product)
156                             );
157
158         asm volatile("smulbb %0, %1, %2"
159              : "=r"(product32)
160                              : "r"(ra), "r"(rd)
161                             );
162
163         asm volatile("mov %0, %1, ASR #15"
164              : "=r"(ra)
165                              : "r"(product32)
166                             );
167         asm volatile("qdadd %0, %1, %2"
168              : "=r"(L_product)
169                              : "r"(L_sum), "r"(ra)
170                             );
171
172         asm volatile("smulbb %0, %1, %2"
173              : "=r"(product32)
174                              : "r"(rb), "r"(rc)
175                             );
176
177         asm volatile("mov %0, %1, ASR #15"
178              : "=r"(rb)
179                              : "r"(product32)
180                             );
181
182         asm volatile("qdadd %0, %1, %2"
183              : "=r"(L_sum)
184                              : "r"(L_product), "r"(rb)
185                             );
186
187         return (L_sum);
188     }
189
190 #else /* C_EQUIVALENT */
191
192     __inline Word32 Mpy_32(Word16 L_var1_hi,
193                            Word16 L_var1_lo,
194                            Word16 L_var2_hi,
195                            Word16 L_var2_lo,
196                            Flag   *pOverflow)
197     {
198         Word32 L_product;
199         Word32 L_sum;
200         Word32 product32;
201
202         OSCL_UNUSED_ARG(pOverflow);
203         L_product = (Word32) L_var1_hi * L_var2_hi;
204
205         if (L_product != (Word32) 0x40000000L)
206         {
207             L_product <<= 1;
208         }
209         else
210         {
211             L_product = MAX_32;
212         }
213
214         /* result = mult (L_var1_hi, L_var2_lo, pOverflow); */
215         product32 = ((Word32) L_var1_hi * L_var2_lo) >> 15;
216
217         /* L_product = L_mac (L_product, result, 1, pOverflow); */
218         L_sum = L_product + (product32 << 1);
219
220         if ((L_product ^ product32) > 0)
221         {
222             if ((L_sum ^ L_product) < 0)
223             {
224                 L_sum = (L_product < 0) ? MIN_32 : MAX_32;
225             }
226         }
227
228         L_product = L_sum;
229
230         /* result = mult (L_var1_lo, L_var2_hi, pOverflow); */
231         product32 = ((Word32) L_var1_lo * L_var2_hi) >> 15;
232
233         /* L_product = L_mac (L_product, result, 1, pOverflow); */
234         L_sum = L_product + (product32 << 1);
235
236         if ((L_product ^ product32) > 0)
237         {
238             if ((L_sum ^ L_product) < 0)
239             {
240                 L_sum = (L_product < 0) ? MIN_32 : MAX_32;
241             }
242         }
243
244         /*----------------------------------------------------------------------------
245         ; Return nothing or data or data pointer
246         ----------------------------------------------------------------------------*/
247         return (L_sum);
248     }
249
250 #endif
251     /*----------------------------------------------------------------------------
252     ; END
253     ----------------------------------------------------------------------------*/
254 #ifdef __cplusplus
255 }
256 #endif
257
258 #endif /* _MPY_32_H_ */