Skip to content

Instantly share code, notes, and snippets.

@y21
Last active January 15, 2023 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y21/c933b35bafa3c48a43cc3d4050acc677 to your computer and use it in GitHub Desktop.
Save y21/c933b35bafa3c48a43cc3d4050acc677 to your computer and use it in GitHub Desktop.
branch prediction hint intrinsics in stable rust
/// std::intrinsics::{likely, unlikely, assume} in stable Rust
mod predict {
#[inline(never)]
#[cold]
fn unlikely_inner() {}
pub fn unlikely<B: Into<bool>>(b: B) -> bool {
let b = b.into();
if b { unlikely_inner(); }
b
}
pub fn likely<B: Into<bool>>(b: B) -> bool {
let b = b.into();
if !b { unlikely_inner(); }
b
}
pub unsafe fn assume<B: Into<bool>>(b: B) {
if !b.into() { std::hint::unreachable_unchecked(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment