Instruction
TEST
ANDs two operands to set SF, ZF and PF and clear CF and OF, discarding the result; TEST with the same register twice checks it for zero.
- Extensions
- Forms
- 14 (14 Legacy)
- Data
- Intel XED v2026.08.23
TEST ANDs its operands, sets the flags from the result and discards it: it is to AND what CMP is to SUB. It has two everyday uses:
- Zero and sign.
test rax, raxsets ZF when RAX is 0 and SF when it is negative, so JZ, JNZ, JS and JNS can follow it. Every condition code then gives the same answer as aftercmp rax, 0, since both leave CF and OF clear, and TEST needs no immediate byte. GCC 14 and Clang 23 at -O2 compile bothx == 0andx < 0totest rdi, rdiand a branch. - Bits.
test reg, masksets ZF when none of the bits in the mask is set, so JNZ means “any of them is”. For a single bit whose number is in a register, BT copies that bit to CF instead.
APX’s CTESTscc is TEST under a condition, as CCMPscc is CMP under one.
The immediate
TEST has no form with a sign-extended 8-bit immediate, which CMP, AND
and the other arithmetic instructions have: its immediate is as wide
as the operand, up to 32 bits. When the mask fits in the low byte, a
byte operand is shorter, and Clang 23 compiles if (x & 8) to
test dil, 8, four bytes, where test edi, 8 would take six.
Parity
PF reflects only the low 8 bits of the result, as it does after the
arithmetic and logical instructions in general, so test eax, eax
followed by JP says nothing about the parity of all 32 bits. Folding
the halves together with XOR first, as GCC and Clang do for
__builtin_parity, or a POPCNT, gives that.
The /1 encodings
TEST with an immediate is F6 /0 or F7 /0. XED decodes the same opcodes with /1 in ModRM.reg as TEST too, and marks those forms undocumented: AMD’s opcode map lists TEST under both /0 and /1, while Intel’s leaves /1 empty. Zen 5 ran the /1 forms recorded here exactly as the /0 ones.
In the optimization guide
What the optimization guide says about TEST, a line per claim. Each links to the claim in the guide, with its sources and where they differ.
Front end
- Macro-fusion: Intel’s cores since Sandy Bridge (Skylake) fuse
testandandwith any conditional branch;cmp,addandsubwith branches on the equal, unsigned and signed comparisons, but not on the sign, parity or overflow flag alone; andincanddeconly with branches on equality and the signed comparisons. - Macro-fusion: AMD’s Zen 3 to Zen 5 fuse more:
cmp,test,add,sub,and,or,xor,incanddecwith any conditional branch, except that on Zen 3 and Zen 4incanddecdo not fuse with a branch that reads the carry flag, which Intel’s cores do not fuse either (Zen 5, Zen 4, Zen 3). sources differ - Macro-fusion: With a memory operand, Intel’s Sandy Bridge to Skylake do not fuse an instruction that also has an immediate, while AMD’s Zen 3 and Zen 4 still fuse
cmpandtestunless a displacement comes with the immediate or the address is relative to RIP. sources differ - Macro-fusion: On Intel’s cores the choice of instruction decides fusion more often, so Intel prefers
testtocmpfor a comparison with zero, astestfuses with every condition.
Bulldozer to Excavator
- Branches: A
cmportestdirectly followed by a conditional branch fuses into one op, unless the compare uses a RIP-relative address or has a displacement as well as an immediate; no other instruction fuses. sources differ
Haswell and Broadwell
- Front end: CMP, ADD and SUB fuse with a following conditional jump that tests carry, zero, or a signed or unsigned comparison; INC and DEC only with one that tests zero or a signed comparison; TEST and AND with any.
Ice Lake to Rocket Lake
- Front end: A
cmp,add,sub,incordecfuses with a following jump on zero, carry or an unsigned or signed comparison, andtestorandwith any conditional jump;or,xor,adc,sbb, shifts andjs,joandjpnever fuse.
Sandy Bridge and Ivy Bridge
- Front end: Sandy Bridge widens macro-fusion from Nehalem’s CMP and TEST to also fuse ADD, SUB, AND, INC and DEC with a following conditional branch, fusing when the first instruction’s first operand is a register and its second operand, if any, is an immediate, a register or memory that is not RIP-relative; the pair does not fuse only when the first instruction ends on the last byte of a 64-byte line and the branch begins the next line’s first byte.
Skylake to Cooper Lake
- Front end:
testandandfuse with any following conditional jump;cmp,addandsubwith the jumps on equality and on the unsigned and signed comparisons, but not withjs,jporjoand their inverses;incanddeconly with those on equality and the signed comparisons.
Zen and Zen+
- Integer execution: Only a
cmportestdirectly followed by a conditional branch fuses into one macro-op, and not when it has a displacement together with an immediate, or an address relative to RIP; no other arithmetic or logic instruction fuses.
Zen 2
- Integer execution: Only a
cmportestfollowed by a conditional branch fuses into one macro-op, not one with a displacement together with an immediate or an address relative to RIP; the arithmetic and logic instructions that fuse on Zen 3 do not.
Zen 3
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, unless it carries a displacement together with an immediate, addresses relative to RIP or makes the pair longer than 15 bytes;incanddecdo not fuse with a branch on the carry flag, andjrcxznever fuses.
Zen 4
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, unless it carries a displacement together with an immediate, addresses relative to RIP or makes the pair longer than 15 bytes;incanddecdo not fuse with a branch on the carry flag.
Zen 5
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, within limits on the sizes of its displacement and immediate.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
TEST m8, imm8
TEST_MEMb_IMMb_F6r0 | F6 /0 ib | Base instruction set |
TEST r8, imm8
TEST_GPR8_IMMb_F6r0 | F6 /0 ib | Base instruction set |
TEST m8, imm8 undocumented
TEST_MEMb_IMMb_F6r1 | F6 /1 ib | Base instruction set |
TEST r8, imm8 undocumented
TEST_GPR8_IMMb_F6r1 | F6 /1 ib | Base instruction set |
TEST m16/32/64, imm16/32
TEST_MEMv_IMMz_F7r0 | F7 /0 iw/id | Base instruction set |
TEST r16/32/64, imm16/32
TEST_GPRv_IMMz_F7r0 | F7 /0 iw/id | Base instruction set |
TEST m16/32/64, imm16/32 undocumented
TEST_MEMv_IMMz_F7r1 | F7 /1 iw/id | Base instruction set |
TEST r16/32/64, imm16/32 undocumented
TEST_GPRv_IMMz_F7r1 | F7 /1 iw/id | Base instruction set |
TEST m8, r8
TEST_MEMb_GPR8 | 84 /r | Base instruction set |
TEST r8, r8
TEST_GPR8_GPR8 | 84 /r | Base instruction set |
TEST m16/32/64, r16/32/64
TEST_MEMv_GPRv | 85 /r | Base instruction set |
TEST r16/32/64, r16/32/64
TEST_GPRv_GPRv | 85 /r | Base instruction set |
TEST AL, imm8
TEST_AL_IMMb | A8 ib | Base instruction set |
TEST rAX, imm16/32
TEST_OrAX_IMMz | A9 iw/id | Base instruction set |
Latency and throughput
| Model | m8, imm8m16, imm16m32, imm32m64, imm32m8, r8m16, r16m32, r32m64, r64 | r8, imm8r32, imm32r64, imm32r8, r8r16, r16r32, r32r64, r64AL, imm8rAX, imm32 | r16, imm16rAX, imm16 |
|---|---|---|---|
| Sandy Bridge | 6 / 0.5p015 | 1 / 0.33p015 | 1 / 0.33p015 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 6 / 0.5p0156 | 1 / 0.25p0156 | 1 / 0.25p0156 |
| Alder Lake, Sapphire Rapids | 7 / 0.33p0 p1 p5 p6 p11 | 2 / 0.2p0 p1 p5 p6 p11 | 1 / 0.2p0 p1 p5 p6 p11 |
| Lunar Lake | 7 / 0.33p012345 | 2 / 0.17p012345 | 1 / 0.33p135 |
| Bonnell | 1 / 1p0 | 1 / 0.5p01 | 1 / 0.5p01 |
| Silvermont | 4 / 1IEC0 IEC1 MEC | 1 / 0.5IEC0 IEC1 | 1 / 0.5IEC0 IEC1 |
| Piledriver | 5 / 1.5EX0 EX1 | 1 / 1EX0 EX1 | 1 / 1EX0 EX1 |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 2, Zen 3, Zen 4 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 4 / 1ALU0 ALU1 | 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.
r = src1 & sext(src2) # as AND, but nothing is written
of = cf = 0
sf = msb(r)
zf = r == 0
pf = parity(r)
af = UNDEFINEDRun on AMD Zen 5 (EPYC 9645): 224 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
TEST r/m8, imm8 TEST r/m16/32/64, imm16/32 | ||
r/m8/16/32/64 | explicit | read |
imm8/16/32 | explicit | read |
RFLAGS | implicit | write |
TEST r/m8, r8 TEST r/m16/32/64, r16/32/64 | ||
r/m8/16/32/64 | explicit | read |
r8/16/32/64 | explicit | read |
RFLAGS | implicit | write |
TEST AL, imm8 TEST rAX, imm16/32 | ||
AL, rAX | explicit | read |
imm8/16/32 | explicit | read |
RFLAGS | implicit | write |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| All forms | |||||
| 0 | M | M | U | M | 0 |
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 |
|---|---|---|
I86 | 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. - 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 2B, page 4-721 (TEST)
- Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1 (253665-093, September 2026)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, 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 timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.