mmlibs/mmfw/Codecs/Src/Gsm610CodecCommon/basicop.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "basicop.h"
       
    17 
       
    18 /*
       
    19 ** int2 add( int2 var1, int2 var2 )
       
    20 **
       
    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
       
    24 **
       
    25 ** Input:
       
    26 **   var1, var2
       
    27 **     16-bit variables to be summed
       
    28 **
       
    29 ** Output:
       
    30 **   Sum of var and var2, see description above
       
    31 **  
       
    32 ** Return value:
       
    33 **   See above
       
    34 */
       
    35 /*
       
    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
       
    39 */
       
    40 int2 add( int2 var1, int2 var2 )
       
    41 {
       
    42   int4 L_temp;
       
    43   
       
    44   L_temp = (int4) var1 + var2;
       
    45   
       
    46   if ( L_temp < MININT2 )
       
    47     return MININT2;
       
    48   else if ( L_temp > MAXINT2 )
       
    49     return MAXINT2;
       
    50   else
       
    51     return (int2) L_temp;
       
    52 }
       
    53 
       
    54 
       
    55 /*
       
    56 ** int2 sub( int2 var1, int2 var2 )
       
    57 **
       
    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
       
    61 **
       
    62 ** Input:
       
    63 **   var1, var2
       
    64 **     16-bit variables to be summed
       
    65 **
       
    66 ** Output:
       
    67 **   Sum of var and var2, see description above
       
    68 **  
       
    69 ** Return value:
       
    70 **   See above
       
    71 */
       
    72 int2 sub( int2 var1, int2 var2 )
       
    73 {
       
    74   int4 L_temp;
       
    75   
       
    76   L_temp = (int4) var1 - var2;
       
    77   
       
    78   if ( L_temp < MININT2 )
       
    79     return MININT2;
       
    80   else if ( L_temp > MAXINT2 )
       
    81     return MAXINT2;
       
    82   else
       
    83     return (int2) L_temp;
       
    84 }
       
    85 
       
    86 
       
    87 
       
    88 /*
       
    89 ** int2 mult( int2 var1, int2 var2 )
       
    90 **
       
    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
       
    94 ** and
       
    95 **   mult( -32768, -32768 ) = 32767 
       
    96 **
       
    97 ** Input:
       
    98 **   var1, var2
       
    99 **     16-bit variables to be multiplied
       
   100 **
       
   101 ** Output:
       
   102 **   Scaled result of multiplication
       
   103 **  
       
   104 ** Return value:
       
   105 **   See above
       
   106 */
       
   107 int2 mult( int2 var1, int2 var2 )
       
   108 {
       
   109   if ( ( var1 == MININT2 ) && ( var1 == var2 ) )
       
   110     return MAXINT2;
       
   111   else
       
   112     /* Note int4 cast of var1 to force 32-bit arithmetic */
       
   113    return( (int2) ( ( (int4) var1 * var2 ) >> 15 ) );
       
   114 }
       
   115 
       
   116 
       
   117 /*
       
   118 ** int2 abs_s( int2 var1 )
       
   119 **
       
   120 ** Function returns absolute value of var1 with possible saturation:
       
   121 **   abs( -32768 ) = 32767
       
   122 **
       
   123 ** Input:
       
   124 **   var1
       
   125 **     16-bit variable
       
   126 **
       
   127 ** Output:
       
   128 **   absolute value of var1
       
   129 **  
       
   130 ** Return value:
       
   131 **   See above
       
   132 */
       
   133 int2 abs_s( int2 var1 )
       
   134 {
       
   135   if ( var1 == MININT2 )
       
   136     return MAXINT2;
       
   137   else
       
   138     return ( var1 > 0 ) ? var1 : int2 (-var1);
       
   139 }
       
   140 
       
   141 
       
   142 #ifdef L_MULTF
       
   143 /* else implemented using macro (basicop.h) */
       
   144 
       
   145 /*
       
   146 ** int4 L_mult( int2 var1, int2 var2 )
       
   147 **
       
   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
       
   151 **
       
   152 **   L_mult( -32768, -32768 ) does not occur in algorithm
       
   153 **
       
   154 ** Input:
       
   155 **   var1, var2
       
   156 **     16-bit variables to be multiplied
       
   157 **
       
   158 ** Output:
       
   159 **   Scaled result of multiplication
       
   160 **  
       
   161 ** Return value:
       
   162 **   See above
       
   163 */
       
   164 int4 L_mult( int2 var1, int2 var2 )
       
   165 {
       
   166   /* Note int4 cast of var1 to force 32-bit arithmetic */
       
   167   return ( L_shl( (int4) var1 * var2 ), 1 );
       
   168 }
       
   169 #endif /* L_MULTF */
       
   170 
       
   171 
       
   172 
       
   173 /*
       
   174 ** int2 shr( int2 var1, int2 var2 )
       
   175 **
       
   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
       
   178 **
       
   179 ** Input:
       
   180 **   var1
       
   181 **     16-bit variable to be shifted
       
   182 **   var2
       
   183 **     amount of bits to be shifted
       
   184 **
       
   185 ** Output:
       
   186 **   16-bit value of shifted var1 is returned 
       
   187 **  
       
   188 ** Return value:
       
   189 **   See above
       
   190 */
       
   191 int2 shr( int2 var1, int2 var2 )
       
   192 {
       
   193   return shl( var1, int2 (-var2) );
       
   194 }
       
   195 
       
   196 
       
   197 /*
       
   198 ** int2 negate( int2 var1 )
       
   199 **
       
   200 ** Function negates 16-bit variable var1. 
       
   201 **   negate( -32768 ) = 32767
       
   202 **
       
   203 ** Input:
       
   204 **   var1
       
   205 **     16-bit variable to be negated
       
   206 **
       
   207 ** Output:
       
   208 **   negated var1
       
   209 **  
       
   210 ** Return value:
       
   211 **   See above
       
   212 */
       
   213 int2 negate( int2 var1 )
       
   214 {
       
   215   if ( var1 == MININT2 )
       
   216     return MAXINT2;
       
   217   else
       
   218     return int2 (-var1);
       
   219 }
       
   220 
       
   221 
       
   222 /*
       
   223 ** int2 extract_h( int4 L_var1 )
       
   224 **
       
   225 ** Function returns upper word (16 most significat bits) of the 
       
   226 ** 32-bit variable L_var1
       
   227 **
       
   228 ** Input:
       
   229 **   L_var1
       
   230 **     32-bit variable
       
   231 **
       
   232 ** Output:
       
   233 **   upper word of the L_var1
       
   234 **  
       
   235 ** Return value:
       
   236 **   See above
       
   237 */
       
   238 int2 extract_h( int4 L_var1 )
       
   239 {
       
   240   return (int2) ( L_var1 >> 16 );
       
   241 }
       
   242 
       
   243 
       
   244 /*
       
   245 ** int2 extract_l( int4 L_var1 )
       
   246 **
       
   247 ** Function returns lower word (16 least significat bits) of the
       
   248 ** 32-bit variable L_var1
       
   249 **
       
   250 ** Input:
       
   251 **   L_var1
       
   252 **     32-bit variable
       
   253 **
       
   254 ** Output:
       
   255 **   lower word of L_var1
       
   256 **  
       
   257 ** Return value:
       
   258 **   See above
       
   259 */
       
   260 int2 extract_l( int4 L_var1 )
       
   261 {
       
   262   return (int2) (L_var1 & 0x0000ffff);
       
   263 }
       
   264 
       
   265 
       
   266 /*
       
   267 ** int4 L_mac( int4 L_var3, int2 var1, int2 var2 )
       
   268 **
       
   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
       
   274 **
       
   275 ** Input:
       
   276 **   var1
       
   277 **     16-bit multiplicant
       
   278 **   var2
       
   279 **     16-bit multiplier
       
   280 **
       
   281 **   L_var3
       
   282 **     32-bit number that is summed with (var1*var2)<<1
       
   283 **
       
   284 ** Output:
       
   285 **   See description above
       
   286 **  
       
   287 ** Return value:
       
   288 **   See above
       
   289 */
       
   290 int4 L_mac( int4 L_var3, int2 var1, int2 var2 )
       
   291 {
       
   292   int4 L_temp;
       
   293 
       
   294   L_temp = ( (int4) var1 * var2 ) << 1;
       
   295   return L_add( L_var3, L_temp );
       
   296 }
       
   297 
       
   298 
       
   299 /*
       
   300 ** int4 L_add( int4 L_var1, int4 L_var2 )
       
   301 **
       
   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
       
   306 **
       
   307 ** Input:
       
   308 **   L_var1, L_var2
       
   309 **     32-bit variables to be summed
       
   310 **
       
   311 ** Output:
       
   312 **   32-bit result, see description above
       
   313 **  
       
   314 ** Return value:
       
   315 **   See above
       
   316 */
       
   317 int4 L_add( int4 L_var1, int4 L_var2 )
       
   318 {
       
   319   int4 L_temp1;
       
   320   int temp2; /* used for storing sign of L_var1 */
       
   321   
       
   322   L_temp1 = L_var1 + L_var2;
       
   323 
       
   324   /*
       
   325    * Overflow
       
   326    *   if sign(L_var1)==sign(L_var2) && sign(L_var1)!=sign(L_temp1).
       
   327    */
       
   328 
       
   329   if ( ( temp2 = (L_var1 < 0) ) == ( L_var2 < 0 ) 
       
   330     && ( temp2 != ( L_temp1 < 0 ) ) ) {
       
   331       L_temp1 = temp2 ? MININT4 : MAXINT4;
       
   332   }
       
   333 
       
   334   return L_temp1;
       
   335 }
       
   336 
       
   337 
       
   338 /*
       
   339 ** int4 L_sub( int4 L_var1, int4 L_var2 )
       
   340 **
       
   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
       
   345 **
       
   346 ** Input:
       
   347 **   L_var1, L_var2
       
   348 **     32-bit variables to be summed
       
   349 **
       
   350 ** Output:
       
   351 **   32-bit result, see description above
       
   352 **  
       
   353 ** Return value:
       
   354 **   See above
       
   355 */
       
   356 int4 L_sub( int4 L_var1, int4 L_var2 )
       
   357 {
       
   358   int4 L_temp1;
       
   359   int temp2;
       
   360 
       
   361   L_temp1 = L_var1 - L_var2;
       
   362 
       
   363   /*
       
   364    * Overflow
       
   365    *   if sign(L_var1)!=sign(L_var2) && sign(L_var1)!=sign(L_temp).
       
   366    */
       
   367 
       
   368   if ( ( temp2 = ( L_var1 < 0 ) ) != ( L_var2 < 0 ) 
       
   369     && ( temp2 != ( L_temp1 < 0 ) ) ) {
       
   370       L_temp1 = temp2 ? MININT4 : MAXINT4;
       
   371   }
       
   372 
       
   373   return L_temp1;
       
   374 }
       
   375 
       
   376 
       
   377 /*
       
   378 ** int2 mult_r( int2 var1, int2 var2 )
       
   379 **
       
   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
       
   383 ** and
       
   384 **   mult_r( -32768, -32768 ) = 32767 
       
   385 **
       
   386 ** Input:
       
   387 **   var1, var2
       
   388 **     16-bit variables to be multiplied
       
   389 **
       
   390 ** Output:
       
   391 **   16-bit scaled result of multiplication
       
   392 **  
       
   393 ** Return value:
       
   394 **   See above
       
   395 */
       
   396 int2 mult_r( int2 var1, int2 var2 )
       
   397 {
       
   398   if ( ( var1 == MININT2 ) && ( var1 == var2 ) )
       
   399     return MAXINT2;
       
   400   else
       
   401     /* Note int4 cast of var1 to force 32-bit arithmetic */
       
   402     return( (int2) ( ( (int4) var1 * var2 + (int4) 16384 ) >> 15 ) );
       
   403 }
       
   404 
       
   405 /*
       
   406 ** int4 L_shr( int4 L_var1, int2 var2 )
       
   407 **
       
   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
       
   410 **
       
   411 ** Input:
       
   412 **   L_var1
       
   413 **     32-bit variable to be shifted
       
   414 **   var2
       
   415 **     amount of bits to be shifted
       
   416 **
       
   417 ** Output:
       
   418 **   16-bit value of shifted var1
       
   419 **  
       
   420 ** Return value:
       
   421 **   See above
       
   422 */
       
   423 int4 L_shr( int4 L_var1, int2 var2 )
       
   424 {
       
   425   return L_shl( L_var1, int2 (-var2) );
       
   426 }
       
   427 
       
   428 
       
   429 /*
       
   430 ** int4 L_deposit_h( int2 var1 )
       
   431 **
       
   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.
       
   435 **
       
   436 ** Input:
       
   437 **   var1
       
   438 **     16-bit variable to be loaded to the upper 16 bits of
       
   439 **     of the 32-bit variable
       
   440 **
       
   441 ** Output:
       
   442 **   32-bit number, see description above
       
   443 **  
       
   444 ** Return value:
       
   445 **   See above
       
   446 */
       
   447 int4 L_deposit_h( int2 var1 )
       
   448 {
       
   449   return ( (int4) var1 ) << 16;
       
   450 }
       
   451 
       
   452 
       
   453 /*
       
   454 ** int4 L_deposit_l( int2 var1 )
       
   455 **
       
   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.
       
   459 **
       
   460 ** Input:
       
   461 **   var1
       
   462 **     16-bit variable to be converted to 32-bit variable
       
   463 **
       
   464 ** Output:
       
   465 **   32-bit variable that has same magnitude than var1
       
   466 **  
       
   467 ** Return value:
       
   468 **   See above
       
   469 */
       
   470 int4 L_deposit_l( int2 var1 )
       
   471 {
       
   472   return (int4) var1;
       
   473 }
       
   474 
       
   475 
       
   476 /*
       
   477 ** int2 norm_s( int2 var1 )
       
   478 **
       
   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)
       
   486 **
       
   487 ** Input:
       
   488 **   var1
       
   489 **     16-bit variable which normalization factor is solved
       
   490 **
       
   491 ** Output:
       
   492 **   see description above
       
   493 **  
       
   494 ** Return value:
       
   495 **   See above
       
   496 */
       
   497 int2 norm_s( int2 var1 )
       
   498 {
       
   499   int2 cntr = 0;
       
   500 
       
   501   /* Special case when L_var1 == -32768: shift leads to underflow */
       
   502   if ( var1 == MININT2 )
       
   503     return 0;
       
   504   /* Special case when var1 == 0: shift does not change the value */
       
   505   else if ( var1 == 0 )
       
   506     return 0;
       
   507   else {
       
   508     if ( var1 < 0 ) {
       
   509       for ( ; var1 >= -16384; var1 *= 2 )
       
   510 	cntr++;      
       
   511     }
       
   512     else {
       
   513       for ( ; var1 < 16384; var1 <<= 1 )
       
   514 	cntr++;
       
   515     }
       
   516 
       
   517     return cntr;
       
   518   }
       
   519 }
       
   520 
       
   521 
       
   522 /*
       
   523 ** int2 norm_l( int4 L_var1 )
       
   524 **
       
   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)
       
   532 **
       
   533 ** Input:
       
   534 **   L_var1
       
   535 **     32-bit variable which normalization factor is solved
       
   536 **
       
   537 ** Output:
       
   538 **   see description above
       
   539 **  
       
   540 ** Return value:
       
   541 **   See above
       
   542 */
       
   543 int2 norm_l( int4 L_var1 )
       
   544 {
       
   545   int2 cntr = 0;
       
   546 
       
   547   /* Special case when L_var1 == -2147483648: shift leads to underflow */
       
   548   if ( L_var1 == MININT4 )
       
   549     return 0;
       
   550   /* Special case when L_var1 == 0: shift does not change the value */
       
   551   else if ( L_var1 == 0 )
       
   552     return 0;
       
   553   else {
       
   554     if ( L_var1 < 0 ) {
       
   555       for ( ; L_var1 >= (int4) -1073741824L; L_var1 *= 2 )
       
   556 	cntr++;
       
   557     }
       
   558     else {
       
   559       for ( ; L_var1 < (int4) 1073741824L; L_var1 <<= 1 )
       
   560 	cntr++;
       
   561     }
       
   562 
       
   563     return cntr;
       
   564   }
       
   565 }
       
   566 
       
   567 
       
   568 /*
       
   569 ** int2 div_s( int2 num, int2 denum )
       
   570 **
       
   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
       
   576 **
       
   577 ** Input:
       
   578 **   L_var1
       
   579 **     32-bit variable which normalization factor is solved
       
   580 **
       
   581 ** Output:
       
   582 **   16-bit result, see description above
       
   583 **  
       
   584 ** Return value:
       
   585 **   See above
       
   586 */
       
   587 int2 div_s( int2 num, int2 denum )
       
   588 {
       
   589   if ( num == denum )
       
   590     return MAXINT2;
       
   591   else
       
   592     return (int2) ( ( ( (int4) num ) << 15 ) / denum );
       
   593 }
       
   594 
       
   595 
       
   596 /*
       
   597 ** int2 shl( int2 var1, int2 var2 )
       
   598 **
       
   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
       
   601 **
       
   602 ** Input:
       
   603 **   var1
       
   604 **     16-bit variable to be shifted
       
   605 **   var2
       
   606 **     amount of bits to be shifted
       
   607 **
       
   608 ** Output:
       
   609 **   16-bit value of shifted var1 is retuned 
       
   610 **  
       
   611 ** Return value:
       
   612 **   See above
       
   613 **
       
   614 ** Notes:
       
   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.
       
   618 */
       
   619 int2 shl( int2 var1, int2 var2 )
       
   620 {
       
   621   int2 result;
       
   622 
       
   623   if ( ( var1 == 0 ) || ( var2 == 0 ) ) {
       
   624     result = var1;
       
   625   }
       
   626   
       
   627   /* var2 > 0: Perform left shift */
       
   628   else if ( var2 > 0 ) {
       
   629     if ( var2 >= 15 ) {
       
   630       result = ( var1 < 0 ) ? int2(MININT2) : int2(MAXINT2);
       
   631     }
       
   632     else {
       
   633 
       
   634       int4 L_temp;
       
   635       
       
   636       L_temp = (int4) var1 << var2;
       
   637       if ( L_temp < MININT2 ) {
       
   638 	result = MININT2;
       
   639       }
       
   640       else if ( L_temp > MAXINT2 ) {
       
   641 	result = MAXINT2;
       
   642       }
       
   643       else {
       
   644 	result = (int2) L_temp;
       
   645       }
       
   646     }
       
   647   }
       
   648   /* var2 < 0: Perform right shift */
       
   649   else {
       
   650     if ( -var2 >= 15 ) {
       
   651       result = ( var1 < 0 ) ? int2 (-1) : int2 (0);
       
   652     }
       
   653     else if ( var1 < 0 ) {
       
   654       result = int2 (~( (~var1) >> -var2 )); /* ~ used to ensure arith. shift */
       
   655     }
       
   656     else {
       
   657       result = int2 (var1 >> -var2);
       
   658     }
       
   659   }
       
   660 
       
   661   return result;
       
   662   
       
   663 }
       
   664 
       
   665 
       
   666 /*
       
   667 ** int4 L_shl( int4 L_var1, int2 var2 )
       
   668 **
       
   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
       
   671 **
       
   672 ** Input:
       
   673 **   L_var1
       
   674 **     32-bit variable to be shifted
       
   675 **   var2
       
   676 **     amount of bits to be shifted
       
   677 **
       
   678 ** Output:
       
   679 **   32-bit value of shifted var1 is retuned 
       
   680 **  
       
   681 ** Return value:
       
   682 **   See above
       
   683 **
       
   684 ** Notes:
       
   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.
       
   688 */
       
   689 
       
   690 int4 L_shl(int4 L_var1, int2 var2 )
       
   691 {
       
   692   if ( ( L_var1 == 0L ) || ( var2 == 0 ) ) {
       
   693     return L_var1;
       
   694   }
       
   695   /* var2 > 0: Perform left shift */
       
   696   else if ( var2 > 0 ) {
       
   697     if ( var2 >= 31 ) {
       
   698       return ( L_var1 < 0 ) ? MININT4 : MAXINT4;
       
   699     }
       
   700     else {
       
   701       for( ; var2 > 0; var2-- ) {
       
   702 	if ( L_var1 > (MAXINT4 >> 1) )
       
   703 	  return MAXINT4;
       
   704 	else if ( L_var1 < (MININT4 / 2) )
       
   705 	  return MININT4;
       
   706 	else
       
   707 	  L_var1 *= 2;
       
   708       }
       
   709       return L_var1;
       
   710     }
       
   711   }
       
   712   /* var2 < 0: Perform right shift */
       
   713   else {
       
   714     if ( -var2 >= 31 ) {
       
   715       return ( L_var1 < 0 ) ? -1L : 0L;
       
   716     }
       
   717     else if ( L_var1 < 0 ) {
       
   718       return ~( (~L_var1) >> -var2 ); /* ~ used to ensure arith. shift */
       
   719     }
       
   720     else {
       
   721       return L_var1 >> -var2;
       
   722     }
       
   723   }
       
   724 
       
   725 }
       
   726