Thread View: comp.arch
82 messages
82 total messages
Page 1 of 2
Started by Anton Ertl
Tue, 13 Oct 2026 08:43
Page 1 of 2 • 82 total messages
Performance of xchg reg,reg
Author: Anton Ertl
Date: Tue, 13 Oct 2026 08:43
Date: Tue, 13 Oct 2026 08:43
110 lines
6612 bytes
6612 bytes
Microbenchmarks (and this text in HTML form) on
<http://www.complang.tuwien.ac.at/anton/xchg/>.
In compilers, in particular in SSA destruction, or, more generally,
register allocation, you sometimes have situations where you want to
swap the contents of registers, e.g., when r1 contains a and r2
contains b on one path, while r1 contains b and r2 contains a on the
other path, and both paths join. This can be achieved with 3 mov
instructions, but requires an extra register. The AMD64 instruction
set contains the instruction xchg, which swaps the registers without
extra register.
More generally, there may be more than two registers whose values have
to be moved in a cyclic way. This also requires an extra register and
(for an n-register cycle) n+1 mov instructions if performed using mov
instructions, or it can be achieved with n-1 xchg instructions.
Mov instructions are supported very well in recent CPU cores, with the
cores usually being able to perform as many independent (and often
even dependent) moves as the register renamer accepts instructions
(e.g., if the register renamer accepts 8 instructions per cycle, 8
moves per cycle can be performed, usually even dependent moves).
But how do xchg instructions perform? Should a register allocator use
them where they are convenient, or prefer to use mov instructions and
an extra register?
<http://www.complang.tuwien.ac.at/anton/xchg/> contains
microbenchmarks and this posting presents results for them on various
cores from Intel and AMD. The first set of microbenchmarks perform 1G
xchg instructions in a loop containing 28 xchg instructions (plus two
instructions for the loop).
One (independent) contains a sequence of 7 xchg instructions that are
fully independent of each other, all accessing different registers,
followed by the same sequence another three times. So the xchg
instructions are as independent as possible with 14 registers
involved.
The other (dependent) contains a sequence of 14 xchg instructions
where each subsequent instruction uses one register of the previous
instruction, implementing a 15-register cycle. This sequence is
followed by the reversal of this sequence, resulting in performing the
15-register cycle in the other direction.
Here are the results in Gcycles on recent microarchitectures, i.e., a
result of 0.25 means that the microarchitecture performs 4 xchg
instructions per cycle. Smaller is better.
indep dep core family CPU
0.26 0.26 Zen5 AMD Ryzen AI Pro 350
0.35 0.35 Zen4 AMD Ryzen 8700G
0.35 0.35 Zen3 AMD Ryzen 5800X
0.61 1.50 Lion Cove Intel P Core Ultra 225
1.00 1.50 Golden Cove Intel P Core i3-1315U
0.82 1.50 Rocket Lake Intel P Xeon E-2388G
2.07 2.05 Skymont Intel E Core Ultra 225
3.45 3.45 Gracemont Intel E Core i3-1315U
Given the results of the dependent benchmark, it looks like the AMD
processors perform xchg in the register renamer, consuming the
resources of two ordinary instructions there (probably because two
result registers are involved).
On Intel microarchitectures, whether P-cores or E-cores, but
especially the latter, xchg seems to have gotten little love.
Let's see how this affects cyclic moves between registers. The
following microbenchmarks perform cyclic moves through 2-5 registers
(cyc2-cyc5) in three different ways:
mem Store the first register to memory, then perform n-1 moves among
registers, then load the memory location into the final register.
reg Move the first register into a spare register, then perform n-1
moves among the cycled registers, then move the spare register
into the final register.
xchg Use n-1 xchg instructions.
The benchmark loop contains 12 cycles, and cycles involving
non-overlapping sets of registers are benchmarked (6 sets for cyc2, 4
for cyc3, 3 for cyc4, 2 for cyc5). At least three different memory
locations are used for holding the value of the first register in
memory, but the same register is used as the spare register. Here are
the results (execution cycles per cyclic move (i.e., n+1 movs in case
of reg, n-1 xchgs in case of xchg).
cyc2 | cyc3 | cyc4 | cyc5 |core
mem reg xchg|mem reg xchg|mem reg xchg|mem reg xchg|
0.50 0.50 0.27|0.67 0.58 0.52|0.83 0.83 0.77|1.08 0.83 1.02|Zen5
1.00 0.51 0.36|1.00 0.68 0.69|1.00 0.85 1.03|1.01 1.01 1.36|Zen4
1.00 0.51 0.42|1.00 0.68 0.70|1.00 0.92 1.03|1.02 1.01 1.36|Zen3
0.50 0.40 0.68|0.54 0.56 1.28|0.67 0.66 1.97|0.81 0.81 4.21|Lion Cove
0.61 0.53 1.00|0.70 0.70 2.00|0.89 0.90 3.00|1.06 1.08 4.41|Golden Cove
0.82 0.67 1.00|0.87 0.83 2.00|1.08 1.08 3.00|1.22 1.22 4.66|Rocket Lake
1.00 0.40 1.45|0.65 0.73 4.14|0.65 0.65 7.35|0.78 0.86 9.44|Skymont
0.67 0.67 3.08|0.83 0.83 6.73|1.03 1.03 10.63|1.23 1.23 13.77|Gracemont
On Zen5, even a 5-register cycle can be profitably implemented with
xchg when you have no spare register, a 4-register cycle even when you
have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 (for
reg) are borderline cases, and for shorter cycles xchg is better.
On Intel processors, xchg is so slow that it never is profitable to
use it. The slowness takes on extreme values in the E-cores, with
slowdowns by a factor >10 for cyc4 and cyc5.
- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Lawrence D’Oliveiro
Date: Wed, 14 Oct 2026 00:23
Date: Wed, 14 Oct 2026 00:23
15 lines
1893 bytes
1893 bytes
On Tue, 13 Oct 2026 08:43:47 GMT, Anton Ertl wrote: > On Zen5, even a 5-register cycle can be profitably implemented with > xchg when you have no spare register, a 4-register cycle even when > you have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 > (for reg) are borderline cases, and for shorter cycles xchg is > better. > > On Intel processors, xchg is so slow that it never is profitable to > use it. The slowness takes on extreme values in the E-cores, with > slowdowns by a factor >10 for cyc4 and cyc5. You’d think both companies would be running similar kinds of instruction workloads to find the things worth optimizing in their CPU architectures. But perhaps not ...
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Wed, 14 Oct 2026 07:00
Date: Wed, 14 Oct 2026 07:00
33 lines
2792 bytes
2792 bytes
Lawrence =?iso-8859-13?q?D=FFOliveiro?= <ldo@nz.invalid> writes: >On Tue, 13 Oct 2026 08:43:47 GMT, Anton Ertl wrote: > >> On Zen5, even a 5-register cycle can be profitably implemented with >> xchg when you have no spare register, a 4-register cycle even when >> you have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 >> (for reg) are borderline cases, and for shorter cycles xchg is >> better. >> >> On Intel processors, xchg is so slow that it never is profitable to >> use it. The slowness takes on extreme values in the E-cores, with >> slowdowns by a factor >10 for cyc4 and cyc5. > >You’d think both companies would be running similar kinds of >instruction workloads to find the things worth optimizing in their CPU >architectures. But perhaps not ... Or maybe, given the structure of the renamer, an AMD engineer saw a relatively easy way to implement xchg efficiently, and did it, without first looking at how often xchg occurs in benchmark programs. And at Intel, either no engineer thought about this, or the structure of the renamer makes an efficient implementation much harder. It makes me wonder, however, why cyc2xchg is more expensive than cyc2reg, though. An easy implementation should be to decode xchg reg, reg into three mov reg, reg (i.e., what cyc2reg implements), where the spare register is a non-architectural register. But on every measured Intel microarchitecture cyc2xchg is substantially slower than cyc2reg. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Michael S
Date: Wed, 14 Oct 2026 19:29
Date: Wed, 14 Oct 2026 19:29
118 lines
6952 bytes
6952 bytes
On Tue, 13 Oct 2026 08:43:47 GMT anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: > Microbenchmarks (and this text in HTML form) on > <http://www.complang.tuwien.ac.at/anton/xchg/>. > > In compilers, in particular in SSA destruction, or, more generally, > register allocation, you sometimes have situations where you want to > swap the contents of registers, e.g., when r1 contains a and r2 > contains b on one path, while r1 contains b and r2 contains a on the > other path, and both paths join. This can be achieved with 3 mov > instructions, but requires an extra register. The AMD64 instruction > set contains the instruction xchg, which swaps the registers without > extra register. > > More generally, there may be more than two registers whose values have > to be moved in a cyclic way. This also requires an extra register and > (for an n-register cycle) n+1 mov instructions if performed using mov > instructions, or it can be achieved with n-1 xchg instructions. > > Mov instructions are supported very well in recent CPU cores, with the > cores usually being able to perform as many independent (and often > even dependent) moves as the register renamer accepts instructions > (e.g., if the register renamer accepts 8 instructions per cycle, 8 > moves per cycle can be performed, usually even dependent moves). > > But how do xchg instructions perform? Should a register allocator use > them where they are convenient, or prefer to use mov instructions and > an extra register? > > <http://www.complang.tuwien.ac.at/anton/xchg/> contains > microbenchmarks and this posting presents results for them on various > cores from Intel and AMD. The first set of microbenchmarks perform 1G > xchg instructions in a loop containing 28 xchg instructions (plus two > instructions for the loop). > > One (independent) contains a sequence of 7 xchg instructions that are > fully independent of each other, all accessing different registers, > followed by the same sequence another three times. So the xchg > instructions are as independent as possible with 14 registers > involved. > > The other (dependent) contains a sequence of 14 xchg instructions > where each subsequent instruction uses one register of the previous > instruction, implementing a 15-register cycle. This sequence is > followed by the reversal of this sequence, resulting in performing the > 15-register cycle in the other direction. > > Here are the results in Gcycles on recent microarchitectures, i.e., a > result of 0.25 means that the microarchitecture performs 4 xchg > instructions per cycle. Smaller is better. > > indep dep core family CPU > 0.26 0.26 Zen5 AMD Ryzen AI Pro 350 > 0.35 0.35 Zen4 AMD Ryzen 8700G > 0.35 0.35 Zen3 AMD Ryzen 5800X > 0.61 1.50 Lion Cove Intel P Core Ultra 225 > 1.00 1.50 Golden Cove Intel P Core i3-1315U > 0.82 1.50 Rocket Lake Intel P Xeon E-2388G > 2.07 2.05 Skymont Intel E Core Ultra 225 > 3.45 3.45 Gracemont Intel E Core i3-1315U > > Given the results of the dependent benchmark, it looks like the AMD > processors perform xchg in the register renamer, consuming the > resources of two ordinary instructions there (probably because two > result registers are involved). > > On Intel microarchitectures, whether P-cores or E-cores, but > especially the latter, xchg seems to have gotten little love. > > Let's see how this affects cyclic moves between registers. The > following microbenchmarks perform cyclic moves through 2-5 registers > (cyc2-cyc5) in three different ways: > > mem Store the first register to memory, then perform n-1 moves among > registers, then load the memory location into the final register. > reg Move the first register into a spare register, then perform n-1 > moves among the cycled registers, then move the spare register > into the final register. > xchg Use n-1 xchg instructions. > > The benchmark loop contains 12 cycles, and cycles involving > non-overlapping sets of registers are benchmarked (6 sets for cyc2, 4 > for cyc3, 3 for cyc4, 2 for cyc5). At least three different memory > locations are used for holding the value of the first register in > memory, but the same register is used as the spare register. Here are > the results (execution cycles per cyclic move (i.e., n+1 movs in case > of reg, n-1 xchgs in case of xchg). > > cyc2 | cyc3 | cyc4 | cyc5 |core > mem reg xchg|mem reg xchg|mem reg xchg|mem reg xchg| > 0.50 0.50 0.27|0.67 0.58 0.52|0.83 0.83 0.77|1.08 0.83 1.02|Zen5 > 1.00 0.51 0.36|1.00 0.68 0.69|1.00 0.85 1.03|1.01 1.01 1.36|Zen4 > 1.00 0.51 0.42|1.00 0.68 0.70|1.00 0.92 1.03|1.02 1.01 1.36|Zen3 > 0.50 0.40 0.68|0.54 0.56 1.28|0.67 0.66 1.97|0.81 0.81 4.21|Lion > Cove 0.61 0.53 1.00|0.70 0.70 2.00|0.89 0.90 3.00|1.06 1.08 > 4.41|Golden Cove 0.82 0.67 1.00|0.87 0.83 2.00|1.08 1.08 3.00|1.22 > 1.22 4.66|Rocket Lake 1.00 0.40 1.45|0.65 0.73 4.14|0.65 0.65 > 7.35|0.78 0.86 9.44|Skymont 0.67 0.67 3.08|0.83 0.83 6.73|1.03 1.03 > 10.63|1.23 1.23 13.77|Gracemont > > On Zen5, even a 5-register cycle can be profitably implemented with > xchg when you have no spare register, a 4-register cycle even when you > have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 (for > reg) are borderline cases, and for shorter cycles xchg is better. > > On Intel processors, xchg is so slow that it never is profitable to > use it. The slowness takes on extreme values in the E-cores, with > slowdowns by a factor >10 for cyc4 and cyc5. > > - anton Interesting. Thank you.
Re: Performance of xchg reg,reg
Author: MitchAlsup
Date: Wed, 14 Oct 2026 22:35
Date: Wed, 14 Oct 2026 22:35
38 lines
2701 bytes
2701 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > /ERROR "unexpected byte sequence starting at index 607: '\xE2'" while decoding/: > > Lawrence =?iso-8859-13?q?D=FFOliveiro?= <ldo@nz.invalid> writes: > >On Tue, 13 Oct 2026 08:43:47 GMT, Anton Ertl wrote: > > > >> On Zen5, even a 5-register cycle can be profitably implemented with > >> xchg when you have no spare register, a 4-register cycle even when > >> you have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 > >> (for reg) are borderline cases, and for shorter cycles xchg is > >> better. > >> > >> On Intel processors, xchg is so slow that it never is profitable to > >> use it. The slowness takes on extreme values in the E-cores, with > >> slowdowns by a factor >10 for cyc4 and cyc5. > > > >You’d think both companies would be running similar kinds of > >instruction workloads to find the things worth optimizing in their CPU > >architectures. But perhaps not ... > > Or maybe, given the structure of the renamer, an AMD engineer saw a > relatively easy way to implement xchg efficiently, and did it, without > first looking at how often xchg occurs in benchmark programs. And at > Intel, either no engineer thought about this, or the structure of the > renamer makes an efficient implementation much harder. Unlike Mainframes where sort is bread-and-butter operation, on PCs (and similar) it is not. > It makes me wonder, however, why cyc2xchg is more expensive than > cyc2reg, though. An easy implementation should be to decode xchg reg, > reg into three mov reg, reg (i.e., what cyc2reg implements), where the > spare register is a non-architectural register. But on every measured > Intel microarchitecture cyc2xchg is substantially slower than cyc2reg. > > - anton
Re: Performance of xchg reg,reg
Author: Lawrence D’Oliveiro
Date: Wed, 14 Oct 2026 23:07
Date: Wed, 14 Oct 2026 23:07
10 lines
1613 bytes
1613 bytes
On Wed, 14 Oct 2026 22:35:53 GMT, MitchAlsup wrote: > Unlike Mainframes where sort is bread-and-butter operation, on PCs > (and similar) it is not. You think Timsort might run faster on a mainframe than a PC? It was invented by one of the Python developers. <https://en.wikipedia.org/wiki/Timsort>
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Thu, 15 Oct 2026 06:05
Date: Thu, 15 Oct 2026 06:05
10 lines
1602 bytes
1602 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes: >Unlike Mainframes where sort is bread-and-butter operation, on PCs >(and similar) it is not. And the relevance of that for "Performance of xchg reg,reg" is? - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Thu, 15 Oct 2026 07:06
Date: Thu, 15 Oct 2026 07:06
104 lines
4278 bytes
4278 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>>Unlike Mainframes where sort is bread-and-butter operation, on PCs
>>(and similar) it is not.
>
>And the relevance of that for "Performance of xchg reg,reg" is?
To give you an idea what xchg reg,reg is good for:
long foo(long a, long b);
long bar(long a, long b)
{
return foo(b,a);
}
Both gcc-16 and clang-19 (even with -mtune=znver5) compile this to:
0000000000000000 <bar>:
0: 48 89 f8 mov %rdi,%rax
3: 48 89 f7 mov %rsi,%rdi
6: 48 89 c6 mov %rax,%rsi
9: e9 00 00 00 00 jmp e <bar+0xe> #actually jmp foo
This could be replaced with
xchg %rdi,%rsi
jmp foo
It's less obvious for
long foo(long a, long b);
long bar(long a, long b, long c, long d)
{
return foo(b-c,a-d);
}
The gcc-12 (different machines) output even with -mtune=znver3 is:
0000000000000000 <bar>:
0: 48 89 f8 mov %rdi,%rax
3: 48 29 d6 sub %rdx,%rsi
6: 48 29 c8 sub %rcx,%rax
9: 48 89 f7 mov %rsi,%rdi
c: 48 89 c6 mov %rax,%rsi
f: e9 00 00 00 00 jmp 14 <bar+0x14>
and the clang-14 output is similar. This could be shortened to:
sub %rdx,%rsi
sub %rcx,%rdi
xchg %rdi,%rsi
jmp foo
It also happens with control flow (that's what the SSA destruction on
my web page refers to):
long bar(long a, long b, long c, long d)
{
if (a<b) {
long tmp=c;
c=d-a;
d=tmp-b;
}
return 2*c+d;
}
compiles (gcc-12 -O3 -mtune=znver3) to:
0000000000000000 <bar>:
0: 48 39 f7 cmp %rsi,%rdi
3: 7d 0f jge 14 <bar+0x14>
5: 48 89 c8 mov %rcx,%rax
8: 48 29 f2 sub %rsi,%rdx
b: 48 29 f8 sub %rdi,%rax
e: 48 89 d1 mov %rdx,%rcx
11: 48 89 c2 mov %rax,%rdx
14: 48 8d 04 51 lea (%rcx,%rdx,2),%rax
18: c3 ret
and could be:
cmp %rsi,%rdi
jge L1
sub %rsi,%rdx
sub %rdi,%rcx
xchg %rdx,%rcx
L1: lea (%rcx,%rdx,2),%rax
ret
Given that most operations on AMD64 are performed with two-address
(for binary operations) or one-address (for unary operations), the
compiler can usually not use target preferencing to get data into the
required registers with little or no extra move instructions. So the
need to rearrange registers at some point is especially relevant on
this architecture; and it has the xchg instruction to make the
rearrangements shorter, and, on AMD processors, faster.
- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Chris M. Thomasson
Date: Thu, 15 Oct 2026 21:21
Date: Thu, 15 Oct 2026 21:21
110 lines
7015 bytes
7015 bytes
On 10/13/2026 1:43 AM, Anton Ertl wrote: > Microbenchmarks (and this text in HTML form) on > <http://www.complang.tuwien.ac.at/anton/xchg/>. > > In compilers, in particular in SSA destruction, or, more generally, > register allocation, you sometimes have situations where you want to > swap the contents of registers, e.g., when r1 contains a and r2 > contains b on one path, while r1 contains b and r2 contains a on the > other path, and both paths join. This can be achieved with 3 mov > instructions, but requires an extra register. The AMD64 instruction > set contains the instruction xchg, which swaps the registers without > extra register. > > More generally, there may be more than two registers whose values have > to be moved in a cyclic way. This also requires an extra register and > (for an n-register cycle) n+1 mov instructions if performed using mov > instructions, or it can be achieved with n-1 xchg instructions. > > Mov instructions are supported very well in recent CPU cores, with the > cores usually being able to perform as many independent (and often > even dependent) moves as the register renamer accepts instructions > (e.g., if the register renamer accepts 8 instructions per cycle, 8 > moves per cycle can be performed, usually even dependent moves). > > But how do xchg instructions perform? Should a register allocator use > them where they are convenient, or prefer to use mov instructions and > an extra register? > > <http://www.complang.tuwien.ac.at/anton/xchg/> contains > microbenchmarks and this posting presents results for them on various > cores from Intel and AMD. The first set of microbenchmarks perform 1G > xchg instructions in a loop containing 28 xchg instructions (plus two > instructions for the loop). > > One (independent) contains a sequence of 7 xchg instructions that are > fully independent of each other, all accessing different registers, > followed by the same sequence another three times. So the xchg > instructions are as independent as possible with 14 registers > involved. > > The other (dependent) contains a sequence of 14 xchg instructions > where each subsequent instruction uses one register of the previous > instruction, implementing a 15-register cycle. This sequence is > followed by the reversal of this sequence, resulting in performing the > 15-register cycle in the other direction. > > Here are the results in Gcycles on recent microarchitectures, i.e., a > result of 0.25 means that the microarchitecture performs 4 xchg > instructions per cycle. Smaller is better. > > indep dep core family CPU > 0.26 0.26 Zen5 AMD Ryzen AI Pro 350 > 0.35 0.35 Zen4 AMD Ryzen 8700G > 0.35 0.35 Zen3 AMD Ryzen 5800X > 0.61 1.50 Lion Cove Intel P Core Ultra 225 > 1.00 1.50 Golden Cove Intel P Core i3-1315U > 0.82 1.50 Rocket Lake Intel P Xeon E-2388G > 2.07 2.05 Skymont Intel E Core Ultra 225 > 3.45 3.45 Gracemont Intel E Core i3-1315U > > Given the results of the dependent benchmark, it looks like the AMD > processors perform xchg in the register renamer, consuming the > resources of two ordinary instructions there (probably because two > result registers are involved). > > On Intel microarchitectures, whether P-cores or E-cores, but > especially the latter, xchg seems to have gotten little love. > > Let's see how this affects cyclic moves between registers. The > following microbenchmarks perform cyclic moves through 2-5 registers > (cyc2-cyc5) in three different ways: > > mem Store the first register to memory, then perform n-1 moves among > registers, then load the memory location into the final register. > reg Move the first register into a spare register, then perform n-1 > moves among the cycled registers, then move the spare register > into the final register. > xchg Use n-1 xchg instructions. > > The benchmark loop contains 12 cycles, and cycles involving > non-overlapping sets of registers are benchmarked (6 sets for cyc2, 4 > for cyc3, 3 for cyc4, 2 for cyc5). At least three different memory > locations are used for holding the value of the first register in > memory, but the same register is used as the spare register. Here are > the results (execution cycles per cyclic move (i.e., n+1 movs in case > of reg, n-1 xchgs in case of xchg). > > cyc2 | cyc3 | cyc4 | cyc5 |core > mem reg xchg|mem reg xchg|mem reg xchg|mem reg xchg| > 0.50 0.50 0.27|0.67 0.58 0.52|0.83 0.83 0.77|1.08 0.83 1.02|Zen5 > 1.00 0.51 0.36|1.00 0.68 0.69|1.00 0.85 1.03|1.01 1.01 1.36|Zen4 > 1.00 0.51 0.42|1.00 0.68 0.70|1.00 0.92 1.03|1.02 1.01 1.36|Zen3 > 0.50 0.40 0.68|0.54 0.56 1.28|0.67 0.66 1.97|0.81 0.81 4.21|Lion Cove > 0.61 0.53 1.00|0.70 0.70 2.00|0.89 0.90 3.00|1.06 1.08 4.41|Golden Cove > 0.82 0.67 1.00|0.87 0.83 2.00|1.08 1.08 3.00|1.22 1.22 4.66|Rocket Lake > 1.00 0.40 1.45|0.65 0.73 4.14|0.65 0.65 7.35|0.78 0.86 9.44|Skymont > 0.67 0.67 3.08|0.83 0.83 6.73|1.03 1.03 10.63|1.23 1.23 13.77|Gracemont > > On Zen5, even a 5-register cycle can be profitably implemented with > xchg when you have no spare register, a 4-register cycle even when you > have a spare register. For Zen4 and Zen3 cyc4 (for mem) and cyc3 (for > reg) are borderline cases, and for shorter cycles xchg is better. > > On Intel processors, xchg is so slow that it never is profitable to > use it. The slowness takes on extreme values in the E-cores, with > slowdowns by a factor >10 for cyc4 and cyc5. > > - anton Well, for some reason the LOCK prefix is implied for XCHG.
Re: Performance of xchg reg,reg
Author: Lawrence D’Oliveiro
Date: Thu, 15 Oct 2026 21:42
Date: Thu, 15 Oct 2026 21:42
6 lines
1444 bytes
1444 bytes
On Thu, 15 Oct 2026 14:21:07 -0700, Chris M. Thomasson wrote: > Well, for some reason the LOCK prefix is implied for XCHG. I can imagine that being useful when accessing memory, but not just for exchanging registers ...
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Fri, 16 Oct 2026 05:50
Date: Fri, 16 Oct 2026 05:50
29 lines
2467 bytes
2467 bytes
"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, but asserting the LOCK# signal would make no difference, because: "the LOCK# signal ensures that the processor has exclusive use of any shared memory while the signal is asserted" and "the processor’s cache coherency mechanism ensures that the operation is carried out atomically with regards to memory", but "xchg reg, reg" does not use memory at all, and other processors have no access to a processor's registers. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Michael S
Date: Fri, 16 Oct 2026 08:34
Date: Fri, 16 Oct 2026 08:34
81 lines
5057 bytes
5057 bytes
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. > but asserting the > LOCK# signal would make no difference, because: "the LOCK# signal > ensures that the processor has exclusive use of any shared memory > while the signal is asserted" and "the processor’s cache coherency > mechanism ensures that the operation is carried out atomically with > regards to memory", but "xchg reg, reg" does not use memory at all, > and other processors have no access to a processor's registers. > > - anton Intel text (325383-060US September 2016): Exchanges the contents of the destination (first) and source (second) operands. The operands can be two general- purpose registers or a register and a memory location. 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. (See the LOCK prefix description in this chapter for more information on the locking protocol.) This instruction is useful for implementing semaphores or similar data structures for process synchronization. (See “Bus Locking” in Chapter 8 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A, for more information on bus locking.) The XCHG instruction can also be used instead of the BSWAP instruction for 16-bit operands. In 64-bit mode, the instruction’s default operation size is 32 bits. Using a REX prefix in the form of REX.R permits access to additional registers (R8-R15). Using a REX prefix in the form of REX.W promotes operation to 64 bits. See the summary chart at the beginning of this section for encoding data and limits. NOTE XCHG (E)AX, (E)AX (encoded instruction byte is 90H) is an alias for NOP regardless of data size prefixes, including REX.W. AMD Text (24594 Rev. 3.04 April 2003): Exchanges the contents of the two operands. The operands can be two general- purpose registers or a register and a memory location. If either operand references memory, the processor locks automatically, whether or not the LOCK prefix is used and independently of the value of IOPL. For details about the LOCK prefix, see “Lock Prefix” on page 10. The x86 architecture commonly uses the XCHG EAX, EAX instruction (opcode 90h) as a one-byte NOP. In 64-bit mode, the processor treats opcode 90h as a true NOP only if it would exchange rAX with itself. Without this special handling, the instruction would zero-extend the upper 32 bits of RAX, and thus it would not be a true no- operation. Opcode 90h can still be used to exchange rAX and r8 if the appropriate REX prefix is used. This special handling does not apply to the two-byte ModRM form of the XCHG instruction.
Re: Performance of xchg reg,reg
Author: Terje Mathisen
Date: Fri, 16 Oct 2026 12:45
Date: Fri, 16 Oct 2026 12:45
18 lines
1880 bytes
1880 bytes
Lawrence D’Oliveiro wrote: > On Thu, 15 Oct 2026 14:21:07 -0700, Chris M. Thomasson wrote: > >> Well, for some reason the LOCK prefix is implied for XCHG. > > I can imagine that being useful when accessing memory, but not just > for exchanging registers ... The implied LOCK only happens with a memory operand specified in the r/m byte. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Re: Performance of xchg reg,reg
Author: EricP
Date: Fri, 16 Oct 2026 13:47
Date: Fri, 16 Oct 2026 13:47
38 lines
2100 bytes
2100 bytes
On 2026-Oct-15 03:06, Anton Ertl wrote:
> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>>> Unlike Mainframes where sort is bread-and-butter operation, on PCs
>>> (and similar) it is not.
>>
>> And the relevance of that for "Performance of xchg reg,reg" is?
>
> To give you an idea what xchg reg,reg is good for:
>
> long foo(long a, long b);
>
> long bar(long a, long b)
> {
> return foo(b,a);
> }
>
> Both gcc-16 and clang-19 (even with -mtune=znver5) compile this to:
>
> 0000000000000000 <bar>:
> 0: 48 89 f8 mov %rdi,%rax
> 3: 48 89 f7 mov %rsi,%rdi
> 6: 48 89 c6 mov %rax,%rsi
> 9: e9 00 00 00 00 jmp e <bar+0xe> #actually jmp foo
>
> This could be replaced with
>
> xchg %rdi,%rsi
> jmp foo
There is also the xor swap which doesn't need an extra register
xor rax,rbx
xor rbx,rax
xor rax,rbx
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Fri, 16 Oct 2026 16:04
Date: Fri, 16 Oct 2026 16:04
35 lines
2737 bytes
2737 bytes
EricP <ThatWouldBeTelling@thevillage.com> writes: >On 2026-Oct-15 03:06, Anton Ertl wrote: >> Both gcc-16 and clang-19 (even with -mtune=znver5) compile this to: >> >> 0000000000000000 <bar>: >> 0: 48 89 f8 mov %rdi,%rax >> 3: 48 89 f7 mov %rsi,%rdi >> 6: 48 89 c6 mov %rax,%rsi >> 9: e9 00 00 00 00 jmp e <bar+0xe> #actually jmp foo >> >> This could be replaced with >> >> xchg %rdi,%rsi >> jmp foo > >There is also the xor swap which doesn't need an extra register > > xor rax,rbx > xor rbx,rax > xor rax,rbx That has a latency of 3 cycles and (probably less of a problem) consumes 3 ALU slots. Maybe Gracemont uses this internally, the number of cycles for the xchg variants of the cycN benchmarks would be compatible with that explanation. With 0-cycle store-to-load forwarding in recent processors, storing to memory and loading from memory is preferable on Intel processors if no spare register is available; and on AMD processors, xchg for cyc2 and cyc3, and sometimes on cyc4, cyc5. My guess is that using xor would lead to huge slowdowns, even compared to storing the value in memory. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Fri, 16 Oct 2026 18:23
Date: Fri, 16 Oct 2026 18:23
64 lines
3754 bytes
3754 bytes
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? Personally, I suspect it's the one in the `XCHG` description. Context is important, and the full paragraph that talks about `XCHG` includes more: |The LOCK prefix can be prepended only to the following |instructions and only to those forms of the instructions where |the destination operand is a memory operand: `ADD`, `ADC`, |`AND`, `BTC`, `BTR`, `BTS`, `CMPXCHG`, `CMPXCH8B`, |`CMPXCHG16B`, `DEC`, `INC`, `NEG`, `NOT`, `OR`, `SBB`, `SUB`, |`XOR`, `XADD`, and `XCHG`. If the `LOCK` prefix is used with |one of these instructions and the source operand is a memory |operand, an undefined opcode exception (`#UD`) may be |generated. An undefined opcode exception will also be |generated if the `LOCK` prefix is used with any instruction not |in the above list. The `XCHG` instruction always asserts the |`LOCK#` signal regardless of the presence or absence of the |`LOCK` prefix. So while the last sentence seems independent of operands, its presence in this paragraph leads me to believe that it only applies in the context of a destination operand that is a memory operand. If not, however, an interesting question is, does an `XCHG` that only specifies register operands affect memory ordering? Looking at secs 11.1 and 11.2, I believe the answer is no: the description of locked instructions explicitly only applies to those with memory operands. - Dan C.
Re: Performance of xchg reg,reg
Author: Scott Lurndal
Date: Fri, 16 Oct 2026 20:48
Date: Fri, 16 Oct 2026 20:48
43 lines
2913 bytes
2913 bytes
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?
Re: Performance of xchg reg,reg
Author: 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.
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sat, 17 Oct 2026 02:50
Date: Sat, 17 Oct 2026 02:50
43 lines
2884 bytes
2884 bytes
In article <11au1vu$3ks5m$2@dont-email.me>, Thomas Koenig <tkoenig@netcologne.de> wrote: >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >> 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. Using the `LOCK` prefix without a destination operand that is a memory operand is an error, and the SDM is quite explicit about that. In fact, it says that an explicit `LOCK` prefix on an `XCHG` a register destination operand is _not_ permitted and "may" generate an `#UD` exception. But sadly, when talking about an `XCHG` _without_ an explicit operand, it is left ambiguous; all it says is that, "The `XCHG` instruction always asserts the `LOCK#` signal regardless of the presence or absence of the `LOCK` prefix." Devoid of the rest of the surrounding context, one could interpret that to mean that an `XCHG` _always_ forces a bus lock, irrespective of the type of its desintation operand. Why? I have no idea; as I said, I'm in the camp of going with what the description of `XCHG` itself says, and not the what the description of the `LOCK` prefix says: that sentence is _not_ devoid of context, and besides, there has not been a physical `LOCK#` signal coming out of an Intel Xeon package since Nehalem. I suppose one could imagine it forcing a poor-man's barrier to prevent later loads from being reorderd before it (loads can't migrate before locked instructions). But we've got explicit fence instructions for that, so I don't buy it. I suspect that sentence is just poorly phrased. - Dan C.
Re: Performance of xchg reg,reg
Author: Lawrence D’Oliveiro
Date: Sat, 17 Oct 2026 05:46
Date: Sat, 17 Oct 2026 05:46
10 lines
1692 bytes
1692 bytes
On Fri, 16 Oct 2026 09:47:39 -0400, EricP wrote: > There is also the xor swap which doesn't need an extra register > > xor rax,rbx > xor rbx,rax > xor rax,rbx It's a clever trick from the old days, but not really performance-oriented ...
Re: Performance of xchg reg,reg
Author: Thomas Koenig
Date: Sat, 17 Oct 2026 09:48
Date: Sat, 17 Oct 2026 09:48
56 lines
3715 bytes
3715 bytes
Scott Lurndal <scott@slp53.sl.home> schrieb: > 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'? Yes. I canceled the article, but either was too late or some server does not honor the cancel. > 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? Exactly. -- This USENET posting was made without artificial intelligence, artificial impertinence, artificial arrogance, artificial stupidity, artificial flavorings or artificial colorants.
Re: Performance of xchg reg,reg
Author: MitchAlsup
Date: Sat, 17 Oct 2026 17:11
Date: Sat, 17 Oct 2026 17:11
57 lines
3512 bytes
3512 bytes
cross@spitfire.i.gajendra.net (Dan Cross) posted: > In article <11au1vu$3ks5m$2@dont-email.me>, > Thomas Koenig <tkoenig@netcologne.de> wrote: > >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: > >> 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. > > Using the `LOCK` prefix without a destination operand that is a > memory operand is an error, and the SDM is quite explicit about > that. In fact, it says that an explicit `LOCK` prefix on an > `XCHG` a register destination operand is _not_ permitted and > "may" generate an `#UD` exception. > > But sadly, when talking about an `XCHG` _without_ an explicit > operand, it is left ambiguous; all it says is that, "The `XCHG` > instruction always asserts the `LOCK#` signal regardless of the > presence or absence of the `LOCK` prefix." Devoid of the rest > of the surrounding context, one could interpret that to mean > that an `XCHG` _always_ forces a bus lock, irrespective of the > type of its desintation operand. > > Why? Older machines performed the read, returned the read data to a core and then sent the data to be written back to memory; where the time between the read and the write was unknowable. Some newer machines take the data to be written and package it up in an interconnect transaction so the read and write happen on back-to-back cycles with no possibility of interference. These are ATOMIC even without any bus-locking. The data read out is returned to the core. > I have no idea; as I said, I'm in the camp of going with > what the description of `XCHG` itself says, and not the what the > description of the `LOCK` prefix says: that sentence is _not_ > devoid of context, and besides, there has not been a physical > `LOCK#` signal coming out of an Intel Xeon package since > Nehalem. > > I suppose one could imagine it forcing a poor-man's barrier to > prevent later loads from being reorderd before it (loads can't > migrate before locked instructions). But we've got explicit > fence instructions for that, so I don't buy it. > > I suspect that sentence is just poorly phrased. > > - Dan C. >
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sat, 17 Oct 2026 17:31
Date: Sat, 17 Oct 2026 17:31
75 lines
4457 bytes
4457 bytes
In article <1792257076-5857@newsgrouper.org>, MitchAlsup <user5857@newsgrouper.org.invalid> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) posted: > >> In article <11au1vu$3ks5m$2@dont-email.me>, >> Thomas Koenig <tkoenig@netcologne.de> wrote: >> >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >> >> 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. >> >> Using the `LOCK` prefix without a destination operand that is a >> memory operand is an error, and the SDM is quite explicit about >> that. In fact, it says that an explicit `LOCK` prefix on an >> `XCHG` a register destination operand is _not_ permitted and >> "may" generate an `#UD` exception. >> >> But sadly, when talking about an `XCHG` _without_ an explicit >> operand, it is left ambiguous; all it says is that, "The `XCHG` >> instruction always asserts the `LOCK#` signal regardless of the >> presence or absence of the `LOCK` prefix." Devoid of the rest >> of the surrounding context, one could interpret that to mean >> that an `XCHG` _always_ forces a bus lock, irrespective of the >> type of its desintation operand. >> >> Why? > >Older machines performed the read, returned the read data to >a core and then sent the data to be written back to memory; >where the time between the read and the write was unknowable. > >Some newer machines take the data to be written and package it >up in an interconnect transaction so the read and write happen >on back-to-back cycles with no possibility of interference. >These are ATOMIC even without any bus-locking. The data read >out is returned to the core. No, I get that; I mean in the case where both operands to `XCHG` are _register_ operands. What I'm saying is that, as written, the bits in the `LOCK` description above _could_ be interpreted to mean that _any_ `XCHG` will always assert the `LOCK#` signal, including an `XCHG` that does not touch memory at all. The "Why?" was rhetorical, as in, "Why would the system do that?" For example, if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? Truly I don't know, and I was just speculating. But as I said, I don't think that is what the sentence in question is _actually_ saying, and I really don't think that `xchg %ecx, %eax` _actually_ asserts `LOCK#` (or, these days, has anything to do with cache coherency and memory ordering). >> I have no idea; as I said, I'm in the camp of going with >> what the description of `XCHG` itself says, and not the what the >> description of the `LOCK` prefix says: that sentence is _not_ >> devoid of context, and besides, there has not been a physical >> `LOCK#` signal coming out of an Intel Xeon package since >> Nehalem. >> >> I suppose one could imagine it forcing a poor-man's barrier to >> prevent later loads from being reorderd before it (loads can't >> migrate before locked instructions). But we've got explicit >> fence instructions for that, so I don't buy it. >> >> I suspect that sentence is just poorly phrased. - Dan C.
Re: Performance of xchg reg,reg
Author: MitchAlsup
Date: Sat, 17 Oct 2026 19:39
Date: Sat, 17 Oct 2026 19:39
86 lines
5140 bytes
5140 bytes
cross@spitfire.i.gajendra.net (Dan Cross) posted: > In article <1792257076-5857@newsgrouper.org>, > MitchAlsup <user5857@newsgrouper.org.invalid> wrote: > >cross@spitfire.i.gajendra.net (Dan Cross) posted: > > > >> In article <11au1vu$3ks5m$2@dont-email.me>, > >> Thomas Koenig <tkoenig@netcologne.de> wrote: > >> >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: > >> >> 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. > >> > >> Using the `LOCK` prefix without a destination operand that is a > >> memory operand is an error, and the SDM is quite explicit about > >> that. In fact, it says that an explicit `LOCK` prefix on an > >> `XCHG` a register destination operand is _not_ permitted and > >> "may" generate an `#UD` exception. > >> > >> But sadly, when talking about an `XCHG` _without_ an explicit > >> operand, it is left ambiguous; all it says is that, "The `XCHG` > >> instruction always asserts the `LOCK#` signal regardless of the > >> presence or absence of the `LOCK` prefix." Devoid of the rest > >> of the surrounding context, one could interpret that to mean > >> that an `XCHG` _always_ forces a bus lock, irrespective of the > >> type of its desintation operand. > >> > >> Why? > > > >Older machines performed the read, returned the read data to > >a core and then sent the data to be written back to memory; > >where the time between the read and the write was unknowable. > > > >Some newer machines take the data to be written and package it > >up in an interconnect transaction so the read and write happen > >on back-to-back cycles with no possibility of interference. > >These are ATOMIC even without any bus-locking. The data read > >out is returned to the core. > > No, I get that; I mean in the case where both operands to `XCHG` > are _register_ operands. > > What I'm saying is that, as written, the bits in the `LOCK` > description above _could_ be interpreted to mean that _any_ > `XCHG` will always assert the `LOCK#` signal, including an > `XCHG` that does not touch memory at all. The "Why?" was > rhetorical, as in, "Why would the system do that?" For example, > if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? > Truly I don't know, and I was just speculating. Writing high quality documentation is fundamentally hard. It took me nearly 40 years of doing so to get to the point a mischievous engineer could NOT misunderstand what I had written. It is harder wo write high quality documentation than perform circuit design. What they wrote may have well been exactly what the HW did back then but somewhere between then and now architects and designers figured out other ways and nobody went back to fully scrub the documents. > But as I said, I don't think that is what the sentence in > question is _actually_ saying, and I really don't think that > `xchg %ecx, %eax` _actually_ asserts `LOCK#` (or, these days, > has anything to do with cache coherency and memory ordering). > > >> I have no idea; as I said, I'm in the camp of going with > >> what the description of `XCHG` itself says, and not the what the > >> description of the `LOCK` prefix says: that sentence is _not_ > >> devoid of context, and besides, there has not been a physical > >> `LOCK#` signal coming out of an Intel Xeon package since > >> Nehalem. > >> > >> I suppose one could imagine it forcing a poor-man's barrier to > >> prevent later loads from being reorderd before it (loads can't > >> migrate before locked instructions). But we've got explicit > >> fence instructions for that, so I don't buy it. > >> > >> I suspect that sentence is just poorly phrased. > > - Dan C. >
Re: Performance of xchg reg,reg
Author: Michael S
Date: Sat, 17 Oct 2026 20:20
Date: Sat, 17 Oct 2026 20:20
97 lines
5913 bytes
5913 bytes
On Sat, 17 Oct 2026 19:39:42 GMT MitchAlsup <user5857@newsgrouper.org.invalid> wrote: > cross@spitfire.i.gajendra.net (Dan Cross) posted: > > > In article <1792257076-5857@newsgrouper.org>, > > MitchAlsup <user5857@newsgrouper.org.invalid> wrote: > > >cross@spitfire.i.gajendra.net (Dan Cross) posted: > > > > > >> In article <11au1vu$3ks5m$2@dont-email.me>, > > >> Thomas Koenig <tkoenig@netcologne.de> wrote: > > >> >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: > > >> >> 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. > > >> > > >> Using the `LOCK` prefix without a destination operand that is a > > >> memory operand is an error, and the SDM is quite explicit about > > >> that. In fact, it says that an explicit `LOCK` prefix on an > > >> `XCHG` a register destination operand is _not_ permitted and > > >> "may" generate an `#UD` exception. > > >> > > >> But sadly, when talking about an `XCHG` _without_ an explicit > > >> operand, it is left ambiguous; all it says is that, "The `XCHG` > > >> instruction always asserts the `LOCK#` signal regardless of the > > >> presence or absence of the `LOCK` prefix." Devoid of the rest > > >> of the surrounding context, one could interpret that to mean > > >> that an `XCHG` _always_ forces a bus lock, irrespective of the > > >> type of its desintation operand. > > >> > > >> Why? > > > > > >Older machines performed the read, returned the read data to > > >a core and then sent the data to be written back to memory; > > >where the time between the read and the write was unknowable. > > > > > >Some newer machines take the data to be written and package it > > >up in an interconnect transaction so the read and write happen > > >on back-to-back cycles with no possibility of interference. > > >These are ATOMIC even without any bus-locking. The data read > > >out is returned to the core. > > > > No, I get that; I mean in the case where both operands to `XCHG` > > are _register_ operands. > > > > What I'm saying is that, as written, the bits in the `LOCK` > > description above _could_ be interpreted to mean that _any_ > > `XCHG` will always assert the `LOCK#` signal, including an > > `XCHG` that does not touch memory at all. The "Why?" was > > rhetorical, as in, "Why would the system do that?" For example, > > if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? > > Truly I don't know, and I was just speculating. > > Writing high quality documentation is fundamentally hard. It took > me nearly 40 years of doing so to get to the point a mischievous > engineer could NOT misunderstand what I had written. How do you know? Nature is inventive. > It is harder > wo write high quality documentation than perform circuit design. > > What they wrote may have well been exactly what the HW did back then > but somewhere between then and now architects and designers figured > out other ways and nobody went back to fully scrub the documents. > > > But as I said, I don't think that is what the sentence in > > question is _actually_ saying, and I really don't think that > > `xchg %ecx, %eax` _actually_ asserts `LOCK#` (or, these days, > > has anything to do with cache coherency and memory ordering). > > > > >> I have no idea; as I said, I'm in the camp of going with > > >> what the description of `XCHG` itself says, and not the what the > > >> description of the `LOCK` prefix says: that sentence is _not_ > > >> devoid of context, and besides, there has not been a physical > > >> `LOCK#` signal coming out of an Intel Xeon package since > > >> Nehalem. > > >> > > >> I suppose one could imagine it forcing a poor-man's barrier to > > >> prevent later loads from being reorderd before it (loads can't > > >> migrate before locked instructions). But we've got explicit > > >> fence instructions for that, so I don't buy it. > > >> > > >> I suspect that sentence is just poorly phrased. > > > > - Dan C. > >
Re: Documentation (was Re: Performance of xchg reg,reg)
Author: Lawrence D’Oliveiro
Date: Sat, 17 Oct 2026 20:54
Date: Sat, 17 Oct 2026 20:54
17 lines
2110 bytes
2110 bytes
On Sat, 17 Oct 2026 19:39:42 GMT, MitchAlsup wrote: > Writing high quality documentation is fundamentally hard. Isn’t that why formal specification languages were invented? For some reason they never caught on. For a classic example, look at the Algol-68 language spec. Yet many people threw up their hands in horror at how it was written. > It took me nearly 40 years of doing so to get to the point a > mischievous engineer could NOT misunderstand what I had written. Presumably you include lots of examples? I remember something Jerry Pournelle said, critiquing the product documentation he had to read for his columns in “BYTE” magazine, back in the day: “You can never have too many examples”.
Re: Performance of xchg reg,reg
Author: George Neuner
Date: Sun, 18 Oct 2026 12:59
Date: Sun, 18 Oct 2026 12:59
19 lines
2116 bytes
2116 bytes
On Sat, 17 Oct 2026 19:39:42 GMT, MitchAlsup <user5857@newsgrouper.org.invalid> wrote: >Writing high quality documentation is fundamentally hard. It took >me nearly 40 years of doing so to get to the point a mischievous >engineer could NOT misunderstand what I had written. It is harder >wo write high quality documentation than perform circuit design. +1 >What they wrote may have well been exactly what the HW did back then >but somewhere between then and now architects and designers figured >out other ways and nobody went back to fully scrub the documents. Also remember that the documents are describing the operations of a family of implementations. However unlikely, there may be members that still have to play by those old rules.
Re: Documentation (was Re: Performance of xchg reg,reg)
Author: George Neuner
Date: Sun, 18 Oct 2026 13:45
Date: Sun, 18 Oct 2026 13:45
40 lines
2954 bytes
2954 bytes
On Sat, 17 Oct 2026 20:54:44 -0000 (UTC), Lawrence D´Oliveiro <ldo@nz.invalid> wrote: >On Sat, 17 Oct 2026 19:39:42 GMT, MitchAlsup wrote: > >> Writing high quality documentation is fundamentally hard. > >Isn’t that why formal specification languages were invented? For some >reason they never caught on. > >For a classic example, look at the Algol-68 language spec. Yet many >people threw up their hands in horror at how it was written. The problem with the original report was that they attempted with the grammar turn [at least some kinds of] semantic errors into syntax errors. Note that the *revised* report used a different grammar - something they called "NEST": still 2-level, still very complex [though less so than the original vW grammar], but they did not try to encode semantics directly, instead referencing discussions in accompanying text. I read both 68 reports - original and revised. Mind you it was long ago, but my recollection is that the revised report really wasn't any more understandable than the original ... just longer because the NEST formalism was somewhat less expressive than the vW. YMMV. >> It took me nearly 40 years of doing so to get to the point a >> mischievous engineer could NOT misunderstand what I had written. > >Presumably you include lots of examples? I remember something Jerry >Pournelle said, critiquing the product documentation he had to read >for his columns in “BYTE” magazine, back in the day: “You can never >have too many examples”. +1
Algol 68 (was: Documentation (was Re: Performance of xchg reg,reg))
Author: Anton Ertl
Date: Sun, 18 Oct 2026 14:08
Date: Sun, 18 Oct 2026 14:08
74 lines
5227 bytes
5227 bytes
George Neuner <gneuner2@comcast.net> writes: >On Sat, 17 Oct 2026 20:54:44 -0000 (UTC), Lawrence D´Oliveiro ><ldo@nz.invalid> wrote: >>Isn’t that why formal specification languages were invented? For some >>reason they never caught on. >> >>For a classic example, look at the Algol-68 language spec. Yet many >>people threw up their hands in horror at how it was written. > >The problem with the original report was that they attempted with the >grammar turn [at least some kinds of] semantic errors into syntax >errors. My take is that BNF was a big success in formalizing a part of a language. So what do you do when you are successful? You try to follow up on that. So they wanted to formalize more of the language, and go beyond the limits of BNF (context-free grammars) with Vw grammars. This was technically successful, but from a usability POV it was not. Nevertheless, there have been attempts to formally specify programming languages completely, or at least more of them than just the context-free grammar part of them. AFAIK all attempts to formally specify everything in the language definition have been unsuccessful: not only did the specification become too hard to understand for the programmers, but it seems to me that the programming language also became to hard to extend by its designers. A team at the IBM Lab in Vienna made a formal specification for PL/I; I attended a lecture by Heinz Zemanek (the team leader) where he mentioned something about an implementor disagreeing with the formal specification for recursion (disagreements between implementor and specification can also happen with specifications written in a natural language, so that may not say much). Anyway, <https://en.wikipedia.org/wiki/PL/I#Standardization> says "The standard is couched as a formal definition[17] using a "PL/I Machine"[21] to specify the semantics." This sounds like the operational semantics that was developed by the IBM Lab Vienna, so maybe the standardization committee picked up this work. But it seems that the first standard never had a followon; was the definition too unwieldy? Standard ML has been formally specified, and, given the audience of this language, if any programming language may have success going there, it should be a language like ML. There is a first revision (1990) and a second revision (1997), and SML seems to have reached the end of its evolution there. Other programming languages have formalized parts of the language beyond the context-free grammar, probably the parts that can be described well with a formalism, such as the type system. I guess some of these attempts have been successful, and others not so much. Concerning terms like "semantic errors" or "static semantics" they make it appear that there is a fundamental difference between syntax and "static semantics" (the part that is not context-free and that is checked by the compiler). It seems to me that that's just a matter of what is expressed in the BNF. E.g., in Algol 60 the computation of conditions for if-statements have special BNF rules, whereas some later programming languages have a boolean type and putting a non-boolean between IF and THEN produces a type error, not a syntax error. So I don't think that there is a fundamental difference, and if there was a formalism that is as easy to work with (both for programmers and for language designers) as BNF is, we would be using that formalism for that; and if that formalism also took over the role that BNF has now, we would be considering all the errors that this formalism finds to be in the same category. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Sun, 18 Oct 2026 15:08
Date: Sun, 18 Oct 2026 15:08
63 lines
3880 bytes
3880 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes: >cross@spitfire.i.gajendra.net (Dan Cross) posted: >> What I'm saying is that, as written, the bits in the `LOCK` >> description above _could_ be interpreted to mean that _any_ >> `XCHG` will always assert the `LOCK#` signal, including an >> `XCHG` that does not touch memory at all. The "Why?" was >> rhetorical, as in, "Why would the system do that?" For example, >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? >> Truly I don't know, and I was just speculating. > >Writing high quality documentation is fundamentally hard. I don't think that's the issue here. They just did not limit the sentence "The XCHG instruction always asserts the LOCK# signal regardless of the presence or absence of the LOCK prefix." to XCHG instructions with memory operands. That would be easy to fix, but they did not. And Chris M. Thomasson understood it as including XCHG reg, reg. My guess is that it is easy to fix in principle, but you have to deal with all kinds of red tape for such a fix, so unless there is good reason, you don't do it. This sentence already exists in my 1994-vintage Pentium Processor User's Manual Vol. 3. There it is preceeded by: |The LOCK prefix functions only with the following instructions: | |... |XCHG reg, mem |XCHG mem, reg |... With no occurence of XCHG reg, reg anywhere. But, OTOH, this is not really a contradiction to the sentence "The XCHG instruction always asserts the LOCK# signal regardless of the presence or absence of the LOCK prefix." >What they wrote may have well been exactly what the HW did back then >but somewhere between then and now architects and designers figured >out other ways and nobody went back to fully scrub the documents. Actually <https://www.felixcloutier.com/x86/lock> then says that "Beginning with the P6 family processors, when [...], the LOCK# signal is generally not asserted [...]" and still explains mechanism, finally saying "the operation is carried out atomically with regards to memory." If they wrote that as the description of the LOCK prefix, much of the rest could be deleted. Generally, an architecture manual should talk about architectural effects, not about asserted signals. The operational semantics given in <https://www.felixcloutier.com/x86/lock> is: | AssertLOCK#(DurationOfAccompaningInstruction); Oh boy. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Michael S
Date: Sun, 18 Oct 2026 15:43
Date: Sun, 18 Oct 2026 15:43
39 lines
3314 bytes
3314 bytes
On Sun, 18 Oct 2026 15:08:23 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> >cross@spitfire.i.gajendra.net (Dan Cross) posted:
> >> What I'm saying is that, as written, the bits in the `LOCK`
> >> description above _could_ be interpreted to mean that _any_
> >> `XCHG` will always assert the `LOCK#` signal, including an
> >> `XCHG` that does not touch memory at all. The "Why?" was
> >> rhetorical, as in, "Why would the system do that?" For example,
> >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`?
> >> Truly I don't know, and I was just speculating.
> >
> >Writing high quality documentation is fundamentally hard.
>
> I don't think that's the issue here. They just did not limit the
> sentence "The XCHG instruction always asserts the LOCK# signal
> regardless of the presence or absence of the LOCK prefix." to XCHG
> instructions with memory operands. That would be easy to fix, but
> they did not. And Chris M. Thomasson understood it as including
> XCHG reg, reg.
>
> My guess is that it is easy to fix in principle, but you have to deal
> with all kinds of red tape for such a fix, so unless there is good
> reason, you don't do it.
>
Frankly, I don't understand what both you and Dan Cross are talking
about.
I don't see how sentence from Intel's manual ("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")
or sentence from AMD's manual ("If either operand references memory,
the processor locks automatically, whether or not the LOCK prefix is
used and independently of the value of IOPL.") can be pssibly
misunderstood.
Re: Performance of xchg reg,reg
Author: EricP
Date: Sun, 18 Oct 2026 16:31
Date: Sun, 18 Oct 2026 16:31
102 lines
5717 bytes
5717 bytes
On 2026-Oct-17 13:31, Dan Cross wrote: > In article <1792257076-5857@newsgrouper.org>, > MitchAlsup <user5857@newsgrouper.org.invalid> wrote: >> cross@spitfire.i.gajendra.net (Dan Cross) posted: >> >>> In article <11au1vu$3ks5m$2@dont-email.me>, >>> Thomas Koenig <tkoenig@netcologne.de> wrote: >>>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >>>>> 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. >>> >>> Using the `LOCK` prefix without a destination operand that is a >>> memory operand is an error, and the SDM is quite explicit about >>> that. In fact, it says that an explicit `LOCK` prefix on an >>> `XCHG` a register destination operand is _not_ permitted and >>> "may" generate an `#UD` exception. >>> >>> But sadly, when talking about an `XCHG` _without_ an explicit >>> operand, it is left ambiguous; all it says is that, "The `XCHG` >>> instruction always asserts the `LOCK#` signal regardless of the >>> presence or absence of the `LOCK` prefix." Devoid of the rest >>> of the surrounding context, one could interpret that to mean >>> that an `XCHG` _always_ forces a bus lock, irrespective of the >>> type of its desintation operand. >>> >>> Why? >> >> Older machines performed the read, returned the read data to >> a core and then sent the data to be written back to memory; >> where the time between the read and the write was unknowable. >> >> Some newer machines take the data to be written and package it >> up in an interconnect transaction so the read and write happen >> on back-to-back cycles with no possibility of interference. >> These are ATOMIC even without any bus-locking. The data read >> out is returned to the core. > > No, I get that; I mean in the case where both operands to `XCHG` > are _register_ operands. > > What I'm saying is that, as written, the bits in the `LOCK` > description above _could_ be interpreted to mean that _any_ > `XCHG` will always assert the `LOCK#` signal, including an > `XCHG` that does not touch memory at all. The "Why?" was > rhetorical, as in, "Why would the system do that?" For example, > if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? > Truly I don't know, and I was just speculating. > > But as I said, I don't think that is what the sentence in > question is _actually_ saying, and I really don't think that > `xchg %ecx, %eax` _actually_ asserts `LOCK#` (or, these days, > has anything to do with cache coherency and memory ordering). > >>> I have no idea; as I said, I'm in the camp of going with >>> what the description of `XCHG` itself says, and not the what the >>> description of the `LOCK` prefix says: that sentence is _not_ >>> devoid of context, and besides, there has not been a physical >>> `LOCK#` signal coming out of an Intel Xeon package since >>> Nehalem. >>> >>> I suppose one could imagine it forcing a poor-man's barrier to >>> prevent later loads from being reorderd before it (loads can't >>> migrate before locked instructions). But we've got explicit >>> fence instructions for that, so I don't buy it. >>> >>> I suspect that sentence is just poorly phrased. > > - Dan C. > Intel Volume 3 System Programmers guide 10.1.2 Bus Locking 10.1.2.1 Automatic Locking is explicit: the automatic locking for XCHG applies to memory operands only. There are a bunch of other situations where automatic locking is asserted, such as setting the Accessed and Modified bit on PTE's. BUT it can also depend on PTE caching attributes and cpu model number. The other manuals probably didn't want to worry programmers pretty little heads with all those grotty details. e.g. 10.1.4 Effects of a LOCK Operation on Internal Processor Caches For the Intel486 and Pentium processors, the LOCK# signal is always asserted on the bus during a LOCK operation, even if the area of memory being locked is cached in the processor. For the P6 and more recent processor families, if the area of memory being locked during a LOCK operation is cached in the processor that is performing the LOCK operation as write-back memory and is completely contained in a cache line, the processor may not assert the LOCK# signal on the bus. ... blah blah blah
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Sun, 18 Oct 2026 16:57
Date: Sun, 18 Oct 2026 16:57
46 lines
3719 bytes
3719 bytes
Michael S <already5chosen@yahoo.com> writes:
>On Sun, 18 Oct 2026 15:08:23 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>> I don't think that's the issue here. They just did not limit the
>> sentence "The XCHG instruction always asserts the LOCK# signal
>> regardless of the presence or absence of the LOCK prefix." to XCHG
>> instructions with memory operands. That would be easy to fix, but
>> they did not. And Chris M. Thomasson understood it as including
>> XCHG reg, reg.
...
>Frankly, I don't understand what both you and Dan Cross are talking
>about.
>I don't see how sentence from Intel's manual ("If a memory operand is
>referenced, the processor=E2=80=99s 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")
>or sentence from AMD's manual ("If either operand references memory,
>the processor locks automatically, whether or not the LOCK prefix is
>used and independently of the value of IOPL.") can be pssibly
>misunderstood.
Chris M. Thomasson wrote in <11arg45$2p70p$1@dont-email.me>:
|Well, for some reason the LOCK prefix is implied for XCHG.
Given that the thread up to that point was aboyt XCHG reg, reg (and
according to the subject it still is), he seems to be an example that
what Intel wrote about XCHG and LOCK can be misunderstood. I looked
at the documentation and cited the relevant sentences in
<2026Oct16.075055@mips.complang.tuwien.ac.at>; most occurences clearly
refer only to XCHG where one operand is in memory. The exception is:
|The XCHG instruction always asserts the LOCK# signal regardless of the
|presence or absence of the LOCK prefix.
And actually all the stuff about XCHG with memory operands does not
contradict this sentence. It could be understood as meaning that
while all the other aspects of locking don't apply to "XCHG reg, reg",
the assertion of the LOCK# signal still has to occur, and that's why
Intel processors implement a slow XCHG reg, reg. I don't think that's
what Intel intends and implements, but that's what a reading of their
manual implies.
- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 17:09
Date: Sun, 18 Oct 2026 17:09
81 lines
4854 bytes
4854 bytes
In article <1792265982-5857@newsgrouper.org>, MitchAlsup <user5857@newsgrouper.org.invalid> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) posted: > >> In article <1792257076-5857@newsgrouper.org>, >> MitchAlsup <user5857@newsgrouper.org.invalid> wrote: >> >cross@spitfire.i.gajendra.net (Dan Cross) posted: >> > >> >> In article <11au1vu$3ks5m$2@dont-email.me>, >> >> Thomas Koenig <tkoenig@netcologne.de> wrote: >> >> >Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >> >> >> 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. >> >> >> >> Using the `LOCK` prefix without a destination operand that is a >> >> memory operand is an error, and the SDM is quite explicit about >> >> that. In fact, it says that an explicit `LOCK` prefix on an >> >> `XCHG` a register destination operand is _not_ permitted and >> >> "may" generate an `#UD` exception. >> >> >> >> But sadly, when talking about an `XCHG` _without_ an explicit >> >> operand, it is left ambiguous; all it says is that, "The `XCHG` >> >> instruction always asserts the `LOCK#` signal regardless of the >> >> presence or absence of the `LOCK` prefix." Devoid of the rest >> >> of the surrounding context, one could interpret that to mean >> >> that an `XCHG` _always_ forces a bus lock, irrespective of the >> >> type of its desintation operand. >> >> >> >> Why? >> > >> >Older machines performed the read, returned the read data to >> >a core and then sent the data to be written back to memory; >> >where the time between the read and the write was unknowable. >> > >> >Some newer machines take the data to be written and package it >> >up in an interconnect transaction so the read and write happen >> >on back-to-back cycles with no possibility of interference. >> >These are ATOMIC even without any bus-locking. The data read >> >out is returned to the core. >> >> No, I get that; I mean in the case where both operands to `XCHG` >> are _register_ operands. >> >> What I'm saying is that, as written, the bits in the `LOCK` >> description above _could_ be interpreted to mean that _any_ >> `XCHG` will always assert the `LOCK#` signal, including an >> `XCHG` that does not touch memory at all. The "Why?" was >> rhetorical, as in, "Why would the system do that?" For example, >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? >> Truly I don't know, and I was just speculating. > >Writing high quality documentation is fundamentally hard. It took >me nearly 40 years of doing so to get to the point a mischievous >engineer could NOT misunderstand what I had written. It is harder >wo write high quality documentation than perform circuit design. > >What they wrote may have well been exactly what the HW did back then >but somewhere between then and now architects and designers figured >out other ways and nobody went back to fully scrub the documents. Maybe? Probably? I don't know. *shrug* I'm not trying to impugn the tech writers at Intel, who I think generally do a great job. But this particular paragraph has obviously lead to some confusion. Again, I think that, at the end of the day, `xchg reg, reg` has no effect on memory ordering at all, and that sentence is meant to be read within the context of that paragraph; ie, the `LOCK` prefix is implied if it's not specifically given _and_ the destination is a memory operand. - Dan C.
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 17:40
Date: Sun, 18 Oct 2026 17:40
80 lines
4693 bytes
4693 bytes
In article <20261018184318.00000bee@yahoo.com>,
Michael S <already5chosen@yahoo.com> wrote:
>On Sun, 18 Oct 2026 15:08:23 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>
>> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>> >cross@spitfire.i.gajendra.net (Dan Cross) posted:
>> >> What I'm saying is that, as written, the bits in the `LOCK`
>> >> description above _could_ be interpreted to mean that _any_
>> >> `XCHG` will always assert the `LOCK#` signal, including an
>> >> `XCHG` that does not touch memory at all. The "Why?" was
>> >> rhetorical, as in, "Why would the system do that?" For example,
>> >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`?
>> >> Truly I don't know, and I was just speculating.
>> >
>> >Writing high quality documentation is fundamentally hard.
>>
>> I don't think that's the issue here. They just did not limit the
>> sentence "The XCHG instruction always asserts the LOCK# signal
>> regardless of the presence or absence of the LOCK prefix." to XCHG
>> instructions with memory operands. That would be easy to fix, but
>> they did not. And Chris M. Thomasson understood it as including
>> XCHG reg, reg.
>>
>> My guess is that it is easy to fix in principle, but you have to deal
>> with all kinds of red tape for such a fix, so unless there is good
>> reason, you don't do it.
>
>Frankly, I don't understand what both you and Dan Cross are talking
>about.
Because you are not reading the same thing we are.
>I don't see how sentence from Intel's manual ("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")
>or sentence from AMD's manual ("If either operand references memory,
>the processor locks automatically, whether or not the LOCK prefix is
>used and independently of the value of IOPL.") can be pssibly
>misunderstood.
Which part of the Intel and AMD manuals?
What Anton raised, and what I'm also referring to, is a a single
sentence in the SDM's description of the `LOCK` prefix. For
reference, I am looking at Volume 2A, order number 253666-093US,
published Sept 2026, page 3-565.
Tucked away in the "Description" section, at the end the third
paragraph, which describes what instructions can be prefaced by
`LOCK` and under what conditions, is the single sentence that
reads: "The `XCHG` instruction always asserts the `LOCK#` signal
regardless of the presence or absence of the `LOCK` prefix."
Note: that is a direct quote from the reference.
We know that `XCHG` with a memory destination operand is atomic
and affects memory ordering, etc, regardless of whether the
`LOCK` prefix is explicitly given or not. The issue is that
that sentence, taken in complete isolation, makes it sound like
this is _also_ the case for `xchg reg, reg`, where the
destination is a _register_ operand.
I claim that that is a misreading, and that a reader should
interpret that last sentence in the context of the first
sentence in that paragraph, that refers specifically to memory
operands; furthermore, other parts of the SDM are clear that
`xchg reg, reg` has no impact on memory ordering, bus locks,
etc.
Regardless, that sentence is ambiguous. References to the
description of `XCHG`, or to the locking protocols in volume 3,
does not change that.
The latest version of the entire Intel SDM is available online:
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
I had already quoted the paragarph in question in its entirely.
- Dan C.
Re: Performance of xchg reg,reg
Author: Michael S
Date: Sun, 18 Oct 2026 17:47
Date: Sun, 18 Oct 2026 17:47
54 lines
4010 bytes
4010 bytes
On Sun, 18 Oct 2026 16:57:26 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> Michael S <already5chosen@yahoo.com> writes:
> >On Sun, 18 Oct 2026 15:08:23 GMT
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> >> I don't think that's the issue here. They just did not limit the
> >> sentence "The XCHG instruction always asserts the LOCK# signal
> >> regardless of the presence or absence of the LOCK prefix." to XCHG
> >> instructions with memory operands. That would be easy to fix, but
> >> they did not. And Chris M. Thomasson understood it as including
> >> XCHG reg, reg.
> ...
> >Frankly, I don't understand what both you and Dan Cross are talking
> >about.
> >I don't see how sentence from Intel's manual ("If a memory operand is
> >referenced, the processor=E2=80=99s 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") or sentence from AMD's manual ("If either operand references
> >memory, the processor locks automatically, whether or not the LOCK
> >prefix is used and independently of the value of IOPL.") can be
> >pssibly misunderstood.
>
> Chris M. Thomasson wrote in <11arg45$2p70p$1@dont-email.me>:
> |Well, for some reason the LOCK prefix is implied for XCHG.
>
> Given that the thread up to that point was aboyt XCHG reg, reg (and
> according to the subject it still is), he seems to be an example that
> what Intel wrote about XCHG and LOCK can be misunderstood. I looked
> at the documentation and cited the relevant sentences in
> <2026Oct16.075055@mips.complang.tuwien.ac.at>; most occurences clearly
> refer only to XCHG where one operand is in memory. The exception is:
>
> |The XCHG instruction always asserts the LOCK# signal regardless of
> the |presence or absence of the LOCK prefix.
>
What is a source of this? Intel, AMD or somebody else?
> And actually all the stuff about XCHG with memory operands does not
> contradict this sentence. It could be understood as meaning that
> while all the other aspects of locking don't apply to "XCHG reg, reg",
> the assertion of the LOCK# signal still has to occur, and that's why
> Intel processors implement a slow XCHG reg, reg. I don't think that's
> what Intel intends and implements, but that's what a reading of their
> manual implies.
>
> - anton
Somehow, it does not imply it to me.
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 17:47
Date: Sun, 18 Oct 2026 17:47
25 lines
1956 bytes
1956 bytes
In article <nv6BS.146451$4b2.140904@fx47.iad>, EricP <ThatWouldBeTelling@thevillage.com> wrote: >On 2026-Oct-17 13:31, Dan Cross wrote: >[snip] >Intel Volume 3 System Programmers guide 10.1.2 Bus Locking >10.1.2.1 Automatic Locking is explicit:... >[snip] Yes, which in my view, lends more support to requiring that the sentence in question in the description of the `LOCK` prefix be read in the context of the overall paragraph, which is clearly talking about cases where the destination is a memory operand. Sadly, however, that doesn't make the sentence itself any less ambiguous, and once you have ambiguity in a technical document like the SDM, you've got a problem. Intel can fix this easily by adding another phrase to that last sentence, ", when the destination is a memory operand." (Or words to that effect.) I'm speculating, but they probably haven't because it seems rather obvious that that is the intent, and no one has brought it up as a problem. - Dan C.
Re: Documentation (was Re: Performance of xchg reg,reg)
Author: MitchAlsup
Date: Sun, 18 Oct 2026 17:48
Date: Sun, 18 Oct 2026 17:48
39 lines
2855 bytes
2855 bytes
Lawrence =?iso-8859-13?q?D=FFOliveiro?= <ldo@nz.invalid> posted:
> On Sat, 17 Oct 2026 19:39:42 GMT, MitchAlsup wrote:
>
> > Writing high quality documentation is fundamentally hard.
>
> Isn’t that why formal specification languages were invented? For some
> reason they never caught on.
There numerous examples of "minutia" in CPU design where one can
never provide a sufficiently formal quantification of what that
part does. A series of unsigned operations culminating in a signed
operation that result in a single multi-wide signed result.
> For a classic example, look at the Algol-68 language spec. Yet many
> people threw up their hands in horror at how it was written.
>
> > It took me nearly 40 years of doing so to get to the point a
> > mischievous engineer could NOT misunderstand what I had written.
>
> Presumably you include lots of examples?
Yes, a while back I was having a bit of trouble describing shift
instructions {both signed and unsigned} in snippets of C-like
code. Eventually, I drew up figures illustrating how each bit
moved through the calculation(s). And the figures did way more
in the definition department than any c-code snippet could.
For example (especially) the double wide shift instructions and
insert instructions that arise when the shift instruction is in
the shadow of a CARRY modifier. It is easy in a figure to illus-
trate how bits cross from one 64-bit container to another, over-
flow off the top and shift up into another from the bottom (and
vice versa).
> I remember something Jerry
> Pournelle said, critiquing the product documentation he had to read
> for his columns in “BYTE” magazine, back in the day: “You can never
> have too many examples”.
Re: Performance of xchg reg,reg
Author: Michael S
Date: Sun, 18 Oct 2026 17:54
Date: Sun, 18 Oct 2026 17:54
93 lines
5627 bytes
5627 bytes
On Sun, 18 Oct 2026 17:40:00 -0000 (UTC)
cross@spitfire.i.gajendra.net (Dan Cross) wrote:
> In article <20261018184318.00000bee@yahoo.com>,
> Michael S <already5chosen@yahoo.com> wrote:
> >On Sun, 18 Oct 2026 15:08:23 GMT
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> >
> >> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> >> >cross@spitfire.i.gajendra.net (Dan Cross) posted:
> >> >> What I'm saying is that, as written, the bits in the `LOCK`
> >> >> description above _could_ be interpreted to mean that _any_
> >> >> `XCHG` will always assert the `LOCK#` signal, including an
> >> >> `XCHG` that does not touch memory at all. The "Why?" was
> >> >> rhetorical, as in, "Why would the system do that?" For example,
> >> >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`?
> >> >> Truly I don't know, and I was just speculating.
> >> >
> >> >Writing high quality documentation is fundamentally hard.
> >>
> >> I don't think that's the issue here. They just did not limit the
> >> sentence "The XCHG instruction always asserts the LOCK# signal
> >> regardless of the presence or absence of the LOCK prefix." to XCHG
> >> instructions with memory operands. That would be easy to fix, but
> >> they did not. And Chris M. Thomasson understood it as including
> >> XCHG reg, reg.
> >>
> >> My guess is that it is easy to fix in principle, but you have to
> >> deal with all kinds of red tape for such a fix, so unless there is
> >> good reason, you don't do it.
> >
> >Frankly, I don't understand what both you and Dan Cross are talking
> >about.
>
> Because you are not reading the same thing we are.
>
> >I don't see how sentence from Intel's manual ("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") or sentence from AMD's manual ("If either operand references
> >memory, the processor locks automatically, whether or not the LOCK
> >prefix is used and independently of the value of IOPL.") can be
> >pssibly misunderstood.
>
> Which part of the Intel and AMD manuals?
>
> What Anton raised, and what I'm also referring to, is a a single
> sentence in the SDM's description of the `LOCK` prefix. For
> reference, I am looking at Volume 2A, order number 253666-093US,
> published Sept 2026, page 3-565.
>
> Tucked away in the "Description" section, at the end the third
> paragraph, which describes what instructions can be prefaced by
> `LOCK` and under what conditions, is the single sentence that
> reads: "The `XCHG` instruction always asserts the `LOCK#` signal
> regardless of the presence or absence of the `LOCK` prefix."
> Note: that is a direct quote from the reference.
>
> We know that `XCHG` with a memory destination operand is atomic
> and affects memory ordering, etc, regardless of whether the
> `LOCK` prefix is explicitly given or not. The issue is that
> that sentence, taken in complete isolation, makes it sound like
> this is _also_ the case for `xchg reg, reg`, where the
> destination is a _register_ operand.
>
> I claim that that is a misreading, and that a reader should
> interpret that last sentence in the context of the first
> sentence in that paragraph, that refers specifically to memory
> operands; furthermore, other parts of the SDM are clear that
> `xchg reg, reg` has no impact on memory ordering, bus locks,
> etc.
>
> Regardless, that sentence is ambiguous. References to the
> description of `XCHG`, or to the locking protocols in volume 3,
> does not change that.
>
> The latest version of the entire Intel SDM is available online:
> https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
> I had already quoted the paragarph in question in its entirely.
>
> - Dan C.
>
When I want to know what particular insruction does I go to respective
section in Instruction Set Reference.
I can believe that there exist other methodes, but I prefere mine.
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 17:59
Date: Sun, 18 Oct 2026 17:59
84 lines
4535 bytes
4535 bytes
In article <2026Oct18.170823@mips.complang.tuwien.ac.at>, Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote: >MitchAlsup <user5857@newsgrouper.org.invalid> writes: >>cross@spitfire.i.gajendra.net (Dan Cross) posted: >>> What I'm saying is that, as written, the bits in the `LOCK` >>> description above _could_ be interpreted to mean that _any_ >>> `XCHG` will always assert the `LOCK#` signal, including an >>> `XCHG` that does not touch memory at all. The "Why?" was >>> rhetorical, as in, "Why would the system do that?" For example, >>> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`? >>> Truly I don't know, and I was just speculating. >> >>Writing high quality documentation is fundamentally hard. > >I don't think that's the issue here. They just did not limit the >sentence "The XCHG instruction always asserts the LOCK# signal >regardless of the presence or absence of the LOCK prefix." to XCHG >instructions with memory operands. That would be easy to fix, but >they did not. And Chris M. Thomasson understood it as including >XCHG reg, reg. > >My guess is that it is easy to fix in principle, but you have to deal >with all kinds of red tape for such a fix, so unless there is good >reason, you don't do it. Nah; the SDM changes all the time. It's hard to hand out citations to it without an explicit version because of that. There are mistakes all over it; most of them are pretty minor, like cross-referring to an old section number or something. Each revision fixes a few and adds a few others. I think it just hasn't occurred to anyone that it could be clearer. >This sentence already exists in my 1994-vintage Pentium Processor >User's Manual Vol. 3. There it is preceeded by: > >|The LOCK prefix functions only with the following instructions: >| >|... >|XCHG reg, mem >|XCHG mem, reg >|... > >With no occurence of XCHG reg, reg anywhere. But, OTOH, this is not >really a contradiction to the sentence "The XCHG instruction always >asserts the LOCK# signal regardless of the presence or absence of the >LOCK prefix." Right. The current text says more or less the same thing, though phrased slightly differently: "The LOCK prefix can be prepended only to the following instructions and only to those forms of the instructions where the destination operand is a memory operand: [list of instructions...]." >>What they wrote may have well been exactly what the HW did back then >>but somewhere between then and now architects and designers figured >>out other ways and nobody went back to fully scrub the documents. > >Actually <https://www.felixcloutier.com/x86/lock> then says that >"Beginning with the P6 family processors, when [...], the LOCK# signal >is generally not asserted [...]" and still explains mechanism, finally >saying "the operation is carried out atomically with regards to >memory." If they wrote that as the description of the LOCK prefix, >much of the rest could be deleted. > >Generally, an architecture manual should talk about architectural >effects, not about asserted signals. > >The operational semantics given in ><https://www.felixcloutier.com/x86/lock> is: > >| AssertLOCK#(DurationOfAccompaningInstruction); > >Oh boy. That part is fine. It's talking about the _explicit_ presence of a `LOCK` prefix, and given the constraints on the 'accompaning' [sic] instruction, that tracks exactly what the description says. Volume 3A, sec 11.1.2, describes bus locking, and what "asserting LOCK#" means. - Dan C.
Re: Performance of xchg reg,reg
Author: MitchAlsup
Date: Sun, 18 Oct 2026 18:01
Date: Sun, 18 Oct 2026 18:01
49 lines
3414 bytes
3414 bytes
Michael S <already5chosen@yahoo.com> posted:
> On Sun, 18 Oct 2026 15:08:23 GMT
> anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>
> > MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> > >cross@spitfire.i.gajendra.net (Dan Cross) posted:
> > >> What I'm saying is that, as written, the bits in the `LOCK`
> > >> description above _could_ be interpreted to mean that _any_
> > >> `XCHG` will always assert the `LOCK#` signal, including an
> > >> `XCHG` that does not touch memory at all. The "Why?" was
> > >> rhetorical, as in, "Why would the system do that?" For example,
> > >> if one wrote, `xchg %ecx, %eax`, why should that assert `LOCK#`?
> > >> Truly I don't know, and I was just speculating.
> > >
> > >Writing high quality documentation is fundamentally hard.
> >
> > I don't think that's the issue here. They just did not limit the
> > sentence "The XCHG instruction always asserts the LOCK# signal
> > regardless of the presence or absence of the LOCK prefix." to XCHG
> > instructions with memory operands. That would be easy to fix, but
> > they did not. And Chris M. Thomasson understood it as including
> > XCHG reg, reg.
> >
> > My guess is that it is easy to fix in principle, but you have to deal
> > with all kinds of red tape for such a fix, so unless there is good
> > reason, you don't do it.
> >
>
> Frankly, I don't understand what both you and Dan Cross are talking
> about.
> I don't see how sentence from Intel's manual ("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")
> or sentence from AMD's manual ("If either operand references memory,
> the processor locks automatically, whether or not the LOCK prefix is
> used and independently of the value of IOPL.") can be pssibly
> misunderstood.
Look at it from the perspective of the guy verifying that on-die
interconnect is being used correctly by the cores. {{not every
designer is inside the core !!}}
Say protocol has space for XCHG-Lock and XCHG-without, and the cores
are using later instead of former because the destination does not
need the -lock part to be ATOMIC.
>
>
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 18:07
Date: Sun, 18 Oct 2026 18:07
64 lines
4146 bytes
4146 bytes
In article <20261018204738.00006b41@yahoo.com>,
Michael S <already5chosen@yahoo.com> wrote:
>On Sun, 18 Oct 2026 16:57:26 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>
>> Michael S <already5chosen@yahoo.com> writes:
>> >On Sun, 18 Oct 2026 15:08:23 GMT
>> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>> >> I don't think that's the issue here. They just did not limit the
>> >> sentence "The XCHG instruction always asserts the LOCK# signal
>> >> regardless of the presence or absence of the LOCK prefix." to XCHG
>> >> instructions with memory operands. That would be easy to fix, but
>> >> they did not. And Chris M. Thomasson understood it as including
>> >> XCHG reg, reg.
>> ...
>> >Frankly, I don't understand what both you and Dan Cross are talking
>> >about.
>> >I don't see how sentence from Intel's manual ("If a memory operand is
>> >referenced, the processor=E2=80=99s 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") or sentence from AMD's manual ("If either operand references
>> >memory, the processor locks automatically, whether or not the LOCK
>> >prefix is used and independently of the value of IOPL.") can be
>> >pssibly misunderstood.
>>
>> Chris M. Thomasson wrote in <11arg45$2p70p$1@dont-email.me>:
>> |Well, for some reason the LOCK prefix is implied for XCHG.
>>
>> Given that the thread up to that point was aboyt XCHG reg, reg (and
>> according to the subject it still is), he seems to be an example that
>> what Intel wrote about XCHG and LOCK can be misunderstood. I looked
>> at the documentation and cited the relevant sentences in
>> <2026Oct16.075055@mips.complang.tuwien.ac.at>; most occurences clearly
>> refer only to XCHG where one operand is in memory. The exception is:
>>
>> |The XCHG instruction always asserts the LOCK# signal regardless of
>> the |presence or absence of the LOCK prefix.
>
>What is a source of this? Intel, AMD or somebody else?
This citation has been given many times now, but here it is yet
again: That is the last sentence of the third paragraph in the
"Description" section of the entry on the `LOCK` prefix in
Volume 2A of the Intel SDM, order number 253668-093US (September
2026), page 3-565. That volume can be downloaded from Intel at:
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
>> And actually all the stuff about XCHG with memory operands does not
>> contradict this sentence. It could be understood as meaning that
>> while all the other aspects of locking don't apply to "XCHG reg, reg",
>> the assertion of the LOCK# signal still has to occur, and that's why
>> Intel processors implement a slow XCHG reg, reg. I don't think that's
>> what Intel intends and implements, but that's what a reading of their
>> manual implies.
>
>Somehow, it does not imply it to me.
Note that Anton said that it is an implication of _A_ reading
of the manual, not of _ALL_ readings of the manual. Hence why
it is ambiguous.
- Dan C.
Re: Performance of xchg reg,reg
Author: Dan Cross
Date: Sun, 18 Oct 2026 18:25
Date: Sun, 18 Oct 2026 18:25
14 lines
1460 bytes
1460 bytes
In article <20261018205430.00001490@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >[snip] >When I want to know what particular insruction does I go to respective >section in Instruction Set Reference. >I can believe that there exist other methodes, but I prefere mine. Unfortunately, that does not change the fact that there are multiple sections in the SDM that _can_ be read as mutually inconsistent with regards to the exact semantics of this particular operation. - Dan C.
Re: Performance of xchg reg,reg
Author: Chris M. Thomasson
Date: Sun, 18 Oct 2026 19:27
Date: Sun, 18 Oct 2026 19:27
52 lines
3570 bytes
3570 bytes
On 10/16/2026 7:50 PM, Dan Cross wrote: > In article <11au1vu$3ks5m$2@dont-email.me>, > Thomas Koenig <tkoenig@netcologne.de> wrote: >> Dan Cross <cross@spitfire.i.gajendra.net> schrieb: >>> 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. > > Using the `LOCK` prefix without a destination operand that is a > memory operand is an error, and the SDM is quite explicit about > that. In fact, it says that an explicit `LOCK` prefix on an > `XCHG` a register destination operand is _not_ permitted and > "may" generate an `#UD` exception. > > But sadly, when talking about an `XCHG` _without_ an explicit > operand, it is left ambiguous; all it says is that, "The `XCHG` > instruction always asserts the `LOCK#` signal regardless of the > presence or absence of the `LOCK` prefix." Devoid of the rest > of the surrounding context, one could interpret that to mean > that an `XCHG` _always_ forces a bus lock, irrespective of the > type of its desintation operand. > > Why? I have no idea; as I said, I'm in the camp of going with > what the description of `XCHG` itself says, and not the what the > description of the `LOCK` prefix says: that sentence is _not_ > devoid of context, and besides, there has not been a physical > `LOCK#` signal coming out of an Intel Xeon package since > Nehalem. Try a LOCK RMW on memory that straddles a L2 cache line. That should trigger a full bus lock. > > I suppose one could imagine it forcing a poor-man's barrier to > prevent later loads from being reorderd before it (loads can't > migrate before locked instructions). But we've got explicit > fence instructions for that, so I don't buy it. > > I suspect that sentence is just poorly phrased. > > - Dan C. >
Re: Performance of xchg reg,reg
Author: Anton Ertl
Date: Sun, 18 Oct 2026 21:04
Date: Sun, 18 Oct 2026 21:04
18 lines
2121 bytes
2121 bytes
Michael S <already5chosen@yahoo.com> writes: >On Sun, 18 Oct 2026 16:57:26 GMT >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: >> |The XCHG instruction always asserts the LOCK# signal regardless of >> the |presence or absence of the LOCK prefix. >> > >What is a source of this? Intel, AMD or somebody else? Intel, from (at least) the 1994 edition of the Pentium Processor User's Manual Vol 3., through the source of <https://www.felixcloutier.com/x86/lock>, to the latest SDM cited by Dan Cross. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Algol 68 (was: Documentation (was Re: Performance of xchg reg,reg))
Author: John Savard
Date: Mon, 19 Oct 2026 00:09
Date: Mon, 19 Oct 2026 00:09
10 lines
1903 bytes
1903 bytes
I thought, though, that the main problem with Algol 68 was not that it was described poorly, but that although people were still able to understand the language well enough despite an unconventional formal description language... it was a large and complicated language, advancing the state of the art, and thus it was found difficult to implement. Hence, Pascal - a language which modernized Algol 60 without trying to be a language on an entirely new level. John Savard
Re: Documentation (was Re: Performance of xchg reg,reg)
Author: John Savard
Date: Mon, 19 Oct 2026 00:18
Date: Mon, 19 Oct 2026 00:18
21 lines
2124 bytes
2124 bytes
On Sun, 18 Oct 2026 09:45:00 -0400, George Neuner <gneuner2@comcast.net> wrote: >The problem with the original report was that they attempted with the >grammar turn [at least some kinds of] semantic errors into syntax >errors. Obviously, there's no way one can enable a compiler to flag logic errors in programs. A Turing-complete language can calculate anything, so how can it tell you that you were calculating this when you wanted to calculate that? Of course, with AI, we might be able to do something there. But already many modern languages do have strict type-checking. So even if Algol 68 was over-ambitious in this regard, trying to have the language help the programmer avoid more mistakes is basically a sound idea. John Savard
Re: Algol 68 (was Re: Documentation)
Author: Lawrence D’Oliveiro
Date: Mon, 19 Oct 2026 01:46
Date: Mon, 19 Oct 2026 01:46
37 lines
3293 bytes
3293 bytes
On Mon, 19 Oct 2026 00:09:11 GMT, John Savard wrote: > I thought, though, that the main problem with Algol 68 was not that > it was described poorly, but that although people were still able to > understand the language well enough despite an unconventional formal > description language... it was a large and complicated language, > advancing the state of the art, and thus it was found difficult to > implement. Algol 60 was a difficult language to implement less than a decade earlier. It was a driver of the state of the art back then, too. (And some still found it hard in 1968.) I think the form of the language specification played a big part in making it seem more intimidating than it was. Algol 68 would be a doddle to implement nowadays. In fact, Marcel Van Der Veer’s “Algol 68 Genie” implementation is being incorporated into GCC, I believe. Many of the terms pioneered in the Algol 68 specification did become popular -- which shows you how new those concepts were at the time: “heap”, “overload”, “dereference”. Others were not so popular: “transput” (for “I/O”), “name” (for “address”, would you believe), “mode” (for “type”). C took syntax words like “struct”, “void”, “int”, “long” and “short” straight from Algol 68. > Hence, Pascal - a language which modernized Algol 60 without trying > to be a language on an entirely new level. Niklaus Wirth was possibly the biggest critic of the Algol 68 effort. He and Tony Hoare had previously created Algol-W, which pioneered the concept of record structures (taking a simpler form than COBOL’s convoluted record structures). Pascal was basically the next step in the evolution of that.
Re: Documentation (was Re: Performance of xchg reg,reg)
Author: Dan Cross
Date: Mon, 19 Oct 2026 01:52
Date: Mon, 19 Oct 2026 01:52
55 lines
3057 bytes
3057 bytes
In article <6ad5609b.8505984@news.eternal-september.org>,
John Savard <quadibloc@invalid.com> wrote:
>On Sun, 18 Oct 2026 09:45:00 -0400, George Neuner
><gneuner2@comcast.net> wrote:
>
>>The problem with the original report was that they attempted with the
>>grammar turn [at least some kinds of] semantic errors into syntax
>>errors.
>
>Obviously, there's no way one can enable a compiler to flag logic
>errors in programs. A Turing-complete language can calculate anything,
>so how can it tell you that you were calculating this when you wanted
>to calculate that?
With apologies, arguments like these always seem to me to miss
the forest for the trees a bit. Some constructs clearly violate
the documented semantics of e.g. a language.
Yes, these langauges are all Turing-complete and so on, and no,
we cannot solve _The_ Halting Problem, in all of its glorious
generality. But, we can solve any number of specific halting
_problems_. For example, I can see that, `for (;;);` is a loop
that does not terminate. With enough effort, I can take that
down some reasonably long paths.
So, too, one can spot a lot of logic errors. There's no reason
a compiler can't see this, for example:
? if (ptr == NULL) {
? int v = *ptr++;
? *ptr = v + 10;
? }
And flag it with a error: "you dereferenced a pointer right
after you asserted it was nil; did you invert the condition?"
Similarly, I posted about Rust a few days ago, which can do all
sorts of fun stuff with its borrow checker to assert that an
object is still alive while references to it exist, and so on,
and reject programs at compilation time that violate those
semantics.
>Of course, with AI, we might be able to do something there.
>
>But already many modern languages do have strict type-checking.
>
>So even if Algol 68 was over-ambitious in this regard, trying to have
>the language help the programmer avoid more mistakes is basically a
>sound idea.
Agreed. Indeed, we've gone much farther than just mere type
checking.
- Dan C.
Re: Algol 68 (was Re: Documentation)
Author: Lawrence D’Oliveiro
Date: Mon, 19 Oct 2026 01:54
Date: Mon, 19 Oct 2026 01:54
29 lines
3008 bytes
3008 bytes
On Mon, 19 Oct 2026 00:18:32 GMT, John Savard wrote:
> Obviously, there's no way one can enable a compiler to flag logic
> errors in programs. A Turing-complete language can calculate
> anything, so how can it tell you that you were calculating this when
> you wanted to calculate that?
All the Algol 68 two-level grammar approach was trying to do was
formalize rules that were already being spelled out in narrative text
in other contemporary (and subsequent) language specifications. Basic
things like
* A name for something must be declared before it can be referenced.
* Types of expected versus actual values must be compatible, with
some predefined set of implicit conversions allowed (e.g.
passing/assigning an integer where a float value is expected, but
not the other way round).
* A redeclaration of a name in an inner scope shadows (makes
inaccessible for the duration) a definition in an outer scope.
There was (and still is) the debate over what is “syntax” and what is
“semantics”? Algol 68 was just trying to push the boundary to take
some of what was thought to be “semantics” and show that it was just
“syntax”, after all.
So you see, they were not trying to do overly clever stuff like the
Java compiler insisting that a variable could possibly be accessed
before its first assignment, and this can’t be allowed, when it’s
quite apparent to any human reader that can follow the program logic,
that no such situation can occur.
Re: PL/I, was Algol 68 (was: Documentation (was Re: Performance of xchg reg,reg))
Author: John Levine
Date: Mon, 19 Oct 2026 03:24
Date: Mon, 19 Oct 2026 03:24
25 lines
2649 bytes
2649 bytes
According to Anton Ertl <anton@mips.complang.tuwien.ac.at>: >A team at the IBM Lab in Vienna made a formal specification for PL/I; >I attended a lecture by Heinz Zemanek (the team leader) where he >mentioned something about an implementor disagreeing with the formal >specification for recursion (disagreements between implementor and >specification can also happen with specifications written in a natural >language, so that may not say much). Anyway, ><https://en.wikipedia.org/wiki/PL/I#Standardization> says "The >standard is couched as a formal definition[17] using a "PL/I >Machine"[21] to specify the semantics." This sounds like the >operational semantics that was developed by the IBM Lab Vienna, so >maybe the standardization committee picked up this work. But it seems >that the first standard never had a followon; was the definition too >unwieldy? I have a copy of the ANSI PL/I standard which uses VDL which I find close to impenetrable. The syntax part is OK but the semantics are endless definitions of something in terms of something else and I could never find the places where your program actually did anything. -- Regards, John Levine, johnl@taugh.com, Primary Perpetrator of "The Internet for Dummies", Please consider the environment before reading this e-mail. https://jl.ly
Page 1 of 2 • 82 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads