Replace __LP64__ with check for pointer size
authorErik Lindahl <erik@kth.se>
Fri, 8 Aug 2014 21:37:49 +0000 (23:37 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sun, 10 Aug 2014 15:05:10 +0000 (17:05 +0200)
The __LP64__ define is not set e.g. on PGI compilers,
so instead we use a portable way of checking for
pointer size based on defines from stdint.h.

Change-Id: Ib2dd6e45c2b168add07f5478d59aec487a91ab9e

cmake/TestInlineASM_gcc_x86.c
src/external/thread_mpi/include/thread_mpi/atomic/gcc_x86.h

index 27f7a07d56984760e5fe3b9de1029630dffa7e01..f5b184b7822e654dc225fce32145b06ada3b4f60 100644 (file)
@@ -4,19 +4,24 @@ main()
   unsigned int _eax,_ebx,_ecx,_edx;
   unsigned int level = 0;
 
-  /* Test gcc inline asm for x86 */
-#if defined (__LP64__) || defined (_M_X64)
+  /* Test gcc inline asm for x86. Note that we CANNOT plainly use __x86_64__
+   * to correspond to a 64-bit environment without also checking that __ILP32__
+   * is NOT set, since x32 uses __x86_64__.
+   */
+#if (defined(__x86_64__) && !defined(__ILP32__))
     __asm__("push %%rbx       \n\t"
             "cpuid            \n\t"
             "movl %%ebx, %1   \n\t"
             "pop %%rbx        \n\t"
             : "=a"(_eax), "=r"(_ebx), "=c"(_ecx), "=d"(_edx) : "0"(level));
-#else
+#elif (defined(__x86_64__) && defined(__ILP32__)) || defined(__i386__)
     __asm__("push %%ebx       \n\t"
             "cpuid            \n\t"
             "movl %%ebx, %1   \n\t"
             "pop %%ebx        \n\t"
             : "=a"(_eax), "=r"(_ebx), "=c"(_ecx), "=d"(_edx) : "0"(level));
+#else
+#    error Cannot detect whether this is a 32-bit or 64-bit x86 build.
 #endif
 
   return 0;
index 559258e8d889a59eb95aa0d366ad14de13e0bd58..1dc5d995d87dc0b2173e3e0c7d6cc69a29af0b24 100644 (file)
@@ -162,16 +162,18 @@ static inline int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t *a,
                                       void              *newval)
 {
     void* prev;
-#ifndef __x86_64__
-    __asm__ __volatile__("lock ; cmpxchgl %1,%2"
+#if (defined(__x86_64__) && !defined(__ILP32__))
+    __asm__ __volatile__("lock ; cmpxchgq %1,%2"
                          : "=a" (prev)
                          : "q" (newval), "m" (a->value), "0" (oldval)
                          : "memory");
-#else
-    __asm__ __volatile__("lock ; cmpxchgq %1,%2"
+#elif (defined(__x86_64__) && defined(__ILP32__)) || defined(__i386__)
+    __asm__ __volatile__("lock ; cmpxchgl %1,%2"
                          : "=a" (prev)
                          : "q" (newval), "m" (a->value), "0" (oldval)
                          : "memory");
+#else
+#    error Cannot detect whether this is a 32-bit or 64-bit x86 build.
 #endif
     return prev == oldval;
 }
@@ -194,17 +196,18 @@ static inline int tMPI_Atomic_swap(tMPI_Atomic_t *a, int b)
 static inline void *tMPI_Atomic_ptr_swap(tMPI_Atomic_ptr_t *a, void *b)
 {
     void *volatile *ret = (void* volatile*)b;
-#ifndef __LP64__
-    __asm__ __volatile__("\txchgl %0, %1;"
+#if (defined(__x86_64__) && !defined(__ILP32__))
+    __asm__ __volatile__("\txchgq %0, %1;"
                          : "+r" (ret), "+m" (a->value)
                          :
                          : "memory");
-
-#else
-    __asm__ __volatile__("\txchgq %0, %1;"
+#elif (defined(__x86_64__) && defined(__ILP32__)) || defined(__i386__)
+    __asm__ __volatile__("\txchgl %0, %1;"
                          : "+r" (ret), "+m" (a->value)
                          :
                          : "memory");
+#else
+#    error Cannot detect whether this is a 32-bit or 64-bit x86 build.
 #endif
     return (void*)ret;
 }