This is the mail archive of the
mailing list for the GCC project.
Re: Why is floor() only compiled to roundsd when using -funsafe-math-optimizations?
- From: Tobias Burnus <tobias dot burnus at physik dot fu-berlin dot de>
- To: Fredrik Tolf <fredrik at dolda2000 dot com>, gcc at gcc dot gnu dot org
- Cc: Jay Foad <jay dot foad at gmail dot com>
- Date: Wed, 28 Jan 2015 16:55:46 +0100
- Subject: Re: Why is floor() only compiled to roundsd when using -funsafe-math-optimizations?
- Authentication-results: sourceware.org; auth=none
On 26 January 2015 at 23:50, Fredrik Tolf <email@example.com> wrote:
> Consider the following small program:
> When compiling this with a -march that supports the roundsd instruction, the
> floor() call seems to only be compiled to such an instruction if
> -funsafe-math-optimizations is specified.
> Why is this? I notice the glibc's floor() implementation (for SSE4.1-enabled
> processors) consists of only this instruction, so barring a bug in glibc,
> that would seem to imply to me the roundsd is IEEE-compliant and safe. Why
> does GCC consider it unsafe?
I first want to point to POSIX, which has:
"floor, floorf, floorl - floor function" [...]
"An application wishing to check for error situations should set errno to
zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions.
On return, if errno is non-zero or fetestexcept(FE_INVALID |
FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an error has occurred."
No one seems to care about the "errno" handling (and prefers to use
the trapping information directly), thus, I wouldn't be surprised if most libc
do not set errno.
And, looking at the handling in GCC itself (for i386), it is secured by
if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
Thus, using -fno-trapping-math or -fno-signaling-nans is enough - no need for
But that doesn't quite answer the question, why the !flag_trapping_math is
needed. At least testing shows that both the GCC version and glibc are
signalling a SNaN (when trapping is enabled).