Skip to main content

elfo_utils/
likely.rs

1//! `likely()` and `unlikely()` hints.
2//! `core::intrinsics::{likely, unlikely}` are unstable fo now.
3//! On stable we can use `#[cold]` to get the same effect.
4
5#[inline]
6#[cold]
7fn cold() {}
8
9/// Hints the compiler that this branch is likely to be taken.
10#[inline(always)]
11pub fn likely(b: bool) -> bool {
12    if !b {
13        cold();
14    }
15    b
16}
17
18/// Hints the compiler that this branch is unlikely to be taken.
19#[inline(always)]
20pub fn unlikely(b: bool) -> bool {
21    if b {
22        cold();
23    }
24    b
25}