Instruction
BSWAP
Reverses the byte order of a 32- or 64-bit register, converting between little- and big-endian values; with a 16-bit register the result is undefined.
- Extensions
- Forms
- 1 (1 Legacy)
- Data
- Intel XED v2026.08.23
BSWAP reverses the order of the bytes in a 32- or 64-bit register. That
converts a value between little-endian, the order in which x86 keeps
integers in memory, and big-endian, the order of network protocols and
many file formats. GCC 14 and Clang 23 at -O2 compile
__builtin_bswap32 and __builtin_bswap64 to it, and find it in plain
C too: a 32-bit value assembled from four bytes, most significant first,
as in p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3], becomes a 4-byte
load and bswap eax.
16 bits
Both manuals leave BSWAP of a 16-bit register undefined and suggest an
XCHG of its two bytes instead. On this site’s Zen 5, every 16-bit case
recorded cleared the low 16 bits and kept bits 63:16. Compilers use
neither: both compile __builtin_bswap16 to rol ax, 8, a
ROL of the word by 8, which swaps its bytes.
Register only
BSWAP has no memory operand: its register is coded in the low three
bits of the opcode, 0F C8 to 0F CF, with REX.B reaching R8 to R15 and
REX.W choosing 64 bits. The 32-bit form clears bits 63:32 like any
32-bit write, so bswap eax is not half of a 64-bit swap.
MOVBE swaps the bytes as part of a load or a store.
With -march=x86-64-v3, a level that includes it, both
compilers turn the big-endian load above into a single
movbe eax, dword ptr [rdi].
For arrays, Intel’s optimization manual prefers PSHUFB with a constant pattern, which reverses four doublewords at once: about twice as fast as BSWAP on the Intel Core microarchitecture, by its account (Section 6.6.5).
Forms
| Instruction | Encoding | Requires |
|---|---|---|
BSWAP r16/32/64
BSWAP_GPRv | 0F C8+r | Base instruction set |
Latency and throughput
| Model | r32 | r64 |
|---|---|---|
| Sandy Bridge | 1 / 1p1 | 2 / 1p015 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 1 / 0.5p15 | 2 / 0.5p0156 |
| Alder Lake, Sapphire Rapids | 1 / 1p1 | 2 / 1p016 |
| Lunar Lake | 1 / 0.33p135 | 2 / 0.67p135 |
| Bonnell | 1 / 1p0 | 1 / 1p0 |
| Silvermont | 1 / 0.5IEC0 IEC1 | 1 / 0.5IEC0 IEC1 |
| Piledriver | 1 / 0.5EX0 EX1 | 1 / 0.5EX0 EX1 |
| Zen, Zen 2 | 1 / 1ALU0 ALU1 ALU2 ALU3 | 1 / 1ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 1 / 0.5ALU0 ALU1 | 1 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
v = dest.read()
if OSIZE == 16:
dest.write(UNDEFINED) # the manuals leave the result undefined;
# Zen 5 writes 0 and keeps bits 63:16
else:
r = 0
for i in range(OSIZE // 8):
r = r << 8 | (v >> 8 * i & 0xFF) # the bytes in reverse order
dest.write(r) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 18 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
BSWAP r16/32/64 | ||
r16/32/64 | explicit | read, write |
Flags
None: XED records no flag that the forms test or change.
Intrinsics
The C intrinsics of Clang 23.1.2 that compile to BSWAP, each with the form it compiles to and the Clang options it needs. The forms come from compiling every intrinsic and decoding the result with Intel XED; all intrinsics A–Z explains the method.
| Intrinsic | Compiles to |
|---|---|
_bswap
int _bswap(int __A) |
|
_bswap64
long long _bswap64(long long A) |
|
__bswapd
int __bswapd(int __A) |
|
__bswapq
long long __bswapq(long long __A) |
|
CPUID and processors
Each form belongs to an XED ISA set. A form can be used when the processor reports every CPUID bit of one of its ISA set's alternatives.
| ISA set | CPUID | Processors |
|---|---|---|
I486REAL | No CPUID bit: part of the base instruction set. | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
More General-purpose instructions
Sources
- Intel XED v2026.08.23 (commit
0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model. - The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s
-marchfeature sets and AMD’s Processor Programming References, which the page of each processor cites. - Clang's x86 intrinsic headers, LLVM 23.1.2 (commit
85ac56026243): the intrinsics, their signatures and doc comments, and the Clang of that release, which compiled each of them. - LLVM 23.1.2's x86 scheduling models (commit
85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them. - Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-111 (BSWAP)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
- Intel 64 and IA-32 Architectures Optimization Reference Manual, Volume 1 (248966-051, October 2026)
The tables are derived from Intel XED, Copyright Intel Corporation, licensed under the Apache License 2.0; x86-64.net converted and reformatted them. The intrinsics and their signatures come from Clang and the timings from LLVM's scheduling models, both part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.