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: /audio/gsm_amr/c/src/div_32.c 32 33 ------------------------------------------------------------------------------ 34 REVISION HISTORY 35 36 Description: Updated template. Changed function interface to pass in a 37 pointer to overflow flag into the function instead of using a 38 global flag. Removed inclusion of unwanted header files. Changed 39 the name of input and output variables for clarity. 40 41 Description: 42 1. Eliminated unused include files. 43 2. Replaced l_extract functionality, code size and speed 44 do not justify calling this function 45 3. Eliminated sub() function call, replace by (-), this knowing 46 that the result will not saturate. 47 48 Description: Added casting to eliminate warnings 49 50 Who: Date: 51 Description: 52 53 54 ------------------------------------------------------------------------------ 55 */ 56 57 /*---------------------------------------------------------------------------- 58 ; INCLUDES 59 ----------------------------------------------------------------------------*/ 60 #include "basic_op.h" 61 62 /*---------------------------------------------------------------------------- 63 ; MACROS 64 ; [Define module specific macros here] 65 ----------------------------------------------------------------------------*/ 66 67 /*---------------------------------------------------------------------------- 68 ; DEFINES 69 ; [Include all pre-processor statements here. Include conditional 70 ; compile variables also.] 71 ----------------------------------------------------------------------------*/ 72 73 /*---------------------------------------------------------------------------- 74 ; LOCAL FUNCTION DEFINITIONS 75 ; [List function prototypes here] 76 ----------------------------------------------------------------------------*/ 77 78 /*---------------------------------------------------------------------------- 79 ; LOCAL VARIABLE DEFINITIONS 80 ; [Variable declaration - defined here and used outside this module] 81 ----------------------------------------------------------------------------*/ 82 83 84 /* 85 ------------------------------------------------------------------------------ 86 FUNCTION NAME: div_32 87 ------------------------------------------------------------------------------ 88 INPUT AND OUTPUT DEFINITIONS 89 90 Inputs: 91 L_num = 32 bit signed integer (Word32) whose value falls in the 92 range : 0x0000 0000 < L_num < L_denom 93 L_denom_hi = 16 bit positive normalized integer whose value falls in 94 the range : 0x4000 < hi < 0x7fff 95 L_denom_lo = 16 bit positive integer whose value falls in the range : 96 0 < lo < 0x7fff 97 98 pOverflow = pointer to overflow (Flag) 99 100 Outputs: 101 pOverflow -> 1 if the 32 bit divide operation resulted in overflow 102 103 Returns: 104 result = 32-bit quotient of of the division of two 32 bit integers 105 L_num / L_denom (Word32) 106 107 Global Variables Used: 108 None 109 110 Local Variables Needed: 111 None 112 113 ------------------------------------------------------------------------------ 114 FUNCTION DESCRIPTION 115 116 This function is a fractional integer division of two 32 bit numbers, the 117 numerator L_num and the denominator L_denom. The denominator is formed by 118 combining denom_hi and denom_lo. Note that denom_hi is a normalized numbers. 119 The numerator and denominator must be positive and the numerator must be 120 less than the denominator. 121 122 The division is done as follows: 123 1. Find 1/L_denom by first approximating: approx = 1 / denom_hi. 124 2. 1/L_denom = approx * (2.0 - L_denom * approx ). 125 3. result = L_num * (1/L_denom). 126 127 ------------------------------------------------------------------------------ 128 REQUIREMENTS 129 130 None 131 132 ------------------------------------------------------------------------------ 133 REFERENCES 134 135 [1] div_32() function in oper_32b.c, UMTS GSM AMR speech codec, R99 - 136 Version 3.2.0, March 2, 2001 137 138 ------------------------------------------------------------------------------ 139 PSEUDO-CODE 140 141 142 ------------------------------------------------------------------------------ 143 RESOURCES USED [optional] 144 145 When the code is written for a specific target processor the 146 the resources used should be documented below. 147 148 HEAP MEMORY USED: x bytes 149 150 STACK MEMORY USED: x bytes 151 152 CLOCK CYCLES: (cycle count equation for this function) + (variable 153 used to represent cycle count for each subroutine 154 called) 155 where: (cycle count variable) = cycle count for [subroutine 156 name] 157 158 ------------------------------------------------------------------------------ 159 CAUTION [optional] 160 [State any special notes, constraints or cautions for users of this function] 161 162 ------------------------------------------------------------------------------ 163 */ 164 165 /*---------------------------------------------------------------------------- 166 ; FUNCTION CODE 167 ----------------------------------------------------------------------------*/ 168 Word32 Div_32(Word32 L_num, 169 Word16 L_denom_hi, 170 Word16 L_denom_lo, 171 Flag *pOverflow) 172 { 173 174 Word16 approx; 175 Word16 hi; 176 Word16 lo; 177 Word16 n_hi; 178 Word16 n_lo; 179 Word32 result; 180 181 /* First approximation: 1 / L_denom = 1/L_denom_hi */ 182 183 approx = div_s((Word16) 0x3fff, L_denom_hi); 184 185 /* 1/L_denom = approx * (2.0 - L_denom * approx) */ 186 187 result = Mpy_32_16(L_denom_hi, L_denom_lo, approx, pOverflow); 188 /* result is > 0 , and less than 1.0 */ 189 result = 0x7fffffffL - result; 190 191 hi = (Word16)(result >> 16); 192 lo = (result >> 1) - (hi << 15); 193 194 result = Mpy_32_16(hi, lo, approx, pOverflow); 195 196 /* L_num * (1/L_denom) */ 197 198 hi = (Word16)(result >> 16); 199 lo = (result >> 1) - (hi << 15); 200 201 n_hi = (Word16)(L_num >> 16); 202 n_lo = (L_num >> 1) - (n_hi << 15); 203 204 result = Mpy_32(n_hi, n_lo, hi, lo, pOverflow); 205 result = L_shl(result, 2, pOverflow); 206 207 return (result); 208 } 209 210