<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
the	O
exclusive	O
or	O
swap	O
(	O
sometimes	O
shortened	O
to	O
XOR	B-Algorithm
swap	I-Algorithm
)	O
is	O
an	O
algorithm	O
that	O
uses	O
the	O
exclusive	O
or	O
bitwise	O
operation	O
to	O
swap	O
the	O
values	O
of	O
two	O
variables	O
without	O
using	O
the	O
temporary	O
variable	O
which	O
is	O
normally	O
required	O
.	O
</s>
<s>
Using	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
,	O
however	O
,	O
no	O
temporary	O
storage	O
is	O
needed	O
.	O
</s>
<s>
The	O
algorithm	O
typically	O
corresponds	O
to	O
three	O
machine-code	B-Language
instructions	O
,	O
represented	O
by	O
corresponding	O
pseudocode	O
and	O
assembly	O
instructions	O
in	O
the	O
three	O
rows	O
of	O
the	O
following	O
table	O
:	O
</s>
<s>
In	O
the	O
above	O
System/370	B-Device
assembly	O
code	O
sample	O
,	O
R1	O
and	O
R2	O
are	O
distinct	O
registers	O
,	O
and	O
each	O
operation	O
leaves	O
its	O
result	O
in	O
the	O
register	B-General_Concept
named	O
in	O
the	O
first	O
argument	O
.	O
</s>
<s>
Using	O
x86	O
assembly	O
,	O
values	O
X	O
and	O
Y	O
are	O
in	O
registers	O
eax	O
and	O
ebx	O
(	O
respectively	O
)	O
,	O
and	O
places	O
the	O
result	O
of	O
the	O
operation	O
in	O
the	O
first	O
register	B-General_Concept
.	O
</s>
<s>
(	O
working	O
with	O
binary	O
values	O
,	O
so	O
)	O
,	O
which	O
expresses	O
the	O
elementary	O
matrix	O
of	O
switching	O
two	O
rows	O
(	O
or	O
columns	O
)	O
in	O
terms	O
of	O
the	O
transvections	B-Algorithm
(	O
shears	B-Algorithm
)	O
of	O
adding	O
one	O
element	O
to	O
the	O
other	O
.	O
</s>
<s>
A	O
C	B-Language
function	O
that	O
implements	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
:	O
</s>
<s>
The	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
can	O
also	O
be	O
defined	O
with	O
a	O
macro	O
:	O
</s>
<s>
On	O
modern	O
CPU	B-General_Concept
architectures	I-General_Concept
,	O
the	O
XOR	O
technique	O
can	O
be	O
slower	O
than	O
using	O
a	O
temporary	O
variable	O
to	O
do	O
swapping	O
.	O
</s>
<s>
Even	O
if	O
there	O
is	O
not	O
any	O
architectural	O
register	B-General_Concept
available	O
to	O
use	O
,	O
the	O
XCHG	O
instruction	O
will	O
be	O
at	O
least	O
as	O
fast	O
as	O
the	O
three	O
XORs	O
taken	O
together	O
.	O
</s>
<s>
Another	O
reason	O
is	O
that	O
modern	O
CPUs	O
strive	O
to	O
execute	O
instructions	O
in	O
parallel	O
via	O
instruction	B-General_Concept
pipelines	I-General_Concept
.	O
</s>
<s>
In	O
the	O
XOR	O
technique	O
,	O
the	O
inputs	O
to	O
each	O
operation	O
depend	O
on	O
the	O
results	O
of	O
the	O
previous	O
operation	O
,	O
so	O
they	O
must	O
be	O
executed	O
in	O
strictly	O
sequential	O
order	O
,	O
negating	O
any	O
benefits	O
of	O
instruction-level	B-Operating_System
parallelism	I-Operating_System
.	O
</s>
<s>
The	O
XOR	B-Algorithm
swap	I-Algorithm
is	O
also	O
complicated	O
in	O
practice	O
by	O
aliasing	B-Application
.	O
</s>
<s>
Therefore	O
,	O
XOR	O
swapping	O
must	O
not	O
be	O
used	O
blindly	O
in	O
a	O
high-level	O
language	O
if	O
aliasing	B-Application
is	O
possible	O
.	O
</s>
<s>
The	O
underlying	O
principle	O
of	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
can	O
be	O
applied	O
to	O
any	O
operation	O
meeting	O
criteria	O
L1	O
through	O
L4	O
above	O
.	O
</s>
<s>
Unlike	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
,	O
this	O
variation	O
requires	O
that	O
the	O
underlying	O
processor	O
or	O
programming	O
language	O
uses	O
a	O
method	O
such	O
as	O
modular	O
arithmetic	O
or	O
bignums	B-Algorithm
to	O
guarantee	O
that	O
the	O
computation	O
of	O
X	O
+	O
Y	O
cannot	O
cause	O
an	O
error	O
due	O
to	O
integer	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
Therefore	O
,	O
it	O
is	O
seen	O
even	O
more	O
rarely	O
in	O
practice	O
than	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
.	O
</s>
<s>
However	O
,	O
the	O
implementation	O
of	O
AddSwap	O
above	O
in	O
the	O
C	B-Language
programming	I-Language
language	I-Language
always	O
works	O
even	O
in	O
case	O
of	O
integer	B-Error_Name
overflow	I-Error_Name
,	O
since	O
,	O
according	O
to	O
the	O
C	B-Language
standard	O
,	O
addition	O
and	O
subtraction	O
of	O
unsigned	O
integers	O
follow	O
the	O
rules	O
of	O
modular	O
arithmetic	O
,	O
i	O
.	O
e	O
.	O
are	O
done	O
in	O
the	O
cyclic	O
group	O
where	O
is	O
the	O
number	O
of	O
bits	O
of	O
unsigned	O
int	O
.	O
</s>
<s>
This	O
generalizes	O
the	O
proof	O
for	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
:	O
XOR	O
is	O
both	O
the	O
addition	O
and	O
subtraction	O
in	O
the	O
abelian	O
group	O
(	O
which	O
is	O
the	O
direct	O
sum	O
of	O
s	O
copies	O
of	O
)	O
.	O
</s>
<s>
Signed	O
integer	B-Error_Name
overflow	I-Error_Name
is	O
an	O
undefined	O
behavior	O
in	O
C	B-Language
and	O
thus	O
modular	O
arithmetic	O
is	O
not	O
guaranteed	O
by	O
the	O
standard	O
,	O
which	O
may	O
lead	O
to	O
incorrect	O
results	O
.	O
</s>
<s>
On	O
architectures	O
lacking	O
a	O
dedicated	O
swap	O
instruction	O
,	O
because	O
it	O
avoids	O
the	O
extra	O
temporary	O
register	B-General_Concept
,	O
the	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
is	O
required	O
for	O
optimal	O
register	B-General_Concept
allocation	O
.	O
</s>
<s>
This	O
is	O
particularly	O
important	O
for	O
compilers	B-Language
using	O
static	O
single	O
assignment	O
form	O
for	O
register	B-General_Concept
allocation	O
;	O
these	O
compilers	B-Language
occasionally	O
produce	O
programs	O
that	O
need	O
to	O
swap	O
two	O
registers	O
when	O
no	O
registers	O
are	O
free	O
.	O
</s>
<s>
The	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
avoids	O
the	O
need	O
to	O
reserve	O
an	O
extra	O
register	B-General_Concept
or	O
to	O
spill	O
any	O
registers	O
to	O
main	O
memory	O
.	O
</s>
<s>
This	O
method	O
of	O
register	B-General_Concept
allocation	O
is	O
particularly	O
relevant	O
to	O
GPU	B-Architecture
shader	O
compilers	B-Language
.	O
</s>
<s>
On	O
modern	O
GPU	B-Architecture
architectures	O
,	O
spilling	O
variables	O
is	O
expensive	O
due	O
to	O
limited	O
memory	O
bandwidth	O
and	O
high	O
memory	O
latency	O
,	O
while	O
limiting	O
register	B-General_Concept
usage	O
can	O
improve	O
performance	O
due	O
to	O
dynamic	O
partitioning	O
of	O
the	O
register	B-General_Concept
file	I-General_Concept
.	O
</s>
<s>
The	O
XOR	B-Algorithm
swap	I-Algorithm
algorithm	I-Algorithm
is	O
therefore	O
required	O
by	O
some	O
GPU	B-Architecture
compilers	B-Language
.	O
</s>
