定点数

定点数

一、介绍

  1. 定点数与浮点数:
    定点数是指小数点在数中的位置是固定保持不变的二进制数。
    浮点数分为几个部分:463269-20190503163357046-840760519.png,其中N表示一个浮点数,Ms表示正负,E表示阶码,R是基数,一般是2,M是尾数。

    float point
    float point

  2. 为什么要用定点数呢

在硬件数字电路中,许多处理器都是整数处理器,需要用整数运算模拟浮点数运算,所以整数运算要比浮点数更快,所以使用定点数可以提升运算效率。

二、使用

在一些实际使用中,我们开发的一些功能都是使用定点数运算的。所以通过一些寄存器配置的一些参数,都是对应的定点数。所以在实际使用中,要清楚地知道定点数的定点位置,这样才能得到相应的定点数喂给寄存器。

定点数计算方法,实际上大家自己想想也能得到如下的结果:

/* The basic operations performed on two numbers a and b of fixed point q
format returning the answer in q format */

#define FADD(a, b)     ((a) + (b))
#define FSUB(a, b)     ((a) - (b))
//#define FMUL(a, b, q)  (((a)*(b))>>(q))
#define FMUL(a, b, q)  ((long)((a)*(b))>>(q))
#define FDIV(a, b, q)  (((a)<<(q))/(b))


/* The basic operation where a is of fixed point q format and b is
an integer */

#define FADDI(a, b, q)  ((a) + ((b)<<(q)))
#define FSUBI(a, b, q) ((a) - ((b)<<(q)))
#define FMULI(a, b)    ((a)*(b))
#define FDIVI(a, b)    ((a)/(b))


/* convert a from q1 format to q2 format */

#define FCONV(a, q1, q2)  (((q2) > (q1)) ? (a)<<((q2)-(q1)) : (a)>>((q1) - (q2)))

/* the general operation between a in q1 format and b in q2 format
returning the result in q3 format */

#define FADDG(a, b, q1, q2, q3)  (FCONV(a, q1, q3) + FCONV(b, q2, q3))
#define FSUBG(a, b, q1, q2, q3)  (FCONV(a, q1, q3) - FCONV(b, q2, q3))
#define FMULG(a, b, q1, q2, q3)   FCONV((a)*(b), (q1)+(q2), q3)
#define FDIVG(a, b, q1, q2, q3)  (FCONV(a, q1, (q2) + (q3))/(b))


/* convert to and from floating point */
#define TOFIX(d, q)  ((int) ( (d)*(double) (1<<(q)) ))
#define TOFLT(a, q)  (  (double)(a) / (double)(1<<(q)) )

Reference

https://blog.csdn.net/whoisleft/article/details/77417541
https://blog.csdn.net/u013580397/article/details/80878213

转载于:https://www.cnblogs.com/gr-nick/p/10805534.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值