2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
24 Contains: Dynamic Predictor encode routines
26 Copyright: (c) 2001-2011 Apple, Inc.
33 #define ALWAYS_INLINE __attribute__((always_inline))
38 #if TARGET_CPU_PPC && (__MWERKS__ >= 0x3200)
39 // align loops to a 16 byte boundary to make the G5 happy
40 #pragma function_align 16
41 #define LOOP_ALIGN asm { align 16 }
46 void init_coefs( int16_t * coefs, uint32_t denshift, int32_t numPairs )
49 int32_t den = 1 << denshift;
51 coefs[0] = (AINIT * den) >> 4;
52 coefs[1] = (BINIT * den) >> 4;
53 coefs[2] = (CINIT * den) >> 4;
54 for ( k = 3; k < numPairs; k++ )
58 void copy_coefs( int16_t * srcCoefs, int16_t * dstCoefs, int32_t numPairs )
62 for ( k = 0; k < numPairs; k++ )
63 dstCoefs[k] = srcCoefs[k];
66 static inline int32_t ALWAYS_INLINE sign_of_int( int32_t i )
70 negishift = ((uint32_t)-i) >> 31;
71 return negishift | (i >> 31);
74 void pc_block( int32_t * in, int32_t * pc1, int32_t num, int16_t * coefs, int32_t numactive, uint32_t chanbits, uint32_t denshift )
76 register int16_t a0, a1, a2, a3;
77 register int32_t b0, b1, b2, b3;
84 uint32_t chanshift = 32 - chanbits;
85 int32_t denhalf = 1 << (denshift - 1);
90 // just copy if numactive == 0 (but don't bother if in/out pointers the same)
91 if ( (num > 1) && (in != pc1) )
92 memcpy( &pc1[1], &in[1], (num - 1) * sizeof(int32_t) );
95 if ( numactive == 31 )
97 // short-circuit if numactive == 31
98 for( j = 1; j < num; j++ )
100 del = in[j] - in[j-1];
101 pc1[j] = (del << chanshift) >> chanshift;
106 for ( j = 1; j <= numactive; j++ )
108 del = in[j] - in[j-1];
109 pc1[j] = (del << chanshift) >> chanshift;
114 if ( numactive == 4 )
116 // optimization for numactive == 4
122 for ( j = lim; j < num; j++ )
134 sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3) >> denshift;
136 del = in[j] - top - sum1;
137 del = (del << chanshift) >> chanshift;
141 sg = sign_of_int(del);
144 sgn = sign_of_int( b3 );
146 del0 -= (4 - 3) * ((sgn * b3) >> denshift);
150 sgn = sign_of_int( b2 );
152 del0 -= (4 - 2) * ((sgn * b2) >> denshift);
156 sgn = sign_of_int( b1 );
158 del0 -= (4 - 1) * ((sgn * b1) >> denshift);
162 a0 -= sign_of_int( b0 );
166 // note: to avoid unnecessary negations, we flip the value of "sgn"
167 sgn = -sign_of_int( b3 );
169 del0 -= (4 - 3) * ((sgn * b3) >> denshift);
173 sgn = -sign_of_int( b2 );
175 del0 -= (4 - 2) * ((sgn * b2) >> denshift);
179 sgn = -sign_of_int( b1 );
181 del0 -= (4 - 1) * ((sgn * b1) >> denshift);
185 a0 += sign_of_int( b0 );
194 else if ( numactive == 8 )
196 // optimization for numactive == 8
197 register int16_t a4, a5, a6, a7;
198 register int32_t b4, b5, b6, b7;
209 for ( j = lim; j < num; j++ )
226 sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3
227 - a4 * b4 - a5 * b5 - a6 * b6 - a7 * b7) >> denshift;
229 del = in[j] - top - sum1;
230 del = (del << chanshift) >> chanshift;
234 sg = sign_of_int(del);
237 sgn = sign_of_int( b7 );
239 del0 -= 1 * ((sgn * b7) >> denshift);
243 sgn = sign_of_int( b6 );
245 del0 -= 2 * ((sgn * b6) >> denshift);
249 sgn = sign_of_int( b5 );
251 del0 -= 3 * ((sgn * b5) >> denshift);
255 sgn = sign_of_int( b4 );
257 del0 -= 4 * ((sgn * b4) >> denshift);
261 sgn = sign_of_int( b3 );
263 del0 -= 5 * ((sgn * b3) >> denshift);
267 sgn = sign_of_int( b2 );
269 del0 -= 6 * ((sgn * b2) >> denshift);
273 sgn = sign_of_int( b1 );
275 del0 -= 7 * ((sgn * b1) >> denshift);
279 a0 -= sign_of_int( b0 );
283 // note: to avoid unnecessary negations, we flip the value of "sgn"
284 sgn = -sign_of_int( b7 );
286 del0 -= 1 * ((sgn * b7) >> denshift);
290 sgn = -sign_of_int( b6 );
292 del0 -= 2 * ((sgn * b6) >> denshift);
296 sgn = -sign_of_int( b5 );
298 del0 -= 3 * ((sgn * b5) >> denshift);
302 sgn = -sign_of_int( b4 );
304 del0 -= 4 * ((sgn * b4) >> denshift);
308 sgn = -sign_of_int( b3 );
310 del0 -= 5 * ((sgn * b3) >> denshift);
314 sgn = -sign_of_int( b2 );
316 del0 -= 6 * ((sgn * b2) >> denshift);
320 sgn = -sign_of_int( b1 );
322 del0 -= 7 * ((sgn * b1) >> denshift);
326 a0 += sign_of_int( b0 );
343 for ( j = lim; j < num; j++ )
351 for ( k = 0; k < numactive; k++ )
352 sum1 -= coefs[k] * (top - pin[-k]);
354 del = in[j] - top - ((sum1 + denhalf) >> denshift);
355 del = (del << chanshift) >> chanshift;
359 sg = sign_of_int( del );
362 for ( k = (numactive - 1); k >= 0; k-- )
365 sgn = sign_of_int( dd );
367 del0 -= (numactive - k) * ((sgn * dd) >> denshift);
374 for ( k = (numactive - 1); k >= 0; k-- )
377 sgn = sign_of_int( dd );
379 del0 -= (numactive - k) * ((-sgn * dd) >> denshift);