diff mbox

Use i386/crtfastmath.c on Solaris 2/x86

Message ID yddaae3rra6.fsf@manam.CeBiTec.Uni-Bielefeld.DE
State New
Headers show

Commit Message

Rainer Orth May 31, 2011, 12:25 p.m. UTC
I had long meant to support -fast-math on Solaris 2/x86.  While working
on the Solaris toplevel libgcc move, I've done it with the following
patch.

The only complication is that I need to make sure that SSE insns are only
used if the host supports them.

Bootstrapped without regressions on i386-pc-solaris2.8,
i386-pc-solaris2.9, i386-pc-solaris2.11, and sparc-sun-solaris2.11.

The libgcc part depends on the toplevel libgcc patch, so actually
applying this patch will have to wait until that one is in.

Ok for mainline?

	Rainer


2011-05-28  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>

	gcc:
	* config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]:
	Include <signal.h>, <ucontext.h>.
	(sigill_caught): Define.
	(sigill_hdlr): New function.
	(set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Check if SSE
	insns can be executed.
	* config/sol2.h (ENDFILE_SPEC): Use crtfastmath.o if -ffast-math
	etc.
	* config/sparc/sol2.h (ENDFILE_SPEC): Remove.

	libgcc:
	* config.host (i[34567]86-*-solaris2*): Add i386/t-crtfm to
	tmake_file.
	Add crtfastmath.o to extra_parts.

Comments

Uros Bizjak May 31, 2011, 12:48 p.m. UTC | #1
On Tue, May 31, 2011 at 2:25 PM, Rainer Orth
<ro@cebitec.uni-bielefeld.de> wrote:

> The only complication is that I need to make sure that SSE insns are only
> used if the host supports them.
>
> Bootstrapped without regressions on i386-pc-solaris2.8,
> i386-pc-solaris2.9, i386-pc-solaris2.11, and sparc-sun-solaris2.11.
>
> The libgcc part depends on the toplevel libgcc patch, so actually
> applying this patch will have to wait until that one is in.
>
> Ok for mainline?
>
>        Rainer
>
>
> 2011-05-28  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
>
>        gcc:
>        * config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]:
>        Include <signal.h>, <ucontext.h>.
>        (sigill_caught): Define.
>        (sigill_hdlr): New function.
>        (set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Check if SSE
>        insns can be executed.
>        * config/sol2.h (ENDFILE_SPEC): Use crtfastmath.o if -ffast-math
>        etc.
>        * config/sparc/sol2.h (ENDFILE_SPEC): Remove.
>
>        libgcc:
>        * config.host (i[34567]86-*-solaris2*): Add i386/t-crtfm to
>        tmake_file.
>        Add crtfastmath.o to extra_parts.

Please just put "if (edx & bit_SSE)" part inside existing check. You
will need to split assignment of mxcsr from the declaration, though.

OK with this change.

Thanks,
Uros.
diff mbox

Patch

diff --git a/gcc/config/i386/crtfastmath.c b/gcc/config/i386/crtfastmath.c
--- a/gcc/config/i386/crtfastmath.c
+++ b/gcc/config/i386/crtfastmath.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2007, 2009, 2011 Free Software Foundation, Inc.
  *
  * This file is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -30,6 +30,26 @@ 
 #include "cpuid.h"
 #endif
 
+#if !defined __x86_64 && defined __sun__ && defined __svr4__
+#include <signal.h>
+#include <ucontext.h>
+
+static volatile sig_atomic_t sigill_caught;
+
+static void
+sigill_hdlr (int sig __attribute((unused)),
+	     siginfo_t *sip __attribute__((unused)),
+	     ucontext_t *ucp)
+{
+  sigill_caught = 1;
+  /* Set PC to the instruction after the faulting one to skip over it,
+     otherwise we enter an infinite loop.  4 is the size of the stmxcsr
+     instruction.  */
+  ucp->uc_mcontext.gregs[EIP] += 4;
+  setcontext (ucp);
+}
+#endif
+
 static void __attribute__((constructor))
 #ifndef __x86_64__
 /* The i386 ABI only requires 4-byte stack alignment, so this is necessary
@@ -45,6 +65,32 @@  set_fast_math (void)
   if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
     return;
 
+#if defined __sun__ && defined __svr4__
+  /* Solaris 2 before Solaris 9 4/04 cannot execute SSE instructions even
+     if the CPU supports them.  Programs receive SIGILL instead, so check
+     for that at runtime.  */
+
+  if (edx & bit_SSE)
+    {
+      struct sigaction act, oact;
+
+      act.sa_handler = sigill_hdlr;
+      sigemptyset (&act.sa_mask);
+      /* Need to set SA_SIGINFO so a ucontext_t * is passed to the handler.  */
+      act.sa_flags = SA_SIGINFO;
+      sigaction (SIGILL, &act, &oact);
+
+      /* We need a single SSE instruction here so the handler can safely skip
+	 over it.  */
+      __asm__ volatile ("movss %xmm2,%xmm1");
+
+      sigaction (SIGILL, &oact, NULL);
+
+      if (sigill_caught)
+	return;
+    }
+#endif /* __sun__ && __svr4__ */
+
   if (edx & bit_SSE)
     {
       unsigned int mxcsr = __builtin_ia32_stmxcsr ();
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -141,7 +141,9 @@  along with GCC; see the file COPYING3.  
      %{p|pg:-ldl} -lc}"
 
 #undef  ENDFILE_SPEC
-#define ENDFILE_SPEC "crtend.o%s crtn.o%s"
+#define ENDFILE_SPEC \
+  "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
+   crtend.o%s crtn.o%s"
 
 /* We don't use the standard svr4 STARTFILE_SPEC because it's wrong for us.  */
 #undef STARTFILE_SPEC
diff --git a/gcc/config/sparc/sol2.h b/gcc/config/sparc/sol2.h
--- a/gcc/config/sparc/sol2.h
+++ b/gcc/config/sparc/sol2.h
@@ -117,11 +117,6 @@  along with GCC; see the file COPYING3.  
 #define NO_DBX_BNSYM_ENSYM 1
 
 
-#undef  ENDFILE_SPEC
-#define ENDFILE_SPEC \
-  "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
-   crtend.o%s crtn.o%s"
-
 /* Select a format to encode pointers in exception handling data.  CODE
    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
    true if the symbol may be affected by dynamic relocations.
diff --git a/libgcc/config.host b/libgcc/config.host
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -338,6 +338,8 @@  i[34567]86-*-rtems*)
 	tmake_file="${tmake_file} t-crtin i386/t-softfp i386/t-crtstuff t-rtems"
 	;;
 i[34567]86-*-solaris2*)
+	tmake_file="$tmake_file i386/t-crtfm"
+	extra_parts="$extra_parts crtfastmath.o"
 	;;
 i[4567]86-wrs-vxworks|i[4567]86-wrs-vxworksae)
 	;;