skills/computer-architecture/branch-prediction-and-speculation/SKILL.md
Branch prediction and speculation skill for CPU security and performance. Use when explaining branch predictors, mispredict penalties, speculative execution, Spectre/Meltdown mitigations, or branchless patterns. Activates on queries about branch prediction, speculative execution, Spectre, Meltdown, mispredict, or branchless code.
npx skillsauth add mohitmishra786/low-level-dev-skills branch-prediction-and-speculationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Explain how modern CPUs predict branches, execute speculatively, recover on mispredict, and why speculation created side channels (Spectre/Meltdown) — linking performance tuning with security-aware low-level coding.
likely/unlikely or branchless refactorsretpoline, IBRS, and similar mitigationsFetch sees conditional branch
├── Predict direction (taken / not-taken)
├── Speculatively execute predicted path
└── On resolve:
├── Correct → commit, ~0 penalty (deep pipelines still cost on mispredict)
└── Wrong → squash, refill from correct PC (10–20+ cycles typical)
Patterns: backward branches often predicted taken (loops); forward not-taken.
/* Predictable — tight loop backward branch */
for (int i = 0; i < n; i++)
sum += a[i];
/* Unpredictable — data-dependent */
if (data[i] > threshold) /* hard to predict */
rare_path();
Techniques: branchless cmov/select, lookup tables, sorting data to reduce branches, splitting hot/cold paths.
/* Branchless min (integer) */
int m = a < b ? a : b; /* compiler may lower to cmov */
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
if (unlikely(ptr == NULL))
return -EINVAL;
Measure with perf — hints are not magic on modern predictors.
CPUs may execute instructions before branch outcome is known. If speculated path touches secret-dependent memory, cache state can leak (Spectre variant 1).
Mitigations (high level):
cpu_show_mitigations)-mspeculative-load-hardening on Clang)Meltdown (Intel): user load from kernel mapping — fixed by KPTI (separate page tables).
perf stat -e branches,branch-misses ./app
High branch-misses ratio → investigate hot branches.
/branch-prediction-and-speculation Make this comparison function constant-time against Spectre-style leakage
| Symptom | Cause | Fix |
|---------|-------|-----|
| Loop slower than expected | Mispredicted exit | Peel iterations; branchless tail |
| likely no help | Predictor already good | Profile first |
| Secret leak in crypto | Branches on secret bytes | Constant-time algorithms |
| Mitigation regression | KPTI/retpoline overhead | Accept or isolate secrets |
| "Branchless" slower | CMOV still executes both | Benchmark on target CPU |
skills/computer-architecture/cpu-pipelines-and-hazards — control hazardsskills/security/kernel-security — KPTI, CET, speculation mitigationsskills/low-level-programming/cpu-cache-opt — cache timing channelsskills/profilers/hardware-counters — branch-misses eventskills/runtimes/binary-hardening — userspace hardening overlapdevelopment
QEMU/KVM skill for virtualization and kernel development. Use when running qemu-system-x86_64 with KVM, configuring virtio devices, VFIO passthrough, QMP monitor, libvirt, or booting custom kernels. Activates on queries about QEMU, KVM, virtio, VFIO, virsh, virt-install, or -kernel -append.
development
Hardware virtualization internals skill for Intel VT-x and AMD-V. Use when studying VMCS/VMCB, EPT/NPT page tables, VMEXIT handling, APIC virtualization, or building minimal hypervisors. Activates on queries about VMX, SVM, VMCS, EPT, NPT, VMEXIT, or type-1 hypervisor.
testing
Linux containers internals skill for namespaces, cgroups, and OCI. Use when understanding clone/unshare namespaces, cgroups v2 limits, overlayfs, runc, seccomp profiles, capabilities, or escape mitigations. Activates on queries about namespaces, cgroups, overlayfs, runc, seccomp-bpf, OCI spec, or container escape.
tools
Reverse engineering skill for binary analysis. Use when decompiling with Ghidra, analyzing with radare2, scripting RE tools, triaging with strings/file/xxd, or diffing binaries. Activates on queries about Ghidra, radare2, r2, decompiler, Binary Ninja, Diaphora, or stripped binary analysis.