c++ - 是否有一个 GNU C 编译器选项可以生成运行速度比默认设置更快但精度更低的浮点程序?

标签 c++ c performance floating-point precision

我一直在寻找 GNU C 的选项,这样我的浮点程序可以生成比默认 gnu 设置生成的代码运行速度更快但精度更低的代码。

假设我有一个程序 my_fp_prog.c

double sqrt_two(return sqrt(2));

虽然准确答案应该是 1.414....,但我只需要答案 1.4,甚至 1.5 或 1.3 对我来说都可以,但我需要一个更快的程序。当然,我们案例使用更便宜的 sqrt 算法,但我正在寻找的是一个可以完成这项工作的编译器选项。希望存在这样的选择。

最佳答案

来自 GCC 的手册页:

-ffast-math

Sets -fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and -fcx-limited-range.

This option causes the preprocessor macro __FAST_MATH__ to be defined.

This option is not turned on by any -O option besides -Ofast since it can result in incorrect output for programs that depend on an exact implementation of IEEE or ISO rules/specifications for math functions. It may, however, yield faster code for programs that do not require the guarantees of these specifications.

关于c++ - 是否有一个 GNU C 编译器选项可以生成运行速度比默认设置更快但精度更低的浮点程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27336321/

相关文章:

用于通信的 C++ 对象到 XML

c - 修复线程以在某些内核上执行

performance - 延迟和响应时间有什么区别?

c++ - 在 C/C++ 中的特定地址边界上对齐内存是否仍能提高 x86 性能?

C++ 虚拟类 : interesting point

c++ - 如何使用 libusb 和 libusb_get_device_descriptor()?

c - 如何修复 char* 和 char** 的格式错误

c - WinDivert 忽略 Tor 流量

java - 限制 try block 范围。有关系吗?

c++ - 为什么快速排序在重复元素多的情况下效率低下?