Subject: Re: [boost] SafeInt code proposal
From: Ilya Bobir (ilya.bobir_at_[hidden])
Date: 2009-06-21 06:00:05
Niels Dekker - address until 2010-10-10 wrote:
>> 4. x86 has hardware support for checking for integer overflows. It
>> means that the OS may provide some means to check if an overflow has
>> occurred. Using hardware may be way faster then doing the checks in
>> the software.
> Interesting! How would you use hardware to check int overflow?
If you are programming in x86 assembler you can use OF flag in FLAGS
register that is set when an integer operation overflows. But you need
to check it after each operation. There is a special INTO instruction
that triggers an overflow interrupt handler if the OF flag is set. It
means that you just have to put INTO after all of instructions that you
want to check for overflow and provide proper handler.
POSIX defines a signal that is delivered to a process upon invalid
integer operation, SIGPFE: http://en.wikipedia.org/wiki/SIGFPE.
On Windows there is a structures exception with code
STATUS_INTEGER_OVERFLOW. There seems to be a sample of how to use it
here: http://msdn.microsoft.com/en-us/library/7sw7cha4.aspx, but it is