critical floating point incompatibility

John Baldwin jhb at freebsd.org
Mon Jan 26 13:57:46 PST 2009


On Friday 21 December 2007 3:16:33 pm Kostik Belousov wrote:
> On Fri, Dec 21, 2007 at 10:11:24AM -0800, Bakul Shah wrote:
> > Peter Jeremy <peterjeremy at optushome.com.au> wrote:
> > > On Wed, Dec 19, 2007 at 09:40:34PM -0800, Carl Shapiro wrote:
> > > >The default setting of the x87 floating point control word on the i386
> > > >port is 0x127F.  Among other things, this value sets the precision
> > > >control to double precision.  The default setting of the x87 floating
> > > >point control word on the AMD64 is 0x37F.
> > > ...
> > > >It seems clear that the right thing to do is to set the floating point
> > > >environment to the i386 default for i386 binaries.  Is the current
> > > >behavior intended?
> > > 
> > > I believe this is an oversight.  See the thread beginning
> > > 
http://lists.freebsd.org/pipermail/freebsd-stable/2007-November/037947.html
> > 
> > >From reading Bruce's last message in that thread, seems to me
> > may be default for 64bit binaries should be the same as on
> > i386. Anyone wanting different behavior can always call
> > fpsetprec() etc.
> > 
> > I think the fix is to change __INITIAL_FPUCW__ in
> > /sys/amd64/include/fpu.h to 0x127F like on i386.
> I think this shall be done for 32-bit processes only, or we get into
> another ABI breaking nightmare.

How about something like this:  (Carl, can you please test this?)

http://www.FreeBSD.org/~jhb/patches/amd64_fpu_i386.patch

--- //depot/vendor/freebsd/src/sys/amd64/amd64/fpu.c	2006/06/19 22:39:16
+++ //depot/user/jhb/acpipci/amd64/amd64/fpu.c	2009/01/26 21:47:49
@@ -391,6 +391,7 @@
 {
 	struct pcb *pcb;
 	register_t s;
+	u_short control;
 
 	if (PCPU_GET(fpcurthread) == curthread) {
 		printf("fpudna: fpcurthread == curthread %d times\n",
@@ -421,6 +422,10 @@
 		 * explicitly load sanitized registers.
 		 */
 		fxrstor(&fpu_cleanstate);
+		if (pcb->pcb_flags & PCB_32BIT) {
+			control = __INITIAL_FPUCW_I386__;
+			fldcw(&control);
+		}
 		pcb->pcb_flags |= PCB_FPUINITDONE;
 	} else
 		fxrstor(&pcb->pcb_save);
--- //depot/vendor/freebsd/src/sys/amd64/include/fpu.h	2004/04/05 21:30:47
+++ //depot/user/jhb/acpipci/amd64/include/fpu.h	2009/01/26 21:47:49
@@ -92,6 +92,7 @@
  * SSE2 based math.  For FreeBSD/amd64, we go with the default settings.
  */
 #define	__INITIAL_FPUCW__	0x037F
+#define	__INITIAL_FPUCW_I386__	0x127F
 #define	__INITIAL_MXCSR__	0x1F80
 #define	__INITIAL_MXCSR_MASK__	0xFFBF
 

-- 
John Baldwin


More information about the freebsd-hackers mailing list