Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / div_32.cpp
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: div_32.cpp
32
33 ------------------------------------------------------------------------------
34 */
35
36 /*----------------------------------------------------------------------------
37 ; INCLUDES
38 ----------------------------------------------------------------------------*/
39 #include    "basic_op.h"
40
41 /*----------------------------------------------------------------------------
42 ; MACROS
43 ; [Define module specific macros here]
44 ----------------------------------------------------------------------------*/
45
46 /*----------------------------------------------------------------------------
47 ; DEFINES
48 ; [Include all pre-processor statements here. Include conditional
49 ; compile variables also.]
50 ----------------------------------------------------------------------------*/
51
52 /*----------------------------------------------------------------------------
53 ; LOCAL FUNCTION DEFINITIONS
54 ; [List function prototypes here]
55 ----------------------------------------------------------------------------*/
56
57 /*----------------------------------------------------------------------------
58 ; LOCAL VARIABLE DEFINITIONS
59 ; [Variable declaration - defined here and used outside this module]
60 ----------------------------------------------------------------------------*/
61
62
63 /*
64 ------------------------------------------------------------------------------
65  FUNCTION NAME: div_32
66 ------------------------------------------------------------------------------
67  INPUT AND OUTPUT DEFINITIONS
68
69  Inputs:
70     L_num = 32 bit signed integer (Word32) whose value falls in the
71                 range : 0x0000 0000 < L_num < L_denom
72     L_denom_hi = 16 bit positive normalized integer whose value falls in
73                the range : 0x4000 < hi < 0x7fff
74     L_denom_lo = 16 bit positive integer whose value falls in the range :
75                0 < lo < 0x7fff
76
77     pOverflow = pointer to overflow (Flag)
78
79  Outputs:
80     pOverflow -> 1 if the 32 bit divide operation resulted in overflow
81
82  Returns:
83     result = 32-bit quotient of of the division of two 32 bit integers
84                 L_num / L_denom (Word32)
85
86  Global Variables Used:
87     None
88
89  Local Variables Needed:
90     None
91
92 ------------------------------------------------------------------------------
93  FUNCTION DESCRIPTION
94
95  This function is a fractional integer division of two 32 bit numbers, the
96  numerator L_num and the denominator L_denom. The denominator is formed by
97  combining denom_hi and denom_lo. Note that denom_hi is a normalized numbers.
98  The numerator and denominator must be positive and the numerator must be
99  less than the denominator.
100
101  The division is done as follows:
102  1. Find 1/L_denom by first approximating: approx = 1 / denom_hi.
103  2. 1/L_denom = approx * (2.0 - L_denom * approx ).
104  3. result = L_num * (1/L_denom).
105
106 ------------------------------------------------------------------------------
107  REQUIREMENTS
108
109  None
110
111 ------------------------------------------------------------------------------
112  REFERENCES
113
114  [1] div_32() function in oper_32b.c,  UMTS GSM AMR speech codec, R99 -
115  Version 3.2.0, March 2, 2001
116
117 ------------------------------------------------------------------------------
118  PSEUDO-CODE
119
120
121 ------------------------------------------------------------------------------
122  CAUTION [optional]
123  [State any special notes, constraints or cautions for users of this function]
124
125 ------------------------------------------------------------------------------
126 */
127
128 /*----------------------------------------------------------------------------
129 ; FUNCTION CODE
130 ----------------------------------------------------------------------------*/
131 Word32 Div_32(Word32 L_num,
132               Word16 L_denom_hi,
133               Word16 L_denom_lo,
134               Flag   *pOverflow)
135 {
136
137     Word16 approx;
138     Word16 hi;
139     Word16 lo;
140     Word16 n_hi;
141     Word16 n_lo;
142     Word32 result;
143
144     /* First approximation: 1 / L_denom = 1/L_denom_hi */
145
146     approx = div_s((Word16) 0x3fff, L_denom_hi);
147
148     /* 1/L_denom = approx * (2.0 - L_denom * approx) */
149
150     result = Mpy_32_16(L_denom_hi, L_denom_lo, approx, pOverflow);
151     /* result is > 0 , and less than 1.0 */
152     result =  0x7fffffffL - result;
153
154     hi = (Word16)(result >> 16);
155     lo = (result >> 1) - (hi << 15);
156
157     result = Mpy_32_16(hi, lo, approx, pOverflow);
158
159     /* L_num * (1/L_denom) */
160
161     hi = (Word16)(result >> 16);
162     lo = (result >> 1) - (hi << 15);
163
164     n_hi = (Word16)(L_num >> 16);
165     n_lo = (L_num >> 1) - (n_hi << 15);
166
167     result = Mpy_32(n_hi, n_lo, hi, lo, pOverflow);
168     result = L_shl(result, 2, pOverflow);
169
170     return (result);
171 }
172