c++ - "-ftrapv"和 "-fwrapv": Which is better for efficiency?

标签 c++ gcc g++ gnu integer-overflow

来自 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

我有两个问题:

  1. 以下哪个选项对性能更好?
  2. -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/

相关文章:

c++ - Scons 选择哪个版本的g++ |无法识别的命令行选项 "-std=c++11"

c - i386 GNU ld : file not recognized: File format not recognized

c++ - __cplusplus < 201402L 在 gcc 中返回真,即使我指定了 -std=c++14

c++ - C++中继承另一个抽象类的抽象类

C++11 thread::id 表示无线程的特殊值

c - 在 gcc/cc 中聚合源文件

c++ - 菜鸟的快速 g++ 错误诊断

c++ - 为什么这个小 C++ 程序不能使用 G++ 编译?

c++ - 如何在检测习语中要求精确的函数签名?

c++ - Xcode 头文件