是否有编译选项可以停止在整数代码中使用浮点指令的 gcc?

Is there a compilation option to stop gcc using floating point instructions in integer code?

当我遇到有关浮点指令的意外异常时,我的纯整数、裸机 C 项目刚刚停止。

查看 gcc 生成的代码,罪魁祸首是 fmov d0, x0,用于将值临时存储在浮点寄存器中,而不是堆栈中。

我不想让它那样做!

我可以用 noinline 属性标记一两个函数,但这不能保证问题不会在其他地方再次发生。

您需要通知编译器您的目标没有浮点硬件。确切的标志取决于您的目标(对于 ARM,它是 -msoft-float)。

这个选项可以解决问题:

-mgeneral-regs-only

https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/AArch64-Options.html

Generate code which uses only the general-purpose registers. This will prevent the compiler from using floating-point and Advanced SIMD registers but will not impose any restrictions on the assembler.