shr.cpp revision 4f1efc098cb5791c3e9f483f2af84aef70d2d0a0
1094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea/* ------------------------------------------------------------------ 2094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * Copyright (C) 1998-2009 PacketVideo 3094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * 4094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * Licensed under the Apache License, Version 2.0 (the "License"); 5094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * you may not use this file except in compliance with the License. 6094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * You may obtain a copy of the License at 7094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * 8094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * http://www.apache.org/licenses/LICENSE-2.0 9094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * 10094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * Unless required by applicable law or agreed to in writing, software 11094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * distributed under the License is distributed on an "AS IS" BASIS, 12094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * express or implied. 14094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * See the License for the specific language governing permissions 15094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * and limitations under the License. 16094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea * ------------------------------------------------------------------- 17094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea */ 18094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea/**************************************************************************************** 19094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel MaleaPortions of this file are derived from the following 3GPP standard: 20094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 21094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 3GPP TS 26.073 22094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec 23094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea Available from http://www.3gpp.org 24094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 25094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) 26094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel MaleaPermission to distribute, modify and use this file under the standard license 27094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Maleaterms listed above has been obtained from the copyright holder. 28094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea****************************************************************************************/ 29094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea/* 30094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 31094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea Filename: /audio/gsm_amr/c/src/shr.c 32094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 33094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea------------------------------------------------------------------------------ 34094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea REVISION HISTORY 35094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 36094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea Description: Created separate file for the shr function. Sync'ed up with 37094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea the current template and fixed tabs. 38094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 39094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea Description: 1. Modified code by seperating var2=0 condition. 40094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 2. Changed Input range definitions. 41094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 42094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea Description: Made changes based on review meeting. 43094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 1. Changed Overflow definition. 44094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 2. Removed pseudo-code. 45094881f513ab366f7ffd0b2c7778ab50281ca59eDaniel Malea 3. Deleted (var2>15&&var1!=0) condition. 46 4. Moved var2>0 condition in front of var2<0 condition. 47 48 Description: Changed the function prototype to pass in a pointer to the 49 overflow flag instead of using global data. 50 51 Description: Made changes per formal review. Updated template. 52 Removed code that updates MOPS counter. 53 Changed parameter name from "overflow" and "pOverflow". 54 Optimized code by eliminating unnecessary typecasting. 55 Filled in the PSEUDO CODE section 56 57 Description: Further optimized typecasting for overflow case 58 59 Who: Date: 60 Description: 61------------------------------------------------------------------------------ 62------------------------------------------------------------------------------ 63 MODULE DESCRIPTION 64 Shift right function with overflow control 65------------------------------------------------------------------------------ 66*/ 67 68/*---------------------------------------------------------------------------- 69; INCLUDES 70----------------------------------------------------------------------------*/ 71#include "basic_op.h" 72 73/*---------------------------------------------------------------------------- 74; MACROS 75; [Define module specific macros here] 76----------------------------------------------------------------------------*/ 77 78/*---------------------------------------------------------------------------- 79; DEFINES 80; [Include all pre-processor statements here. Include conditional 81; compile variables also.] 82----------------------------------------------------------------------------*/ 83 84/*---------------------------------------------------------------------------- 85; LOCAL FUNCTION DEFINITIONS 86; [List function prototypes here] 87----------------------------------------------------------------------------*/ 88 89/*---------------------------------------------------------------------------- 90; LOCAL VARIABLE DEFINITIONS 91; [Variable declaration - defined here and used outside this module] 92----------------------------------------------------------------------------*/ 93 94 95/* 96------------------------------------------------------------------------------ 97 FUNCTION NAME: shr 98------------------------------------------------------------------------------ 99 INPUT AND OUTPUT DEFINITIONS 100 101 Inputs: 102 var1 = 16 bit short signed integer (Word16) whose value falls in 103 the range : 0xffff 8000 <= var1 <= 0x0000 7fff. 104 105 var2 = 16 bit short signed integer (Word16) whose value falls in 106 the range : 0xffff 8000 <= var2 <= 0x0000 7fff. 107 108 pOverflow = pointer to overflow (Flag) 109 110 Outputs: 111 pOverflow -> 1 if the shift operation resulted in overflow 112 113 Returns: 114 product = Shifted result limited to 16 bits (Word16) 115 116 Global Variables Used: 117 None 118 119 Local Variables Needed: 120 None 121 122------------------------------------------------------------------------------ 123 FUNCTION DESCRIPTION 124 125 This function arithmetically shifts the 16 bit input var1 right var2 positions 126 with sign extension. If var2 is negative, arithmetically shift var1 left by 127 -var2 with sign extension. Saturate the result in case of underflows or 128 overflows. 129 130------------------------------------------------------------------------------ 131 REQUIREMENTS 132 None 133------------------------------------------------------------------------------ 134 REFERENCES 135 136 [1] shr() function in basic_op2.c, UMTS GSM AMR speech codec, R99 - 137 Version 3.2.0, March 2, 2001 138 139------------------------------------------------------------------------------ 140 PSEUDO-CODE 141 142Word16 shr_std (Word16 var1, Word16 var2) 143{ 144 Word16 var_out; 145 146 if (var2 < 0) 147 { 148 if (var2 < -16) 149 var2 = -16; 150 var_out = shl_std (var1, -var2); 151#if (WMOPS) 152 mult_stdiCounter[currCounter].shl_std--; 153#endif 154 } 155 else 156 { 157 if (var2 >= 15) 158 { 159 var_out = (var1 < 0) ? -1 : 0; 160 } 161 else 162 { 163 if (var1 < 0) 164 { 165 var_out = ~((~var1) >> var2); 166 } 167 else 168 { 169 var_out = var1 >> var2; 170 } 171 } 172 } 173 174#if (WMOPS) 175 mult_stdiCounter[currCounter].shr_std++; 176#endif 177 return (var_out); 178} 179------------------------------------------------------------------------------ 180 RESOURCES USED [optional] 181 182 When the code is written for a specific target processor the 183 the resources used should be documented below. 184 185 HEAP MEMORY USED: x bytes 186 187 STACK MEMORY USED: x bytes 188 189 CLOCK CYCLES: (cycle count equation for this function) + (variable 190 used to represent cycle count for each subroutine 191 called) 192 where: (cycle count variable) = cycle count for [subroutine 193 name] 194 195------------------------------------------------------------------------------ 196 CAUTION [optional] 197 [State any special notes, constraints or cautions for users of this function] 198 199------------------------------------------------------------------------------ 200*/ 201 202/*---------------------------------------------------------------------------- 203; FUNCTION CODE 204----------------------------------------------------------------------------*/ 205Word16 shr(register Word16 var1, register Word16 var2, Flag *pOverflow) 206{ 207 register Word16 result; 208 register Word32 temp_res; 209 210 if (var2 != 0) 211 { 212 if (var2 > 0) 213 { 214 if (var2 >= 15) 215 { 216 result = ((var1 < 0) ? -1 : 0); 217 } 218 else 219 { 220 if (var1 < 0) 221 { 222 result = (~((~var1) >> var2)); 223 } 224 else 225 { 226 result = (var1 >> var2); 227 } 228 } 229 } 230 else 231 { 232 if (var2 < -16) 233 { 234 var2 = -16; 235 } 236 237 var2 = -var2; /* Shift right negative is equivalent */ 238 /* to shifting left positive. */ 239 240 temp_res = ((Word32) var1) << var2; 241 result = (Word16)(temp_res); 242 243 if (temp_res != (Word32) result) 244 { 245 *pOverflow = 1; 246 result = ((var1 > 0) ? MAX_16 : MIN_16); 247 } 248 } 249 250 } 251 else 252 { 253 result = var1; 254 } 255 256 return (result); 257} 258 259