On 05/20/2013 03:57 AM, dw wrote:
In summary, this patch:
1) Correctly returns the "old" value.
2) Uses builtin function instead of inline asm.
3) Fixes both winnt.h and the crt intrinsic files.
4) Uses intrin-mac.h to avoid duplicating code.
On 5/24/2013 2:43 PM, Václav Zeman wrote:
I think your implementation is less efficient than it could be.
You are absolutely correct.
it would be more efficient if you used GCC's __sync_fetch_and_OP()
built-ins instead of hand crafted compare exchange loop
I looked at the hand-crafted and the built-in, and they both generate
the same code. In the end, I went with the __sync_fetch_and_OP()
built-in (attached). The comments above still apply.
dw
Index: mingw-w64-crt/intrincs/ilockand.c
===================================================================
--- mingw-w64-crt/intrincs/ilockand.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockand.c (working copy)
@@ -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")));
Index: mingw-w64-crt/intrincs/ilockand64.c
===================================================================
--- mingw-w64-crt/intrincs/ilockand64.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockand64.c (working copy)
@@ -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);
Index: mingw-w64-crt/intrincs/ilockor.c
===================================================================
--- mingw-w64-crt/intrincs/ilockor.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockor.c (working copy)
@@ -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")));
Index: mingw-w64-crt/intrincs/ilockor64.c
===================================================================
--- mingw-w64-crt/intrincs/ilockor64.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockor64.c (working copy)
@@ -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);
Index: mingw-w64-crt/intrincs/ilockxor.c
===================================================================
--- mingw-w64-crt/intrincs/ilockxor.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockxor.c (working copy)
@@ -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")));
Index: mingw-w64-crt/intrincs/ilockxor64.c
===================================================================
--- mingw-w64-crt/intrincs/ilockxor64.c (revision 5872)
+++ mingw-w64-crt/intrincs/ilockxor64.c (working copy)
@@ -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);
Index: mingw-w64-headers/include/psdk_inc/intrin-mac.h
===================================================================
--- mingw-w64-headers/include/psdk_inc/intrin-mac.h (revision 5872)
+++ mingw-w64-headers/include/psdk_inc/intrin-mac.h (working copy)
@@ -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_ */
Index: mingw-w64-headers/include/winnt.h
===================================================================
--- mingw-w64-headers/include/winnt.h (revision 5872)
+++ mingw-w64-headers/include/winnt.h (working copy)
@@ -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);
------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public