Linux kernel supports floating point operations.

Source: Internet
Author: User

Linux kernel supports floating point operations.
Currently, most CPUs support the floating point operation unit FPU. FPU is placed out of the core of the processor as a separate coprocessor. However, for embedded processors, floating point operations are rarely used, some embedded processors will remove the floating point coprocessor.
X86 processors generally have FPU. However, the arm ppc mips processor does not have FPU.

How to handle floating point operations in linux kernel is discussed in the following sections: FPU processors and FPU-free processors.

(The following is a summary of my personal knowledge. I hope you can correct the mistakes and learn together)


1. For FPU Processors

1 For linux kernel, the-msoft-float option is used by default for kernel Compiling. The default value is soft floating point program, which means that the gcc compiler simulates floating point operations (provided by the glibc library ), replace the floating point operation code with the fixed point operation.

For FPU processors, we can remove the compilation option-msoft-float, usually in arch/xxx/Makefile. Compile the kernel into a hard floating point, that is, let the floating point instruction of the processor calculate the floating point,
Hard floating point operations must be more efficient than simulated fixed-point operations. (There is no floating point operation in the kernel code, so the efficiency is not significant)

2 For apps running on the kernel, especially for graphics programs such as QT, there are many floating point operations. We can compile them directly because the processor supports floating point operations and floating point operation commands.

2. For non-FPU Processors

1 For linux kernel, the-msoft-float option is used by default for compiling. By default, the program is compiled as a soft floating point program. linux kernel compilation does not depend on any libraries, and the corresponding simulated floating point ABI is implemented in kernel.


2 For apps running on the kernel, there are two methods to deal with floating point operations:

(1) Simulate soft floating point using kernel.

The application uses hard floating point for direct compilation (by default, the compiler is compiled into a hard floating point program ).
For kernel, all the ppc mips processors I know have special floating point operation Exception Handling. When a program runs on a floating point instruction and cannot run a floating point instruction, the hardware will generate an interrupt exception, the kernel floating point exception handler performs soft floating point simulation based on the instruction content, returns the operation result, and then restores it to the user space for execution.
For ARM, I did not find an exception entry for floating point computing in its exception description, but the kernel also has support for its soft floating point,
When configuring the ARM Linux kernel, you should see the following Configuration:
Menu "Floating point emulation"
Comment "At least one emulation must be selected"
Config FPE_NWFPE
...
This is used to simulate a floating point processor in the kernel.
The specific implementation of ARM supports exception simulation soft floating point. The specific implementation time also requires careful reading of the Code, in arch/arm/nwfpe.
The advantage of this method is that the application does not need to be re-compiled. You need to enable the floating point simulation in the kernel, which is very convenient to use.

However, the disadvantage is also obvious. Every floating point operation triggers an interrupt exception. switching between user space and kernel space leads to low execution efficiency.

(2) recompile the app with soft floating point 
This can avoid the above problems. When the app is compiling, it needs to connect to the glibc library, use -- msoft-float, use the glibc analog floating point, and replace it with the fixed point operation, the advantage is that the running performance is better.
However, the disadvantage is that different compilation options are used, and the ABI used may change. If a library or application does not use the same compilation option (the ABI is different ),
Unexpected situations or even crashes occur during system operation.


According to the recent debugging record of a PPC processor, after the kernel is started to enter the console normally, it will die at a certain address. There are many floating point operations in the user space. After asking the IC, the FPU will be removed, the floating point exception of the processor is not enabled.

In this case, the processor does not trigger an exception or know how to run the instruction.

Therefore, we need to clarify whether the processor has FPU during kernel transplantation. If the processor has removed FPU, and the core has not done the corresponding processing (enable floating point exception ), the running result of the floating-point commands of the APP is unpredictable. In this case, you can use the soft floating-point tool chain to compile the APP.


Here is a bit of thinking:

For a processor, there is a floating point exception in the design of the processor (both mips ppc), and FPU can also be connected outside of it.

After FPU is connected, the core of the processor will block floating point exceptions. Otherwise, floating point operations will still generate floating point exceptions, and FPU will have no practical significance.

If there is no FPU, it is necessary to enable floating point exceptions in the core of the processor. Otherwise, just like the problem I encountered above, the processor cannot predict how to run the floating point command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.