Re: Floating Point Exception in Integer Math?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Jay,

On Fri, Jul 29, 2011 at 3:59 PM, Jay K <jay.krell@xxxxxxxxxxx> wrote:
>  > I'm testing signed int32 operations (division, modular reduction, and
>  > overflow), and came across a floating point exception:
>
>  >  int32_t aa = INT32_MIN;
>  >  int32_t bb = -1;
>  >  int32_t rr = aa % bb;
>  >
>  >  cout << rr << " = " << aa << " % " << bb << endl;
>
>  > There's not much to the command line: `g++ sitest.cpp -o sitest.exe`.
>  > I was kind of surprised GCC [silently] moved from the integer domain
>  > to the floating point domain.
>
>  > Is this expected behavior?
>
> What occurs here is implementation defined.
> See also 1 / 0.
Thanks. So its apparently undefined for GCC, and its consistent with
Seacord's Secure Programming Guide (see INT33-CPP at [1]).

Interestingly, using Crypto++ Integers (which don't suffer limited
data ranges) and MSVC, both arrive at 0.

> Gcc didn't do anything floating point-related.
> Integer operations esp. division and mod can have "exceptions" or "traps" or
> such.
The OS message "Floating point exception" was a bit misleading. (I
don't think it was unreasonable to guess the floating point unit
generated the floating point exception).

> Look at the assembly code.
> g++ -S -c sitest.cpp
> vi sitest.S
You are right - the idiv is there:

	movl	$-2147483648, -4(%rbp)
	movl	$-1, -8(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %edx
	sarl	$31, %edx
	idivl	-8(%rbp)

Thanks for the help. They were the results I was looking for, I just
did not recognize it.

Jeff

[1] https://www.securecoding.cert.org/confluence/display/cplusplus/INT33-CPP.+Ensure+that+division+and+modulo+operations+do+not+result+in+divide-by-zero+errors



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux