Is it possible to access the overflow flag registe

2020-06-10 15:16发布

问题:

After performing a mathematical operation, for say, multiplying two integers, is it possible to access the overflow flag register in a CPU with C++ ? If not what are other fast ways to check for an overflow ?

回答1:

No, generally it's impossible. Some CPUs don't even have such a flag (e.g. MIPS).

The link provided in one of the comments will give you ideas on how you can do overflow checks.

Remember that in C and C++ signed integer overflows cause undefined behavior and legally you cannot perform overflow checks after the fact. You either need to use unsigned arithmetic or do the checks before arithmetic operations.



回答2:

I recommend this reading in every appropriate case. From Optimizing software in C++ -

Integer overflow is another security problem. The official C standard says that the behavior of signed integers in case of overflow is "undefined". This allows the compiler to ignore overflow or assume that it doesn't occur. In the case of the Gnu compiler, the assumption that signed integer overflow doesn't occur has the unfortunate consequence that it allows the compiler to optimize away an overflow check. There are a number of possible remedies against this problem: (1) check for overflow before it occurs, (2) use unsigned integers - they are guaranteed to wrap around, (3) trap integer overflow with the option -ftrapv, but this is extremely inefficient, (4) get a compiler warning for such optimizations with option -Wstrict-overflow=2, or (5) make the overflow behavior well-defined with option -fwrapv or -fno-strict-overflow.



回答3:

No. Best approach to check in advance as here

If not what are other fast ways to check for an overflow ?

If you need to test after operation you can use floating point representation (double precision) - every 32-bit integer can be represented exactly as floating point number. If all of the machines you target support IEEE (which is probably the case if you don't have to consider mainframes), you can just do the operations, then use isfinite or isinf on the results. Fast (in terms of programmer's efforts) way is: The IEEE Standard for Floating-Point Arithmetic (IEEE 754) defines five exceptions, each of which returns a default value and has a corresponding status flag that (except in certain cases of underflow) is raised when the exception occurs. The five possible exceptions are:

  • Invalid operation: mathematically undefined, e.g., the square root of a negative number. By default, returns qNaN.
  • Division by zero: an operation on finite operands gives an exact infinite result, e.g., 1/0 or log(0). By default, returns ±infinity.
  • Overflow: a result is too large to be represented correctly (i.e., its exponent with an unbounded exponent range would be larger than emax). By default, returns ±infinity for the round-to-nearest modes (and follows the rounding rules for the directed rounding modes).
  • Underflow: a result is very small (outside the normal range) and is inexact. By default, returns a subnormal or zero (following the rounding rules).
  • Inexact: the exact (i.e., unrounded) result is not representable exactly. By default, returns the correctly rounded result.


回答4:

This is probably not what you want to do for two reasons:

  1. not every CPU has an overflow flag
  2. using C++ there is actually no way to access the overflow flag

the overflow checking tips that people have posted before might be useful.

if you really want to very write fast code that multiplies two integers and checks the overflow flag, you will have to use assembly. if you want some examples for x86, then do ask



回答5:

You'd have to do the operation and check the overflow bit in inline assembly. You could do that and jump to a label on overflow, or (more generally but less efficiently) set a variable if it overflowed.



回答6:

Yea it's possible. You'll have to use some asm.

inline bool isOverflow() {
_asm jo isOverflow_true
return false;
isOverflow_true:return true;
}

That's it.

EDIT: Apparently it wasn't clear that you need to use this after a calculation to see if the calculation overflows. Since you don't know what the compiler will do with your calculations it's recommend to do the calculation in asm too.