循环内部存在 if判断程序的优化
1、源程序
for (k = 0; k < NB_PULSE; k++)
{
i = codvec[k];
j = sign;
index = mult(i, Q15_1_5);
track = sub(i, extract_l(L_shr(L_mult(index, 5), 1)));
if (j > 0)
{
if (i < l_subfr) code = add(code, 4096);
codvec[k] += (2 * L_SUBFR);
}
else
{
if (i < l_subfr) code = sub(code, 4096);
index = add(index, 16);
}
if (indx[track] < 0)
{
indx[track] = index;
}
else
{
if (((index ^ indx[track]) & 16) == 0)
{
if (sub(indx[track], index) <= 0)
{
indx[track] = shl((indx[track] & 16), 3)
+ shr(extract_l(L_mult((indx[track]&15), NB_POS)), 1) + (index & 15);
}
else
{ indx[track] = shl((index & 16), 3)
+ shr(extract_l(L_mult((index & 15),NB_POS)), 1) + (indx[track] & 15);
}
}
else
{
if (sub((indx[track] & 15), (index & 15)) <= 0)
{
indx[track] = shl((index & 16), 3)
+ shr(extract_l(L_mult((index & 15),NB_POS)), 1) + (indx[track] & 15)
}
else
{ indx[track] = shl((indx[track] & 16), 3)
+ shr(extract_l(L_mult((indx[track] & 15),NB_POS)), 1) + (index & 1
}
}
}
}
2、优化后的程序
for (k = 0; k < 8; k++)
{
i = codvec[k];
j = sign;
index = _smpy(i, 6554)>>16;
track = i - index*5;
con = (j > 0);
codvec[k] = codvec[k] + 110*con;
index = index + (!con)*16;
conn = (i < l_subfr);
cono = (j > 0)? 1:-1;
code = code + 4096*conn*cono;
n0 = index;
t0 = indx[track];
n1 = n0&16;
t1 = t0&16;
n2 = n0&15;
t2 = t0&15;
tmp0 = (_sshl(n1,19)>>16) + n2*NB_POS + t2;
tmp1 = (_sshl(t1,19)>>16) + t2*NB_POS + n2;
conp = (((n1 == t1)&&(t0 > n0))||((n1 != t1)&&(t2 <= n2)));
tmp = conp*tmp0 + (!conp)*tmp1;
if (t0 < 0)
indx[track] = n0;
else
indx[track] = tmp;
}
3、优化说明
源程序中在循环中含有许多的 if 结构,在优化时对 if 结构首先进行化简,再将化简后的 if结构用条件运算表达式进行改写,最后使循环可以 Pipeline。 |