Is it possible to access the overflow flag register in a CPU with C++?

Loers Antario picture Loers Antario · Jan 16, 2013 · Viewed 8.3k times · Source

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 ?

Answer

Alexey Frunze picture Alexey Frunze · Jan 16, 2013

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.