Linux 硬浮点数和软浮点数有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3321468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between hard and soft floating point numbers?
提问by Evan Kroske
When I compile C code with my cross toolchain, the linker prints pages of warnings saying that my executable uses hard floats but my libc uses soft floats. What's the difference?
当我使用交叉工具链编译 C 代码时,链接器会打印警告页面,指出我的可执行文件使用硬浮点数,但我的 libc 使用软浮点数。有什么不同?
采纳答案by nmichaels
Hard floats use an on-chip floating point unit. Soft floats emulate one in software. The difference is speed. It's strange to see both used on the same target architecture, since the chip either has an FPU or doesn't. You can enable soft floating point in GCC with -msoft-float. You may want to recompile your libc to use hardware floating point if you use it.
硬浮点数使用片上浮点单元。软浮动在软件中模拟一个。区别在于速度。看到两者都用在同一个目标架构上很奇怪,因为芯片要么有 FPU,要么没有。您可以使用 -msoft-float 在 GCC 中启用软浮点。如果您使用它,您可能希望重新编译您的 libc 以使用硬件浮点。
回答by Digikata
It sounds like your libc was built for software floating point operations while your exe was compiled assuming hardware support for floating point. In the short term, you could force soft floats as a compiler flag. (if you're using gcc I think it's -msoft-float)
听起来您的 libc 是为软件浮点运算而构建的,而您的 exe 是假设硬件支持浮点而编译的。在短期内,您可以强制软浮动作为编译器标志。(如果您使用的是 gcc,我认为它是 -msoft-float)
Longer term, if your target's processor has hardware support for floating point operations you'll generally want to build or find a cross toolchain with hardware float enabled for speed. Some processor families have model variants some with and some without hardware support. So, for example, just saying your processor is an ARM is insufficient to know if you have hardware floating point support.
从长远来看,如果您的目标处理器具有对浮点运算的硬件支持,您通常希望构建或找到一个启用硬件浮点以提高速度的交叉工具链。某些处理器系列具有型号变体,有些具有硬件支持,有些没有硬件支持。因此,例如,仅说您的处理器是 ARM 不足以知道您是否具有硬件浮点支持。
回答by ninjalj
There are three ways to do floating point arithmetic:
浮点运算的三种方式:
- Use float instructions if your CPU has a FPU. (fast)
- Have your compiler translate floating point arithmetic to integer arithmetic. (slow)
- Use float instructions and a CPU with no FPU. Your CPU will generate a exception (Reserved Instruction, Unimplemented Instruction or similar), and if your OS kernel includes a floating point emulator it will emulate those instructions (slowest).
- 如果您的 CPU 有 FPU,请使用浮点指令。(快速地)
- 让您的编译器将浮点运算转换为整数运算。(减缓)
- 使用浮点指令和没有 FPU 的 CPU。您的 CPU 将生成异常(保留指令、未实现指令或类似指令),如果您的操作系统内核包含浮点仿真器,它将模拟这些指令(最慢)。
回答by starblue
The computation may be done either by floating-point hardware or in software based on integer arithmetic.
计算可以通过浮点硬件或基于整数算法的软件来完成。
Doing it in hardware is much faster, but many microcontrollers don't have floating-point hardware. In that case you may either avoid using floating point (usually the best option) or rely on an implementation in software, which will be part of the C library.
在硬件中执行要快得多,但许多微控制器没有浮点硬件。在这种情况下,您可以避免使用浮点(通常是最好的选择)或依赖软件中的实现,这将是 C 库的一部分。
In some families of controllers, for example ARM, the floating-point hardware is present in some models of the family but not in others, so gcc for these families supports both. Your problem seems to be that you mixed up the two options.
在某些控制器系列中,例如 ARM,浮点硬件存在于该系列的某些模型中,而在其他模型中不存在,因此这些系列的 gcc 支持两者。您的问题似乎是您混淆了两个选项。
回答by artless noise
Strictly speaking, all of these answers seem wrong to me.
严格来说,所有这些答案在我看来都是错误的。
When I compile C code with my cross toolchain, the linker prints pages of warnings saying that my executable uses hard floats but my libc uses soft floats. What's the difference?
当我使用交叉工具链编译 C 代码时,链接器会打印警告页面,指出我的可执行文件使用硬浮点数,但我的 libc 使用软浮点数。有什么不同?
The Debian VFP wikihas information on the three choices for -mfloat-abi
,
Debian VFP wiki提供了关于以下三个选项的信息-mfloat-abi
,
soft
- this is pure softwaresoftfp
- this supports a hardware FPU, but the ABIis soft compatible.hard
- the ABI uses floator VFPregisters.
soft
- 这是纯软件softfp
- 这支持硬件 FPU,但ABI是软兼容的。hard
- ABI 使用浮点或VFP寄存器。
The linker (loader) error is because you have a shared library that will pass floating point values in integer registers. You can still compile your code with a -mfpu=vfp
, etc but you should use -mfloat-abi=softfp
so that if the libcneeds a float it is passed in a way the library understands.
链接器(加载器)错误是因为您有一个共享库,它将在整数寄存器中传递浮点值。您仍然可以使用 a-mfpu=vfp
等编译代码,但您应该使用它,-mfloat-abi=softfp
以便在libc需要浮点数时以库理解的方式传递它。
The Linux kernel can support emulation of the VFP instructions. Obviously, you are better off to compile with -mfpu=none
for this case and have the compile generate code directly instead of relying on any Linux kernel emulation. However, I don't believe the OP's error is actually related to this issue. It is separate and must also be dealt with along with the -mfloat-abi
.
Linux 内核可以支持 VFP 指令的仿真。显然,您最好-mfpu=none
针对这种情况进行编译,并让编译直接生成代码,而不是依赖于任何 Linux 内核仿真。但是,我认为 OP 的错误实际上与此问题无关。它是独立的,也必须与-mfloat-abi
.
Armv5 shared library with ArmV7 CPUis an opposite of this one; the libcwas hard float but the application was only soft. It has some ways to work around the issue, but recompiling with correct options is always the easiest.
Armv5 与 ArmV7 CPU 的共享库与此相反;在libc中是很难浮动,但应用程序只是软。它有一些方法可以解决这个问题,但使用正确的选项重新编译总是最简单的。
Another issue is that the Linux kernel must support VFP tasks (or whatever ARM floating point is present) to save/restore the registers on a context switch.
另一个问题是 Linux 内核必须支持 VFP 任务(或任何存在的 ARM 浮点数)才能在上下文切换时保存/恢复寄存器。