Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting underflow during execution

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.

like image 475
Andrea Zilio Avatar asked Apr 22 '11 04:04

Andrea Zilio


1 Answers

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.

like image 113
Jerry Coffin Avatar answered Oct 14 '22 02:10

Jerry Coffin