This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] Add CPU feature bitmap to crt*


This is a prototype to add a runtime cpu feature detection for
versioning of target libraries and maybe user programs.  The Intel
compiler is providing __intel_cpu_indicator and 
__intel_cpu_indicator_init() (in libimf) for this purpose.  There
seems to be no sign of it supporting that as an official interface,
though, and such, it is undocumented.  For this reason, this prototype
patch does not try to expose the information apart from having the
__gcc_cpu_feature_indicator symbol visible.

Any thoughts?

Thanks,
Richard.


2005-11-14  Richard Guenther  <rguenther@suse.de>

	* crtstuff.c (__gcc_cpu_feature_indicator): Add symbol.
	* config.gcc (i[34567]86-*): Use i386/t-crtcpu.
	* config/i386/t-crtcpu: New file.
	* config/i386/crtcpu.c: Likewise.
	* config/i386/linux.h (ENDFILE_SPEC): Link against crtcpu.o.

=== config/i386/linux.h
==================================================================
--- config/i386/linux.h	(revision 71856)
+++ config/i386/linux.h	(local)
@@ -125,7 +125,7 @@
 #undef  ENDFILE_SPEC
 #define ENDFILE_SPEC \
   "%{ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
-   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s"
+   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s crtcpu.o%s"
 
 /* A C statement (sans semicolon) to output to the stdio stream
    FILE the assembler definition of uninitialized global DECL named
=== config/i386/crtcpu.c
==================================================================
--- config/i386/crtcpu.c	(revision 71856)
+++ config/i386/crtcpu.c	(local)
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2005 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
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ * 
+ * In addition to the permissions in the GNU General Public License, the
+ * Free Software Foundation gives you unlimited permission to link the
+ * compiled version of this file with other programs, and to distribute
+ * those programs without any restriction coming from the use of this
+ * file.  (The General Public License restrictions do apply in other
+ * respects; for example, they cover modification of the file, and
+ * distribution when not linked into another program.)
+ * 
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ * 
+ *    As a special exception, if you link this library with files
+ *    compiled with GCC to produce an executable, this does not cause
+ *    the resulting executable to be covered by the GNU General Public License.
+ *    This exception does not however invalidate any other reasons why
+ *    the executable file might be covered by the GNU General Public License.
+ */
+
+
+/* CPUID level 0x00000001 (edx).  */
+#define CXS     (1 << 8)
+#define MMX     (1 << 23)
+#define SSE	(1 << 25)
+#define SSE2	(1 << 26)
+
+/* CPUID level 0x00000001 (ecx).  */
+#define SSE3    (1 << 0)
+
+/* We compress all the different CPU feature masks from different
+   vendors to one 32bit mask, thereby including only features that
+   are useful for either target library or user programs.  */
+
+#define GCC_CXS (1 << 0)  /* cmpxchg8b */
+#define GCC_MMX (1 << 1)
+#define GCC_SSE (1 << 2)
+#define GCC_SSE2 (1 << 3)
+#define GCC_SSE3 (1 << 4)
+
+extern int __gcc_cpu_feature_indicator;
+
+static void __attribute__((constructor))
+__set_gcc_cpu_feature_indicator (void)
+{
+  unsigned int eax, ebx, ecx, edx;
+
+  /* See if we can use cpuid.  */
+  asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
+		"pushl %0; popfl; pushfl; popl %0; popfl"
+		: "=&r" (eax), "=&r" (ebx)
+		: "i" (0x00200000));
+
+  if (((eax ^ ebx) & 0x00200000) == 0)
+    return;
+
+  /* Check the highest input value for eax.  */
+  asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
+		: "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
+		: "0" (0));
+
+  if (eax == 0)
+    return;
+
+  /* Get processor version and feature information.  */
+  asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
+		: "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
+		: "0" (1));
+
+  if (edx & CXS)
+    __gcc_cpu_feature_indicator |= GCC_CXS;
+  if (edx & MMX)
+    __gcc_cpu_feature_indicator |= GCC_MMX;
+  if (edx & SSE)
+    __gcc_cpu_feature_indicator |= GCC_SSE;
+  if (edx & SSE2)
+    {
+      __gcc_cpu_feature_indicator |= GCC_SSE2;
+      if (ecx & SSE3)
+        __gcc_cpu_feature_indicator |= GCC_SSE3;
+    }
+}
=== config/i386/t-crtcpu
==================================================================
--- config/i386/t-crtcpu	(revision 71856)
+++ config/i386/t-crtcpu	(local)
@@ -0,0 +1,6 @@
+EXTRA_PARTS += crtcpu.o
+
+$(T)crtcpu.o: $(srcdir)/config/i386/crtcpu.c $(GCC_PASSES)
+	$(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -c \
+		$(srcdir)/config/i386/crtcpu.c \
+		-o $(T)crtcpu$(objext)
=== config.gcc
==================================================================
--- config.gcc	(revision 71856)
+++ config.gcc	(local)
@@ -1033,7 +1033,7 @@
 	i[34567]86-*-knetbsd*-gnu) tm_file="${tm_file} knetbsd-gnu.h i386/knetbsd-gnu.h" ;;
 	i[34567]86-*-kfreebsd*-gnu) tm_file="${tm_file} kfreebsd-gnu.h i386/kfreebsd-gnu.h" ;;
 	esac
-	tmake_file="${tmake_file} i386/t-crtstuff i386/t-crtfm"
+	tmake_file="${tmake_file} i386/t-crtstuff i386/t-crtfm i386/t-crtcpu"
 	;;
 x86_64-*-linux* | x86_64-*-kfreebsd*-gnu | x86_64-*-knetbsd*-gnu)
 	tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h svr4.h linux.h \
=== crtstuff.c
==================================================================
--- crtstuff.c	(revision 71856)
+++ crtstuff.c	(local)
@@ -150,6 +150,12 @@
 
 #ifdef CRT_BEGIN
 
+/* A place to store a mask of CPU features detected from the running
+   program.  A value of zero denotes the lowest common dominator of
+   the architecture.  Other values are target dependent.  */
+
+int __gcc_cpu_feature_indicator;
+
 /* NOTE:  In order to be able to support SVR4 shared libraries, we arrange
    to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
    __DTOR_END__ } per root executable and also one set of these symbols


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]