执行期间检测下溢

执行期间检测下溢

本文介绍了执行期间检测下溢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在执行期间自动检测下溢吗?



特别是我相信应该有一个编译器选项生成代码检查下溢和类似的falgs

解决方案



我在谈论G ++编译器。 / div>

C99 / C ++ 11具有浮点控制函数(例如 fetestexcept )和定义的标志(包括 FE_UNDERFLOW ),它应该让你可以合理地可移植地检测浮点下溢(即任何支持这些的编译器/库)。



虽然它们不是可移植的, gcc有一个 feenableexcept ,它将允许您设置被捕获的浮点异常。当您启用了其中一个异常时,您的程序将收到 SIGFPE 信号。



大多数硬件,没有等价的整数操作 - 下溢简单地产生2的补码(或任何)结果,并且(例如)设置标志(例如,进位和符号位)来指示发生了什么。 C99 / C ++ 11确实有一些标志,像整数溢出,但我不相信他们几乎被广泛支持。


Is there any way to detect underflow automatically during execution?

Specifically I believe there should be a compiler option to generate code that checks for underflows and similar falgs right after mathematical operations that could cause them.

I'm talking about the G++ compiler.

解决方案

C99/C++11 have floating point control functions (e.g. fetestexcept) and defined flags (including FE_UNDERFLOW) that should let you detect floating point underflow reasonably portably (i.e., with any compiler/library that supports these).

Though they're not as portable, gcc has an feenableexcept that will let you set floating point exceptions that are trapped. When one of the exceptions you've enabled fires, your program will receive a SIGFPE signal.

At least on most hardware, there's no equivalent for integer operations -- an underflow simply produces a 2's complement (or whatever) result and (for example) sets the the flags (e.g., carry and sign bits) to signal what happened. C99/C++11 do have some flags for things like integer overflow, but I don't believe they're nearly as widely supported.

这篇关于执行期间检测下溢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:25