math_lib.c
资源名称:melp.rar [点击查看]
上传用户:csczyc
上传日期:2021-02-19
资源大小:1051k
文件大小:28k
源码类别:
语音压缩
开发平台:
C/C++
- #include <stdio.h>
- #include <math.h>
- #include "spbstd.h"
- #include "mathhalf.h"
- #include "mathdp31.h"
- #include "wmops.h"
- #include "math_lib.h"
- #include "constant.h"
- //extern int complexity;
- extern int saturation;
- Shortword DEBUG;
- /***************************************************************************
- *
- * FUNCTION NAME: L_divider2
- *
- * PURPOSE:
- *
- * Divide numer by denom. If numer is bigger than denom, a warning
- * is given and a zero is returned.
- *
- *
- * INPUTS:
- *
- * numer
- * 32 bit long signed integer (Longword).
- * denom
- * 32 bit long signed integer (Longword).
- * numer_shift
- * 16 bit short signed integer (Shortword) represents
- * number of right shifts for numer.
- * denom_shift
- * 16 bit short signed integer (Shortword) represents
- * number of left shifts for denom.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * result
- * 16 bit short signed integer (Shortword).
- *
- *************************************************************************/
- Shortword L_divider2(Longword numer, Longword denom, Shortword numer_shift,
- Shortword denom_shift)
- {
- Shortword result;
- Shortword sign=0;
- Shortword short_shift=0;
- Longword L_temp;
- Shortword temp1,temp2;
- Longword L_temp1;
- DEBUG = 1;
- if (denom==0&& DEBUG )
- printf("L_divider2: division by 0n");
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (numer<0) {
- sign = !sign;
- // data_move();mark del
- }
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (denom<0) {
- sign = !sign;
- // data_move();mark del
- }
- /* L_temp = L_shl(denom, denom_shift); */
- /* denom = L_abs(L_temp); */
- /* // L_data_move();mark del */
- /* L_temp = L_shr(numer, numer_shift); */
- /* numer = L_abs(L_temp); */
- /* // L_data_move();mark del */
- L_temp = _sshvl(denom, denom_shift);
- denom = _abs(L_temp);
- // L_data_move();mark del
- L_temp = _sshvr(numer, numer_shift);
- numer = _abs(L_temp);
- // L_data_move();mark del
- while (denom>(Longword)SW_MAX)
- {
- /* increment complexity for while statement */
- // L_compare_nonzero();mark del
- /* denom = L_shr(denom,1); */
- denom = _sshvr(denom,1);
- // L_data_move();mark del
- /* short_shift = add(short_shift,1); */
- L_temp1 = _sadd2((Longword)short_shift,1);
- short_shift = (Shortword) (0x0000ffffL & L_temp1);
- // data_move();mark del
- }
- /* numer = L_shr(numer, short_shift); */
- numer = _sshvr(numer, short_shift);
- // L_data_move();mark del
- if (numer > denom && DEBUG)
- printf("warning: L_divide2>1: numer %.1f times denomn",
- (float)numer / (float)denom);
- /* result = divide_s(extract_l(numer),extract_l(denom)); */
- temp1 = (Shortword) (0x0000ffffL & numer);
- temp2 = (Shortword) (0x0000ffffL & denom);
- result = divide_s(temp1,temp2);
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (sign) {
- result = negate(result);
- // data_move();mark del
- }
- return(result);
- } /* L_divider2 */
- /***************************************************************************
- *
- * FUNCTION NAME: log10_fxp
- *
- * PURPOSE:
- *
- * Compute the logarithm to the base 10 of x.
- *
- *
- * INPUTS:
- *
- * x
- * 16 bit short signed integer (Shortword).
- * Q
- * 16 bit short signed integer (Shortword) represents
- * Q value of x.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * y
- * 16 bit short signed integer (Shortword) in Q12.
- *
- *************************************************************************/
- Shortword log10_fxp(Shortword x, Shortword Q)
- {
- /* table is Q13 */
- static Shortword table[256]=
- {
- 0, 2466, 3908, 4932, 5725, 6374, 6923, 7398, 7817, 8192,
- 8531, 8840, 9125, 9389, 9634, 9864, 10079, 10283, 10475, 10658,
- 10831, 10997, 11155, 11306, 11451, 11591, 11725, 11855, 11979, 12100,
- 12217, 12330, 12439, 12545, 12649, 12749, 12846, 12941, 13034, 13124,
- 13211, 13297, 13381, 13463, 13543, 13621, 13697, 13772, 13846, 13917,
- 13988, 14057, 14125, 14191, 14257, 14321, 14384, 14446, 14506, 14566,
- 14625, 14683, 14740, 14796, 14851, 14905, 14959, 15011, 15063, 15115,
- 15165, 15215, 15264, 15312, 15360, 15407, 15454, 15500, 15545, 15590,
- 15634, 15677, 15721, 15763, 15805, 15847, 15888, 15929, 15969, 16009,
- 16048, 16087, 16125, 16163, 16201, 16238, 16275, 16312, 16348, 16384,
- 16419, 16454, 16489, 16523, 16557, 16591, 16624, 16657, 16690, 16723,
- 16755, 16787, 16818, 16850, 16881, 16912, 16942, 16972, 17002, 17032,
- 17062, 17091, 17120, 17149, 17177, 17206, 17234, 17262, 17289, 17317,
- 17344, 17371, 17398, 17425, 17451, 17477, 17504, 17529, 17555, 17581,
- 17606, 17631, 17656, 17681, 17705, 17730, 17754, 17778, 17802, 17826,
- 17850, 17873, 17896, 17920, 17943, 17966, 17988, 18011, 18033, 18056,
- 18078, 18100, 18122, 18144, 18165, 18187, 18208, 18229, 18250, 18271,
- 18292, 18313, 18334, 18354, 18374, 18395, 18415, 18435, 18455, 18475,
- 18494, 18514, 18533, 18553, 18572, 18591, 18610, 18629, 18648, 18667,
- 18686, 18704, 18723, 18741, 18759, 18778, 18796, 18814, 18832, 18850,
- 18867, 18885, 18903, 18920, 18937, 18955, 18972, 18989, 19006, 19023,
- 19040, 19057, 19074, 19090, 19107, 19123, 19140, 19156, 19172, 19189,
- 19205, 19221, 19237, 19253, 19269, 19284, 19300, 19316, 19331, 19347,
- 19362, 19378, 19393, 19408, 19423, 19438, 19453, 19468, 19483, 19498,
- 19513, 19528, 19542, 19557, 19572, 19586, 19600, 19615, 19629, 19643,
- 19658, 19672, 19686, 19700, 19714, 19728 };
- Shortword y, interp_factor, interp_component;
- Shortword index1, index2;
- Shortword shift;
- Shortword temp1;
- Longword L_temp;
- Longword L_temp1,L_temp2,L_y;
- shift = sub(7,Q);
- // data_move();mark del
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (!x)
- {
- return(-SW_MAX);
- }
- while( !(index2=shr(x,7)) && x)
- {
- /* increment complexity for while statement */
- // data_move(); compare_zero();mark del
- /* x=shl(x,1); */
- x = (Shortword)(_sshl((Longword)x,1));
- // data_move();mark del
- shift = sub(shift,1);
- // data_move();mark del
- }
- index1 = sub(index2,1);
- // data_move();mark del
- /* interpolation */
- interp_factor = shl_a((Shortword)(x&127),8);
- // logic();mark del
- temp1 = sub(table[index2],table[index1]);
- /* interp_component = mult(temp1,interp_factor); */
- L_temp1 = _smpy(temp1,interp_factor);
- interp_component = (Shortword) (0x0000ffffL & (L_temp1 >> 16));
- // data_move();mark del
- /* return a Q12 */
- /* L_temp = L_mult(table[1],shift); */
- /* L_temp = L_shr(L_temp,2); */
- /* temp1 = shr(table[index1],1); */
- /* temp1 = add(temp1,extract_l(L_temp)); */
- /* temp2 = shr(interp_component,1); */
- /* y = add(temp1,temp2); */
- L_temp = _smpy(table[1],shift);
- L_temp = _sshvr(L_temp,2);
- L_temp1 = (Longword)(table[index1]);
- L_temp1 = _shr2(L_temp1,1);
- L_temp1 = _sadd2(L_temp1,L_temp);
- L_temp2 = (Longword)interp_component;
- L_temp2 = _shr2(L_temp2,1);
- L_y = _sadd2(L_temp1,L_temp2);
- y = (Shortword) (0x0000ffffL & L_y);
- // data_move();mark del
- return(y);
- } /* log10_fxp */
- /***************************************************************************
- *
- * FUNCTION NAME: L_log10_fxp
- *
- * PURPOSE:
- *
- * Compute the logarithm to the base 10 of x.
- *
- *
- * INPUTS:
- *
- * x
- * 32 bit long signed integer (Longword).
- * Q
- * 16 bit short signed integer (Shortword) represents
- * Q value of x.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * y
- * 16 bit short signed integer (Shortword) in Q11.
- *
- *************************************************************************/
- Shortword L_log10_fxp(Longword x, Shortword Q)
- {
- /* table is Q13 */
- static Shortword table[256]=
- {
- 0, 2466, 3908, 4932, 5725, 6374, 6923, 7398, 7817, 8192,
- 8531, 8840, 9125, 9389, 9634, 9864, 10079, 10283, 10475, 10658,
- 10831, 10997, 11155, 11306, 11451, 11591, 11725, 11855, 11979, 12100,
- 12217, 12330, 12439, 12545, 12649, 12749, 12846, 12941, 13034, 13124,
- 13211, 13297, 13381, 13463, 13543, 13621, 13697, 13772, 13846, 13917,
- 13988, 14057, 14125, 14191, 14257, 14321, 14384, 14446, 14506, 14566,
- 14625, 14683, 14740, 14796, 14851, 14905, 14959, 15011, 15063, 15115,
- 15165, 15215, 15264, 15312, 15360, 15407, 15454, 15500, 15545, 15590,
- 15634, 15677, 15721, 15763, 15805, 15847, 15888, 15929, 15969, 16009,
- 16048, 16087, 16125, 16163, 16201, 16238, 16275, 16312, 16348, 16384,
- 16419, 16454, 16489, 16523, 16557, 16591, 16624, 16657, 16690, 16723,
- 16755, 16787, 16818, 16850, 16881, 16912, 16942, 16972, 17002, 17032,
- 17062, 17091, 17120, 17149, 17177, 17206, 17234, 17262, 17289, 17317,
- 17344, 17371, 17398, 17425, 17451, 17477, 17504, 17529, 17555, 17581,
- 17606, 17631, 17656, 17681, 17705, 17730, 17754, 17778, 17802, 17826,
- 17850, 17873, 17896, 17920, 17943, 17966, 17988, 18011, 18033, 18056,
- 18078, 18100, 18122, 18144, 18165, 18187, 18208, 18229, 18250, 18271,
- 18292, 18313, 18334, 18354, 18374, 18395, 18415, 18435, 18455, 18475,
- 18494, 18514, 18533, 18553, 18572, 18591, 18610, 18629, 18648, 18667,
- 18686, 18704, 18723, 18741, 18759, 18778, 18796, 18814, 18832, 18850,
- 18867, 18885, 18903, 18920, 18937, 18955, 18972, 18989, 19006, 19023,
- 19040, 19057, 19074, 19090, 19107, 19123, 19140, 19156, 19172, 19189,
- 19205, 19221, 19237, 19253, 19269, 19284, 19300, 19316, 19331, 19347,
- 19362, 19378, 19393, 19408, 19423, 19438, 19453, 19468, 19483, 19498,
- 19513, 19528, 19542, 19557, 19572, 19586, 19600, 19615, 19629, 19643,
- 19658, 19672, 19686, 19700, 19714, 19728 };
- Shortword y, interp_component;
- Longword interp_factor;
- Shortword index1, index2;
- Shortword shift;
- Shortword temp1;
- Longword L_temp;
- Longword L_temp1,L_y;
- shift = sub(23,Q);
- // data_move();mark del
- /* increment complexity for if statement */
- //compare_zero();mark del
- if (!x)
- {
- return((Shortword)-SW_MAX);
- }
- while( !(index2=extract_l(_sshvr(x,23))) && x)
- {
- /* increment complexity for while statement */
- // compare_zero(); data_move();mark del
- /* x=L_shl(x,1); */
- x = _sshl(x,1);
- shift = sub(shift,1);// data_move();mark del
- }
- index1 = sub(index2,1);// data_move();mark del
- /* interpolation */
- /* interp_factor = L_shl(x&(Longword)0x7fffff, 8);// L_logic();mark del */
- /* temp1 = sub(table[index2],table[index1]); */
- /* interp_component = extract_h(L_mpy_ls(interp_factor,temp1)); */
- interp_factor = _sshl(x&(Longword)0x7fffff, 8);// L_logic();mark del
- temp1 = sub(table[index2],table[index1]);
- L_temp1 = L_mpy_ls(interp_factor,temp1);
- interp_component = (Shortword) (0x0000ffffL & (L_temp1 >> 16));
- /* return a Q11 */
- /* L_temp = L_mult(table[1],shift); log10(2^shift) */
- /* L_temp = L_shr(L_temp,3); */
- /* temp1 = shr(table[index1],2); */
- /* temp1 = add(temp1,extract_l(L_temp)); */
- /* temp2 = shr(interp_component,2); */
- /* y = add(temp1,temp2); // data_move();mark del */
- L_temp = _smpy(table[1],shift); /* log10(2^shift) */
- L_temp = _sshvr(L_temp,3);
- L_temp1 = _shr2((Longword)(table[index1]),2);
- L_temp1 = _sadd2(L_temp1,L_temp);
- L_temp = _shr2((Longword)interp_component,2);
- L_y = _sadd2(L_temp1,L_temp); // data_move();mark del
- y = (Shortword) (0x0000ffffL & L_y);
- return(y);
- } /* L_log10_fxp */
- /***************************************************************************
- *
- * FUNCTION NAME: pow10_fxp
- *
- * PURPOSE:
- *
- * Compute 10 raised to the power x.
- *
- *
- * INPUTS:
- *
- * x
- * 16 bit short signed integer (Shortword) in Q12.
- * Q
- * 16 bit short signed integer (Shortword) represents
- * required Q value of returned result.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * y
- * 16 bit short signed integer (Shortword).
- *
- *************************************************************************/
- Shortword pow10_fxp(Shortword x, Shortword Q)
- {
- /* table in Q11 */
- Shortword table[257]=
- {
- 2048, 2066, 2085, 2104, 2123, 2142, 2161, 2181, 2200, 2220,
- 2240, 2260, 2281, 2302, 2322, 2343, 2364, 2386, 2407, 2429,
- 2451, 2473, 2496, 2518, 2541, 2564, 2587, 2610, 2634, 2658,
- 2682, 2706, 2731, 2755, 2780, 2805, 2831, 2856, 2882, 2908,
- 2934, 2961, 2988, 3015, 3042, 3069, 3097, 3125, 3153, 3182,
- 3211, 3240, 3269, 3298, 3328, 3358, 3389, 3419, 3450, 3481,
- 3513, 3544, 3576, 3609, 3641, 3674, 3708, 3741, 3775, 3809,
- 3843, 3878, 3913, 3948, 3984, 4020, 4056, 4093, 4130, 4167,
- 4205, 4243, 4281, 4320, 4359, 4399, 4438, 4478, 4519, 4560,
- 4601, 4643, 4684, 4727, 4769, 4813, 4856, 4900, 4944, 4989,
- 5034, 5079, 5125, 5172, 5218, 5266, 5313, 5361, 5410, 5458,
- 5508, 5558, 5608, 5658, 5710, 5761, 5813, 5866, 5919, 5972,
- 6026, 6081, 6136, 6191, 6247, 6303, 6360, 6418, 6476, 6534,
- 6593, 6653, 6713, 6774, 6835, 6897, 6959, 7022, 7085, 7149,
- 7214, 7279, 7345, 7411, 7478, 7546, 7614, 7683, 7752, 7822,
- 7893, 7964, 8036, 8109, 8182, 8256, 8331, 8406, 8482, 8559,
- 8636, 8714, 8793, 8872, 8952, 9033, 9115, 9197, 9280, 9364,
- 9449, 9534, 9620, 9707, 9795, 9883, 9973, 10063, 10154, 10245,
- 10338, 10431, 10526, 10621, 10717, 10813, 10911, 11010, 11109, 11210,
- 11311, 11413, 11516, 11620, 11725, 11831, 11938, 12046, 12155, 12265,
- 12375, 12487, 12600, 12714, 12829, 12945, 13062, 13180, 13299, 13419,
- 13540, 13663, 13786, 13911, 14036, 14163, 14291, 14420, 14550, 14682,
- 14815, 14948, 15084, 15220, 15357, 15496, 15636, 15777, 15920, 16064,
- 16209, 16355, 16503, 16652, 16803, 16955, 17108, 17262, 17418, 17576,
- 17734, 17895, 18056, 18220, 18384, 18550, 18718, 18887, 19058, 19230,
- 19404, 19579, 19756, 19934, 20114, 20296, 20480 };
- static Shortword tens_table[9]=
- {
- 26844, 16777, 20972, 26214, 1, 10, 100, 1000, 10000
- };
- static Shortword Q_table[4]=
- {
- 28, 24, 21, 18
- };
- Shortword y, interp_factor, interp_component;
- Shortword index1, index2;
- Shortword ten_multiple;
- Longword L_y;
- Shortword temp1, temp2;
- Shortword temp;
- Longword L_temp;
- /* ten_multiple = shr(x,12); // data_move();mark del */
- L_temp = _shr2((Longword)x,12);
- ten_multiple = (Shortword) (0x0000ffffL & L_temp);
- if (ten_multiple < -4)
- return((Shortword)0);
- else if (ten_multiple > 4) {
- saturation = saturation + 1;
- return((Shortword)SW_MAX);
- }
- /* index1 = shr((Shortword)(x&(Shortword)0x0ff0), 4); // logic(); data_move();mark del */
- /* index2 = add(index1,1); // data_move();mark del */
- L_temp = _shr2((Longword)(x&(Shortword)0x0ff0),4);
- index1 = (Shortword) (0x0000ffffL & L_temp); // logic(); data_move();mark del
- L_temp = _sadd2(L_temp,(Longword)1);
- index2 = (Shortword) (0x0000ffffL & L_temp);; // data_move();mark del
- /* interpolation */
- /* shift by 11 to make it a number between 0 & 1 in Q15 */
- interp_factor = shl_a((Shortword)(x&(Shortword)0x000f),11); // logic(); data_move();mark del
- temp1 = sub(table[index2],table[index1]);
- /* interp_component = mult(temp1,interp_factor); // data_move();mark del */
- L_temp = _smpy(temp1,interp_factor); // data_move();mark del
- interp_component = (Shortword) (0x0000ffffL & (L_temp >> 16));
- /* L_y in Q12 if x >= 0 and in Q_table[temp2]+12 if x < 0 */
- /* temp1 = add(table[index1], interp_component); */
- /* temp2 = add(ten_multiple,4); */
- /* L_y = L_mult(tens_table[temp2],temp1); // L_data_move();mark del */
- L_temp = _sadd2((Longword)(table[index1]),(Longword)interp_component);
- temp1 = (Shortword) (0x0000ffffL & L_temp);
- L_temp = _sadd2((Longword)ten_multiple,(Longword)4);
- temp2 = (Shortword) (0x0000ffffL & L_temp);
- L_y = _smpy(tens_table[temp2],temp1); // L_data_move();mark del
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (ten_multiple >= 0)
- {
- /* temp1 = sub(12,Q); */
- /* L_y = L_shr(L_y, temp1); */
- /* y = extract_l(L_y); */
- temp1 = sub(12,Q);
- L_y = _sshvr(L_y, temp1);
- y = (Shortword) (0x0000ffffL & L_y);
- temp = (Shortword) (0x0000ffffL & (L_y >> 16));
- if (temp) {
- y=SW_MAX; // data_move();mark del
- saturation = saturation + 1;
- }
- }
- else
- {
- /* temp1 = add(Q_table[temp2],12); */
- L_temp = _sadd2((Longword)(Q_table[temp2]),(Longword)12);
- temp1 = (Shortword) (0x0000ffffL & L_temp);
- temp1 = sub(temp1,Q);
- /* y = extract_l(L_shr(L_y, temp1)); */
- L_temp = _sshvr(L_y, temp1);
- y = (Shortword) (0x0000ffffL & L_temp);
- }
- return(y);
- } /* pow10_fxp */
- /***************************************************************************
- *
- * FUNCTION NAME: sqrt_fxp
- *
- * PURPOSE:
- *
- * Compute the square root of x.
- *
- *
- * INPUTS:
- *
- * x
- * 16 bit short signed integer (Shortword).
- * Q
- * 16 bit short signed integer (Shortword) represents
- * Q value of x.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * temp
- * 16 bit short signed integer (Shortword) in same Q.
- *
- *************************************************************************/
- Shortword sqrt_fxp(Shortword x, Shortword Q)
- {
- Shortword temp;
- Longword L_temp;
- /* increment complexity for if statement */
- // compare_zero();mark del @@@@@
- if (!x)
- return((Shortword)0);
- /* temp = shr(log10_fxp(x,Q),1); */
- temp = log10_fxp(x,Q);
- L_temp = _shr2((Longword)temp,1);
- temp = (Shortword) (0x0000ffffL & L_temp);
- temp = pow10_fxp(temp,Q);
- return (temp);
- }
- /***************************************************************************
- *
- * FUNCTION NAME: L_sqrt_fxp
- *
- * PURPOSE:
- *
- * Compute the square root of x.
- *
- *
- * INPUTS:
- *
- * x
- * 32 bit long signed integer (Longword).
- * Q
- * 16 bit short signed integer (Shortword) represents
- * Q value of x.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * temp
- * 16 bit short signed integer (Shortword) in same Q.
- *
- *************************************************************************/
- Shortword L_sqrt_fxp(Longword x, Shortword Q)
- {
- Shortword temp;
- /* increment complexity for if statement */
- // compare_zero();@@@@@
- if (!x)
- return((Shortword)0);
- temp = L_log10_fxp(x,Q);
- /* temp in Q11, pow10 treat it as Q12, => no need to shr by 1. */
- temp = pow10_fxp(temp,Q);
- return (temp);
- }
- /***************************************************************************
- *
- * FUNCTION NAME: L_pow_fxp
- *
- * PURPOSE:
- *
- * Compute the value of x raised to the power 'power'.
- *
- *
- * INPUTS:
- *
- * x
- * 32 bit long signed integer (Longword).
- * power
- * 16 bit short signed integer (Shortword) in Q15.
- * Q_in
- * 16 bit short signed integer (Shortword) represents
- * Q value of x.
- * Q_out
- * 16 bit short signed integer (Shortword) represents
- * required Q value of returned result.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * temp
- * 16 bit short signed integer (Shortword).
- *
- *************************************************************************/
- Shortword L_pow_fxp(Longword x,Shortword power,Shortword Q_in,Shortword Q_out)
- {
- Shortword temp;
- Longword L_temp;
- /* increment complexity for if statement */
- // compare_zero();@@@@@
- if (!x)
- return((Shortword)0);
- temp = L_log10_fxp(x,Q_in); /* temp in Q11 */
- /* temp = mult(power,shl(temp,1)); // data_move(); mark del temp in Q12 */
- L_temp = _smpy(power,shl_a(temp,1));
- temp = (Shortword) (0x0000ffffL & (L_temp >> 16));
- temp = pow10_fxp(temp,Q_out);
- return (temp);
- }
- /***************************************************************************
- *
- * FUNCTION NAME: sin_fxp
- *
- * PURPOSE:
- *
- * Compute the sine of x whose value is expressed in radians/PI.
- *
- *
- * INPUTS:
- *
- * x
- * 16 bit short signed integer (Shortword) in Q15.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * ty
- * 16 bit short signed integer (Shortword) in Q15.
- *
- *************************************************************************/
- #define PI_Q13 24576 /* M_PI*(1<<13) */
- Shortword sin_fxp(Shortword x)
- {
- static Shortword table[129] =
- {
- 0, 402, 804, 1206, 1608, 2009, 2411, 2811, 3212,
- 3612, 4011, 4410, 4808, 5205, 5602, 5998, 6393, 6787,
- 7180, 7571, 7962, 8351, 8740, 9127, 9512, 9896, 10279,
- 10660, 11039, 11417, 11793, 12167, 12540, 12910, 13279, 13646,
- 14010, 14373, 14733, 15091, 15447, 15800, 16151, 16500, 16846,
- 17190, 17531, 17869, 18205, 18538, 18868, 19195, 19520, 19841,
- 20160, 20475, 20788, 21097, 21403, 21706, 22006, 22302, 22595,
- 22884, 23170, 23453, 23732, 24008, 24279, 24548, 24812, 25073,
- 25330, 25583, 25833, 26078, 26320, 26557, 26791, 27020, 27246,
- 27467, 27684, 27897, 28106, 28311, 28511, 28707, 28899, 29086,
- 29269, 29448, 29622, 29792, 29957, 30118, 30274, 30425, 30572,
- 30715, 30853, 30986, 31114, 31238, 31357, 31471, 31581, 31686,
- 31786, 31881, 31972, 32058, 32138, 32214, 32286, 32352, 32413,
- 32470, 32522, 32568, 32610, 32647, 32679, 32706, 32729, 32746,
- 32758, 32766, 32767
- };
- Shortword tx, ty;
- Shortword sign;
- Shortword index1,index2;
- Shortword m;
- Shortword temp;
- Longword L_temp,L_ty;
- sign = 0;
- if (x < 0) {
- tx = negate(x); // data_move();mark del
- sign = -1; // data_move();mark del
- }
- else {
- tx = x; // data_move();mark del
- }
- /* if angle > pi/2, sin(angle) = sin(pi-angle) */
- if (tx > X05_Q15)
- {
- tx = sub(ONE_Q15,tx); // data_move();mark del
- }
- /* convert input to be within range 0-128 */
- /* index1 = shr(tx,7); // data_move();mark del */
- /* index2 = add(index1,1); // data_move();mark del */
- L_temp = _shr2((Longword)tx,7);
- index1 = (Shortword) (0x0000ffffL & L_temp);
- L_temp = _sadd2(L_temp,(Longword)1);
- index2 = (Shortword) (0x0000ffffL & L_temp);
- /* increment complexity for if statement */
- // compare_nonzero();mark del
- if (index1 == 128) {
- if (sign != 0)
- return(negate(table[index1]));
- else
- return(table[index1]);
- }
- m = sub(tx,shl_a(index1,7));
- /* convert decimal part to Q15 */
- m = shl_a(m,8); // data_move();mark del
- /* interpolate */
- temp = sub(table[index2],table[index1]);
- /* temp = mult(m,temp); */
- /* ty = add(table[index1],temp); // data_move();mark del */
- L_temp = _smpy(m,temp);
- temp = (Shortword) (0x0000ffffL & (L_temp >> 16));
- L_ty = _sadd2((Longword)(table[index1]),(Longword)temp);
- ty = (Shortword) (0x0000ffffL & L_ty);
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (sign != 0)
- return(negate(ty));
- else
- return(ty);
- } /* sin_fxp */
- /***************************************************************************
- *
- * FUNCTION NAME: cos_fxp
- *
- * PURPOSE:
- *
- * Compute the cosine of x whose value is expressed in radians/PI.
- *
- *
- * INPUTS:
- *
- * x
- * 16 bit short signed integer (Shortword) in Q15.
- *
- * OUTPUTS:
- *
- * none
- *
- * RETURN VALUE:
- *
- * ty
- * 16 bit short signed integer (Shortword) in Q15.
- *
- *************************************************************************/
- Shortword cos_fxp(Shortword x)
- {
- static Shortword table[129] =
- {
- 32767, 32766, 32758, 32746, 32729, 32706, 32679, 32647, 32610,
- 32568, 32522, 32470, 32413, 32352, 32286, 32214, 32138, 32058,
- 31972, 31881, 31786, 31686, 31581, 31471, 31357, 31238, 31114,
- 30986, 30853, 30715, 30572, 30425, 30274, 30118, 29957, 29792,
- 29622, 29448, 29269, 29086, 28899, 28707, 28511, 28311, 28106,
- 27897, 27684, 27467, 27246, 27020, 26791, 26557, 26320, 26078,
- 25833, 25583, 25330, 25073, 24812, 24548, 24279, 24008, 23732,
- 23453, 23170, 22884, 22595, 22302, 22006, 21706, 21403, 21097,
- 20788, 20475, 20160, 19841, 19520, 19195, 18868, 18538, 18205,
- 17869, 17531, 17190, 16846, 16500, 16151, 15800, 15447, 15091,
- 14733, 14373, 14010, 13646, 13279, 12910, 12540, 12167, 11793,
- 11417, 11039, 10660, 10279, 9896, 9512, 9127, 8740, 8351,
- 7962, 7571, 7180, 6787, 6393, 5998, 5602, 5205, 4808,
- 4410, 4011, 3612, 3212, 2811, 2411, 2009, 1608, 1206,
- 804, 402, 0
- };
- Shortword tx, ty;
- Shortword sign;
- Shortword index1,index2;
- Shortword m;
- Shortword temp;
- Longword L_temp,L_ty;
- sign = 0;
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (x < 0) {
- tx = negate(x); // data_move();mark del
- }
- else {
- tx = x; // data_move();mark del
- }
- /* if angle > pi/2, cos(angle) = -cos(pi-angle) */
- /* increment complexity for if statement */
- // compare_nonzero();mark del
- if (tx > X05_Q15)
- {
- tx = sub(ONE_Q15,tx); // data_move();mark del
- sign = -1; // data_move();mark del
- }
- /* convert input to be within range 0-128 */
- /* index1 = shr(tx,7); // data_move();mark del */
- /* index2 = add(index1,1); // data_move();mark del */
- L_temp = _shr2((Longword)tx,7);
- index1 = (Shortword) (0x0000ffffL & L_temp);
- L_temp = _sadd2(L_temp,(Longword)1);
- index2 = (Shortword) (0x0000ffffL & L_temp);
- /* increment complexity for if statement */
- // compare_nonzero();mark del
- if (index1 == 128)
- return((Shortword)0);
- m = sub(tx,shl_a(index1,7));
- /* convert decimal part to Q15 */
- m = shl_a(m,8); // data_move();mark del
- temp = sub(table[index2],table[index1]);
- /* temp = mult(m,temp); */
- /* ty = add(table[index1],temp); // data_move();mark del */
- L_temp = _smpy(m,temp);
- temp = (Shortword) (0x0000ffffL & (L_temp >> 16));
- L_ty = _sadd2((Longword)(table[index1]),(Longword)temp);
- ty = (Shortword) (0x0000ffffL & L_ty);
- /* increment complexity for if statement */
- // compare_zero();mark del
- if (sign != 0)
- return(negate(ty));
- else
- return(ty);
- } /* cos_fxp */