public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* disabling the effects of -ffast-math at specific locations
@ 2011-09-10  2:54 Sam Hocevar
  2011-09-11  2:44 ` Tim Prince
  0 siblings, 1 reply; 3+ messages in thread
From: Sam Hocevar @ 2011-09-10  2:54 UTC (permalink / raw)
  To: gcc-help

   I am using -ffast-math because 99% of the time it has a very positive
impact on my resulting binaries. However, from time to time I would like
to avoid some of the assumptions it makes.

   Consider the following code, doing some fast rounding magic:

     static double moo(double f, double g)
     {
         g *= 4503599627370496.0; // 2 ** 52
         f += g;
         f -= g;
         return f;
     }

   On amd64, it compiles to the following assembly code using -Os:

	.cfi_startproc
	mulsd	.LC0(%rip), %xmm1
	addsd	%xmm1, %xmm0
	subsd	%xmm1, %xmm0
	ret
	.cfi_endproc

   As documented, when using -ffast-math everything gets optimised away:

	.cfi_startproc
	rep
	ret
	.cfi_endproc

   But everything goes back to what I want with this asm call:

     static double moo(double f, double g)
     {
         g *= 4503599627370496.0; // 2 ** 52
         f += g;
         __asm__("" : "+x" (f));
         f -= g;
         return f;
     }

   So my question is: is this guaranteed to always work? Am I sure that
f will always be in an "x" register?

   And do I have a way to make this portable? On PowerPC it's just a
matter of replacing "+x" with "+f" (which is exactly what Apple does in
its libm). But on i386, I don't know in what kind of register f will be,
and I'm stuck with using "+m", which has an awful performance impact.

   Or maybe I'm trying to solve the wrong problem? Is there another
method that will more likely do what I want?

Cheers,
-- 
Sam.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: disabling the effects of -ffast-math at specific locations
  2011-09-10  2:54 disabling the effects of -ffast-math at specific locations Sam Hocevar
@ 2011-09-11  2:44 ` Tim Prince
  2011-09-11  9:17   ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Prince @ 2011-09-11  2:44 UTC (permalink / raw)
  To: gcc-help

On 9/9/2011 10:54 PM, Sam Hocevar wrote:
>     I am using -ffast-math because 99% of the time it has a very positive
> impact on my resulting binaries. However, from time to time I would like
> to avoid some of the assumptions it makes.
>
>     Consider the following code, doing some fast rounding magic:
>
>       static double moo(double f, double g)
>       {
>           g *= 4503599627370496.0; // 2 ** 52
>           f += g;
>           f -= g;
>           return f;
>       }
>
>     On amd64, it compiles to the following assembly code using -Os:
>
> 	.cfi_startproc
> 	mulsd	.LC0(%rip), %xmm1
> 	addsd	%xmm1, %xmm0
> 	subsd	%xmm1, %xmm0
> 	ret
> 	.cfi_endproc
>
>     As documented, when using -ffast-math everything gets optimised away:
>
> 	.cfi_startproc
> 	rep
> 	ret
> 	.cfi_endproc
>
>     But everything goes back to what I want with this asm call:
>
>       static double moo(double f, double g)
>       {
>           g *= 4503599627370496.0; // 2 ** 52
>           f += g;
>           __asm__("" : "+x" (f));
>           f -= g;
>           return f;
>       }
>
>     So my question is: is this guaranteed to always work? Am I sure that
> f will always be in an "x" register?

Perhaps you want the -protect-parens option (and a recent enough version 
of gcc to support that)?  I think I've been informed that this option is 
a default for gcc 4.6.  This option would allow
f = (f + g) -g
to produce the result you appear to want.


-- 
Tim Prince

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: disabling the effects of -ffast-math at specific locations
  2011-09-11  2:44 ` Tim Prince
@ 2011-09-11  9:17   ` Jonathan Wakely
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2011-09-11  9:17 UTC (permalink / raw)
  To: tprince; +Cc: gcc-help

On 11 September 2011 03:43, Tim Prince wrote:
>
> Perhaps you want the -protect-parens option (and a recent enough version of
> gcc to support that)?  I think I've been informed that this option is a
> default for gcc 4.6.  This option would allow
> f = (f + g) -g
> to produce the result you appear to want.


According to the manual -fprotect-parens is the default for 4.5+ but
it's only valid for Fortran.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-09-11  9:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-10  2:54 disabling the effects of -ffast-math at specific locations Sam Hocevar
2011-09-11  2:44 ` Tim Prince
2011-09-11  9:17   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).