From owner-freebsd-alpha@FreeBSD.ORG Thu Aug 7 07:33:07 2003 Return-Path: Delivered-To: freebsd-alpha@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 92CD637B401; Thu, 7 Aug 2003 07:33:07 -0700 (PDT) Received: from duke.cs.duke.edu (duke.cs.duke.edu [152.3.140.1]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8D96E43FD7; Thu, 7 Aug 2003 07:33:04 -0700 (PDT) (envelope-from gallatin@cs.duke.edu) Received: from grasshopper.cs.duke.edu (grasshopper.cs.duke.edu [152.3.145.30]) by duke.cs.duke.edu (8.12.9/8.12.9) with ESMTP id h77EX3nq026344 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO); Thu, 7 Aug 2003 10:33:03 -0400 (EDT) Received: (from gallatin@localhost) by grasshopper.cs.duke.edu (8.11.6/8.9.1) id h77EWwZ93373; Thu, 7 Aug 2003 10:32:58 -0400 (EDT) (envelope-from gallatin@cs.duke.edu) From: Andrew Gallatin MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16178.25370.731486.809755@grasshopper.cs.duke.edu> Date: Thu, 7 Aug 2003 10:32:58 -0400 (EDT) To: deischen@freebsd.org In-Reply-To: References: X-Mailer: VM 6.75 under 21.1 (patch 12) "Channel Islands" XEmacs Lucid cc: alpha@freebsd.org Subject: Re: Atomic swap X-BeenThere: freebsd-alpha@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Porting FreeBSD to the Alpha List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Aug 2003 14:33:07 -0000 Daniel Eischen writes: > [ I'm not subscribed to alpha@; please keep me on the CC ] > > I need an atomic swap function for libpthread. Here's my hack > of an implementation: > > /* > * Atomic swap: > * Atomic (tmp = *dst, *dst = val), then *res = tmp > * > * void atomic_swap_long(long *dst, long val, long *res); > */ > static __inline > void atomic_swap_long(volatile long *dst, long val, long *res) > { > u_int64_t result; > > __asm __volatile ( > "1:\tldq_l %0,%1\n\t" > "stq_c %2,%1\n\t" > "beq %2,2f\n\t" /* Why is this beq instead of bne 1b? */ > "br 3f\n" > "2:\tbr 1b\n" > "3:\n" > : "=&r" (result) > : "m" (*dst), "r" (val) > : "memory"); > > *res = result; > } > > As annotated above, there seems to be one more branch than > necessary. Its actually an optimization. Alphas predict that backward branches will always be taken (think loops). If you were to branch directly back to 1:, then if the store succeeds (which it nearly always should), then the cpu would have been betting on taking the branch, and that would slow things down. > Can someone look this over for me? I really don't quite > know what I'm doing when it comes to inline assembly. I think it looks OK, but I'm also terrible at inline asm. Drew