09.Ubuntu下开发STM32--浮点运算

16 篇文章 1 订阅

cortex-m4内核具有浮点运算单元。如果不使用的话,还是有点浪费的。

软浮点

gcc在默认的情况下使用软浮点运算。相关浮点运算测试的代码在softfloat.zip中。为了反汇编观察代码,这里特意去掉了Os优化。使用Newlib-nano库。

使用make sram,sudo make burns下载到SRAM中运行。从输出的时间间隔上来看,软件浮点也是挺快的。这里我们反编译一下main.o的代码看一下。

float add(float a,float b,float c )
{
    return a+b+c;
}

==> $ arm-none-eabi-objdump -d main.o

img

根据ARM C/C++过程调用标准,函数的前4个参数存放在R0-R3。如果超过4个参数,剩余的参数存放在栈中。并且最后一个参数先入栈。正是因为最后一个参数先入栈,我们才能使用可变参数这个特性。

从反汇编的代码上看,在栈中新开辟了3个字(每个字占4个字节)的槽依次存放参数a,b,c。由于__aeabi_fadd仅有两个参数,对三个数字相加需要分为两次计算。第一次将a,b相加,第二次将c与前一次的结果相加。

硬浮点

可以在编译和链接的时候添加参数-mfloat-abi=hard -mfpu=fpv4-sp-d16。(注意,在链接的时候也需要添加这个参数。否则会报错。大概意思是你编写的C使用了硬件浮点。而链接的库使用的是软件浮点。两者不能合并。)相应的代码在hardfloat.zip中。

同样,还是对main.o进行反汇编:

img

从反汇编的代码上来看,这次使用的是特殊的浮点运算的指令。

可以将softfloat和hardfloat的可执行文件下载至单片机中。可以发现使用硬件浮点运算的效果还是非常明显的。

W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed. E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' E: Sub-process returned an error code
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值