八、 循环内部存在 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。
九、 少量中值问题的优化
1、源程序
- for (i = 0; i < n; i++)
- {
- max = -32767;
- for (j = 0; j < n; j++)
- {
- if (sub (tmp2[j], max) >= 0)
- {
- max = tmp2[j];
- ix = j;
- }
- }
- tmp2[ix] = -32768;
- tmp = ix;
- }
复制代码
2、优化后的程序
- if (n0>n1) {temp=n0;n0=n1;n1=temp;}
- if (n1>n2) {temp=n1;n1=n2;n2=temp;}
- if (n2>n3) {temp=n2;n2=n3;n3=temp;}
- if (n3>n4) {temp=n3;n3=n4;n4=temp;}
- if (n0>n1) {temp=n0;n0=n1;n1=temp;}
- if (n1>n2) {temp=n1;n1=n2;n2=temp;}
- if (n2>n3) {temp=n2;n2=n3;n3=temp;}
- if (n0>n1) {temp=n0;n0=n1;n1=temp;}
- if (n1>n2) {return n1;}
复制代码
3、优化说明
源程序也为一个求中值的问题,由于已知循环次数固定为 5,因此将循环展开使用 if语句直接求取中值。
十、 位操作优化
1、源程序
- static Word16 Bin2int (Word16 no_of_bits, Word16 *bitstream)
- {
- Word16 value, i, bit;
- value = 0;
- for (i = 0; i < no_of_bits; i++)
- {
- value = shl (value, 1);
- bit = *bitstream++;
- if (sub (bit, BIT_1) == 0)
- value = add (value, 1);
- }
- return (value);
- }
- for (i = 0; i < prmno[mode]; i++)
- {
- prm = Bin2int (bitno[mode], bits);
- bits += bitno[mode];
- }
复制代码
2、优化后的程序
- value = 0;
- bitsp = bits;
- bitnop= &bitno[mode][0];
- j = *bitnop++;
- j1 = *bitnop++;
- j2 = *bitnop++;
- j3 = *bitnop++;
- j4 = *bitnop++;
- _nassert(loop[mode]>=35);
- for (i = 0; i < loop[mode]; i++)
- {
- value = value*2 + *bitsp++;
- j--;
- if (j == 0)
- {
- *prm++ = value;
- value = 0;
- j = j1;
- j1 = j2;
- j2 = j3;
- j3 = j4;
- j4 = *bitnop++;
- }
- }
复制代码
3、优化说明
源程序按照数据位流定义取出参数,为双重循环结构,优化中采用重新根据位流的 bit长度定义循环次数,化简为单重循环,然后优化循环,去除 boundary,使 pipeline 的数目最小。
一周热门 更多>