1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
33 ------------------------------------------------------------------------------
36 /*----------------------------------------------------------------------------
38 ----------------------------------------------------------------------------*/
41 /*----------------------------------------------------------------------------
43 ; [Define module specific macros here]
44 ----------------------------------------------------------------------------*/
46 /*----------------------------------------------------------------------------
48 ; [Include all pre-processor statements here. Include conditional
49 ; compile variables also.]
50 ----------------------------------------------------------------------------*/
52 /*----------------------------------------------------------------------------
53 ; LOCAL FUNCTION DEFINITIONS
54 ; [List function prototypes here]
55 ----------------------------------------------------------------------------*/
57 /*----------------------------------------------------------------------------
58 ; LOCAL VARIABLE DEFINITIONS
59 ; [Variable declaration - defined here and used outside this module]
60 ----------------------------------------------------------------------------*/
64 ------------------------------------------------------------------------------
66 ------------------------------------------------------------------------------
67 INPUT AND OUTPUT DEFINITIONS
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 :
77 pOverflow = pointer to overflow (Flag)
80 pOverflow -> 1 if the 32 bit divide operation resulted in overflow
83 result = 32-bit quotient of of the division of two 32 bit integers
84 L_num / L_denom (Word32)
86 Global Variables Used:
89 Local Variables Needed:
92 ------------------------------------------------------------------------------
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.
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).
106 ------------------------------------------------------------------------------
111 ------------------------------------------------------------------------------
114 [1] div_32() function in oper_32b.c, UMTS GSM AMR speech codec, R99 -
115 Version 3.2.0, March 2, 2001
117 ------------------------------------------------------------------------------
121 ------------------------------------------------------------------------------
123 [State any special notes, constraints or cautions for users of this function]
125 ------------------------------------------------------------------------------
128 /*----------------------------------------------------------------------------
130 ----------------------------------------------------------------------------*/
131 Word32 Div_32(Word32 L_num,
144 /* First approximation: 1 / L_denom = 1/L_denom_hi */
146 approx = div_s((Word16) 0x3fff, L_denom_hi);
148 /* 1/L_denom = approx * (2.0 - L_denom * approx) */
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;
154 hi = (Word16)(result >> 16);
155 lo = (result >> 1) - (hi << 15);
157 result = Mpy_32_16(hi, lo, approx, pOverflow);
159 /* L_num * (1/L_denom) */
161 hi = (Word16)(result >> 16);
162 lo = (result >> 1) - (hi << 15);
164 n_hi = (Word16)(L_num >> 16);
165 n_lo = (L_num >> 1) - (n_hi << 15);
167 result = Mpy_32(n_hi, n_lo, hi, lo, pOverflow);
168 result = L_shl(result, 2, pOverflow);