Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 04 Apr 2012 09:45:25 -0500
From:      Pedro Giffuni <pfg@FreeBSD.org>
To:        Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua>
Cc:        freebsd-current@freebsd.org, Steve Kargl <sgk@troutmask.apl.washington.edu>
Subject:   Re: -ffast-math in Ports and wrong generated code
Message-ID:  <4F7C5E85.5090801@FreeBSD.org>
In-Reply-To: <20120404092923.GA1087@pm513-1.comsys.ntu-kpi.kiev.ua>
References:  <20120403112111.GA39616@pm513-1.comsys.ntu-kpi.kiev.ua> <20120403134300.GA98102@troutmask.apl.washington.edu> <20120404092923.GA1087@pm513-1.comsys.ntu-kpi.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
On 04/04/12 04:29, Andrey Simonenko wrote:
> On Tue, Apr 03, 2012 at 06:43:00AM -0700, Steve Kargl wrote:
>> On Tue, Apr 03, 2012 at 02:21:11PM +0300, Andrey Simonenko wrote:
>>> I use one port from the Ports Collection, that works with FP.  Having
>>> reinstalled it (its version was not changed) I noticed that it started
>>> to work incorrectly.  After debugging and disassembling its code I found
>>> out that the -ffast-math option used for building was the result of
>>> wrongly generated code (I did not specify this option in /etc/make.conf).
>>>
>>> At least finite() function call was eliminated from the result Assembler
>>> code when -ffast-math option is used, tested on 9.0-STABLE and 10.0-CURRENT.
>>>
>>> Example test source code and generated code under 9.0-STABLE on amd64
>>> by gcc from the base system:
>>>
>>> -----------------------------
>>> #include<math.h>
>>> #include<stdio.h>
>>>
>>> void
>>> check_finite(double x)
>>> {
>>> 	printf("%d\n", finite(x));
>>> }
>>> -----------------------------
>>>
>>> % gcc -Wall -O2 -S finite.c
>>> -----------------------------
>>> check_finite:
>>> .LFB3:
>>> 	subq	$8, %rsp
>>> .LCFI0:
>>> 	call	finite			<-- call to finite()
>>> 	movl	$.LC0, %edi
>>> 	movl	%eax, %esi
>>> 	addq	$8, %rsp
>>> 	xorl	%eax, %eax
>>> 	jmp	printf
>>> .LFE3:
>>> 	.size	check_finite, .-check_finite
>>> -----------------------------
>>>
>>> % gcc -Wall -O2 -ffast-math -S finite.c
>>> -----------------------------
>>> check_finite:
>>> .LFB3:
>>> 	xorl	%esi, %esi		<-- fake result from finite()
>>> 	movl	$.LC0, %edi
>>> 	xorl	%eax, %eax
>>> 	jmp	printf
>>> .LFE3:
>>> 	.size	check_finite, .-check_finite
>>> -----------------------------
>>>
>>> Can somebody comment this?
>> Read the man page for gcc.  With --fast-math,
>> gcc assumes that the result of any FP operation
>> is finite.  So, the function call to finite()
>> is eliminated as it is always true.
> Looks like that I was misunderstood.  I did not ask why finite() was
> eliminated, I asked why fake result from finite() is wrong.  Obviously
> that -ffast-math can optimize FP arithmetics and as a result some functions
> can be eliminated.  The problem is not respecting IEEE specifications for
> FP, the problem is wrongly generated code when -ffast-math is used.
>
> Actually there is a bug in GCC used in the base system.  There was made
> a change to builtins.c from gcc in revision 1.12 [1] and as a result gcc
> started to eliminate finite() function calls with -ffinite-math-only.
>
> ...
>
> After this change the corresponding Assembler code for my test file is:
>
> % gcc -Wall -O2 -ffast-math -S finite.c
> -----------------------------
> check_finite:
> .LFB3:
> 	movl	$1, %esi		<-- fake result from finite()
> 	movl	$.LC0, %edi
> 	xorl	%eax, %eax
> 	jmp	printf
> .LFE3:
> 	.size	check_finite, .-check_finite
> -----------------------------
>
> What do you think?  If there is no objections, I'll create PR.
>
> [1] http://www.freebsd.org/cgi/cvsweb.cgi/src/contrib/gcc/builtins.c.diff?r1=1.11;r2=1.12
> _______________________________________________

The SVN commit

http://svnweb.freebsd.org/base?view=revision&revision=228756

will point you to this:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28796

and we are keeping consistency with both upstream and Apple's gcc.

Which is the affected port again? It should be fixed there or else
the problem will continue with future compilers.

Pedro.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4F7C5E85.5090801>