Article View: comp.arch
Article #158Re: Performance of xchg reg,reg
From: Chris M. Thomasson
Date: Fri, 16 Oct 2026 21:28
Date: Fri, 16 Oct 2026 21:28
49 lines
3766 bytes
3766 bytes
On 10/16/2026 1:48 PM, Scott Lurndal wrote: > Thomas Koenig <tkoenig@netcologne.de> writes: >> Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >>> In article <20261016113441.00002a5f@yahoo.com>, >>> Michael S <already5chosen@yahoo.com> wrote: >>>> On Fri, 16 Oct 2026 05:50:55 GMT >>>> anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: >>>> >>>>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes: >>>>>> Well, for some reason the LOCK prefix is implied for XCHG. >>>>> >>>>> According to <https://www.felixcloutier.com/x86/xchg> (which is based >>>>> on (I think) Intel's documentation): >>>>> >>>>> |If a memory operand is referenced, the processor’s locking protocol is >>>>> |automatically implemented for the duration of the exchange operation, >>>>> |regardless of the presence or absence of the LOCK prefix or of the >>>>> |value of the IOPL. >>>>> >>>>> This does not cover the "xchg reg, reg" case. >>>>> >>>>> <https://www.felixcloutier.com/x86/lock> says: >>>>> |The XCHG instruction always asserts the LOCK# signal regardless of the >>>>> |presence or absence of the LOCK prefix. >>>>> >>>>> This seems to include the "xchg reg, reg" case, >>>> >>>> No, it does not. >>> >>> Unfortunately, the information is spread across two sections, >>> and they contradict. The description in the `LOCK` prefix >>> section in the latest version of the Intel SDM includes the text >>> Anton quoted, while the description of `XCHG` itself makes clear >>> that the destination operand must be a memory operand for the >>> locking protocol to automatically apply. Which is correct? >> >> What would be the meaning of LOCK with memory operands? I can't >> think of any. > > Did you mean 'without memory operands'? It's clear that there > is no need to acquire the cache and/or system lock if there > are no memory operands, and a substantial performance penalty; > besides, which cache line would it use for the case where both > operands are registers? One on the threads stack? Kidding, but I have seen people use LOCK XCHG for memory operands, and XCHG for "local" registers and such. But, the Intel docs are rather interesting. Using XCHG works, without LOCK prefix. But, its a point of confusion.
Technical Details
Message-ID:
<11au4u4$3llb5$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at> <20261016113441.00002a5f@yahoo.com> <11atq3a$nqi$1@reader2.panix.com> <11au1vu$3ks5m$2@dont-email.me> <g4wAS.119956$2%g.24960@fx41.iad>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.arch Subject: Re: Performance of xchg reg,reg Date: Fri, 16 Oct 2026 14:28:35 -0700 Organization: A noiseless patient Spider Lines: 49 Message-ID: <11au4u4$3llb5$1@dont-email.me> References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11arg45$2p70p$1@dont-email.me> <2026Oct16.075055@mips.complang.tuwien.ac.at> <20261016113441.00002a5f@yahoo.com> <11atq3a$nqi$1@reader2.panix.com> <11au1vu$3ks5m$2@dont-email.me> <g4wAS.119956$2%g.24960@fx41.iad> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 16 Oct 2026 21:28:36 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3855717"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QEvjKEKZo3vG36w7apE6c8zb3srKAYHQ="; posting-host="ad07cddd8e9cb81b08b3efe183ab26be" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:0Tz5V9oEICc5jwVD75YzSbDaX+4= sha256:S1AkbImNBvqYZCGWTZqWCgmPvCfZ0WqJ0qBrpjmxqTY= sha1:NMNUaLJ07QYwdsurCM9vSknoDAk= sha256:iXkn3Pu8o3Q8WrArJUVJzBfsXogX6NkA3gztJPXyzXQ= In-Reply-To: <g4wAS.119956$2%g.24960@fx41.iad> Content-Language: en-US