Skip to main content

frozen_core/
hints.rs

1//! Implementation of branch preditor hints
2
3/// empty function used as a placeholder to influence branch prediction
4#[cold]
5#[inline]
6const fn cold_fn() {}
7
8/// Branch predictor hint, which marks given condition as *likely* to be
9#[inline]
10pub const fn likely(b: bool) -> bool {
11    if !b {
12        cold_fn();
13    }
14    b
15}
16
17/// Branch predictor hint, which marks given condition as *unlikely* to be
18#[inline]
19pub const fn unlikely(b: bool) -> bool {
20    if b {
21        cold_fn();
22    }
23    b
24}
25
26#[cfg(test)]
27mod hints {
28    use super::*;
29
30    #[test]
31    fn sanity_checks_for_likely() {
32        assert!(likely(true));
33        assert!(!likely(false));
34    }
35
36    #[test]
37    fn sanity_checks_for_unlikely() {
38        assert!(unlikely(true));
39        assert!(!unlikely(false));
40    }
41}