[AVR单片机] 请教GCC库中两定点乘除函数编译出错?

[复制链接]
2453|4
 楼主| mcusir 发表于 2008-9-17 18:46 | 显示全部楼层 |阅读模式
如下两函数均是从网上的GCC定点函数库中抽取出来的,此两函数返回类型和实参变量均是定点数格式。我使用的定点数为无符号32位(整数部分和小数部分各占16位),即16位.16位格式。但我在使用中均发现出错,且又没想到怎么去修改成功,望达人看过后指正一二,谢谢!

#define FixedPtBits 16

unsigned long fixedptMultiply(unsigned long a, unsigned long b)
{
    // multiply a and b (a*b) with fixed-point math
    return (a*b)>>FixedPtBits;
}

unsigned long fixedptDivide(unsigned long numer, unsigned long denom)
{
    // divide numer by denom (numer/denom) with fixed-point math
    return (numer<<FixedPtBits)/denom;
}
mxh0506 发表于 2008-9-17 20:35 | 显示全部楼层

贴个出错的实例看看

 楼主| mcusir 发表于 2008-9-17 21:18 | 显示全部楼层

一实例如下:

y=fixedptMultiply(0x20000,0x40000);

编译出来的结果为 y=0x00000000;可我要得到结果毫无疑问应该是0x80000.

 楼主| mcusir 发表于 2008-9-17 23:30 | 显示全部楼层

问题已解决!

我将函数内容细分解如下才算编译通过计算无误!

#define FixedPtBits 16
#define s32 unsigned long

s32 fixedptMultiply(s32 a, s32 b)
{
    // multiply a and b (a*b) with fixed-point math
    unsigned long long temp;
    temp=a;
    temp*=b;
    temp>>=FixedPtBits;
    return temp;
}

s32 fixedptDivide(s32 numer, s32 denom)
{
    // divide numer by denom (numer/denom) with fixed-point math
    unsigned long long temp;
    temp=numer;
    temp <<=FixedPtBits;
    return (temp/denom);
}
mxh0506 发表于 2008-9-18 22:10 | 显示全部楼层

与优化级别有关吗?

您需要登录后才可以回帖 登录 | 注册

本版积分规则

58

主题

177

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部