来自 GNU 的网站:
-ftrapv
This option generates traps for signed overflow on addition, subtraction, multiplication operations.
-fwrapv
This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others.
https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Code-Gen-Options.html
我有两个问题:
- 以下哪个选项对性能更好?
-ftrapv
定义说它生成“陷阱”是什么意思?这是否意味着异常(exception)? (我猜不是,但值得一问。)
最佳答案
这两个选项的重点是给优化器更少的回旋余地。因此,除非您在优化器中遇到错误,否则最快的方法应该是两者都不使用,因为优化器假定您的代码没有任何溢出并且不会发出代码来处理溢出。
What what does it mean when the -ftrapv definition says it generates "traps?" Does this mean exceptions?
这并不意味着 C++ 异常。它依赖于目标,但假设是 x86,这意味着 GCC 运行时库会引发 SIGABRT,这通常会中止您的程序。在其他平台上,它可能会使用导致硬件异常的特殊 CPU 指令。它主要用于调试目的,也许在少数情况下用于安全,溢出后继续运行的风险大于程序突然终止的风险。
关于c++ - "-ftrapv"和 "-fwrapv": Which is better for efficiency?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960763/