This is the mail archive of the gcc-help@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]

Inline assembler dokumentation?



I've been trying for a couple of days to make use of the
ia32 instruction "cmpxchg8b" in using gcc inline assembler.
I've come up with the following, which seemed to work at
first:

static inline int
atomic_compare_and_swap64 (INT64 *p, INT64 oldval, INT64 newval)
{
  char ret;
#define HIGH(X) ((INT32 *)&(X))[1]
#define LOW(X) ((INT32 *)&(X))[0]

  __asm__ __volatile__ (
    "xchgl %%ebx, %%edi ;"
    "lock; cmpxchg8b (%%esi); sete %%al;"
    "movl  %%edi, %%ebx; "
    "movb  %%al, %0 "
    :"=q" (ret)
    :"S" (p),
     "a" (LOW(oldval)), "d" (HIGH(oldval)),
     "D" (LOW(newval)), "c" (HIGH(newval))
    :"cc", "memory");

  return ret;
}

The reason for saving %ebx is that I also use -fPIC which seems
to require %ebx. However, I noticed that the cmpxchg8b instruction
(sometimes) zaps %ecx and %edx, and I assume I need to inform
gcc of this, so I tried the following:

static inline int
atomic_compare_and_swap64 (INT64 *p, INT64 oldval, INT64 newval)
{
  char ret;
  INT32 t1, t2, t3;
#define HIGH(X) ((INT32 *)&(X))[1]
#define LOW(X) ((INT32 *)&(X))[0]

  __asm__ __volatile__ (
    "xchgl %%ebx, %%edi ;"
    "lock; cmpxchg8b (%%esi); sete %%al;"
    "movl  %%edi, %%ebx; "
    "movb  %%al, %0 "
    :"=q" (ret),
     "a"(t1), "d"(t2), "D"(t3)
    :"S" (p),
     "a" (LOW(oldval)), "d" (HIGH(oldval)),
     "D" (LOW(newval)), "c" (HIGH(newval))
    :"cc", "memory");

  return ret;
}


However, this seems to lead to somewhat reduced efficiency, and
something still goes wrong when atomic_compare_and_swap64 is
inlined.  My grasp of the __asm__ directive is limited, so I
need some help... Could someone please help me out? I can supply
the code that goes wrong if need be, but I don't have a short
test example yet.

I suppose it could be a bug in gcc too, I am using:
  gcc version 2.95.3 19991030 (prerelease)
from the Mandrake 7.1 linux distribution.

     /Fredrik Hubinette


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