Atomic swap

Marcel Moolenaar marcel at xcllnt.net
Thu Aug 7 12:12:37 PDT 2003


On Thu, Aug 07, 2003 at 03:05:28PM -0400, Daniel Eischen wrote:
> > The following has been written down without testing (I
> > dropped the cosmetic \t and instead indented by hand to
> > make the source code readable, not what is given to the
> > assembler (per se):
> > 
> > static __inline void
> > atomic_swap_long(volatile long *dst, long val, long *res)
> > {
> > 	__asm (	"1: ldq_l   t0,%0\n"
> > 		"   mov     %1,t1\n"
> 
> If I swap the first 2 instructions:
> 
> 	__asm ( "1: mov     %1,t1\n"
> 		    ldq_l   t0,%0\n"
> 
> that eliminates 1 instruction from between the locked
> instructions.  Is there anything wrong with doing that?

No.

> 
> So it's OK to hardcode registers (t0, t1) in inline assembly?
> 

Not generally. On some platforms the compiler needs temporary
registers to move the C types into registers. You need to stay
away from the registers the compiler uses. I don't think this
is the case here, but you might want to check (ie cc -S and
see if the compiler uses t0 and t1). Since arguments are passed
in registers, the compiler can directly use 0(a0), a1 and 0(a2)
without needing anything else.

-- 
 Marcel Moolenaar	  USPA: A-39004		 marcel at xcllnt.net


More information about the freebsd-alpha mailing list