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]

[PATCH] Add __GCC_HAVE_SYNC_COMPARE_AND_SWAP_*


Hi,

this patch follows-up to an old thread:

http://gcc.gnu.org/ml/libstdc++/2006-07/msg00106.html

In short, in order to take advantage in the C++ library of __sync_*_compare_and_swap (in shared_ptr<> for instance) we need a runtime feature macro. In that thread it was decided that such macros should begin with __GCC_HAVE_*, and here we are adding 4 of them, for that specific builtin and 1, 2, 4, 8 bytes types. This fine grained approach seems optimal, more accurate than lumping everything under a yes or no __GCC_HAVE_ATOMIC_BUILTINS.

This patch, a joint work with Paolo Bonzini, has been tested on x86-linux and x86_64-linux.

Ok for mainline?

Paolo.

///////////////////
2007-05-17  Paolo Bonzini  <bonzini@gnu.org>
            Paolo Carlini  <pcarlini@suse.de>

	* c-cppbuiltin.c (c_cpp_builtins): Define
	__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1, __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2,
	__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4, __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8,
	if appropriate.
	* Makefile.in: Adjust.
	* doc/cpp.texi [Standard Predefined Macros]: Document.

2007-05-17  Paolo Bonzini  <bonzini@gnu.org>
            Paolo Carlini  <pcarlini@suse.de>

	* gcc.dg/gcc-have-sync-compare-and-swap.c,
	gcc.target/i386/gcc-have-sync-compare-and-swap-1.c,
	gcc.target/i386/gcc-have-sync-compare-and-swap-2.c,
	gcc.target/i386/gcc-have-sync-compare-and-swap-3.c:
	New testcases.
Index: doc/cpp.texi
===================================================================
--- doc/cpp.texi	(revision 124776)
+++ doc/cpp.texi	(working copy)
@@ -2179,6 +2179,13 @@
 (once per compilation) and @code{__TIMESTAMP__} will expand to
 @code{@w{"??? ??? ?? ??:??:?? ????"}}.
 
+@item __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
+@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
+@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
+These macros are defined when the target processor supports atomic compare
+and swap operations on operands 1, 2, 4 or 8 bytes in length, respectively.
+
 @end table
 
 @node System-specific Predefined Macros
Index: c-cppbuiltin.c
===================================================================
--- c-cppbuiltin.c	(revision 124776)
+++ c-cppbuiltin.c	(working copy)
@@ -34,6 +34,7 @@
 #include "toplev.h"
 #include "tm_p.h"		/* Target prototypes.  */
 #include "target.h"
+#include "insn-codes.h"
 
 #ifndef TARGET_OS_CPP_BUILTINS
 # define TARGET_OS_CPP_BUILTINS()
@@ -547,6 +548,20 @@
   if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node))
     cpp_define (pfile, "__WCHAR_UNSIGNED__");
 
+  /* Tell source code if the compiler makes sync_compare_and_swap
+     builtins available.  */
+  if (HAVE_sync_compare_and_swapqi)
+    cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
+
+  if (HAVE_sync_compare_and_swaphi)
+    cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
+
+  if (HAVE_sync_compare_and_swapsi)
+    cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
+
+  if (HAVE_sync_compare_and_swapdi)
+    cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
+
   /* Make the choice of ObjC runtime visible to source code.  */
   if (c_dialect_objc () && flag_next_runtime)
     cpp_define (pfile, "__NEXT_RUNTIME__");
Index: testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-1.c
===================================================================
--- testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-1.c	(revision 0)
+++ testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-1.c	(revision 0)
@@ -0,0 +1,18 @@
+/* { dg-do preprocess { target i?86-*-* } } */
+/* { dg-options "-march=i386" } */
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
+#error nonono
+#endif
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
+#error nonono
+#endif
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+#error nonono
+#endif
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
+#error nonono
+#endif
Index: testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-2.c
===================================================================
--- testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-2.c	(revision 0)
+++ testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-2.c	(revision 0)
@@ -0,0 +1,14 @@
+/* { dg-do preprocess { target i?86-*-* } } */
+/* { dg-options "-march=i486" } */
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
+#error nonono
+#endif
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
+#error nonono
+#endif
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+#error nonono
+#endif
Index: testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-3.c
===================================================================
--- testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-3.c	(revision 0)
+++ testsuite/gcc.target/i386/gcc-have-sync-compare-and-swap-3.c	(revision 0)
@@ -0,0 +1,18 @@
+/* { dg-do preprocess { target i?86-*-* } } */
+/* { dg-options "-march=i586" } */
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
+#error nonono
+#endif
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
+#error nonono
+#endif
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+#error nonono
+#endif
+
+#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
+#error nonono
+#endif
Index: testsuite/gcc.dg/gcc-have-sync-compare-and-swap.c
===================================================================
--- testsuite/gcc.dg/gcc-have-sync-compare-and-swap.c	(revision 0)
+++ testsuite/gcc.dg/gcc-have-sync-compare-and-swap.c	(revision 0)
@@ -0,0 +1,46 @@
+/* { dg-do link } */
+
+void f1()
+{
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
+  typedef int __attribute__  ((__mode__ (__QI__))) qi_int_type;
+  qi_int_type qi_int;
+  __sync_bool_compare_and_swap (&qi_int, (qi_int_type)0, (qi_int_type)1);
+#endif
+}
+
+void f2()
+{
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
+  typedef int __attribute__ ((__mode__ (__HI__))) hi_int_type;
+  hi_int_type hi_int;
+  __sync_bool_compare_and_swap (&hi_int, (hi_int_type)0, (hi_int_type)1);
+#endif
+}
+
+void f4()
+{
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+  typedef int __attribute__ ((__mode__ (__SI__))) si_int_type;
+  si_int_type si_int;
+  __sync_bool_compare_and_swap (&si_int, (si_int_type)0, (si_int_type)1);
+#endif
+}
+
+void f8()
+{
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
+  typedef int __attribute__ ((__mode__ (__DI__))) di_int_type;
+  di_int_type di_int;
+  __sync_bool_compare_and_swap (&di_int, (di_int_type)0, (di_int_type)1);
+#endif
+}
+
+int main()
+{
+  f1();
+  f2();
+  f4();
+  f8();
+  return 0;
+}
Index: Makefile.in
===================================================================
--- Makefile.in	(revision 124776)
+++ Makefile.in	(working copy)
@@ -1739,7 +1739,7 @@
 
 c-cppbuiltin.o : c-cppbuiltin.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
 	$(TREE_H) version.h $(C_COMMON_H) $(C_PRAGMA_H) $(FLAGS_H) toplev.h \
-	output.h except.h $(REAL_H) $(TARGET_H) $(TM_P_H)
+	output.h except.h $(REAL_H) $(TARGET_H) $(TM_P_H) insn-codes.h
 
 # A file used by all variants of C and some other languages.
 

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