Tizen 2.0 Release
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / include / mult.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: mult.h
32
33 ------------------------------------------------------------------------------
34  INCLUDE DESCRIPTION
35
36  This file contains all the constant definitions and prototype definitions
37  needed by the mult function.
38
39 ------------------------------------------------------------------------------
40 */
41
42 #ifndef MULT_H
43 #define MULT_H
44
45 /*----------------------------------------------------------------------------
46 ; INCLUDES
47 ----------------------------------------------------------------------------*/
48
49 #include    "basicop_malloc.h"
50
51 /*--------------------------------------------------------------------------*/
52 #ifdef __cplusplus
53 extern "C"
54 {
55 #endif
56
57     /*----------------------------------------------------------------------------
58     ; MACROS
59     ; Define module specific macros here
60     ----------------------------------------------------------------------------*/
61
62     /*----------------------------------------------------------------------------
63     ; DEFINES
64     ; Include all pre-processor statements here.
65     ----------------------------------------------------------------------------*/
66
67     /*----------------------------------------------------------------------------
68     ; EXTERNAL VARIABLES REFERENCES
69     ; Declare variables used in this module but defined elsewhere
70     ----------------------------------------------------------------------------*/
71
72     /*----------------------------------------------------------------------------
73     ; SIMPLE TYPEDEF'S
74     ----------------------------------------------------------------------------*/
75
76     /*----------------------------------------------------------------------------
77     ; ENUMERATED TYPEDEF'S
78     ----------------------------------------------------------------------------*/
79
80     /*----------------------------------------------------------------------------
81     ; STRUCTURES TYPEDEF'S
82     ----------------------------------------------------------------------------*/
83
84     /*----------------------------------------------------------------------------
85     ; GLOBAL FUNCTION DEFINITIONS
86     ; Function Prototype declaration
87     ----------------------------------------------------------------------------*/
88 #if defined(PV_ARM_V5)
89
90     __inline Word16 mult(Word16 var1, Word16 var2, Flag *pOverflow)
91     {
92         Word32 product;
93
94         OSCL_UNUSED_ARG(pOverflow);
95
96         __asm
97         {
98             SMULBB product, var1, var2
99             MOV    product, product, ASR #15
100             CMP    product, 0x7FFF
101             MOVGE  product, 0x7FFF
102         }
103
104         return ((Word16) product);
105     }
106
107 #elif defined(PV_ARM_GCC_V5)
108
109     __inline Word16 mult(Word16 var1, Word16 var2, Flag *pOverflow)
110     {
111         register Word32 ra = var1;
112         register Word32 rb = var2;
113         Word32 product;
114         Word32 temp = 0x7FFF;
115
116         OSCL_UNUSED_ARG(pOverflow);
117
118         asm volatile("smulbb %0, %1, %2"
119              : "=r"(product)
120                              : "r"(ra), "r"(rb)
121                             );
122         asm volatile("mov %0, %1, ASR #15"
123              : "=r"(product)
124                              : "r"(product)
125                             );
126         asm volatile("cmp %0, %1"
127              : "=r"(product)
128                              : "r"(temp)
129                             );
130         asm volatile("movge %0, %1"
131              : "=r"(product)
132                              : "r"(temp)
133                             );
134
135         return ((Word16) product);
136     }
137
138 #else /* C EQUIVALENT */
139
140     static inline Word16 mult(Word16 var1, Word16 var2, Flag *pOverflow)
141     {
142         register Word32 product;
143
144         product = ((Word32) var1 * var2) >> 15;
145
146         /* Saturate result (if necessary). */
147         /* var1 * var2 >0x00007fff is the only case */
148         /* that saturation occurs. */
149
150         if (product > 0x00007fffL)
151         {
152             *pOverflow = 1;
153             product = (Word32) MAX_16;
154         }
155
156
157         /* Return the product as a 16 bit value by type casting Word32 to Word16 */
158
159         return ((Word16) product);
160     }
161
162 #endif
163     /*----------------------------------------------------------------------------
164     ; END
165     ----------------------------------------------------------------------------*/
166 #ifdef __cplusplus
167 }
168 #endif
169
170 #endif  /* _MULT_H_ */
171