在C ++中使用Multiply Accumulate指令内联汇编

时间:2010-08-23 17:59:08

标签: c++ assembly arm filtering

我正在ARM9处理器上实现FIR滤波器,并尝试使用SMLAL指令。

最初我实现了以下过滤器并且它完美地工作,除了这种方法在我们的应用程序中使用了太多的处理能力。

uint32_t DDPDataAcq::filterSample_8k(uint32_t sample)
 {
    // This routine is based on the fir_double_z routine outline by Grant R Griffin
    // - www.dspguru.com/sw/opendsp/alglib.htm 
    int i = 0; 
    int64_t accum = 0; 
    const int32_t *p_h = hCoeff_8K; 
    const int32_t *p_z = zOut_8K + filterState_8K;


    /* Cast the sample to a signed 32 bit int 
     * We need to preserve the signdness of the number, so if the 24 bit
     * sample is negative we need to move the sign bit up to the MSB and pad the number
     * with 1's to preserve 2's compliment. 
     */
    int32_t s = sample; 
    if (s & 0x800000)
        s |= ~0xffffff;

    // store input sample at the beginning of the delay line as well as ntaps more
    zOut_8K[filterState_8K] = zOut_8K[filterState_8K+NTAPS_8K] = s;

    for (i =0; i<NTAPS_8K; ++i)
    {
        accum += (int64_t)(*p_h++) * (int64_t)(*p_z++);
    }

    //convert the 64 bit accumulator back down to 32 bits
    int32_t a = (int32_t)(accum >> 9);


    // decrement state, wrapping if below zero
    if ( --filterState_8K < 0 )
        filterState_8K += NTAPS_8K;

    return a; 
} 

我一直在尝试用内联汇编替换乘法累加,因为即使打开了优化,GCC也没有使用MAC指令。我用以下内容替换了for循环:

uint32_t accum_low = 0; 
int32_t accum_high = 0; 

for (i =0; i<NTAPS_4K; ++i)
{
    __asm__ __volatile__("smlal %0,%1,%2,%3;"
        :"+r"(accum_low),"+r"(accum_high)
        :"r"(*p_h++),"r"(*p_z++)); 
} 

accum = (int64_t)accum_high << 32 | (accum_low); 

我现在使用SMLAL指令获得的输出不是我期望的过滤数据。我得到的随机值似乎与原始信号或我期望的数据没有模式或连接。

我感觉我在将64位累加器分成指令的高位和低位寄存器时做错了,或者我把它们重新组合错了。无论哪种方式,我都不确定为什么我无法通过内联汇编交换C代码来获得正确的输出。

1 个答案:

答案 0 :(得分:3)

您使用的是哪个编译器版本?我尝试使用选项-O3 -march = armv5te使用GCC 4.4.3编译你的C代码,并生成了smlal指令。

相关问题