[学习笔记]

关于7801x的协处理器(MMDIVSQRT)介绍使用

[复制链接]
637|2
手机看帖
扫描二维码
随时随地手机跟帖
RunningX|  楼主 | 2020-7-17 13:53 | 显示全部楼层 |阅读模式
本帖最后由 RunningX 于 2020-7-20 19:28 编辑

7801x的协处理器,其实就是用硬件上的开方根器跟除法器。其公式原型如下: 函数原型.PNG
下面介绍一下这个硬件除法及开方根运算的函数使用,如下图(直接从文档截的图,文档已打包在附件中):
运算函数.PNG
当然软件调用了math.h后,也能实现上述的运算。两者的差异在于运算的速度,也就是完成运算所需要的时间,这里用GPIO翻转来测试运算的时间(代码已附在下面)。下面我们就来对比一下:
1、开方根运算
int32_t rcndx = -8;
int32_t rcndy = 32768;
MMDIVSQRT_CalSquareRoot(rcndx, rcndy,&squareRootResult); //1.29us
squareRootResult = sqrt((rcndx*rcndx)+(rcndy*rcndy)); //19.4us
第一句代码为硬件除法,耗时1.29us;但第二句的软件除法,需要耗时19.4us。相差十多倍,硬件除法优势明显。


2、除法运算
int32_t sign_dend = 9;  //被除数
int32_t sing_dsor = -4;  //除数
uint8_t dsft1 = 1;    //被除数的左移位数
uint32_t unsign_dend = 600;
uint32_t unsign_dsor = 500;
uint8_t dsft2 = 3;

int32_t sign_dend_complex = 88777;  //被除数
int32_t sing_dsor_complex = -7;  //除数
uint8_t dsft1_complex = 10;    //被除数的左移位数
uint32_t unsign_dend_complex = 8777;
uint32_t unsign_dsor_complex = 8;
uint8_t dsft2_complex = 10;
MMDIVSQRT_CalSignDivWithDsft(sign_dend, sing_dsor, dsft1, &sign_quotientResult);         //1.7us        
MMDIVSQRT_CalSignDivWithDsft_Remainder(sign_dend, sing_dsor, dsft1, &sign_remainderResult);        //1.7us        
MMDIVSQRT_CalUnsignDivWithDsft(unsign_dend, unsign_dsor, dsft2, &unsign_quotientResult);        //1.7us        
MMDIVSQRT_CalUnsignDivWithDsft_Remainder(unsign_dend, unsign_dsor, dsft2, &unsign_remainderResult);        //1.8us   
sign_quotientResult = (sign_dend<<dsft1)/sing_dsor; //2.1us
sign_remainderResult = (sign_dend<<dsft1)%sing_dsor;  //2.1us
unsign_quotientResult = (unsign_dend<<dsft2)/unsign_dsor; //1.5us
unsign_remainderResult = (unsign_dend<<dsft2)%unsign_dsor; //1.5us
MMDIVSQRT_CalSignDivWithDsft(sign_dend_complex, sing_dsor_complex, dsft1_complex, &sign_quotientResult); //1.96us
MMDIVSQRT_CalSignDivWithDsft_Remainder(sign_dend_complex, sing_dsor_complex, dsft1_complex, &sign_remainderResult);        //1.96us
MMDIVSQRT_CalUnsignDivWithDsft(unsign_dend_complex, unsign_dsor_complex, dsft2_complex, &unsign_quotientResult);        //1.85us
MMDIVSQRT_CalUnsignDivWithDsft_Remainder(unsign_dend_complex, unsign_dsor_complex, dsft2_complex, &unsign_remainderResult);        //1.82us
sign_quotientResult = (sign_dend_complex<<dsft1_complex)/sing_dsor_complex; //5.7us
sign_remainderResult = (sign_dend_complex<<dsft1_complex)%sing_dsor_complex;  //5.7us
unsign_quotientResult = (unsign_dend_complex<<dsft2_complex)/unsign_dsor_complex; //5.4us
unsign_remainderResult = (unsign_dend_complex<<dsft2_complex)%unsign_dsor_complex;  //5.4us

其中第1到第4条为硬件除法,按顺序分别为有符号除法取商,有符号除法取余,无符号除法取商,无符号除法取余;第9到12条也是硬件除法,顺序一致,只是换了一组数据,可以看到硬件除法的时间基本在2us及以下
其中第5到第8条为软件除法,按顺序分别为有符号除法取商,有符号除法取余,无符号除法取商,无符号除法取余;第13到16条也是软件除法,顺序一致,只是换了一组数据,可以看到软件除法的时间有较大的差异。软件除法的运算时间取决于运算数据的复杂度(增大被除数,减小除数,增大移位数,都会使得运算时间增长)
相比之下,硬件除法也有明显的优势,特别是在数据复杂的情况下,优先使用硬件除法

附上相关的测试数据(excel发不了,Word黏贴格式会出错,只能截图这样子):
开方根测试.PNG
除法测试1.PNG
除法测试2.PNG
除法测试3.PNG

数据总结:硬件除法运算耗时基本在2us以下,且耗时受数据影响的差异很小。硬件开方根耗时稳定在1.29us。

相应的代码如下:
mmdivsqrt_sample.rar (659.69 KB)

使用特权

评论回复

相关帖子

liangshuang95| | 2020-9-17 18:21 | 显示全部楼层
感谢楼主分享

使用特权

评论回复
caigang13| | 2020-9-18 07:27 | 显示全部楼层
做浮点运算不错。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

42

主题

223

帖子

4

粉丝