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