1 /*/////////////////////////////////////////////////////////////////////////////
2 //
3 // INTEL CORPORATION PROPRIETARY INFORMATION
4 // This software is supplied under the terms of a license agreement or
5 // nondisclosure agreement with Intel Corporation and may not be copied
6 // or disclosed except in accordance with the terms of that agreement.
7 // Copyright(c) 2005-2011 Intel Corporation. All Rights Reserved.
8 //
9 // Intel(R) Integrated Performance Primitives
10 // USC - Unified Speech Codec interface library
11 //
12 // By downloading and installing USC codec, you hereby agree that the
13 // accompanying Materials are being provided to you under the terms and
14 // conditions of the End User License Agreement for the Intel(R) Integrated
15 // Performance Primitives product previously accepted by you. Please refer
16 // to the file ippEULA.rtf or ippEULA.txt located in the root directory of your Intel(R) IPP
17 // product installation for more information.
18 //
19 // Purpose: USC: auxiliary functions.
20 //
21 */
22
23 #ifndef __AUX_FNXS__
24 #define __AUX_FNXS__
25
26 #if defined( _WIN32_WCE)
27 #pragma warning( disable : 4505 )
28 #endif
29
30 #include <ipps.h>
31 #include "scratchmem.h"
32
33 extern void InvSqrt_32s16s_I(Ipp32s *valFrac, Ipp16s *valExp);
34
35 /********************************************************
36 * auxiliary inline functions declarations
37 *********************************************************/
Cnvrt_32s16s(Ipp32s x)38 __INLINE Ipp16s Cnvrt_32s16s(Ipp32s x){
39 if (IPP_MAX_16S < x) return IPP_MAX_16S;
40 else if (IPP_MIN_16S > x) return IPP_MIN_16S;
41 return (Ipp16s)(x);
42 }
43
Cnvrt_NR_32s16s(Ipp32s x)44 __INLINE Ipp16s Cnvrt_NR_32s16s(Ipp32s x) {
45 Ipp16s s = IPP_MAX_16S;
46 if(x<(Ipp32s)0x7fff8000) s = (Ipp16s)((x+0x8000)>>16);
47 return s;
48 }
49
Cnvrt_64s32s(__INT64 z)50 __INLINE Ipp32s Cnvrt_64s32s(__INT64 z) {
51 if(IPP_MAX_32S < z) return IPP_MAX_32S;
52 else if(IPP_MIN_32S > z) return IPP_MIN_32S;
53 return (Ipp32s)z;
54 }
55
56 /* Random generator */
Rand_16s(Ipp16s * seed)57 __INLINE Ipp16s Rand_16s(Ipp16s *seed)
58 {
59 *seed = (Ipp16s)(*seed * 31821 + 13849);
60
61 return(*seed);
62 }
63
Rand2_16s(Ipp16s * pSeed)64 __INLINE Ipp16s Rand2_16s( Ipp16s *pSeed )
65 {
66 *pSeed = (Ipp16s)(*pSeed * 521 + 259);
67 return *pSeed ;
68 }
69
NormRand_16s(Ipp16s N,Ipp16s * nRandom)70 __INLINE Ipp16s NormRand_16s(Ipp16s N, Ipp16s *nRandom)
71 {
72 Ipp16s sTmp;
73
74 sTmp = (Ipp16s)(Rand2_16s(nRandom) & 0x7FFF);
75 sTmp = (Ipp16s)((sTmp * N)>>15);
76 return sTmp;
77 }
78
Abs_16s(Ipp16s x)79 __INLINE Ipp16s Abs_16s(Ipp16s x){
80 if(x<0){
81 if(IPP_MIN_16S == x) return IPP_MAX_16S;
82 x = (Ipp16s)-x;
83 }
84 return x;
85 }
Abs_32s(Ipp32s x)86 __INLINE Ipp32s Abs_32s(Ipp32s x){
87 if(x<0){
88 if(IPP_MIN_32S == x) return IPP_MAX_32S;
89 x = -x;
90 }
91 return x;
92 }
93
Add_32s(int x,int y)94 __INLINE int Add_32s(int x, int y) {
95 return Cnvrt_64s32s((__INT64)x + y);
96 }
97
98 extern CONST Ipp16s ExpPosNormTbl[256];
99 extern CONST Ipp16s ExpPosNormTbl2[256];
100
Exp_16s_Pos(Ipp16u x)101 __INLINE Ipp16s Exp_16s_Pos(Ipp16u x)
102 {
103 if((x>>8)==0)
104 return ExpPosNormTbl2[x];
105 else {
106 return ExpPosNormTbl[(x>>8)];
107 }
108 }
109
Exp_16s(Ipp16s x)110 __INLINE Ipp16s Exp_16s(Ipp16s x){
111 if (x == -1) return 15;
112 if (x == 0) return 0;
113 if (x < 0) x = (Ipp16s)(~x);
114 return Exp_16s_Pos(x);
115 }
116
117 //__INLINE Ipp16s Exp_32s_Pos(Ipp32s x){
118 // Ipp16s i;
119 // if (x == 0) return 0;
120 // for(i = 0; x < (Ipp32s)0x40000000; i++) x <<= 1;
121 // return i;
122 //}
Exp_32s_Pos(unsigned int x)123 __INLINE short Exp_32s_Pos(unsigned int x){
124 if (x == 0) return 0;
125 if((x>>16)==0) return (short)(16+Exp_16s_Pos((unsigned short) x));
126 return Exp_16s_Pos((unsigned short)(x>>16));
127 }
128
Exp_32s(Ipp32s x)129 __INLINE Ipp16s Exp_32s(Ipp32s x){
130 if (x == 0) return 0;
131 if (x == -1) return 31;
132 if (x < 0) x = ~x;
133 return Exp_32s_Pos(x);
134 }
135
Norm_32s_I(Ipp32s * x)136 __INLINE Ipp16s Norm_32s_I(Ipp32s *x){
137 Ipp16s i;
138 if (*x == 0) return 0;
139 if (*x < 0){
140 for(i = 0; *x >= (Ipp32s)0xC0000000; i++) *x <<= 1;
141 }else
142 for(i = 0; *x < (Ipp32s)0x40000000; i++) *x <<= 1;
143 return i;
144 }
Norm_16s_I(short * x)145 __INLINE short Norm_16s_I(short *x){
146 short i;
147 short y = *x;
148 if (y == 0) return 0;
149 if (y < 0) y = (short)~y;
150 i = Exp_16s_Pos(y);
151 *x <<= i;
152 return i;
153 }
Norm_32s_Pos_I(Ipp32s * x)154 __INLINE Ipp16s Norm_32s_Pos_I(Ipp32s *x){
155 Ipp16s i;
156 if (*x == 0) return 0;
157 for(i = 0; *x < (Ipp32s)0x40000000; i++) *x <<= 1;
158 return i;
159 }
160
MulHR_16s(Ipp16s x,Ipp16s y)161 __INLINE Ipp16s MulHR_16s(Ipp16s x, Ipp16s y) {
162 return (Ipp16s)( ((((Ipp32s)x * (Ipp32s)y)) + 0x4000) >> 15 );
163 }
Negate_16s(Ipp16s x)164 __INLINE Ipp16s Negate_16s(Ipp16s x) {
165 if(IPP_MIN_16S == x)
166 return IPP_MAX_16S;
167 return (Ipp16s)-x;
168 }
Negate_32s(Ipp32s x)169 __INLINE Ipp32s Negate_32s(Ipp32s x) {
170 if(IPP_MIN_32S == x)
171 return IPP_MAX_32S;
172 return (Ipp32s)-x;
173 }
Mul2_16s(Ipp16s x)174 __INLINE Ipp16s Mul2_16s(Ipp16s x) {
175 if(x > IPP_MAX_16S/2) return IPP_MAX_16S;
176 else if( x < IPP_MIN_16S/2) return IPP_MIN_16S;
177 x = (Ipp16s)(x << 1);
178 return x;
179 }
Mul2_32s(Ipp32s x)180 __INLINE Ipp32s Mul2_32s(Ipp32s x) {
181 if(x > IPP_MAX_32S/2) return IPP_MAX_32S;
182 else if( x < IPP_MIN_32S/2) return IPP_MIN_32S;
183 return x <<= 1;
184 }
185
Mul16_32s(Ipp32s x)186 __INLINE Ipp32s Mul16_32s(Ipp32s x) {
187 if(x > (Ipp32s) 0x07ffffffL) return IPP_MAX_32S;
188 else if( x < (Ipp32s) 0xf8000000L) return IPP_MIN_32S;
189 return (x <<= 4);
190 }
191
MulC_32s(Ipp16s val,Ipp32s x)192 __INLINE Ipp32s MulC_32s(Ipp16s val, Ipp32s x) {
193 Ipp32s z ;
194 Ipp32s xh, xl;
195 xh = x >> 16;
196 xl = x & 0xffff;
197 z = 2*val*xh;
198 z = Add_32s(z,(xl*val)>>15);
199
200 return( z );
201 }
202
ShiftL_32s(Ipp32s x,Ipp16u n)203 __INLINE Ipp32s ShiftL_32s(Ipp32s x, Ipp16u n)
204 {
205 Ipp32s max = IPP_MAX_32S >> n;
206 Ipp32s min = IPP_MIN_32S >> n;
207 if(x > max) return IPP_MAX_32S;
208 else if(x < min) return IPP_MIN_32S;
209 return (x<<n);
210 }
211
ShiftL_16s(Ipp16s n,Ipp16u x)212 __INLINE Ipp16s ShiftL_16s(Ipp16s n, Ipp16u x)
213 {
214 Ipp16s max = (Ipp16s)(IPP_MAX_16S >> x);
215 Ipp16s min = (Ipp16s)(IPP_MIN_16S >> x);
216 if(n > max) return IPP_MAX_16S;
217 else if(n < min) return IPP_MIN_16S;
218 return (Ipp16s)(n<<x);
219 }
220
ShiftR_NR_16s(Ipp16s x,Ipp16u n)221 __INLINE Ipp16s ShiftR_NR_16s(Ipp16s x, Ipp16u n){
222 return (Ipp16s)((x + (1<<(n-1)))>>n);
223 }
224
ExtractHigh(Ipp32s x)225 __INLINE Ipp16s ExtractHigh(Ipp32s x){
226 return ((Ipp16s)(x >> 16));
227 }
228
Unpack_32s(Ipp32s number,Ipp16s * hi,Ipp16s * lo)229 __INLINE void Unpack_32s (Ipp32s number, Ipp16s *hi, Ipp16s *lo)
230 {
231 *hi = (Ipp16s)(number >> 16);
232 *lo = (Ipp16s)((number>>1)&0x7fff);
233 }
Pack_16s32s(Ipp16s hi,Ipp16s lo)234 __INLINE Ipp32s Pack_16s32s(Ipp16s hi, Ipp16s lo)
235 {
236 return ( ((Ipp32s)hi<<16) + ((Ipp32s)lo<<1) );
237 }
Mpy_32_16(Ipp16s hi,Ipp16s lo,Ipp16s n)238 __INLINE Ipp32s Mpy_32_16 (Ipp16s hi, Ipp16s lo, Ipp16s n)
239 {
240 return (2 * (hi * n) + 2 * ((lo * n) >> 15));
241 }
242
Mul_16s_Sfs(Ipp16s x,Ipp16s y,Ipp32s scaleFactor)243 __INLINE Ipp16s Mul_16s_Sfs(Ipp16s x, Ipp16s y, Ipp32s scaleFactor) {
244 return (Ipp16s)((x * y) >> scaleFactor);
245 }
Mul_32s(Ipp32s x,Ipp32s y)246 __INLINE Ipp32s Mul_32s(Ipp32s x,Ipp32s y) {
247 Ipp32s z,z1,z2;
248 Ipp16s xh, xl, yh, yl;
249 xh = (Ipp16s)(x >> 15);
250 yh = (Ipp16s)(y >> 15);
251 xl = (Ipp16s)(x & 0x7fff);
252 yl = (Ipp16s)(y & 0x7fff);
253 z1 = Mul_16s_Sfs(xh,yl,15);
254 z2 = Mul_16s_Sfs(xl,yh,15);
255 z = xh * yh;
256 z += z1;
257 z += z2;
258 return (z<<1);
259 }
Mul4_32s(Ipp32s x)260 __INLINE Ipp32s Mul4_32s(Ipp32s x) {
261 if(x > IPP_MAX_32S/4) return IPP_MAX_32S;
262 else if( x < IPP_MIN_32S/4) return IPP_MIN_32S;
263 return x <<= 2;
264 }
Mul_32s16s(Ipp32s x,Ipp16s y)265 __INLINE Ipp32s Mul_32s16s(Ipp32s x, Ipp16s y) {
266 short xh, xl;
267 xh = (short)(x >> 15);
268 xl = (short)(x & 0x7fff);
269 return ((xh*y)+((xl*y)>>15));
270 }
Add_16s(Ipp16s x,Ipp16s y)271 __INLINE Ipp16s Add_16s(Ipp16s x, Ipp16s y) {
272 return Cnvrt_32s16s((Ipp32s)x + (Ipp32s)y);
273 }
Sub_16s(Ipp16s x,Ipp16s y)274 __INLINE Ipp16s Sub_16s(Ipp16s x, Ipp16s y) {
275 return Cnvrt_32s16s((Ipp32s)x - (Ipp32s)y);
276 }
277
Sub_32s(Ipp32s x,Ipp32s y)278 __INLINE Ipp32s Sub_32s(Ipp32s x,Ipp32s y){
279 return Cnvrt_64s32s((__INT64)x - y);
280 }
281
ownSqrt_32s(Ipp32s n)282 __INLINE Ipp32s ownSqrt_32s( Ipp32s n )
283 {
284 Ipp32s i ;
285
286 Ipp16s x = 0 ;
287 Ipp16s y = 0x4000 ;
288
289 Ipp32s z ;
290
291 for ( i = 0 ; i < 14 ; i ++ ) {
292 z = (x + y) * (x + y ) ;
293 if ( n >= z )
294 x = (Ipp16s)(x + y);
295 y >>= 1 ;
296 }
297 return x;
298 }
299
Exp_64s_Pos(Ipp64u x)300 __INLINE Ipp32s Exp_64s_Pos(Ipp64u x)
301 {
302 if (x == 0) return 0;
303 if((x>>32)==0) return (Ipp32s)(32+Exp_32s_Pos((Ipp32u) x));
304 return (Ipp32s)Exp_32s_Pos((Ipp32u)(x>>32));
305 }
306
307
Norm_64s(Ipp64s x)308 __INLINE Ipp32s Norm_64s(Ipp64s x){
309 Ipp32s i;
310 if (x == 0) return 0;
311 if (x == -1) return 63;
312 if (x < 0) x = ~x;
313 i = (Ipp32s)Exp_64s_Pos((Ipp64u)x);
314 return i;
315 }
Norm_32s(Ipp32s x)316 __INLINE Ipp32s Norm_32s(Ipp32s x){
317 Ipp32s i;
318 if (x == 0) return 0;
319 if (x == -1) return 31;
320 if (x < 0) x = ~x;
321 i = (Ipp32s)Exp_32s_Pos((Ipp32u)x);
322 return i;
323 }
324
Div_16s(Ipp16s num,Ipp16s den)325 __INLINE Ipp16s Div_16s(Ipp16s num, Ipp16s den)
326 {
327 if ((num < den) && (num > 0) && (den > 0)) {
328 return (Ipp16s)( (((Ipp32s)num)<<15) / den);
329 } else if ((den != 0) && (num == den)) {
330 return IPP_MAX_16S;
331 }
332 return 0;
333 }
Div_32s16s(int num32,short den)334 __INLINE short Div_32s16s(int num32, short den)
335 {
336 return (den>0) ? (short)((num32>>1)/den) : IPP_MAX_16S; /* div: den # 0*/
337 }
338
339 extern CONST short LostFrame[200];
340
341 #endif /* __AUX_FNXS__ */
342