📄 utils.c
字号:
/* State update complete. Get the output bit from the state, add/or it into output */ swPnBits = shl(swPnBits,1); swPnBits = swPnBits | (*pL_PNSeed & 1); } return(swPnBits);}/*************************************************************************** * * FUNCTION NAME: L_shl * * PURPOSE: * * Arithmetic shift left (or right). * Arithmetically shift the input left by var2. If var2 is * negative then an arithmetic shift right (L_shr) of L_var1 by * -var2 is performed. * * INPUTS: * * var2 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff. * L_var1 * 32 bit long signed integer (Longword) whose value * falls in the range * 0x8000 0000 <= L_var1 <= 0x7fff ffff. * OUTPUTS: * * none * * RETURN VALUE: * * L_Out * 32 bit long signed integer (Longword) whose value * falls in the range * 0x8000 0000 <= L_var1 <= 0x7fff ffff. * * * IMPLEMENTATION: * * Arithmetically shift the 32 bit input left by var2. This * operation maintains the sign of the input number. If var2 is * negative then an arithmetic shift right (L_shr) of L_var1 by * -var2 is performed. See description of L_shr for details. * * Equivalent to the Full-Rate GSM ">> n" operation. Note that * ANSI-C does not guarantee operation of the C ">>" or "<<" * operator for negative numbers. * * KEYWORDS: shift, arithmetic shift left, * *************************************************************************/Longword L_shl(Longword L_var1, Shortword var2){ Longword L_Mask, L_Out; int i, iOverflow = 0; if (var2 == 0 || L_var1 == 0) { L_Out = L_var1; } else if (var2 < 0) { if (var2 <= -31) { if (L_var1 > 0) L_Out = 0; else L_Out = 0xffffffffL; } else L_Out = L_shr(L_var1, -var2); } else { if (var2 >= 31) iOverflow = 1; else { if (L_var1 < 0) L_Mask = LW_SIGN; /* sign bit mask */ else L_Mask = 0x0; L_Out = L_var1; for (i = 0; i < var2 && !iOverflow; i++) { /* check the sign bit */ L_Out = (L_Out & 0x7fffffffL) << 1; if ((L_Mask ^ L_Out) & LW_SIGN) iOverflow = 1; } } if (iOverflow) { /* saturate */ if (L_var1 > 0) L_Out = LW_MAX; else L_Out = LW_MIN; } } return (L_Out);}/*************************************************************************** * * FUNCTION NAME: L_shr * * PURPOSE: * * Arithmetic shift right (or left). * Arithmetically shift the input right by var2. If var2 is * negative then an arithmetic shift left (shl) of var1 by * -var2 is performed. * * INPUTS: * * var2 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff. * L_var1 * 32 bit long signed integer (Longword) whose value * falls in the range * 0x8000 0000 <= L_var1 <= 0x7fff ffff. * OUTPUTS: * * none * * RETURN VALUE: * * L_Out * 32 bit long signed integer (Longword) whose value * falls in the range * 0x8000 0000 <= L_var1 <= 0x7fff ffff. * * * IMPLEMENTATION: * * Arithmetically shift the input right by var2. This * operation maintains the sign of the input number. If var2 is * negative then an arithmetic shift left (shl) of L_var1 by * -var2 is performed. See description of L_shl for details. * * The input is a 32 bit number, as is the output. * * Equivalent to the Full-Rate GSM ">> n" operation. Note that * ANSI-C does not guarantee operation of the C ">>" or "<<" * operator for negative numbers. * * KEYWORDS: shift, arithmetic shift right, * *************************************************************************/Longword L_shr(Longword L_var1, Shortword var2){ Longword L_Mask, L_Out; if (var2 == 0 || L_var1 == 0) { L_Out = L_var1; } else if (var2 < 0) { /* perform a left shift */ /*----------------------*/ if (var2 <= -31) { /* saturate */ if (L_var1 > 0) L_Out = LW_MAX; else L_Out = LW_MIN; } else L_Out = L_shl(L_var1, -var2); } else { if (var2 >= 31) { if (L_var1 > 0) L_Out = 0; else L_Out = 0xffffffffL; } else { L_Mask = 0; if (L_var1 < 0) { L_Mask = ~L_Mask << (32 - var2); } L_var1 >>= var2; L_Out = L_Mask | L_var1; } } return (L_Out);}/*************************************************************************** * * FUNCTION NAME: shl * * PURPOSE: * * Arithmetically shift the input left by var2. * * * INPUTS: * * var1 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff. * var2 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff. * * OUTPUTS: * * none * * RETURN VALUE: * * swOut * 16 bit short signed integer (Shortword) whose value * falls in the range * 0xffff 8000 <= swOut <= 0x0000 7fff. * * IMPLEMENTATION: * * If Arithmetically shift the input left by var2. If var2 is * negative then an arithmetic shift right (shr) of var1 by * -var2 is performed. See description of shr for details. * When an arithmetic shift left is performed the var2 LS bits * are zero filled. * * The only exception is if the left shift causes an overflow * or underflow. In this case the LS bits are not modified. * The number returned is 0x8000 in the case of an underflow or * 0x7fff in the case of an overflow. * * The shl is equivalent to the Full-Rate GSM "<< n" operation. * Note that ANSI-C does not guarantee operation of the C ">>" * or "<<" operator for negative numbers - it is not specified * whether this shift is an arithmetic or logical shift. * * KEYWORDS: asl, arithmetic shift left, shift * *************************************************************************/Shortword shl(Shortword var1, Shortword var2){ Shortword swOut; Longword L_Out; if (var2 == 0 || var1 == 0) { swOut = var1; } else if (var2 < 0) { /* perform a right shift */ /*-----------------------*/ if (var2 <= -15) { if (var1 < 0) swOut = (Shortword) 0xffff; else swOut = 0x0; } else swOut = shr(var1, -var2); } else { /* var2 > 0 */ if (var2 >= 15) { /* saturate */ if (var1 > 0) swOut = SW_MAX; else swOut = SW_MIN; } else { L_Out = (Longword) var1 *(1 << var2); swOut = (Shortword) L_Out; /* copy low portion to swOut, * overflow could have hpnd */ if (swOut != L_Out) { /* overflow */ if (var1 > 0) swOut = SW_MAX; /* saturate */ else swOut = SW_MIN; /* saturate */ } } } return (swOut);}/*************************************************************************** * * FUNCTION NAME: shr * * PURPOSE: * * Arithmetic shift right (or left). * Arithmetically shift the input right by var2. If var2 is * negative then an arithmetic shift left (shl) of var1 by * -var2 is performed. * * INPUTS: * * var1 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff. * var2 * 16 bit short signed integer (Shortword) whose value * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff. * * OUTPUTS: * * none * * RETURN VALUE: * * swOut * 16 bit short signed integer (Shortword) whose value * falls in the range * 0xffff 8000 <= swOut <= 0x0000 7fff. * * IMPLEMENTATION: * * Arithmetically shift the input right by var2. This * operation maintains the sign of the input number. If var2 is * negative then an arithmetic shift left (shl) of var1 by * -var2 is performed. See description of shl for details. * * Equivalent to the Full-Rate GSM ">> n" operation. Note that * ANSI-C does not guarantee operation of the C ">>" or "<<" * operator for negative numbers.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -