⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mathhalf.c

📁 MELPe 1200 bps, fixed point
💻 C
📖 第 1 页 / 共 5 页
字号:
		if (var1 < 0)
			swOut = negate(var1);
		else
			swOut = var1;
	}
	return(swOut);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: L_abs
 *
 *	 PURPOSE:
 *
 *	   Take the absolute value of the 32 bit input.  An input of
 *	   -0x8000 0000 results in a return value of 0x7fff ffff.
 *
 *	 INPUTS:
 *
 *	   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.
 *
 *
 *
 *	 KEYWORDS: absolute value, abs
 *
 *************************************************************************/
Longword L_abs(Longword L_var1)
{
	Longword	L_Out;

	if (L_var1 == LW_MIN)
		L_Out = LW_MAX;
	else {
		if (L_var1 < 0)
			L_Out = -L_var1;
		else
			L_Out = L_var1;
	}
	return(L_Out);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_add                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   40 bits addition of 40 bits accumulator with 32 bits variable (L_var1)  |
 |   with overflow control and saturation; the result is set at MAX40        |
 |   when overflow occurs or at MIN40 when underflow occurs.                 |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    L_var1   32 bit long signed integer (Longword) whose value falls in the  |
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40	L40_add(Word40 acc, Longword L_var1)
{
	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	acc = acc + (Word40)L_var1;

	if (acc > MAX_40){
		acc = MAX_40;
		/* Overflow = 1; */
	}
	if (acc < MIN_40){
		acc = MIN_40;
		/* Overflow = 1; */
	}
	return(acc);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_sub                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   40 bits subtraction of 40 bits accumulator with 32 bits variable        |
 |   (L_var1) with overflow control and saturation; the result is set at     |
 |   MAX40 when overflow occurs or at MIN40 when underflow occurs.           |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    L_var1   32 bit long signed integer (Longword) whose value falls in the  |
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_sub(Word40 acc, Longword L_var1)
{
	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	acc = acc - (Word40)L_var1;

	if (acc > MAX_40){
		acc = MAX_40;
		/* Overflow = 1; */
	}
	if (acc < MIN_40){
		acc = MIN_40;
		/* Overflow = 1; */
	}
	return(acc);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_mac                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Multiply var1 by var2 and shift the result left by 1. Add the result    |
 |   to 40 bits accumulator with overflow control and saturation; the result |
 |   is set at MAX40 when overflow occurs or at MIN40 when underflow occurs. |
 |                                                                           |
 |   Complexity weight : 1                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    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 <= var1 <= 0x0000 7fff.                   |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_mac(Word40 acc, Shortword var1, Shortword var2)
{
	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	acc = acc + ((Word40)var1 * (Word40)var2 * 2);

	if (acc > MAX_40){
		acc = MAX_40;
		/* Overflow = 1; */
	}
	if (acc < MIN_40){
		acc = MIN_40;
		/* Overflow = 1; */
	}
	return(acc);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_msu                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Multiply var1 by var2 and shift the result left by 1. Subtract the      |
 |   result to 40 bits accumulator with overflow control and saturation;     |
 |   the result is set at MAX40 when overflow occurs or at MIN40 when        |
 |   underflow occurs.                                                       |
 |                                                                           |
 |   Complexity weight : 1                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    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 <= var1 <= 0x0000 7fff.                   |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_msu(Word40 acc, Shortword var1, Shortword var2)
{
	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	acc = acc - ((Word40)var1 * (Word40)var2 * 2);

	if (acc > MAX_40){
		acc = MAX_40;
		/* Overflow = 1; */
	}
	if (acc < MIN_40){
		acc = MIN_40;
		/* Overflow = 1; */
	}
	return(acc);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_shl                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Arithmetically shift the 40 bits accumulator left var1 positions. Zero  |
 |   fill the var1 LSB of the result. If var1 is negative, arithmetically    |
 |   shift 40 bits accumulator right by -var1 with sign extension. Saturate  |
 |   the result in case of underflows or overflows.                          |              |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    var1     16 bit short signed integer (Shortword) whose value falls in the |
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_shl(Word40 acc, Shortword var1)
{

	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	if (var1 < 0){
		acc = L40_shr(acc, (Shortword) (-var1));
	} else {
		for(; var1 > 0; var1--){
			acc = acc * 2;
			if (acc > MAX_40){
				acc = MAX_40;
				break;
			} else if (acc < MIN_40){
				acc = MIN_40;
				break;
			}
		}
	}
	return(acc);
}


/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_shr                                                 |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Arithmetically shift the 40 bits accumulator right var1 positions with  |
 |   sign extension. If var1 is negative, arithmetically shift 40 bits       |
 |   accumulator left by -var1 and zero fill the var1 LSB of the result.     |
 |   Saturate the result in case of underflows or overflows.                 |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |    var1     16 bit short signed integer (Shortword) whose value falls in the |
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_shr(Word40 acc, Shortword var1)
{

	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	if (var1 < 0){
		acc = L40_shl(acc, (Shortword) -var1);
	} else { 
		for ( ; var1 > 0; var1--){
			acc = floor(acc * 0.5);
			if ((acc == 0) || (acc == -1))
				break;
		}
	}
	return(acc);
}


/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : L40_negate                                              |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Negate the 40 bits accumulator with saturation; saturate to MAX40 in    |
 |   the case where input is MIN40.                                          |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |___________________________________________________________________________|
*/

Word40 L40_negate(Word40 acc)
{
	if ((acc > MAX_40) || (acc < MIN_40) || (acc != floor(acc))){
		fprintf(stderr, "BASIC_OP: Error in 40 bits format.\n");
		exit(0);
	}

	acc = -acc;

	if (acc > MAX_40){
		acc = MAX_40;
	}
	return(acc);
}

/*___________________________________________________________________________
 |                                                                           |
 |   Function Name : norm32                                                  |
 |                                                                           |
 |   Purpose :                                                               |
 |                                                                           |
 |   Produces the number of left shift needed to normalize the 40 bits       |
 |   accumulator in 32 bits format (for positive value on the interval with  |
 |   minimum of 1073741824 and maximum of 2147483647, and for negative value |
 |   on the interval with minimum of -2147483648 and maximum of -1073741824);|
 |   in order to normalize the result, the following operation must be done: |
 |                   norm32_acc = L40_shl(acc, norm32(acc)).                 |
 |                                                                           |
 |   Complexity weight : 2                                                   |
 |                                                                           |
 |   Inputs :                                                                |
 |                                                                           |
 |    acc      40 bits accumulator (Word40) whose value falls in the         |
 |             range : MIN40 <= acc <= MAX40.                                |
 |                                                                           |
 |   Return Value :                                                          |
 |                                                                           |
 |    var1     16 bit short signed integer (Shortword) whose value falls in the |
 |             range : -8 <= var_out <= 31.                                  |
 |___________________________________________________________________________|
*/

Shortword norm32(Word40 acc)
{
	Shortword	var1;

	if ((acc >

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -