From nobody@FreeBSD.org  Thu Aug 16 16:42:03 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 50C7016A418
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 16 Aug 2007 16:42:03 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 3FF5713C428
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 16 Aug 2007 16:42:03 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.1/8.14.1) with ESMTP id l7GGg078056253
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 16 Aug 2007 16:42:00 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.1/8.14.1/Submit) id l7GGg0Vu056249;
	Thu, 16 Aug 2007 16:42:00 GMT
	(envelope-from nobody)
Message-Id: <200708161642.l7GGg0Vu056249@www.freebsd.org>
Date: Thu, 16 Aug 2007 16:42:00 GMT
From: Simun Mikecin <numisemis@yahoo.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [fix] -mfancy-math-387 has no effect
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         115581
>Category:       amd64
>Synopsis:       [Makefile] [patch] -mfancy-math-387 has no effect
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-amd64
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 16 16:50:01 GMT 2007
>Closed-Date:    Sun Dec 05 16:57:30 UTC 2010
>Last-Modified:  Sun Dec 05 16:57:30 UTC 2010
>Originator:     Simun Mikecin
>Release:        FreeBSD/amd64 7-CURRENT
>Organization:
>Environment:
>Description:
32-bit compatibility libraries on FreeBSD/amd64 are compiled using -mfancy-math-387 gcc option. As stated in gcc(1):

       -mno-fancy-math-387
           Some 387 emulators do not support the "sin", "cos" and "sqrt"
           instructions for the 387.  Specify this option to avoid generating
           those instructions.  This option is the default on FreeBSD, OpenBSD
           and NetBSD.  This option is overridden when -march indicates that
           the target cpu will always have an FPU and so the instruction will
           not need emulation.  As of revision 2.6.1, these instructions are
           not generated unless you also use the -funsafe-math-optimizations
           switch.

So, using just -mfancy-math-387 has no effect. It should be used in combination with -funsafe-math-optimizations or it should not be used.

>How-To-Repeat:
double test(double x) {
  return sin(x);
}

Try to compile using:
gcc -m32 -S -mfancy-math-387 -funsafe-math-optimizations -O2 -march=athlon64 test.c

and with:
gcc -m32 -S -mfancy-math-387 -O2 -march=athlon64 test.c

There will be a difference in assembler output. First one will use machine instruction 'fsin' and the second one will use a libm routine called 'sin'.

>Fix:


Patch attached with submission follows:

--- Makefile.inc1.orig	Tue Jul 10 18:39:36 2007
+++ Makefile.inc1	Thu Aug 16 18:30:44 2007
@@ -238,7 +238,7 @@
 .else
 LIB32CPUTYPE=	${TARGET_CPUTYPE}
 .endif
-LIB32FLAGS=	-m32 -march=${LIB32CPUTYPE} -mfancy-math-387 -DCOMPAT_32BIT \
+LIB32FLAGS=	-m32 -march=${LIB32CPUTYPE} -mfancy-math-387 -funsafe-math-optimizations -DCOMPAT_32BIT \
 		-iprefix ${LIB32TMP}/usr/ \
 		-L${LIB32TMP}/usr/lib32 \
 		-B${LIB32TMP}/usr/lib32


>Release-Note:
>Audit-Trail:

From: Bruce Evans <brde@optusnet.com.au>
To: Simun Mikecin <numisemis@yahoo.com>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-amd64@freebsd.org
Subject: Re: amd64/115581: [fix] -mfancy-math-387 has no effect
Date: Fri, 17 Aug 2007 16:49:04 +1000 (EST)

 On Thu, 16 Aug 2007, Simun Mikecin wrote:
 
 > So, using just -mfancy-math-387 has no effect. It should be used in
 > combination with -funsafe-math-optimizations or it should not be used.
 
 It should not be used, especially on amd64 systems since basic FP
 instructions are relatively fast compared with the fancy instructions
 (except for sqrt).  The 64-bit amd64 libm intentionally never uses the
 fancy instructions (except for sqrt), partly because they are not much
 faster and partly because they are much less accurate.  The fancy
 instructions are not used for float precision (unless you pessimize
 things using -mfancy-math-387) since they are about 3 times slower
 than the library versions on small args.
 
 Is -mno-fancy-math-387 still actually the default on FreeBSD (with FreeBSD's
 config/i386/freebsd.h which is quite different (mostly gratuitously different)
 from the distribution one)?  FreeBSD hasn't supported the math emulator for
 about 10 years.
 
 > --- Makefile.inc1.orig	Tue Jul 10 18:39:36 2007
 > +++ Makefile.inc1	Thu Aug 16 18:30:44 2007
 > @@ -238,7 +238,7 @@
 > .else
 > LIB32CPUTYPE=	${TARGET_CPUTYPE}
 > .endif
 > -LIB32FLAGS=	-m32 -march=${LIB32CPUTYPE} -mfancy-math-387 -DCOMPAT_32BIT \
 > +LIB32FLAGS=	-m32 -march=${LIB32CPUTYPE} -mfancy-math-387 -funsafe-math-optimizations -DCOMPAT_32BIT \
 > 		-iprefix ${LIB32TMP}/usr/ \
 > 		-L${LIB32TMP}/usr/lib32 \
 > 		-B${LIB32TMP}/usr/lib32
 
 -unsafe-math-optimizations should be named
 -broken-floating-point-optimizations and should almost never be used.  It
 should never be used for compiling FreeBSD's math library, since the library
 depends on floating point not being very broken.
 
 gcc-4.2 still says the above, but doesn't actually do the above for
 sqrt.  It inlines sqrt (but not cos or sin) without
 -funsafe-math-optimizations.  I think the difference is just due to
 inlining sqrt not actually being unsafe and the documentation of
 -ffancy-math-387 being many years out of date (this difference is not
 new).  Inlining cos and sin would be safe if the inline code were large
 enough to detect the unsafe cases, but the inline code only checks for
 the result being a NaN (?), which is more than enough for sqrt but not
 enough for cos or sin.
 
 gcc (at least in 4.2) has a -fno-math-errno option which defaults to the
 wrong thing for FreeBSD (-fmath-errno) but the correct think on Darwin.
 -funsafe-math-optimizations apparently has the apparently-undocumented
 effect of turning off on -fno-math-errno.  So -march=pentium4 gives the
 following inlining of the fancy functions:
 
 Default:
  	sqrt: inlined, bogus errno handing
  	cos, sin: not inlined
 with -fno-math-errno:
  	sqrt: inlined, optimal
  	cos, sin: not inlined
 with -funsafe-math-optimizations:
  	sqrt: inlined, optimal
  	cos, sin: inlined, broken (additional breakage only for large args)
  	cosf: inlined, optimal
  	cosf, sinf: inlined, broken (for large args, and small args near a
  		    multiple of pi/2), pessimized (only for small args)
 
 Bruce

From: "Simun Mikecin" <numisemis@yahoo.com>
To: bug-followup@freebsd.org, brde@optusnet.com.au
Cc:  
Subject: Re: amd64/115581: [patch] -mfancy-math-387 has no effect
Date: Tue, 21 Aug 2007 14:12:29 +0200

 On Fri, 17 Aug 2007, Bruce Evans wrote:
 > It should not be used, especially on amd64 systems since basic FP
 > instructions are relatively fast compared with the fancy instructions
 > (except for sqrt). The 64-bit amd64 libm intentionally never uses the
 > fancy instructions (except for sqrt), partly because they are not much
 > faster and partly because they are much less accurate. The fancy
 > instructions are not used for float precision (unless you pessimize
 > things using -mfancy-math-387) since they are about 3 times slower
 > than the library versions on small args.
 
 This PR is about -mfancy-math-387 usage when compiling 32-bit  
 compatibility libraries that are gone be used on FreeBSD/amd64.
 As far as I can see FreeBSD's libm on i386 uses those fancy instructions  
 (for example /usr/src.current/lib/msun/i387/s_sin.S), so the same libm  
 will be used for running 32-bit apps on FreeBSD/amd64.
 Is your statement about fancy instructions been 3 times slower also valid  
 for FreeBSD/i386 and 32-bit apps running on FreeBSD/amd64?

From: Simun Mikecin <numisemis@yahoo.com>
To: Bruce Evans <brde@optusnet.com.au>
Cc: bug-followup@freebsd.org
Subject: Re: amd64/115581: [patch] -mfancy-math-387 has no effect
Date: Tue, 21 Aug 2007 07:37:41 -0700 (PDT)

 --- Bruce Evans <brde@optusnet.com.au> wrote:
 > > This PR is about -mfancy-math-387 usage when compiling 32-bit compatibility 
 > > libraries that are gone be used on FreeBSD/amd64.
 > > As far as I can see FreeBSD's libm on i386 uses those fancy instructions (for 
 > > example /usr/src.current/lib/msun/i387/s_sin.S), so the same libm will be 
 > > used for running 32-bit apps on FreeBSD/amd64.
 > Well, that's in asm so it is not affected by compiler flags.  Compiler
 > flags can cause the library to be not used at all in some cases where
 > the library is better.
 
 I'm just wondering will it have a positive impact on performance if FreeBSD/i386 libm is changed
 not to use that asm code with fancy instructions (just like FreeBSD/amd64 does).
 
 Sime
 
 
 
        
 ____________________________________________________________________________________
 Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more. 
 http://mobile.yahoo.com/go?refer=1GNXIC

From: Bruce Evans <brde@optusnet.com.au>
To: Simun Mikecin <numisemis@yahoo.com>
Cc: bug-followup@freebsd.org, brde@optusnet.com.au
Subject: Re: amd64/115581: [patch] -mfancy-math-387 has no effect
Date: Tue, 21 Aug 2007 22:47:15 +1000 (EST)

 On Tue, 21 Aug 2007, Simun Mikecin wrote:
 
 > On Fri, 17 Aug 2007, Bruce Evans wrote:
 >> It should not be used, especially on amd64 systems since basic FP
 >> instructions are relatively fast compared with the fancy instructions
 >> (except for sqrt). The 64-bit amd64 libm intentionally never uses the
 >> fancy instructions (except for sqrt), partly because they are not much
 >> faster and partly because they are much less accurate. The fancy
 >> instructions are not used for float precision (unless you pessimize
 >> things using -mfancy-math-387) since they are about 3 times slower
 >> than the library versions on small args.
 >
 > This PR is about -mfancy-math-387 usage when compiling 32-bit compatibility 
 > libraries that are gone be used on FreeBSD/amd64.
 > As far as I can see FreeBSD's libm on i386 uses those fancy instructions (for 
 > example /usr/src.current/lib/msun/i387/s_sin.S), so the same libm will be 
 > used for running 32-bit apps on FreeBSD/amd64.
 
 Well, that's in asm so it is not affected by compiler flags.  Compiler
 flags can cause the library to be not used at all in some cases where
 the library is better.
 
 > Is your statement about fancy instructions been 3 times slower also valid for 
 > FreeBSD/i386 and 32-bit apps running on FreeBSD/amd64?
 
 In some cases -- not for most cases, but for float precision trig
 functions on small args, except possibly on very old CPUs.  The i387
 library intentionally doesn't use many hardware transcendental instructions
 in float precision since they are slower and/or very inaccurate.  This
 includes all trig instructions.
 
 Bruce

From: Bruce Evans <brde@optusnet.com.au>
To: Simun Mikecin <numisemis@yahoo.com>
Cc: Bruce Evans <brde@optusnet.com.au>, bug-followup@FreeBSD.org
Subject: Re: amd64/115581: [patch] -mfancy-math-387 has no effect
Date: Wed, 22 Aug 2007 19:50:17 +1000 (EST)

 On Tue, 21 Aug 2007, Simun Mikecin wrote:
 
 > --- Bruce Evans <brde@optusnet.com.au> wrote:
 >>> This PR is about -mfancy-math-387 usage when compiling 32-bit compatibility
 >>> libraries that are gone be used on FreeBSD/amd64.
 >>> As far as I can see FreeBSD's libm on i386 uses those fancy instructions (for
 >>> example /usr/src.current/lib/msun/i387/s_sin.S), so the same libm will be
 >>> used for running 32-bit apps on FreeBSD/amd64.
 >> Well, that's in asm so it is not affected by compiler flags.  Compiler
 >> flags can cause the library to be not used at all in some cases where
 >> the library is better.
 >
 > I'm just wondering will it have a positive impact on performance if FreeBSD/i386 libm is changed
 > not to use that asm code with fancy instructions (just like FreeBSD/amd64 does).
 
 Maybe someday.  The easy cases have already been looked at and resulted in
 removing the asm implementations of functions like sinf() and asin().
 
 Bruce

From: Andriy Gapon <avg@freebsd.org>
To: bug-followup@freebsd.org, numisemis@yahoo.com,
        Bruce Evans <bde@zeta.org.au>
Cc:  
Subject: Re: amd64/115581: [Makefile] [patch] -mfancy-math-387 has no effect
Date: Sun, 05 Dec 2010 15:46:41 +0200

 Can this PR be simply closed based on the discussion and its age?
 Or is there any merit for looking into this again/further?
 
 -- 
 Andriy Gapon
State-Changed-From-To: open->closed 
State-Changed-By: avg 
State-Changed-When: Sun Dec 5 16:56:45 UTC 2010 
State-Changed-Why:  
Closing per the discussion. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=115581 
>Unformatted:
