Intrinsics patch by dw aka mud aka limegreensocks ad yahoo d0t com

git-svn-id: svn+ssh://svn.code.sf.net/p/mingw-w64/code/trunk@5876 4407c894-4637-0410-b4f5-ada5f102cad1
diff --git a/mingw-w64-crt/intrincs/ilockand.c b/mingw-w64-crt/intrincs/ilockand.c
index ee37d05..646bb8b 100644
--- a/mingw-w64-crt/intrincs/ilockand.c
+++ b/mingw-w64-crt/intrincs/ilockand.c
@@ -1,12 +1,7 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
-__LONG32 _InterlockedAnd(__LONG32 volatile *Destination, __LONG32 Value)
-{
-  __asm__ __volatile__("lock ; andl %0,%1"
-    : :"r"(Value),"m"(*Destination)
-    : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedAnd, __LONG32, and)
 
 __LONG32 InterlockedAnd(__LONG32 volatile *, __LONG32) __attribute__((alias("_InterlockedAnd")));
 
diff --git a/mingw-w64-crt/intrincs/ilockand64.c b/mingw-w64-crt/intrincs/ilockand64.c
index 2ae54d1..5d60e0f 100644
--- a/mingw-w64-crt/intrincs/ilockand64.c
+++ b/mingw-w64-crt/intrincs/ilockand64.c
@@ -1,12 +1,8 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
 #ifdef _WIN64
-__int64 _InterlockedAnd64(__int64 volatile *Destination, __int64 Value)
-{
-  __asm__ __volatile__("lock ; andq %0,%1"
-    : : "r"(Value),"m"(*Destination) : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedAnd64, __int64, and)
 #else
 __int64 __stdcall InterlockedCompareExchange64(__int64 volatile *Destination,
   __int64 Exchange, __int64 Comperand);
diff --git a/mingw-w64-crt/intrincs/ilockor.c b/mingw-w64-crt/intrincs/ilockor.c
index 875670e..52c37b3 100644
--- a/mingw-w64-crt/intrincs/ilockor.c
+++ b/mingw-w64-crt/intrincs/ilockor.c
@@ -1,11 +1,7 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
-__LONG32 _InterlockedOr(__LONG32 volatile *Destination, __LONG32 Value)
-{
-  __asm__ __volatile__("lock ; orl %0,%1"
-    : : "r"(Value),"m"(*Destination) : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedOr, __LONG32, or)
 
 __LONG32 InterlockedOr(__LONG32 volatile *, __LONG32) __attribute__((alias("_InterlockedOr")));
 
diff --git a/mingw-w64-crt/intrincs/ilockor64.c b/mingw-w64-crt/intrincs/ilockor64.c
index 09ecf9f..5e63034 100644
--- a/mingw-w64-crt/intrincs/ilockor64.c
+++ b/mingw-w64-crt/intrincs/ilockor64.c
@@ -1,12 +1,8 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
 #ifdef _WIN64
-__int64 _InterlockedOr64(__int64 volatile *Destination, __int64 Value)
-{
-  __asm__ __volatile__("lock ; orq %0,%1"
-    : : "r"(Value),"m"(*Destination) : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedOr64, __int64, or)
 #else
 __int64 __stdcall InterlockedCompareExchange64(__int64 volatile *Destination,
   __int64 Exchange, __int64 Comperand);
diff --git a/mingw-w64-crt/intrincs/ilockxor.c b/mingw-w64-crt/intrincs/ilockxor.c
index 36942c6..0d91272 100644
--- a/mingw-w64-crt/intrincs/ilockxor.c
+++ b/mingw-w64-crt/intrincs/ilockxor.c
@@ -1,11 +1,7 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
-__LONG32 _InterlockedXor(__LONG32 volatile *Destination, __LONG32 Value)
-{
-  __asm__ __volatile__("lock ; xorl %0,%1"
-    : : "r"(Value),"m"(*Destination) : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedXor, __LONG32, xor)
 
 __LONG32 InterlockedXor(__LONG32 volatile *, __LONG32) __attribute__((alias("_InterlockedXor")));
 
diff --git a/mingw-w64-crt/intrincs/ilockxor64.c b/mingw-w64-crt/intrincs/ilockxor64.c
index 39bb1c5..cf4cc4c 100644
--- a/mingw-w64-crt/intrincs/ilockxor64.c
+++ b/mingw-w64-crt/intrincs/ilockxor64.c
@@ -1,12 +1,8 @@
 #include <intrin.h>
+#include <psdk_inc/intrin-mac.h>
 
 #ifdef _WIN64
-__int64 _InterlockedXor64(__int64 volatile *Destination, __int64 Value)
-{
-  __asm__ __volatile__("lock ; xorq %0,%1"
-    : : "r"(Value),"m"(*Destination) : "memory");
-  return *Destination;
-}
+__buildlogicali(_InterlockedXor64, __int64, xor)
 #else
 __int64 __stdcall InterlockedCompareExchange64(__int64 volatile *Destination,
   __int64 Exchange, __int64 Comperand);
diff --git a/mingw-w64-headers/include/psdk_inc/intrin-mac.h b/mingw-w64-headers/include/psdk_inc/intrin-mac.h
index 93b2b59..8a2167b 100644
--- a/mingw-w64-headers/include/psdk_inc/intrin-mac.h
+++ b/mingw-w64-headers/include/psdk_inc/intrin-mac.h
@@ -4,12 +4,12 @@
  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
  */
 
-// The macros in this file are included in multiple places
+/* The macros in this file are included in multiple places */
 
 #ifndef _INTRIN-MAC_
 #define _INTRIN-MAC_
 
-// This macro is used by __stosb, __stosw, __stosd, __stosq
+/* This macro is used by __stosb, __stosw, __stosd, __stosq */
 #define __buildstos(x, y) void x(y *Dest, y Data, size_t Count) \
 { \
    __asm__ __volatile__ ("rep stos%z[Data]" \
@@ -18,4 +18,10 @@
       : "memory"); \
 }
 
+// This macro is used by InterlockedAnd, InterlockedOr, InterlockedXor, InterlockedAnd64, InterlockedOr64, InterlockedXor64
+#define __buildlogicali(x, y, o) y x(volatile y *Destination, y Value) \
+{ \
+    return __sync_fetch_and_ ## o(Destination, Value); \
+}
+
 #endif /* _INTRIN-MAC_ */
diff --git a/mingw-w64-headers/include/winnt.h b/mingw-w64-headers/include/winnt.h
index 23ffafb..c6b596b 100644
--- a/mingw-w64-headers/include/winnt.h
+++ b/mingw-w64-headers/include/winnt.h
@@ -1417,37 +1417,14 @@
 	: "memory");
       return prev;
     }
-    __CRT_INLINE LONG InterlockedAnd(LONG volatile *Destination,LONG Value) {
-      __asm__ __volatile__("lock ; andl %0,%1"
-	: :"r"(Value),"m"(*Destination)
-	: "memory");
-      return *Destination;
-    }
-    __CRT_INLINE LONG InterlockedOr(LONG volatile *Destination,LONG Value) {
-      __asm__ __volatile__("lock ; orl %0,%1"
-	: : "r"(Value),"m"(*Destination) : "memory");
-      return *Destination;
-    }
-    __CRT_INLINE LONG InterlockedXor(LONG volatile *Destination,LONG Value) {
-      __asm__ __volatile__("lock ; xorl %0,%1"
-	: : "r"(Value),"m"(*Destination) : "memory");
-      return *Destination;
-    }
-    __CRT_INLINE LONG64 InterlockedAnd64(LONG64 volatile *Destination,LONG64 Value) {
-      __asm__ __volatile__("lock ; andq %0,%1"
-	: : "r"(Value),"m"(*Destination) : "memory");
-      return *Destination;
-    }
-    __CRT_INLINE LONG64 InterlockedOr64(LONG64 volatile *Destination,LONG64 Value) {
-      __asm__ __volatile__("lock ; orq %0,%1"
-	: : "r"(Value),"m"(*Destination) : "memory");
-      return *Destination;
-    }
-    __CRT_INLINE LONG64 InterlockedXor64(LONG64 volatile *Destination,LONG64 Value) {
-      __asm__ __volatile__("lock ; xorq %0,%1"
-	: : "r"(Value),"m"(*Destination) : "memory");
-      return *Destination;
-    }
+
+__CRT_INLINE __buildlogicali(InterlockedAnd, LONG, and)
+__CRT_INLINE __buildlogicali(InterlockedOr, LONG, or)
+__CRT_INLINE __buildlogicali(InterlockedXor, LONG, xor)
+
+__CRT_INLINE __buildlogicali(InterlockedAnd64, LONG64, and)
+__CRT_INLINE __buildlogicali(InterlockedOr64, LONG64, or)
+__CRT_INLINE __buildlogicali(InterlockedXor64, LONG64, xor)
 #endif /* !__CRT__NO_INLINE */
 
     LONG InterlockedExchangeAdd(LONG volatile *Addend,LONG Value);