| 1 | /* $Id: dspfunc.c 1.4 2006/12/14 18:07:01 rzopf Exp $ */ |
|---|
| 2 | /***************************************************************************** |
|---|
| 3 | Copyright 2004 Broadcom Corp. All Rights Reserved. |
|---|
| 4 | |
|---|
| 5 | $Log: dspfunc.c $ |
|---|
| 6 | Revision 1.4 2006/12/14 18:07:01 rzopf |
|---|
| 7 | *** empty log message *** |
|---|
| 8 | |
|---|
| 9 | Revision 1.3 2006/02/28 21:35:02 rzopf |
|---|
| 10 | modified frame size related code to use a variable instead of a constant. |
|---|
| 11 | |
|---|
| 12 | Revision 1.2 2006/02/27 21:01:44 rzopf |
|---|
| 13 | Replaced short and long data types with Word16 and Word32 |
|---|
| 14 | |
|---|
| 15 | Revision 1.1 2006/02/27 17:07:33 rzopf |
|---|
| 16 | Initial Version. |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | ******************************************************************************/ |
|---|
| 20 | |
|---|
| 21 | #include "avl_dspfunc.h" |
|---|
| 22 | |
|---|
| 23 | /* following is from fixmath.c */ |
|---|
| 24 | |
|---|
| 25 | /*-----------------------------------------------------* |
|---|
| 26 | | Table for routine Pow2(). | |
|---|
| 27 | -----------------------------------------------------*/ |
|---|
| 28 | |
|---|
| 29 | Word16 bvxx_tabpow[33] = { |
|---|
| 30 | 16384, 16743, 17109, 17484, 17867, 18258, 18658, 19066, 19484, 19911, |
|---|
| 31 | 20347, 20792, 21247, 21713, 22188, 22674, 23170, 23678, 24196, 24726, |
|---|
| 32 | 25268, 25821, 26386, 26964, 27554, 28158, 28774, 29405, 30048, 30706, |
|---|
| 33 | 31379, 32066, 32767 }; |
|---|
| 34 | |
|---|
| 35 | /*-----------------------------------------------------* |
|---|
| 36 | * | Table for routine Log2(). | |
|---|
| 37 | * -----------------------------------------------------*/ |
|---|
| 38 | |
|---|
| 39 | Word16 bvxx_tablog[33] = { |
|---|
| 40 | 0, 1455, 2866, 4236, 5568, 6863, 8124, 9352, 10549, 11716, |
|---|
| 41 | 12855, 13967, 15054, 16117, 17156, 18172, 19167, 20142, 21097, 22033, |
|---|
| 42 | 22951, 23852, 24735, 25603, 26455, 27291, 28113, 28922, 29716, 30497, |
|---|
| 43 | 31266, 32023, 32767 }; |
|---|
| 44 | |
|---|
| 45 | /*___________________________________________________________________________ |
|---|
| 46 | | | |
|---|
| 47 | | Function Name : Pow2() | |
|---|
| 48 | | | |
|---|
| 49 | | L_x = pow(2.0, exponent.fraction) | |
|---|
| 50 | |---------------------------------------------------------------------------| |
|---|
| 51 | | Algorithm: | |
|---|
| 52 | | | |
|---|
| 53 | | The function Pow2(L_x) is approximated by a table and linear | |
|---|
| 54 | | interpolation. | |
|---|
| 55 | | | |
|---|
| 56 | | 1- i = bit10-b15 of fraction, 0 <= i <= 31 | |
|---|
| 57 | | 2- a = bit0-b9 of fraction | |
|---|
| 58 | | 3- L_x = tabpow[i]<<16 - (tabpow[i] - tabpow[i+1]) * a * 2 | |
|---|
| 59 | | 4- L_x = L_x >> (30-exponent) (with rounding) | |
|---|
| 60 | |___________________________________________________________________________| |
|---|
| 61 | */ |
|---|
| 62 | |
|---|
| 63 | Word32 bvxx_Pow2( /* (o) Q0 : result (range: 0<=val<=0x7fffffff) */ |
|---|
| 64 | Word16 exponent, /* (i) Q0 : Integer part. (range: 0<=val<=30) */ |
|---|
| 65 | Word16 fraction /* (i) Q15 : Fractional part. (range: 0.0<=val<1.0) */ |
|---|
| 66 | ) |
|---|
| 67 | { |
|---|
| 68 | Word16 exp, i, a, tmp; |
|---|
| 69 | Word32 L_x; |
|---|
| 70 | |
|---|
| 71 | L_x = ((Word32)fraction)<<6; /* L_x = fraction<<6 */ |
|---|
| 72 | i = (Word16)EXTRACT_HI(L_x); /* Extract b10-b15 of fraction */ |
|---|
| 73 | L_x = L_x>>1; |
|---|
| 74 | a = (Word16)L_x; /* Extract b0-b9 of fraction */ |
|---|
| 75 | a = a & (Word16)0x7fff; |
|---|
| 76 | |
|---|
| 77 | L_x = ((Word32)bvxx_tabpow[i])<<16; /* tabpow[i] << 16 */ |
|---|
| 78 | tmp = bvxx_tabpow[i]-bvxx_tabpow[i+1]; /* tabpow[i] - tabpow[i+1] */ |
|---|
| 79 | L_x -= (((Word32)tmp)*a)<<1; /* L_x -= tmp*a*2 */ |
|---|
| 80 | |
|---|
| 81 | exp = 30-exponent; |
|---|
| 82 | L_x = L_x>>exp; |
|---|
| 83 | |
|---|
| 84 | return(L_x); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | /*___________________________________________________________________________ |
|---|
| 89 | | | |
|---|
| 90 | | Function Name : Log2() | |
|---|
| 91 | | | |
|---|
| 92 | | Compute log2(L_x). | |
|---|
| 93 | | L_x is positive. | |
|---|
| 94 | | | |
|---|
| 95 | | if L_x is negative or zero, result is 0. | |
|---|
| 96 | |---------------------------------------------------------------------------| |
|---|
| 97 | | Algorithm: | |
|---|
| 98 | | | |
|---|
| 99 | | The function Log2(L_x) is approximated by a table and linear | |
|---|
| 100 | | interpolation. | |
|---|
| 101 | | | |
|---|
| 102 | | 1- Normalization of L_x. | |
|---|
| 103 | | 2- exponent = 30-exponent | |
|---|
| 104 | | 3- i = bit25-b31 of L_x, 32 <= i <= 63 ->because of normalization. | |
|---|
| 105 | | 4- a = bit10-b24 | |
|---|
| 106 | | 5- i -=32 | |
|---|
| 107 | | 6- fraction = tablog[i]<<16 - (tablog[i] - tablog[i+1]) * a * 2 | |
|---|
| 108 | |___________________________________________________________________________| |
|---|
| 109 | */ |
|---|
| 110 | |
|---|
| 111 | void bvxx_Log2( |
|---|
| 112 | Word32 L_x, /* (i) Q0 : input value */ |
|---|
| 113 | Word16 *exponent, /* (o) Q0 : Integer part of Log2. (range: 0<=val<=30) */ |
|---|
| 114 | Word16 *fraction /* (o) Q15: Fractional part of Log2. (range: 0<=val<1) */ |
|---|
| 115 | ) |
|---|
| 116 | { |
|---|
| 117 | Word16 exp, i, a, tmp; |
|---|
| 118 | Word32 L_y; |
|---|
| 119 | |
|---|
| 120 | if( L_x <= (Word32)0 ) |
|---|
| 121 | { |
|---|
| 122 | *exponent = 0; |
|---|
| 123 | *fraction = 0; |
|---|
| 124 | return; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | exp = (Word16)Norm(L_x); |
|---|
| 128 | L_x <<= exp; /* L_x is normalized */ |
|---|
| 129 | |
|---|
| 130 | *exponent = 30-exp; |
|---|
| 131 | |
|---|
| 132 | L_x = L_x>>9; |
|---|
| 133 | i = (Word16)EXTRACT_HI(L_x); /* Extract b25-b31 */ |
|---|
| 134 | L_x = L_x>>1; |
|---|
| 135 | a = (Word16)L_x; /* Extract b10-b24 of fraction */ |
|---|
| 136 | a = a & (Word16)0x7fff; |
|---|
| 137 | |
|---|
| 138 | i = i-32; |
|---|
| 139 | |
|---|
| 140 | L_y = ((Word32)(bvxx_tablog[i]))<<16; /* tablog[i] << 16 */ |
|---|
| 141 | tmp = bvxx_tablog[i]-bvxx_tablog[i+1]; /* tablog[i] - tablog[i+1] */ |
|---|
| 142 | L_y -= (((Word32)tmp)*a)<<1; /* L_y -= tmp*a*2 */ |
|---|
| 143 | |
|---|
| 144 | *fraction = (Word16) EXTRACT_HI( L_y); |
|---|
| 145 | |
|---|
| 146 | return; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | #define PWR_SHIFT 9 |
|---|
| 150 | void CalcPower(short *inbuf, Word32 *power, Word32 *shift, Word32 frsz) |
|---|
| 151 | { |
|---|
| 152 | int i; |
|---|
| 153 | long acc; |
|---|
| 154 | long prdct; |
|---|
| 155 | long lshift; |
|---|
| 156 | |
|---|
| 157 | acc = 0; |
|---|
| 158 | for (i=0;i<frsz;i++) |
|---|
| 159 | { |
|---|
| 160 | prdct = ((Word32)inbuf[i])*inbuf[i]; |
|---|
| 161 | prdct >>= PWR_SHIFT; |
|---|
| 162 | acc += prdct; |
|---|
| 163 | } |
|---|
| 164 | lshift = Norm(acc); |
|---|
| 165 | lshift = PWR_SHIFT+2-lshift; |
|---|
| 166 | if (lshift<0) |
|---|
| 167 | lshift=0; |
|---|
| 168 | |
|---|
| 169 | if (lshift&1) |
|---|
| 170 | lshift++; |
|---|
| 171 | |
|---|
| 172 | acc = 0; |
|---|
| 173 | for (i=0;i<frsz;i++) |
|---|
| 174 | { |
|---|
| 175 | prdct = ((Word32)inbuf[i])*inbuf[i]; |
|---|
| 176 | prdct >>= lshift; |
|---|
| 177 | acc += prdct; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | *shift = lshift; |
|---|
| 181 | *power = acc; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | Word16 Norm(Word32 in) |
|---|
| 185 | { |
|---|
| 186 | Word16 shift; |
|---|
| 187 | |
|---|
| 188 | shift=0; |
|---|
| 189 | if (in > 0) |
|---|
| 190 | { |
|---|
| 191 | |
|---|
| 192 | while(in< (((Word32)1)<<(30))) |
|---|
| 193 | { |
|---|
| 194 | in<<=1; |
|---|
| 195 | shift++; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | else if (in < 0) |
|---|
| 199 | { |
|---|
| 200 | while(in>= (((Word32)-1)<<(30))) |
|---|
| 201 | { |
|---|
| 202 | in<<=1; |
|---|
| 203 | shift++; |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | return(shift); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | Word16 Norm16(Word16 in) |
|---|
| 210 | { |
|---|
| 211 | Word16 shift; |
|---|
| 212 | |
|---|
| 213 | shift=0; |
|---|
| 214 | if (in > 0) |
|---|
| 215 | { |
|---|
| 216 | |
|---|
| 217 | while(in< (((Word16)1)<<(14))) |
|---|
| 218 | { |
|---|
| 219 | in<<=1; |
|---|
| 220 | shift++; |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | else if (in < 0) |
|---|
| 224 | { |
|---|
| 225 | while(in>= (((Word16)-1)<<(14))) |
|---|
| 226 | { |
|---|
| 227 | in<<=1; |
|---|
| 228 | shift++; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | return(shift); |
|---|
| 232 | } |
|---|
| 233 | /*___________________________________________________________________________ |
|---|
| 234 | | | |
|---|
| 235 | | Function Name : div_s | |
|---|
| 236 | | | |
|---|
| 237 | | Purpose : | |
|---|
| 238 | | | |
|---|
| 239 | | Produces a result which is the fractional integer division of var1 by | |
|---|
| 240 | | var2; var1 and var2 must be positive and var2 must be greater or equal | |
|---|
| 241 | | to var1; the result is positive (leading bit equal to 0) and truncated | |
|---|
| 242 | | to 16 bits. | |
|---|
| 243 | | If var1 = var2 then div(var1,var2) = 32767. | |
|---|
| 244 | | | |
|---|
| 245 | | Complexity weight : 18 | |
|---|
| 246 | | | |
|---|
| 247 | | Inputs : | |
|---|
| 248 | | | |
|---|
| 249 | | var1 | |
|---|
| 250 | | 16 bit short signed integer (Word16) whose value falls in the | |
|---|
| 251 | | range : 0x0000 0000 <= var1 <= var2 and var2 != 0. | |
|---|
| 252 | | | |
|---|
| 253 | | var2 | |
|---|
| 254 | | 16 bit short signed integer (Word16) whose value falls in the | |
|---|
| 255 | | range : var1 <= var2 <= 0x0000 7fff and var2 != 0. | |
|---|
| 256 | | | |
|---|
| 257 | | Outputs : | |
|---|
| 258 | | | |
|---|
| 259 | | none | |
|---|
| 260 | | | |
|---|
| 261 | | Return Value : | |
|---|
| 262 | | | |
|---|
| 263 | | var_out | |
|---|
| 264 | | 16 bit short signed integer (Word16) whose value falls in the | |
|---|
| 265 | | range : 0x0000 0000 <= var_out <= 0x0000 7fff. | |
|---|
| 266 | | It's a Q15 value (point between b15 and b14). | |
|---|
| 267 | |___________________________________________________________________________| |
|---|
| 268 | */ |
|---|
| 269 | Word16 div_s (Word16 var1, Word16 var2) |
|---|
| 270 | { |
|---|
| 271 | Word16 var_out = 0; |
|---|
| 272 | Word16 iteration; |
|---|
| 273 | Word32 L_num; |
|---|
| 274 | Word32 L_denom; |
|---|
| 275 | |
|---|
| 276 | if (var1 == 0) |
|---|
| 277 | { |
|---|
| 278 | var_out = 0; |
|---|
| 279 | } |
|---|
| 280 | else |
|---|
| 281 | { |
|---|
| 282 | if (var1 == var2) |
|---|
| 283 | { |
|---|
| 284 | var_out = 32767; |
|---|
| 285 | } |
|---|
| 286 | else |
|---|
| 287 | { |
|---|
| 288 | L_num = var1; |
|---|
| 289 | L_denom = var2; |
|---|
| 290 | for (iteration = 0; iteration < 15; iteration++) |
|---|
| 291 | { |
|---|
| 292 | var_out <<= 1; |
|---|
| 293 | L_num <<= 1; |
|---|
| 294 | |
|---|
| 295 | if (L_num >= L_denom) |
|---|
| 296 | { |
|---|
| 297 | L_num = L_num-L_denom; |
|---|
| 298 | var_out += 1; |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | return (var_out); |
|---|
| 304 | } |
|---|
| 305 | /* ------------------------- End of div_s() ------------------------- */ |
|---|
| 306 | |
|---|
| 307 | /* assume b < a */ |
|---|
| 308 | Word16 imod(Word16 a, Word16 b, Word16 *dividend) |
|---|
| 309 | { |
|---|
| 310 | Word16 t1, t2, t3, t4, t5; |
|---|
| 311 | |
|---|
| 312 | t1 = Norm16(a); |
|---|
| 313 | t1 --; |
|---|
| 314 | t2 = a<<t1; |
|---|
| 315 | t3 = Norm16(b); |
|---|
| 316 | t4 = b<<t3; |
|---|
| 317 | t5 = div_s(t2, t4); |
|---|
| 318 | t5 = t5>>(15+t1-t3); |
|---|
| 319 | t1 = a - b*t5; |
|---|
| 320 | |
|---|
| 321 | *dividend = t5; |
|---|
| 322 | return(t1); |
|---|
| 323 | } |
|---|