Is it possible to use some C or compiler extension to catch
integer overflow?
lcc-win provides the extension:
bool _overflow();
that returns the value of the overflow flag.
Is checking the overflow flag the only possibility?
No, you could check whether you would have overflow before doing
the operation that causes overflow. This is generally a lot more
expensive than checking the overflow flag (on machines that have
one, and it does what you want in the situation). And the OP wanted
NO overhead unless there was overflow.
>How is the overflow flag reset?
Generally any operation that can overflow sets the flag on overflow
and resets it on no overflow. You cannot do a whole series of
operations and then check the overflow flag to determine if any of
them overflowed - that check will only check the last operation.
>Would it be possible to minimize the checks for the flag?
No, the overflow flag is not generally "sticky" (and is not
sticky for the x86).
>Since I want to raise an exception, all integer variables
below the (stack) level of the exception handler could
theoreticall y contain wrong (overflowed) values without
having an infuence on the rest of the program.
>lcc-win also offers the
lcc -checkoverflow
flag when invoked. This flag will test automatically ALL operations for
overflow and abort the program if an overflow is found.
Does this use the x86 trap-on-overflow instruction? If so, you can
do something that doesn't quite meet the OP's requirements, but is
probably about as close as you can get:
1. The compiler puts trap-on-overflow instructions after all signed
integer operations.
2. The trap for trap-on-overflow raises a signal. (Typically an
OS will assign a signal to any traps it doesn't otherwise do anythng
with, so this is done for you. I'm not sure what Windows does).
3. The program catches the signal from (2) and does something with it.
However, your overhead for no overflow is 1 trap-on-overflow
instruction (a few CPU clock cycles) for each calculation. What
was requested was NO overhead on no overflow and unspecified overhead
on overflow (say, 5 minutes to generate a core dump).
>Todays hardware does so many things.
And most of them require code to make it do what you want.
>Is it really impossible to tell the (x86) processor
to interupt when the overflow flag is set?
Yes. And it won't trap when the zero flag is set, or when you load
EAX with a poor approximation of pi * 1000000, either. You have
to write code for that. Note also that there are NOT separate
signed and unsigned math instructions, and signed and unsigned
overflow are different (besides the issue that C defines unsigned
math with no overflow), so constant fiddling with the "interrupt
on overflow" flag would be needed. A compromise is the "trap-on-overflow"
instruction which lets you test specific operations.
Note that the designers of the floating-point instruction set had
more of this approach in mind. You can set bits for floating-point
exceptions on overflow, underflow, loss of significance, and division
by zero. Error flags can be made sticky, so you can avoid the
traps, just do a whole series of calculations and check at the end
to see whether you blew it.
I would hate to see traps for integer loss of significance (in other
words, the result of an operation is zero, so 2-2 would cause a
trap). It would be very difficult to get the C library to work
correctly with that set.