linux内核支持浮点吗,【转】Linux内核浮点计算支持

Linux内核默认使用-msoft-float编译,依赖于glibc的浮点运算模拟。文章解释了浮点运算在硬件和软件层面的处理,包括x87 FPU的历史和浮点运算的硬件支持。内核通过math_state_restore()函数实现浮点上下文的条件保存和懒惰恢复,以避免不必要的开销。kernel_fpu_begin()和kernel_fpu_end()确保内核安全使用浮点运算。
摘要由CSDN通过智能技术生成

00000000 __divdf3

references this function                    // 这里有调用__divdf3地址

So what is happening is that, gcc replaced the expression (arg1/arg2) by a call to __divdf3 function which is supposed to carry out the floating point division using integer arithmetic. Why did gcc do that ? and not generate actual assembly instructions to do floating point divide.

这里出现了什么情况呢,gcc使用一个对__divdf3的函数调用代替了(arg1/arg2)这个原本可能引发浮点数除法的函数调用,gcc为什么要这么做,而不是直接产生浮点数除法的汇编代码?

This is because, the module code was compiled with -msoft-float gcc option, which instructs gcc to not generate floating point assembly instructions and instead generate calls to the glibc s/w floating point emulation functions. -msoft-float is useful when compiling programs for platforms that do not have hardware floating point support. Nothing is wrong with this. Infact if you compile an equivalent user program with -msoft-float, it should work (pls read note below)

这是因为,这个模块在编译的时候加入了 -msoft-float 的gcc选项,这个选项使得gcc不直接产生浮点运算的汇编代码而是通过产生glibc中的s/w模拟浮点运算的函数调用来实现浮点数运算. -msoft-float在当前的运行平台没有浮点数运算硬件支持的时候,非常的有用.

P.S. Actually it depends on whether your glibc is compiled with software floating point emulation support. Usually x86 based default glibc distributions come w/o soft floating point emulation, as almost all x86 platforms have h/w floating point support. If h/w floating point support is present, it is preferred because of its speed and the fact that it puts less load on the CPU (for applications with extensive floating point usage, f.e. sone gaming applications or CAD design applications etc).

P.S. 事实上,是否使用硬件计算浮点数运算取决于你的glibc是否使用软件浮点数模拟编译出来的.通常x86基础的默认glibc版本带有w/o软件浮点计算模拟器,同时几乎所有的x86平台都有h/w浮点数支持.如果h/w浮点数支持存在,那么优先选择硬件的浮点数运算器件,因为时间开销更小,同时会减少CPU的负荷.

You can use the following command to see if your glibc distribution has software floating point emulation support

# ldd /bin/ls | grep libc | awk '{print $3}' | xargs readlink -f | xargs nm -D | grep __divdf3

下面命令可以察看你自己的glibc版本是否有软件的浮点数模拟器.

I believe the reason why default glibc does not come with soft float support enabled, is to prevent applications from accidentally using soft float. Otherwise if some application is unintentionally compiled with -msoft-float, the user will never know and the application will be using the inefficient soft float, even though h/w float support is available :-( So, till now we know the following things :

1. Linux kernel (and all its modules) are compiled with -msoft-float gcc option (to know why, read on)

2. Linux kernel (and all its modules) are _not_ linked with glibc and hence we do not have access to soft floating point emulation functions (like __divdf3).

3. Linux kernel itself does not provide its own implementation of __divdf3 (and other soft floating point functions).

我相信默认的glibc没有软件浮点数运算器主要是防止应用程序偶然的使用软浮点数运算.或者可能是如果某些软件没有显示的使用 -msoft-float选项编译,那么用户就不会知道了解到当前的这个应用程序使用了低效率的浮点数运算方式.现在我们知道,

1 linux内核以及它所有模块在编译阶段都有-msoft-float选项

2 linux内核以及他所有模块没有和gblic链接,所以我们不能直接访问软件浮点数模拟器

3 Linux内核没有提供自己的浮点数运算实现方法)

The above explains why we get the error while compiling and loading the module, but the inquisitive of us will still be having few questions. Lets try to find answers to those questions. What is floating point and how is it handled ?Before we get into the main topic of the discussion, i.e. the state of floating point support in Linux, and the reasons behind that, lets take a quick look at what it takes to support floating point operations.

以上的解释是针对在编译和载入模块的时候产生的错误,但是我们的好奇使得我们还有几个未解决的问题.什么是浮点数以及如何handled?

Floating point usage is not very common. So much so that x86 designers did not make the floating point unit (the CPU real estate needed for floating point operations) part of the original CPU. In-fact floating point instructions were supported by a special coprocessor. For the 8086 this was called 8087. Similarly for other 80×86 processors the corresponding floating point coprocessor was called 80×87. Till the 80386 processor, this coprocessor came as a separate chip which used to sit alongside the main CPU and all the floating point calculations were directed to it, which then could use its floating point unit (FPU) to do the calculations and pass the result back to the main processor. Starting 80486, the FPU was integrated with the main CPU, but still the FPU was a logically separate unit, i.e. it used a separate register set to load/store the floating point values and it used a different ALU for carrying out the floating point calculations.

浮点数运算很不普遍.以至于x86设计者让浮点运算部件成为CPU的部件.事实上,浮点运

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值