Instruction
VPTERNLOGQ
Computes any bitwise function of three inputs, given as an 8-bit truth table in an immediate, masked at quadword granularity.
- Extensions
- Forms
- 6 (6 EVEX)
- Data
- Intel XED v2026.08.23
VPTERNLOGQ computes a bitwise function of three inputs whose truth table
is its immediate, exactly as VPTERNLOGD does; that
page explains how to read and build the immediate. The two differ only
where elements matter: VPTERNLOGQ applies a write mask per quadword and
broadcasts a quadword from memory, so it is the one to use with a mask
from a 64-bit compare such as VPCMPQ, or with a 64-bit
constant in memory. Without either, both give the same bits, and the
compilers pick freely: GCC 14 and Clang 23 at -O2 fill a vector of
uint64_t with ones using VPTERNLOGD and the immediate FFh.
Three-input steps in 64-bit algorithms
Hash functions built on 64-bit words, such as SHA-512 and SHA-3, are full of three-input logic. With F0h, CCh and AAh standing for the first, second and third operand, the immediate is the expression evaluated on them:
| Step | Expression | imm8 |
|---|---|---|
| Keccak chi | a ^ (~b & c) | F0h ^ (33h & AAh) = D2h |
| SHA-512 Ch | (e & f) ^ (~e & g), e as the first operand | CAh |
| SHA-512 Maj | majority of a, b, c | E8h |
| Keccak theta, parity | a ^ b ^ c | 96h |
On uint64_t vector types with -march=x86-64-v4, Clang 23 compiles
a ^ (~b & c) to one VPTERNLOGQ with D2h, and GCC 14 to one with B4h,
having put the operands in the order a, c, b. Both find 96h for the
three-way XOR and a single instruction for Ch. For the majority, whether
written as (a & b) ^ (a & c) ^ (b & c), with ORs, or as
(a & b) | (c & (a | b)), both emit two VPTERNLOGQ instead of one with
E8h; the intrinsic _mm512_ternarylogic_epi64(a, b, c, 0xE8) gives the
single instruction.
Recorded on Zen 5
The cases recorded on Zen 5 include E8h with a broadcast quadword and zeroing: each selected quadword became the majority of its three inputs, and the others zero. Immediate 0Fh, NOT of the first operand, inverted only the quadwords whose mask bits were set.
AMD’s Zen 5 guide lists VPTERNLOGQ as one macro-op with a latency of one cycle and a throughput of four per cycle, at every width.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
VPTERNLOGQ zmm {k}{z}, zmm, zmm, imm8
VPTERNLOGQ_ZMMu64_MASKmskw_ZMMu64_ZMMu64_IMM8_AVX512 | EVEX.512.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F |
VPTERNLOGQ zmm {k}{z}, zmm, m512/m64bcst, imm8
VPTERNLOGQ_ZMMu64_MASKmskw_ZMMu64_MEMu64_IMM8_AVX512 | EVEX.512.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F |
VPTERNLOGQ xmm {k}{z}, xmm, xmm, imm8
VPTERNLOGQ_XMMu64_MASKmskw_XMMu64_XMMu64_IMM8_AVX512 | EVEX.128.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F + AVX512VL |
VPTERNLOGQ xmm {k}{z}, xmm, m128/m64bcst, imm8
VPTERNLOGQ_XMMu64_MASKmskw_XMMu64_MEMu64_IMM8_AVX512 | EVEX.128.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F + AVX512VL |
VPTERNLOGQ ymm {k}{z}, ymm, ymm, imm8
VPTERNLOGQ_YMMu64_MASKmskw_YMMu64_YMMu64_IMM8_AVX512 | EVEX.256.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F + AVX512VL |
VPTERNLOGQ ymm {k}{z}, ymm, m256/m64bcst, imm8
VPTERNLOGQ_YMMu64_MASKmskw_YMMu64_MEMu64_IMM8_AVX512 | EVEX.256.66.0F3A.W1 25 /r ib | AVX10.1 or AVX512F + AVX512VL |
Latency and throughput
| Model | zmm {k}{z}, zmm, zmm, imm8 | zmm {k}{z}, zmm, m512/m64bcst, imm8 | xmm {k}{z}, xmm, xmm, imm8ymm {k}{z}, ymm, ymm, imm8 | xmm {k}{z}, xmm, m128/m64bcst, imm8 | ymm {k}{z}, ymm, m256/m64bcst, imm8 |
|---|---|---|---|---|---|
| Skylake-SP, Ice Lake | 1 / 0.33p015 | 8 / 0.5p015 | 1 / 0.33p015 | 7 / 0.5p015 | 8 / 0.5p015 |
| Sapphire Rapids | 1 / 0.5p05 | 9 / 0.5p05 | 1 / 0.33p015 | 8 / 0.33p015 | 9 / 0.33p015 |
| Zen 4 | 1 / 0.5FP0 FP1 FP2 FP3 | 8 / 0.5FP0 FP1 FP2 FP3 | 1 / 0.25FP0 FP1 FP2 FP3 | 8 / 0.5FP0 FP1 FP2 FP3 | 8 / 0.5FP0 FP1 FP2 FP3 |
Data flow
Where the elements of the sources go, element 0 on the right; results that depend on a control value are drawn for an example. How to read.
Every quadword works alike: 2, 4 and 8 quadwords in the xmm, ymm and zmm forms. The forms write under a mask: where its bit is 0, the element keeps its old value, or zeroes it with {z}. With m64bcst, one quadword from memory stands for every element of the last source. The sources are drawn above the result, each element in its own column.
Result quadword i comes from quadword i of xmm1, xmm2 and xmm3. Result quadwords 1 to 0: r1, r0.
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
for i in range(VL // 64):
if k1[i]:
a = zmm1.q[i]
b = zmm2.q[i]
c = src3.q[i]
zmm1.q[i] = ternlog(imm8, a, b, c)
elif zeroing:
zmm1.q[i] = 0
# else: zmm1.q[i] keeps its value
# m64bcst: src3.q[i] is the m64 for every i
# zmm1 is zeroed from bit VL upRun on AMD Zen 5 (EPYC 9645): 6 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
VPTERNLOGQ xmm {k}{z}, xmm, xmm/m128/m64bcst, imm8 VPTERNLOGQ ymm {k}{z}, ymm, ymm/m256/m64bcst, imm8 VPTERNLOGQ zmm {k}{z}, zmm, zmm/m512/m64bcst, imm8 | ||
xmm, ymm, zmm | explicit | read, write (masked) |
{k} | explicit | read (write mask) |
xmm, ymm, zmm | explicit | read |
xmm/m128/m64bcst, ymm/m256/m64bcst, zmm/m512/m64bcst | explicit | read |
imm8 | explicit | read |
Flags
None: XED records no flag that the forms test or change.
Exceptions
The exception class is XED's name for the set of exception conditions that a form follows. How to read.
- All forms
AVX512-E4
Attributes
What else XED records about the forms. How to read.
- All forms
- write masking
- Memory forms
- embedded broadcast (tuple Full); memory fault suppression
Intrinsics
The C intrinsics of Clang 23.1.2 that compile to VPTERNLOGQ, 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 |
|---|---|
_mm_ternarylogic_epi64
__m128i _mm_ternarylogic_epi64(__m128i A, __m128i B, __m128i C, const unsigned char imm) |
|
_mm_mask_ternarylogic_epi64
__m128i _mm_mask_ternarylogic_epi64(__m128i A, __mmask8 U, __m128i B, __m128i C, const unsigned char imm) |
|
_mm_maskz_ternarylogic_epi64
__m128i _mm_maskz_ternarylogic_epi64(__mmask8 U, __m128i A, __m128i B, __m128i C, const unsigned char imm) |
|
_mm256_ternarylogic_epi64
__m256i _mm256_ternarylogic_epi64(__m256i A, __m256i B, __m256i C, const unsigned char imm) |
|
_mm256_mask_ternarylogic_epi64
__m256i _mm256_mask_ternarylogic_epi64(__m256i A, __mmask8 U, __m256i B, __m256i C, const unsigned char imm) |
|
_mm256_maskz_ternarylogic_epi64
__m256i _mm256_maskz_ternarylogic_epi64(__mmask8 U, __m256i A, __m256i B, __m256i C, const unsigned char imm) |
|
_mm512_ternarylogic_epi64
__m512i _mm512_ternarylogic_epi64(__m512i A, __m512i B, __m512i C, const unsigned char imm) |
|
_mm512_mask_ternarylogic_epi64
__m512i _mm512_mask_ternarylogic_epi64(__m512i A, __mmask8 U, __m512i B, __m512i C, const unsigned char imm) |
|
_mm512_maskz_ternarylogic_epi64
__m512i _mm512_maskz_ternarylogic_epi64(__mmask8 U, __m512i A, __m512i B, __m512i C, const unsigned char imm) |
|
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 |
|---|---|---|
AVX512F_512 | AVX10.1 or AVX512F | Cannon Lake, Ice Lake (client), Tiger Lake, Nova Lake, Skylake-SP, Cascade Lake, Cooper Lake, Ice Lake (server), Sapphire Rapids, Emerald Rapids, Granite Rapids, Diamond Rapids, Knights Landing, Knights Mill, Zen 4, Zen 5, Zen 6 |
AVX512F_128 | AVX10.1 or AVX512F + AVX512VL | Cannon Lake, Ice Lake (client), Tiger Lake, Nova Lake, Skylake-SP, Cascade Lake, Cooper Lake, Ice Lake (server), Sapphire Rapids, Emerald Rapids, Granite Rapids, Diamond Rapids, Zen 4, Zen 5, Zen 6 |
AVX512F_256 | Same as AVX512F_128 | Same as AVX512F_128 |
More AVX10.1 instructions
- VPSRAVD
- VPSRAVQ
- VPSRAVW
- VPSRAW
- VPSRLD
- VPSRLDQ
- VPSRLQ
- VPSRLVD
- VPSRLVQ
- VPSRLVW
- VPSRLW
- VPSUBB
- VPSUBD
- VPSUBQ
- VPSUBSB
- VPSUBSW
- VPSUBUSB
- VPSUBUSW
- VPSUBW
- VPTERNLOGD
- VPTESTMB
- VPTESTMD
- VPTESTMQ
- VPTESTMW
- VPTESTNMB
- VPTESTNMD
- VPTESTNMQ
- VPTESTNMW
- VPUNPCKHBW
- VPUNPCKHDQ
- VPUNPCKHQDQ
- VPUNPCKHWD
- VPUNPCKLBW
- VPUNPCKLDQ
- VPUNPCKLQDQ
- VPUNPCKLWD
- VPXORD
- VPXORQ
- VRANGEPD
- VRANGEPS
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 Advanced Vector Extensions 10.2 Architecture Specification, revision 7.0 (361050-007, April 2026)
- Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2C, page 5-651 (VPTERNLOGD/
VPTERNLOGQ) - NIST FIPS 180-4: Secure Hash Standard (SHS) (August 2015)
- Software Optimization Guide for the AMD Zen5 Microarchitecture (58455, rev. 1.00, August 2024), Wayback Machine copy of AMD's ZIP
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.