Last active
January 15, 2023 16:11
-
-
Save y21/c933b35bafa3c48a43cc3d4050acc677 to your computer and use it in GitHub Desktop.
branch prediction hint intrinsics in stable rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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