First public contribution.
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 ** int2 add( int2 var1, int2 var2 )
21 ** Function performs the addition (var1+var2) with overflow control
22 ** and saturation; the result is set at +32767 when overflow occurs
23 ** or at -32768 when underflow occurs
27 ** 16-bit variables to be summed
30 ** Sum of var and var2, see description above
36 ** One way to implement saturation control to add and sub is to
37 ** use temporary result that has enough bits to make overflow
38 ** impossible and the limit the final result
40 int2 add( int2 var1, int2 var2 )
44 L_temp = (int4) var1 + var2;
46 if ( L_temp < MININT2 )
48 else if ( L_temp > MAXINT2 )
56 ** int2 sub( int2 var1, int2 var2 )
58 ** Function performs the subtraction (var1-var2) with overflow control
59 ** and saturation; the result is set at +32767 when overflow occurs
60 ** or at -32768 when underflow occurs
64 ** 16-bit variables to be summed
67 ** Sum of var and var2, see description above
72 int2 sub( int2 var1, int2 var2 )
76 L_temp = (int4) var1 - var2;
78 if ( L_temp < MININT2 )
80 else if ( L_temp > MAXINT2 )
89 ** int2 mult( int2 var1, int2 var2 )
91 ** Function performs the multiplication of var1 by var2 and gives a
92 ** 16-bit result which is scaled ie
93 ** mult( var1, var2 ) = (var1 times var2) >> 15
95 ** mult( -32768, -32768 ) = 32767
99 ** 16-bit variables to be multiplied
102 ** Scaled result of multiplication
107 int2 mult( int2 var1, int2 var2 )
109 if ( ( var1 == MININT2 ) && ( var1 == var2 ) )
112 /* Note int4 cast of var1 to force 32-bit arithmetic */
113 return( (int2) ( ( (int4) var1 * var2 ) >> 15 ) );
118 ** int2 abs_s( int2 var1 )
120 ** Function returns absolute value of var1 with possible saturation:
121 ** abs( -32768 ) = 32767
128 ** absolute value of var1
133 int2 abs_s( int2 var1 )
135 if ( var1 == MININT2 )
138 return ( var1 > 0 ) ? var1 : int2 (-var1);
143 /* else implemented using macro (basicop.h) */
146 ** int4 L_mult( int2 var1, int2 var2 )
148 ** Function performs the multiplication of var1 by var2 and gives a
149 ** 32-bit result which is scaled by shifting result one bit left ie
150 ** L_mult( var1, var2 ) = (var1 times var2) << 1
152 ** L_mult( -32768, -32768 ) does not occur in algorithm
156 ** 16-bit variables to be multiplied
159 ** Scaled result of multiplication
164 int4 L_mult( int2 var1, int2 var2 )
166 /* Note int4 cast of var1 to force 32-bit arithmetic */
167 return ( L_shl( (int4) var1 * var2 ), 1 );
174 ** int2 shr( int2 var1, int2 var2 )
176 ** Function makes arithmetic var2-bit shift right of var1. If var2 is
177 ** less than 0, this operation becomes arithmetic left shift of -var2
181 ** 16-bit variable to be shifted
183 ** amount of bits to be shifted
186 ** 16-bit value of shifted var1 is returned
191 int2 shr( int2 var1, int2 var2 )
193 return shl( var1, int2 (-var2) );
198 ** int2 negate( int2 var1 )
200 ** Function negates 16-bit variable var1.
201 ** negate( -32768 ) = 32767
205 ** 16-bit variable to be negated
213 int2 negate( int2 var1 )
215 if ( var1 == MININT2 )
223 ** int2 extract_h( int4 L_var1 )
225 ** Function returns upper word (16 most significat bits) of the
226 ** 32-bit variable L_var1
233 ** upper word of the L_var1
238 int2 extract_h( int4 L_var1 )
240 return (int2) ( L_var1 >> 16 );
245 ** int2 extract_l( int4 L_var1 )
247 ** Function returns lower word (16 least significat bits) of the
248 ** 32-bit variable L_var1
255 ** lower word of L_var1
260 int2 extract_l( int4 L_var1 )
262 return (int2) (L_var1 & 0x0000ffff);
267 ** int4 L_mac( int4 L_var3, int2 var1, int2 var2 )
269 ** Function multiplies var1 by var2 and shifts result left by one bit.
270 ** Shifted result of multiplication is then added to L_var3 and result
271 ** is returned. Summation is done with overflow control and saturation;
272 ** the result is set at 2147483647 when overflow occurs and at
273 ** -2147483648 when underflow occurs
277 ** 16-bit multiplicant
282 ** 32-bit number that is summed with (var1*var2)<<1
285 ** See description above
290 int4 L_mac( int4 L_var3, int2 var1, int2 var2 )
294 L_temp = ( (int4) var1 * var2 ) << 1;
295 return L_add( L_var3, L_temp );
300 ** int4 L_add( int4 L_var1, int4 L_var2 )
302 ** Function performs 32-bit addition of two 32-bit variables
303 ** (L_var1 + L_var2) with overflow control and saturation; the
304 ** result is set at 2147483647 when overflow occurs and at
305 ** -2147483648 when underflow occurs
309 ** 32-bit variables to be summed
312 ** 32-bit result, see description above
317 int4 L_add( int4 L_var1, int4 L_var2 )
320 int temp2; /* used for storing sign of L_var1 */
322 L_temp1 = L_var1 + L_var2;
326 * if sign(L_var1)==sign(L_var2) && sign(L_var1)!=sign(L_temp1).
329 if ( ( temp2 = (L_var1 < 0) ) == ( L_var2 < 0 )
330 && ( temp2 != ( L_temp1 < 0 ) ) ) {
331 L_temp1 = temp2 ? MININT4 : MAXINT4;
339 ** int4 L_sub( int4 L_var1, int4 L_var2 )
341 ** Function performs 32-bit subtraction of two 32-bit variables
342 ** (L_var1 - L_var2) with overflow control and saturation; the
343 ** result is set at 2147483647 when overflow occurs and at
344 ** -2147483648 when underflow occurs
348 ** 32-bit variables to be summed
351 ** 32-bit result, see description above
356 int4 L_sub( int4 L_var1, int4 L_var2 )
361 L_temp1 = L_var1 - L_var2;
365 * if sign(L_var1)!=sign(L_var2) && sign(L_var1)!=sign(L_temp).
368 if ( ( temp2 = ( L_var1 < 0 ) ) != ( L_var2 < 0 )
369 && ( temp2 != ( L_temp1 < 0 ) ) ) {
370 L_temp1 = temp2 ? MININT4 : MAXINT4;
378 ** int2 mult_r( int2 var1, int2 var2 )
380 ** Function performs the multiplication of var1 by var2 and gives a
381 ** 16-bit result which is scaled with rounding ie
382 ** mult_r(var1, var2) = ((var1 times var2) + 16384) >> 15
384 ** mult_r( -32768, -32768 ) = 32767
388 ** 16-bit variables to be multiplied
391 ** 16-bit scaled result of multiplication
396 int2 mult_r( int2 var1, int2 var2 )
398 if ( ( var1 == MININT2 ) && ( var1 == var2 ) )
401 /* Note int4 cast of var1 to force 32-bit arithmetic */
402 return( (int2) ( ( (int4) var1 * var2 + (int4) 16384 ) >> 15 ) );
406 ** int4 L_shr( int4 L_var1, int2 var2 )
408 ** Function makes arithmetic var2-bit shift right of var1. If var2 is
409 ** less than 0, this operation becomes arithmetic left shift of -var2
413 ** 32-bit variable to be shifted
415 ** amount of bits to be shifted
418 ** 16-bit value of shifted var1
423 int4 L_shr( int4 L_var1, int2 var2 )
425 return L_shl( L_var1, int2 (-var2) );
430 ** int4 L_deposit_h( int2 var1 )
432 ** Function deposits the 16-bit variable var1 into the 16 most
433 ** significant bits of the 32-bit word. The 16 least significant
434 ** bits of the result are zeroed.
438 ** 16-bit variable to be loaded to the upper 16 bits of
439 ** of the 32-bit variable
442 ** 32-bit number, see description above
447 int4 L_deposit_h( int2 var1 )
449 return ( (int4) var1 ) << 16;
454 ** int4 L_deposit_l( int2 var1 )
456 ** Function deposits the 16-bit variable var1 into the 16 least
457 ** significant bits of the 32-bit word. The 16 most significant bits
458 ** of the result are sign extended.
462 ** 16-bit variable to be converted to 32-bit variable
465 ** 32-bit variable that has same magnitude than var1
470 int4 L_deposit_l( int2 var1 )
477 ** int2 norm_s( int2 var1 )
479 ** Function produces number of left shifts needed to normalize the
480 ** 16-bit variable var1 for positive values on the interval with
481 ** minimum of 16384 and maximum of 32767 and for negative
482 ** values on the interval with minimum of -32768 and maximum of
483 ** -16384; in order to normalize the result, the following
484 ** operation must be done:
485 ** norm_var1 = var1 << norm_s(var1)
489 ** 16-bit variable which normalization factor is solved
492 ** see description above
497 int2 norm_s( int2 var1 )
501 /* Special case when L_var1 == -32768: shift leads to underflow */
502 if ( var1 == MININT2 )
504 /* Special case when var1 == 0: shift does not change the value */
505 else if ( var1 == 0 )
509 for ( ; var1 >= -16384; var1 *= 2 )
513 for ( ; var1 < 16384; var1 <<= 1 )
523 ** int2 norm_l( int4 L_var1 )
525 ** Function produces number of left shifts needed to normalize the
526 ** 32-bit variable L_var1 for positive values on the interval with
527 ** minimum of 1073741824 and maximum of 2147483647 and for negative
528 ** values on the interval with minimum of -2147483648 and maximum of
529 ** -1073741824; in order to normalize the result, the following
530 ** operation must be done:
531 ** L_norm_var1 = L_var1 << norm_l(L_var1)
535 ** 32-bit variable which normalization factor is solved
538 ** see description above
543 int2 norm_l( int4 L_var1 )
547 /* Special case when L_var1 == -2147483648: shift leads to underflow */
548 if ( L_var1 == MININT4 )
550 /* Special case when L_var1 == 0: shift does not change the value */
551 else if ( L_var1 == 0 )
555 for ( ; L_var1 >= (int4) -1073741824L; L_var1 *= 2 )
559 for ( ; L_var1 < (int4) 1073741824L; L_var1 <<= 1 )
569 ** int2 div_s( int2 num, int2 denum )
571 ** Function produces a result which is the fractional integer division
572 ** of var1 by var2; var1 and var2 must be positive and var2 must be
573 ** greater or equal to var1; The result is positive (leading bit equal
574 ** to 0) and truncated to 16 bits. If var1 == var2 then
575 ** div_s( var1, var2 ) = 32767
579 ** 32-bit variable which normalization factor is solved
582 ** 16-bit result, see description above
587 int2 div_s( int2 num, int2 denum )
592 return (int2) ( ( ( (int4) num ) << 15 ) / denum );
597 ** int2 shl( int2 var1, int2 var2 )
599 ** Function makes arithmetic var2-bit shift left of var1. If var2 is
600 ** less than 0, this operation becomes arithmetic right shift of -var2
604 ** 16-bit variable to be shifted
606 ** amount of bits to be shifted
609 ** 16-bit value of shifted var1 is retuned
615 ** ANSI C does not guarantee that right shift is arithmetical
616 ** (that sign extension is done during shifting). That is why in this
617 ** routine negative values are complemented before shifting.
619 int2 shl( int2 var1, int2 var2 )
623 if ( ( var1 == 0 ) || ( var2 == 0 ) ) {
627 /* var2 > 0: Perform left shift */
628 else if ( var2 > 0 ) {
630 result = ( var1 < 0 ) ? int2(MININT2) : int2(MAXINT2);
636 L_temp = (int4) var1 << var2;
637 if ( L_temp < MININT2 ) {
640 else if ( L_temp > MAXINT2 ) {
644 result = (int2) L_temp;
648 /* var2 < 0: Perform right shift */
651 result = ( var1 < 0 ) ? int2 (-1) : int2 (0);
653 else if ( var1 < 0 ) {
654 result = int2 (~( (~var1) >> -var2 )); /* ~ used to ensure arith. shift */
657 result = int2 (var1 >> -var2);
667 ** int4 L_shl( int4 L_var1, int2 var2 )
669 ** Function makes arithmetic var2-bit shift left of var1. If var2 is
670 ** less than 0, this operation becomes arithmetic right shift of -var2
674 ** 32-bit variable to be shifted
676 ** amount of bits to be shifted
679 ** 32-bit value of shifted var1 is retuned
685 ** ANSI C does not guarantee that right shift is arithmetical
686 ** (that sign extension is done during shifting). That is why in this
687 ** routine negative values are complemented before shifting.
690 int4 L_shl(int4 L_var1, int2 var2 )
692 if ( ( L_var1 == 0L ) || ( var2 == 0 ) ) {
695 /* var2 > 0: Perform left shift */
696 else if ( var2 > 0 ) {
698 return ( L_var1 < 0 ) ? MININT4 : MAXINT4;
701 for( ; var2 > 0; var2-- ) {
702 if ( L_var1 > (MAXINT4 >> 1) )
704 else if ( L_var1 < (MININT4 / 2) )
712 /* var2 < 0: Perform right shift */
715 return ( L_var1 < 0 ) ? -1L : 0L;
717 else if ( L_var1 < 0 ) {
718 return ~( (~L_var1) >> -var2 ); /* ~ used to ensure arith. shift */
721 return L_var1 >> -var2;