Ian Chivers wrote:
> Jane and I were contacted and asked about compilers
> and integer overflow.
Regarding GCC:
If the system does not support integer trapping, the compiler replaces
the arithmetic op code by a call to the libgcc library. For instance,
for addition, libgcc has:
#ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
SItype
__addvsi3 (SItype a, SItype b)
{
const SItype w = (USItype) a + (USItype) b;
if (b >= 0 ? w < a : w > a)
abort ();
return w;
}
#endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
As it uses the compat implementation on i386 and x86-64 hardware, I
assume that trapping does not work in hardware, but I might be wrong. (I
have not checked the hardware documentation.)
The trapping library function (of libgcc) are documented in 4.1.3 at
http://gcc.gnu.org/onlinedocs/gcc-4.0.4/gccint/Integer-library-routines.html
Trapping is enabled in GCC (gcc, g++, gfortran, …) with the flag:
-ftrapv
This option generates traps for signed overflow on addition,
subtraction, multiplication operations.
However, at least on platforms where GCC does not do trapping in
hardware (or at least on i386 and x86-64 under Linux), there seems to be
a bug*: Thus, on x86-64 it only works for INTEGER(8) while on i386 it
only works for INTEGER(4).
Tobias
* See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52478
|